Code: Select all
' Playing a self-composed sound buffer with FMOD.
' -shiftlynx [12/Aug/2005]
' cdsoft.co.uk
'
'$include: 'fmod.bi'
const PI = 3.141592654
const BUFFERSIZE = 96000 ' 2 seconds at 48kHz
const FREQUENCY = 4000 ' 4kHz wave
const VOLUME = 128
dim sample as integer
dim soundBuffer(BUFFERSIZE) as byte
dim buffer1 as byte ptr
dim buffer2 as byte ptr
dim bufferLen1 as integer
dim bufferLen2 as integer
FSOUND_Init(48000, 8, 0)
' set up the sound buffer... a 4kHz wave.
for i% = 0 to BUFFERSIZE-1
amp! = 120.0 * sin(((cdbl(i%) * cdbl(FREQUENCY)) / 48000.0) * 2.0 * PI)
soundBuffer(i%) = cint(amp!)
next i%
' format = 8 bit, signed, mono, 48kHz sample rate.
' allocate a new sample.
sample = FSOUND_Sample_Alloc(FSOUND_FREE, BUFFERSIZE, FSOUND_MONO or FSOUND_8BITS or FSOUND_UNSIGNED, 48000, VOLUME, 0, 0)
if sample = 0 then
print "Failed to allocate the sound sample!"
print "Error: " + ltrim$(str$(FSOUND_GetError()))
sleep
end
end if
' lock the sample.
FSOUND_Sample_Lock(sample, 0, BUFFERSIZE, @bufffer1, @buffer2, @bufferLen1, @bufferLen2)
' upload our buffer.
FSOUND_Sample_Upload(sample, @soundBuffer(0), FSOUND_8BITS or FSOUND_MONO or FSOUND_SIGNED)
' unlock the sample.
FSOUND_Sample_Unlock(sample, buffer1, buffer2, bufferLen1, bufferLen2)
' play the sample.
FSOUND_PlaySound(FSOUND_FREE, sample)
print "The sound should now be playing... it should be a " + ltrim$(str$(FREQUENCY)) + "Hz sine wave."
print "Press a key to stop the sound and quit..."
sleep
FSOUND_StopSound(FSOUND_ALL)
FSOUND_Sample_Free(sample)
FSOUND_Close
end