October 29, 2020, 01:46:32 AM

Author Topic: [bb] Sound Engine by TartanTangerine (was Indiepath) [ 1+ years ago ]  (Read 1217 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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
  1. ; ****************************************************************************
  2. ; MOD_SoundEngine
  3.  
  4. DebugLog "MOD_SoundEngine.bb"
  5.  
  6.  
  7. ; AUDIO CHANNEL CONSTANTS
  8.  
  9. Const CH_ANY = - 1
  10. Const CH1 = 1
  11. Const CH2 = 2
  12. Const CH3 = 3
  13. Const CH4 = 4
  14. Const CH5 = 5
  15. Const CH6 = 6
  16. Const CH7 = 7
  17. Const CH8 = 8
  18. Const CH9 = 9
  19. Const CH10 = 10
  20.  
  21. ; ****************************************************************************
  22.  
  23. Type ActiveChannel
  24.         Field SampleID
  25.     Field Address  ; Channel handle
  26.     Field FadeTime#
  27.     Field FadeStart#
  28.     Field Volume#  
  29.         Field VolumeS# ; Start Volume
  30.         Field Queue[4]
  31.         Field QueueCount
  32. End Type
  33.  
  34. ; ****************************************************************************
  35.  
  36. Function PlaySample ( SampleAddress% , SampleID% = -1, Volume# = 1, Queue% = False )
  37.     Local Channel.ActiveChannel
  38.     Local Simultaneous% = False
  39.     Local Playing% = False
  40.  
  41.     If SampleID < 0 Then Simultaneous = True
  42.     For Channel = Each ActiveChannel
  43.  
  44.         If ChannelSampleID = SampleID
  45.  
  46.                         If Queue = True
  47.                                 ChannelQueueCount = ChannelQueueCount + 1
  48.                                 ChannelQueue[ChannelQueueCount] = SampleAddress
  49.                                 Playing = True
  50.                         Else
  51.                                 Playing = True
  52.                         EndIf
  53.                        
  54.                 EndIf
  55.                
  56.         If Not ChannelPlaying ( ChannelAddress ) Then Delete Channel
  57.  
  58.     Next
  59.     If Not ( Simultaneous = False And Playing = True )
  60.         Channel = New ActiveChannel
  61.         ChannelSampleID = SampleID
  62.         ChannelAddress = PlaySound ( SampleAddress )
  63.                 ChannelVolume ChannelAddress,Volume
  64.                 ChannelVolume# = Volume
  65.                 ChannelVolumes# = Volume
  66.                 channelFadeTime = 0
  67.     EndIf
  68. End Function
  69.  
  70. ; ****************************************************************************
  71.  
  72.  
  73. Function UpdateChannels ()
  74.         Local Channel.ActiveChannel
  75.  
  76.     For Channel = Each ActiveChannel
  77.         If Not ChannelPlaying ( ChannelAddress ) Then
  78.  
  79.                         If ChannelQueueCount = 0 Then
  80.                                 Delete Channel
  81.                                 Return
  82.                         Else
  83.                                 ChannelAddress = PlaySound(ChannelQueue[1])
  84.                                 For a = 1 To ChannelQueueCount - 1
  85.                                         ChannelQueue[a] = ChannelQueue[a+1]
  86.                                 Next
  87.                                 ChannelQueueCount = ChannelQueueCount - 1
  88.                         EndIf
  89.                 EndIf
  90.         If ChannelFadeTime
  91.                 If MilliSecs () - ChannelFadeStart >= ChannelFadeTime
  92.                 StopChannel ChannelAddress
  93.                 Delete Channel
  94.             Else
  95.                 ChannelVolume = ChannelVolumeS - Float ( MilliSecs () - ChannelFadeStart ) / ChannelFadeTime
  96.                 ChannelVolume ChannelAddress , ChannelVolume
  97.             End If
  98.         End If
  99.     Next
  100. End Function
  101.  
  102. ; ****************************************************************************
  103.  
  104. Function SoundOff ()  
  105.         Local Channel.ActiveChannel  
  106.         For Channel = Each ActiveChannel  
  107.         StopChannel ChannelAddress  
  108.         Delete Channel  
  109.         Next
  110. End Function
  111.  
  112. ; ****************************************************************************
  113.  
  114. Function ReleaseSample ( SampleID , TimeOut# = 0 )
  115.         Local Channel.ActiveChannel
  116.     For Channel = Each ActiveChannel
  117.             If Not ChannelFadeTime
  118.              If ChannelSampleID = SampleID
  119.                      If TimeOut
  120.                      ChannelFadeTime = TimeOut
  121.                      ChannelFadeStart = MilliSecs ()
  122.                  Else
  123.                      StopChannel ChannelAddress
  124.                      Delete Channel
  125.                  End If
  126.              End If
  127.         End If
  128.     Next
  129. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal