Playing a Self-Composed Sound Buffer

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
shiftlynx
Posts: 6
Joined: May 27, 2005 18:37
Location: United Kingdom
Contact:

Playing a Self-Composed Sound Buffer

Post by shiftlynx »

I received an e-mail asking how to do this in FMOD. Here is some sample code for playing a 4kHz sine wave for 2 seconds. The basic procedure is to fill up your own array with the data you want to play, then allocate a sound buffer the same size (using FMOD_Sample_Alloc), then locking that sample (FMOD_Sample_Lock) and uploading your buffer (FMOD_Sample_Upload). After that, you must unlock the sample (FMOD_Sample_Unlock) and then you can play it just like you normally would do.

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
Hope this helps (could be used to make a generic SOUND statement).
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Post by DrV »

Neat, except I don't think a generic SOUND should be implemented with FMOD, since it's got that ugly $$$ licensing crap... Doing it with OpenAL probably wouldn't be too much different, though.
D.J.Peters
Posts: 8631
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

(sorry bad english)
hi shiftlynx,
for WINDOWS sndPlaySound can play an BUFFER too (including the waveheader)

for LINUX write an BUFFER to /dev/dsp works too (after set the format)
or /dev/audio /dev/sound ...

joshy
Post Reply