Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to improve this: QB like Sound in FB with OpenAL
#1
Taking Chris Davies' wonderful tutorial on OpenAl, I've rigged (read this as not nice or pretty) a Sound command in FB.

What follows is the code for the command (looks a lot like Chris' huh?):

Code:
'$include: 'al/al.bi'
'$include: 'al/alut.bi'

declare sub UnloadQBSound()
declare Sub InitQBSound()
declare sub Sound (Freq as Single, Duration as single)

dim shared Buffer as ALuint   ' buffer to hold audio data.
dim shared Source as ALuint   ' source for the sound to come from.
dim shared SourcePos(3) as ALfloat   ' 3D position of the sound source.
dim shared SourceVel(3) as ALfloat   ' 3D velocity of the sound source.
dim shared ListenerPos(3) as ALfloat ' 3D position of the listener.
dim shared ListenerVel(3) as ALfloat ' 3D velocity of the listener.
dim shared ListenerOri(6) as ALfloat ' Listener orientation.

sub Sound (Freq as Single, Duration as single)
   dim sndHz as single
   Dim sndSR as integer
   dim sndA as Integer
   dim sndT as integer
   dim sndLT as integer
   dim sndL as Integer
   dim sndTime as single
  
   sndHz = Freq ' Frequency of Waveform
   sndSR = 44100 ' Sampling Rate
   sndL = int(sndSR/sndHZ)
   sndA = (2^7)-1


   dim sndO(sndL) as byte
  
   sndlt=int((sndSR/sndHZ)+.5)
  
   For sndT = 0 to sndL
      sndO(sndT) = int(sndA * sin(sndT/sndlt)+.5)
   Next sndT

   alGenBuffers(1, @Buffer)
   alGenSources(1, @Source)

   alBufferData (Buffer, AL_FORMAT_MONO8, @sndO(0), sndL, sndSR)
  
   alSourcei (Source,   AL_BUFFER,     Buffer)
   alSourcef (Source,   AL_PITCH,      1.0)
   alSourcef (Source,   AL_GAIN,       1.0)
   alSourcefv(Source,   AL_POSITION,   @SourcePos(0))
   alSourcefv(Source,   AL_VELOCITY,   @SourceVel(0))
   alSourcei(Source, AL_LOOPING, AL_TRUE)
            
   ' play.
   alSourcePlay(Source)
   sndTime = timer
   do while sndTime+duration > timer
      sleep 10
   loop
   alSourceStop(Source)

end sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' InitResources
'
Sub InitQBSound()
alutInit(0, 0)

' configure source and listener 3-d info.
SourcePos(0) = 0.0      ' x
SourcePos(1) = 0.0      ' y
SourcePos(2) = 0.0      ' z

SourceVel(0) = 0.0      ' x
SourceVel(1) = 0.0      ' y
SourceVel(2) = 0.0      ' z

ListenerPos(0) = 0.0    ' x
ListenerPos(1) = 0.0    ' y
ListenerPos(2) = 0.0    ' z

ListenerVel(0) = 0.0    ' x
ListenerVel(1) = 0.0    ' y
ListenerVel(2) = 0.0    ' z

ListenerOri(0) = 0.0    ' at(x)
ListenerOri(1) = 0.0    ' at(y)
ListenerOri(2) = -1.0   ' at(z)
ListenerOri(3) = 0.0    ' up(x)
ListenerOri(4) = 0.0    ' up(y)
ListenerOri(5) = 1.0    ' up(z)

alListenerfv(AL_POSITION, @ListenerPos(0))
alListenerfv(AL_VELOCITY, @ListenerVel(0))
alListenerfv(AL_ORIENTATION, @ListenerOri(0))

end sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' UnloadResources
'
sub UnloadQBSound()
   alutExit
   alDeleteBuffers(1, @Buffer)
   alDeleteSources(1, @Source)
  
end sub

And this is a simple driver program
Code:
'$include: 'qbSound.bi'
initQBSound
? "Hi"
sound 440,1.2
sleep
unloadQBSound

Now the question is how would I (or someone wiser than I) go about making this safe. By that I mean, if someone is calling the OpenAL stuff already, I would image this would conflict with the way this is coded.

Any advise?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)