[bb] Sound Engine by TartanTangerine (was Indiepath) [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Sound Engine
Author : TartanTangerine (was Indiepath)
Posted : 1+ years ago

Description : The bulk of this code was written by someone else on this forum. It's a great bit of code but was missing something I needed. I needed to be able to chain sounds together, for example I needed snippits of speech to follow one another and I wanted this to happen without any interaction, other than some simple commands to set it up.

Anyway here it is... No comments I'm afriad, I've got too much other work to do...


Code :
Code (blitzbasic) Select
; ****************************************************************************
; MOD_SoundEngine

DebugLog "MOD_SoundEngine.bb"


; AUDIO CHANNEL CONSTANTS

Const CH_ANY = - 1
Const CH1 = 1
Const CH2 = 2
Const CH3 = 3
Const CH4 = 4
Const CH5 = 5
Const CH6 = 6
Const CH7 = 7
Const CH8 = 8
Const CH9 = 9
Const CH10 = 10

; ****************************************************************************

Type ActiveChannel
Field SampleID
    Field Address  ; Channel handle
    Field FadeTime#
    Field FadeStart#
    Field Volume#  
Field VolumeS# ; Start Volume
Field Queue[4]
Field QueueCount
End Type

; ****************************************************************************

Function PlaySample ( SampleAddress% , SampleID% = -1, Volume# = 1, Queue% = False )
    Local Channel.ActiveChannel
    Local Simultaneous% = False
    Local Playing% = False

    If SampleID < 0 Then Simultaneous = True
    For Channel = Each ActiveChannel

      If ChannelSampleID = SampleID

If Queue = True
ChannelQueueCount = ChannelQueueCount + 1
ChannelQueue[ChannelQueueCount] = SampleAddress
Playing = True
Else
Playing = True
EndIf

EndIf

      If Not ChannelPlaying ( ChannelAddress ) Then Delete Channel

    Next
    If Not ( Simultaneous = False And Playing = True )
      Channel = New ActiveChannel
      ChannelSampleID = SampleID
      ChannelAddress = PlaySound ( SampleAddress )
ChannelVolume ChannelAddress,Volume
ChannelVolume# = Volume
ChannelVolumes# = Volume
channelFadeTime = 0
    EndIf
End Function

; ****************************************************************************


Function UpdateChannels ()
Local Channel.ActiveChannel

    For Channel = Each ActiveChannel
    If Not ChannelPlaying ( ChannelAddress ) Then

If ChannelQueueCount = 0 Then
Delete Channel
Return
Else
ChannelAddress = PlaySound(ChannelQueue[1])
For a = 1 To ChannelQueueCount - 1
ChannelQueue[a] = ChannelQueue[a+1]
Next
ChannelQueueCount = ChannelQueueCount - 1
EndIf
EndIf
        If ChannelFadeTime
        If MilliSecs () - ChannelFadeStart >= ChannelFadeTime
            StopChannel ChannelAddress
                Delete Channel
            Else
                ChannelVolume = ChannelVolumeS - Float ( MilliSecs () - ChannelFadeStart ) / ChannelFadeTime
                ChannelVolume ChannelAddress , ChannelVolume
            End If
        End If
    Next
End Function

; ****************************************************************************

Function SoundOff ()  
  Local Channel.ActiveChannel  
  For Channel = Each ActiveChannel  
    StopChannel ChannelAddress  
    Delete Channel  
  Next
End Function

; ****************************************************************************

Function ReleaseSample ( SampleID , TimeOut# = 0 )
Local Channel.ActiveChannel
    For Channel = Each ActiveChannel
   If Not ChannelFadeTime
        If ChannelSampleID = SampleID
            If TimeOut
                ChannelFadeTime = TimeOut
                     ChannelFadeStart = MilliSecs ()
                 Else
                     StopChannel ChannelAddress
                     Delete Channel
                 End If
             End If
        End If
    Next
End Function


Comments : none...