Creating sound through speakers
Author |
Message |
Just #1 / 13
|
 Creating sound through speakers
Anyone know how to easily create sound through the soundcard. I want to create sounds and then play them through the PC speakers. I know how to play a WAV or MIDI file but this is no good. I don't want to use files. Thanks Justin
|
Sun, 01 Feb 2004 00:36:03 GMT |
|
 |
Michael William #2 / 13
|
 Creating sound through speakers
Exactly what sounds do you want to create? And why, exactly, do you not wish to play WAV files? Mike
Quote: > Anyone know how to easily create sound through the soundcard. > I want to create sounds and then play them through the PC speakers. > I know how to play a WAV or MIDI file but this is no good. I don't > want to use files. > Thanks > Justin
|
Sun, 01 Feb 2004 01:22:05 GMT |
|
 |
BeastFi #3 / 13
|
 Creating sound through speakers
If you don't want external files, you could always put the wav files in a resource file. Quote:
> Anyone know how to easily create sound through the soundcard. > I want to create sounds and then play them through the PC speakers. > I know how to play a WAV or MIDI file but this is no good. I don't > want to use files. > Thanks > Justin
|
Sun, 01 Feb 2004 06:58:07 GMT |
|
 |
Billy Jo #4 / 13
|
 Creating sound through speakers
Justin, Ripping the sound card out of its connector while the power is on produces amazing sounds!!! Death knells I believe they are called! Danny Elfman has used this technique to rise to new Hollywood heights!! Sorry, I am a Danny {*filter*}, but felt the irresistible compunction to respond foolishly! Regards, Billy Joe
Anyone know how to easily create sound through the soundcard. I want to create sounds and then play them through the PC speakers. I know how to play a WAV or MIDI file but this is no good. I don't want to use files. Thanks Justin
|
Sun, 01 Feb 2004 13:14:03 GMT |
|
 |
Just #5 / 13
|
 Creating sound through speakers
Well, I need to create the sound on the fly. Currently I play sound straight through the PC internal speaker which is now not good enough. It is for a ringtone program I have developed and the notes are in a text format. I want to read this text and generate the appropriate notes. How I do this I don't care. If it means I create a WAV and then play it then fine - but I don't know how to create WAV or MIDI files. The program is on my site here if you are interested. http://www.convertyourtone.com Justin Quote:
> Exactly what sounds do you want to create? And why, exactly, do you not wish > to play WAV files? > Mike
> > Anyone know how to easily create sound through the soundcard. > > I want to create sounds and then play them through the PC speakers. > > I know how to play a WAV or MIDI file but this is no good. I don't > > want to use files. > > Thanks > > Justin
|
Sun, 01 Feb 2004 19:31:04 GMT |
|
 |
Just #6 / 13
|
 Creating sound through speakers
You are correct, this would strictly speaking solve my problem... Quote:
> Justin, > Ripping the sound card out of its connector while the power is on produces > amazing sounds!!! Death knells I believe they are called! Danny Elfman has > used this technique to rise to new Hollywood heights!! > Sorry, I am a Danny {*filter*}, but felt the irresistible compunction to respond > foolishly! > Regards, > Billy Joe
> Anyone know how to easily create sound through the soundcard. > I want to create sounds and then play them through the PC speakers. > I know how to play a WAV or MIDI file but this is no good. I don't > want to use files. > Thanks > Justin
|
Sun, 01 Feb 2004 19:32:27 GMT |
|
 |
Stuart Dougla #7 / 13
|
 Creating sound through speakers
I found a program that turns a text file into a MIDI file. I don't remember where I got it from now, but I still have the small program. Might be useful? Stuart
Quote: > Justin, > Ripping the sound card out of its connector while the power is on produces > amazing sounds!!! Death knells I believe they are called! Danny Elfman has > used this technique to rise to new Hollywood heights!! > Sorry, I am a Danny {*filter*}, but felt the irresistible compunction to respond > foolishly! > Regards, > Billy Joe
> Anyone know how to easily create sound through the soundcard. > I want to create sounds and then play them through the PC speakers. > I know how to play a WAV or MIDI file but this is no good. I don't > want to use files. > Thanks > Justin
|
Mon, 02 Feb 2004 02:11:28 GMT |
|
 |
Michael William #8 / 13
|
 Creating sound through speakers
You can easily create simple sinusoidal (or other shape) waveforms with a little bit of math and you can then create a standard Windows wav file from the data. The wav file does not need to be stored as a disk file. Instead, you can create the wav file in a standard VB string and then use the SndPlaySound API function to play that string from memory. Here is some code that does just that. The example creates a simple 440 Hz sinusoidal waveform lasting 0.2 seconds and plays it. You can use exactly the same code to create dozens (or even hundreds) of waveforms (wav files held in strings), each of a different specified frequency, which you can then play as required. This simple example plays the wav file exactly, and so it lasts for exactly 0.2 seconds, but you can, if you wish, create files of a shorter (or longer) length or you can play a wav file continuously or can play it "continuously" for a specified period controlled by a Timer, to give you a variable length sound. I wrote this code some time ago, and now that I have learnt a little bit more about Visual Basic I would probably now write it in a simpler way. But it certainly works okay as it stands and I'm just too lazy to alter it! Just start a new project and place a single Command Button on your Form. Then paste the following code into the (general) (declarations) section. Mike Option Explicit Private Declare Function sndPlaySound Lib "winmm.dll" _ Alias "sndPlaySoundA" (ByVal lpszSoundName _ As String, ByVal uFlags As Long) As Long Const SND_ALIAS = &H10000 Const SND_ASYNC = &H1 Const SND_FILENAME = &H20000 Const SND_LOOP = &H8 Const SND_MEMORY = &H4 Const SND_NODEFAULT = &H2 Const SND_NOSTOP = &H10 Const SND_SYNC = &H0 Dim wavedata(0 To 100) As String Private Sub Make16BitMonoWav(wavnum As Integer, _ rawdata() As Integer, samplespersec As Double) Dim s8 As String * 8, n As Long, k As Long Dim s4 As String * 4, s0 As String * 4 s0 = String(4, "0") ' set string to exact size for wav file wavedata(wavnum) = String((UBound(rawdata) - _ LBound(rawdata) + 1) * 2 + 44, 0) ' First write the string "RIFF" Mid(wavedata(wavnum), 1, 4) = "RIFF" ' next 4 bytes are longword = total length of wav file minus 8 k = Len(wavedata(wavnum)) - 8 s8 = Right((String(8, "0") + Hex(k)), 8) Mid(wavedata(wavnum), 5, 1) = Chr("&H" + Mid(s8, 7, 2)) Mid(wavedata(wavnum), 6, 1) = Chr("&H" + Mid(s8, 5, 2)) Mid(wavedata(wavnum), 7, 1) = Chr("&H" + Mid(s8, 3, 2)) Mid(wavedata(wavnum), 8, 1) = Chr("&H" + Mid(s8, 1, 2)) ' Now write the string "WAVE" and "fmt " Mid(wavedata(wavnum), 9, 4) = "WAVE" Mid(wavedata(wavnum), 13, 4) = "fmt " ' Now write a longword giving chunk size (decimal 16) Mid(wavedata(wavnum), 17, 4) = Chr(16) & String(3, 0) ' Now write integer for format tag = data not ' compressed (decimal 1) Mid(wavedata(wavnum), 21, 4) = Chr(1) & Chr(0) ' Now write integer for number of channels (1) Mid(wavedata(wavnum), 23, 4) = Chr(1) & Chr(0) ' Now write playback rate in samples per second (longword) s8 = Right(String(8, "0") + Hex(samplespersec), 8) Mid(wavedata(wavnum), 25, 1) = Chr("&H" + Mid(s8, 7, 2)) Mid(wavedata(wavnum), 26, 1) = Chr("&H" + Mid(s8, 5, 2)) Mid(wavedata(wavnum), 27, 1) = Chr("&H" + Mid(s8, 3, 2)) Mid(wavedata(wavnum), 28, 1) = Chr("&H" + Mid(s8, 1, 2)) ' Now write average bytes per second _ (16 bit mono = samplespersec * 2) (longword) s8 = Right(String(8, "0") + Hex(samplespersec * 2), 8) Mid(wavedata(wavnum), 29, 1) = Chr("&H" + Mid(s8, 7, 2)) Mid(wavedata(wavnum), 30, 1) = Chr("&H" + Mid(s8, 5, 2)) Mid(wavedata(wavnum), 31, 1) = Chr("&H" + Mid(s8, 3, 2)) Mid(wavedata(wavnum), 32, 1) = Chr("&H" + Mid(s8, 1, 2)) ' Now write blockalign (bytes per sample frame) _ (16 bit mono = 2) (integer) Mid(wavedata(wavnum), 33, 2) = Chr(2) + Chr(0) ' Now write bits per sample (16 for 16 bit samples) (integer) Mid(wavedata(wavnum), 35, 2) = Chr(16) + Chr(0) ' Now write the Data Chunk ID Mid(wavedata(wavnum), 37, 4) = "data" ' Now write data size (longword) (this is the actual number ' of bytes forming the sound data not counting the 8 bytes ' used by ID and Size fields nor any possible pad byte ' needed to make the chunk an even size s8 = Right(String(8, "0") + Hex((UBound(rawdata) - _ LBound(rawdata) + 1) * 2), 8) Mid(wavedata(wavnum), 41, 1) = Chr("&H" + Mid(s8, 7, 2)) Mid(wavedata(wavnum), 42, 1) = Chr("&H" + Mid(s8, 5, 2)) Mid(wavedata(wavnum), 43, 1) = Chr("&H" + Mid(s8, 3, 2)) Mid(wavedata(wavnum), 44, 1) = Chr("&H" + Mid(s8, 1, 2)) ' display data generated so far ' Now insert the actual data k = 43 For n = LBound(rawdata) To UBound(rawdata) k = k + 2 s4 = Right(s0 + Hex(rawdata(n)), 4) Mid(wavedata(wavnum), k, 1) = Chr("&H" + Right(s4, 2)) Mid(wavedata(wavnum), k + 1, 1) = Chr("&H" + Left(s4, 2)) Next n End Sub Private Sub MakeRawSineWave(wavenumber As Integer, _ frequency As Double, amplitude As Integer) Dim samplerate As Double, cycles As Double, totalsamples As Long Dim n As Long, k As Double, pi As Double, b1() As Integer If amplitude > 32767 Then amplitude = 32767 If amplitude < 0 Then amplitude = 0 samplerate = 11025 ' a standard wav sample rate cycles = Round(frequency * 0.2) ' 0.2 second sample time totalsamples = Round(samplerate / frequency * cycles) ReDim b1(1 To totalsamples) pi = Atn(1) * 4 For k = 0 To cycles * 2 * pi - 0.01 Step (cycles * 2 * pi) _ / totalsamples n = n + 1 b1(n) = Round(amplitude * Sin(k)) Next k Make16BitMonoWav wavenumber, b1, samplerate End Sub Private Sub Form_Load() Me.AutoRedraw = True Me.Show Command1.Enabled = False Dim wavenumber As Integer Dim frequency As Double Dim amplitude As Integer sndPlaySound wavedata(0), SND_SYNC Or SND_MEMORY Print " Please wait whilst data is generated ....." amplitude = 16000 ' about half full volume frequency = 440 wavenumber = 1 ' Use 1 to 100 (0 is reserved for "clear" wave) MakeRawSineWave wavenumber, frequency, amplitude DoEvents Command1.Enabled = True Cls End Sub Private Sub Command1_Click() Dim ret As Long ret = sndPlaySound(wavedata(1), SND_ASYNC Or SND_MEMORY) End Sub
Quote: > Well, I need to create the sound on the fly. Currently I play sound > straight through the PC internal speaker which is now not good enough. > It is for a ringtone program I have developed and the notes are in a > text format. I want to read this text and generate the appropriate > notes. How I do this I don't care. If it means I create a WAV and > then play it then fine - but I don't know how to create WAV or MIDI > files. > The program is on my site here if you are interested. > http://www.convertyourtone.com > Justin
Quote: > > Exactly what sounds do you want to create? And why, exactly, do you not wish > > to play WAV files? > > Mike
> > > Anyone know how to easily create sound through the soundcard. > > > I want to create sounds and then play them through the PC speakers. > > > I know how to play a WAV or MIDI file but this is no good. I don't > > > want to use files. > > > Thanks > > > Justin
|
Mon, 02 Feb 2004 05:41:47 GMT |
|
 |
Doug Ros #9 / 13
|
 Creating sound through speakers
Check here at the bottom: http://magma.ca/~dross/code.html
Quote: > Anyone know how to easily create sound through the soundcard. > I want to create sounds and then play them through the PC speakers. > I know how to play a WAV or MIDI file but this is no good. I don't > want to use files. > Thanks > Justin
|
Tue, 03 Feb 2004 07:11:29 GMT |
|
 |
Doug Ros #10 / 13
|
 Creating sound through speakers
Oh my god, that's something I have to hear! Can you post a link to it or email it?
Quote: > I found a program that turns a text file into a MIDI file. > I don't remember where I got it from now, but I still have the small > program. Might be useful? > Stuart
> > Justin, > > Ripping the sound card out of its connector while the power is on produces > > amazing sounds!!! Death knells I believe they are called! Danny Elfman > has > > used this technique to rise to new Hollywood heights!! > > Sorry, I am a Danny {*filter*}, but felt the irresistible compunction to > respond > > foolishly! > > Regards, > > Billy Joe
> > Anyone know how to easily create sound through the soundcard. > > I want to create sounds and then play them through the PC speakers. > > I know how to play a WAV or MIDI file but this is no good. I don't > > want to use files. > > Thanks > > Justin
|
Tue, 03 Feb 2004 07:11:30 GMT |
|
 |
Michael William #11 / 13
|
 Creating sound through speakers
Ah Yes, Doug. The only little problem is that the man can already produce sounds through his little internal speaker and he wanted to instead play some generated sounds through the soundcard so that it came out of the main speakers. The answer that I posted the day before yesterday will definitely allow him to do that. Mike Quote:
> Check here at the bottom: > http://magma.ca/~dross/code.html
> > Anyone know how to easily create sound through the soundcard. > > I want to create sounds and then play them through the PC speakers. > > I know how to play a WAV or MIDI file but this is no good. I don't > > want to use files. > > Thanks > > Justin
|
Tue, 03 Feb 2004 07:35:01 GMT |
|
 |
Doug Ros #12 / 13
|
 Creating sound through speakers
Damn. I misread the original post...
Quote: > Ah Yes, Doug. The only little problem is that the man can already produce > sounds through his little internal speaker and he wanted to instead play > some generated sounds through the soundcard so that it came out of the main > speakers. The answer that I posted the day before yesterday will definitely > allow him to do that. > Mike
> > Check here at the bottom: > > http://magma.ca/~dross/code.html
> > > Anyone know how to easily create sound through the soundcard. > > > I want to create sounds and then play them through the PC speakers. > > > I know how to play a WAV or MIDI file but this is no good. I don't > > > want to use files. > > > Thanks > > > Justin
|
Wed, 04 Feb 2004 01:33:14 GMT |
|
 |
Just #13 / 13
|
 Creating sound through speakers
Thanks guys! Just got back to reading all your posts - been away... Michael, looks like I'll use your code - seems to work rather well. Thanks for that! Justin http://www.convertyourtone.com Quote:
> Damn. I misread the original post...
> > Ah Yes, Doug. The only little problem is that the man can already produce > > sounds through his little internal speaker and he wanted to instead play > > some generated sounds through the soundcard so that it came out of the > main > > speakers. The answer that I posted the day before yesterday will > definitely > > allow him to do that. > > Mike
> > > Check here at the bottom: > > > http://magma.ca/~dross/code.html
> > > > Anyone know how to easily create sound through the soundcard. > > > > I want to create sounds and then play them through the PC speakers. > > > > I know how to play a WAV or MIDI file but this is no good. I don't > > > > want to use files. > > > > Thanks > > > > Justin
|
Mon, 01 Mar 2004 16:17:44 GMT |
|
|
|