Code archives/Audio/Sound Engine

This code has been declared by its author to be Public Domain code.

Download source code

Sound Engine by TartanTangerine (was Indiepath)2005
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...
; ****************************************************************************
; 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 Channel\SampleID = SampleID 

			If Queue = True
				Channel\QueueCount = Channel\QueueCount + 1
				Channel\Queue[Channel\QueueCount] = SampleAddress
				Playing = True
			Else
				Playing = True
			EndIf
			
		EndIf
		
      	If Not ChannelPlaying ( Channel\Address ) Then Delete Channel

    Next
    If Not ( Simultaneous = False And Playing = True )
      	Channel = New ActiveChannel
      	Channel\SampleID = SampleID
      	Channel\Address = PlaySound ( SampleAddress )
		ChannelVolume Channel\Address,Volume
		Channel\Volume# = Volume
		Channel\Volumes# = Volume
		channel\FadeTime = 0
    EndIf
End Function

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


Function UpdateChannels ()
	Local Channel.ActiveChannel

    For Channel = Each ActiveChannel
    	If Not ChannelPlaying ( Channel\Address ) Then 

			If Channel\QueueCount = 0 Then
				Delete Channel
				Return
			Else
				Channel\Address = PlaySound(Channel\Queue[1])
				For a = 1 To Channel\QueueCount - 1
					Channel\Queue[a] = Channel\Queue[a+1]
				Next
				Channel\QueueCount = Channel\QueueCount - 1
			EndIf
		EndIf
        If Channel\FadeTime
        	If MilliSecs () - Channel\FadeStart >= Channel\FadeTime
            	StopChannel Channel\Address
                Delete Channel
            Else
                Channel\Volume = Channel\VolumeS - Float ( MilliSecs () - Channel\FadeStart ) / Channel\FadeTime
                ChannelVolume Channel\Address , Channel\Volume
            End If
        End If
    Next
End Function

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

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

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

Function ReleaseSample ( SampleID , TimeOut# = 0 )
	Local Channel.ActiveChannel
    For Channel = Each ActiveChannel
	    If Not Channel\FadeTime
    	     If Channel\SampleID = SampleID
        	     If TimeOut
            	     Channel\FadeTime = TimeOut
                     Channel\FadeStart = MilliSecs ()
                 Else
                     StopChannel Channel\Address
                     Delete Channel
                 End If
             End If
        End If
    Next
End Function

Comments

None.

Code Archives Forum