"Realtime" Sound

Just starting out? Need help? Post your questions and find answers here.
NA4U
New User
New User
Posts: 1
Joined: Tue May 13, 2003 1:58 am
Location: South Carolina

"Realtime" Sound

Post by NA4U »

Can't seem to get the "realtime" sound commands to work (SoundVolume, SoundFrequency). I use InitSound() and get no error. Declare #Sound = 1. Then try to set the volume with SoundVolume(#Sound,50) and get a "#sound object not initalized" error on complile. Same compile error if I try to set SoundFrequency(#Sound,1000). I am rookie and clueless. Does someone have a small code snip that illustrates how to send a simple tone to the soundcard using the PB commands?

Thank you,

Tom
Tom
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

Sorry, you misunderstood the Sound-Commands.

First call InitSound().

Then you have to load a sound with LoadSound(#Sound,"filename") and give an ID to it.

Then you can play it with PlaySound(#Sound,[,flags])

With SoundFrequency(#Sound, frequence) you can change the playbacktempo, so normally 44100 will be the normal speed.

There's no Sound-Command as the Beep_() for the PC-Speaker. But somewhere else in this forum there's a code for it, that just creates a by it's frequence defined temp-soundfile and plays it.

Another possibility is to use bass.dll or fmod.dll for it. So as here:
viewtopic.php?t=6039
%1>>1+1*1/1-1!1|1&1<<$1=1
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Little tip:

Code: Select all

Structure WAVE 
wFormatTag.w 
nChannels.w 
nSamplesPerSec.l 
nAvgBytesPerSec.l 
nBlockAlign.w 
wBitsPerSample.w 
cbSize.w 
EndStructure 

Procedure MakeSound(nr,Frequency, Duration); 
 SoundValue.b 
 w.f; // omega ( 2 * pi * frequency) 
 #Mono= $0001; 
 #SampleRate= 11025; // 8000, 11025, 22050, or 44100 
 #RiffId.s = "RIFF"; 
 #WaveId.s= "WAVE"; 
 #FmtId.s=  "fmt "; 
 #DataId.s= "data"; 
 #pi=3.1415 

 WaveFormatEx.WAVE 
 WaveFormatEx\wFormatTag=#WAVE_FORMAT_PCM; 
 WaveFormatEx\nChannels =#Mono; 
 WaveFormatEx\nSamplesPerSec = #SampleRate; 
 WaveFormatEx\wBitsPerSample = $0008; 
 WaveFormatEx\nBlockAlign = (WaveFormatEx\nChannels * WaveFormatEx\wBitsPerSample) /8 
 WaveFormatEx\nAvgBytesPerSec = WaveFormatEx\nSamplesPerSec * WaveFormatEx\nBlockAlign; 
 WaveFormatEx\cbSize = 0; 
 
 DataCount = (Duration * #SampleRate)/1000; // sound data 
 RiffCount = 4+4 +4+ SizeOf(WAVE)+4 +4+ DataCount 
 
 start=AllocateMemory(nr,RiffCount+100) 
 MS=start 
 
 PokeS(MS,#RiffId):MS+4  ;'RIFF' 
 PokeL(MS,RiffCount):MS+4 ;file Data size 
 PokeS(MS,#WaveId):MS+4  ;'WAVE' 
 PokeS(MS,#FmtId):MS+4  ;'fmt ' 
 TempInt = SizeOf(WAVE); 
 PokeL(MS,TempInt):MS+4  ;TWaveFormat data size 
 
 PokeW(MS,WaveFormatEx\wFormatTag):MS+2; WaveFormatEx record 
 PokeW(MS,WaveFormatEx\nChannels):MS+2 
 PokeL(MS,WaveFormatEx\nSamplesPerSec):MS+4 
 PokeL(MS,WaveFormatEx\nAvgBytesPerSec):MS+4 
 PokeW(MS,WaveFormatEx\nBlockAlign):MS+2 
 PokeW(MS,WaveFormatEx\wBitsPerSample):MS+2 
 PokeW(MS,WaveFormatEx\cbSize):MS+2 
 
 PokeS(MS,#DataId):MS+4  ;'data' 
 PokeL(MS,DataCount):MS+4 ;sound data size 
 
 ;{Calculate And write out the tone signal} // now the Data values 
 
 w = 2 * #pi * Frequency; omega 
 For i = 0 To DataCount - 1 
  ;// wt = w *i /SampleRate 
  ;SoundValue := 127 + trunc(127 * Sin(i * w / SampleRate)); 
  SoundValue = 127 + 127 * Sin(i * w / #SampleRate); 
  PokeB(MS,SoundValue):MS+1; 
 Next 
 ;// you could save the wave tone To file with : 
 ;// MS.Seek(0, soFromBeginning); 
 ;// MS.SaveToFile('C:\MyFile.wav'); 
 ;// then reload And play them without having To 
 ;// construct them each time. 
 ;{now play the sound} 
 ;sndPlaySound(MS.Memory, SND_MEMORY Or SND_SYNC); 
 ;MS.Free; 
 
 CatchSound(nr,start) 
EndProcedure 
Procedure QuitSound(nr) 
 StopSound(nr) 
 FreeSound(nr) 
 FreeMemory(nr) 
EndProcedure 
InitSound() 
MakeSound(0,1100,2000) 

PlaySound(0) 
Delay(2000) 
QuitSound(0) 
Debug "done"
Post Reply