[Gambas-user] gb3: gb.sdl.sound disallows more than 8 sounds/channels at once

Kevin Fishburne kevinfishburne at ...1887...
Sun Jul 24 09:43:49 CEST 2011


I can play eight sounds simultaneously, adjusting their volumes as 
needed, but when I try to set the volume of the channel of the ninth 
sound I receive an error saying that the channel is null (even though 
the ninth sound actually starts playing at a volume of 1).

Here is my code for the module:

' Gambas module file

' Audio module

' General declarations.
Public Struct Effect  ' Structure containing an individual sound 
effect's data.
   Sample As Sound       ' Waveform data (file).
   Chan As Channel       ' Default channel to play on.
   AmpCurrent As Single  ' Current amplitude.
   AmpTarget As Single   ' Target amplitude.
   AmpScale As Single    ' Target amplitude multiplier.
End Struct
Public Environment[16] As Struct Effect ' Environmental sound effects.
Public Dynamic[16] As Struct Effect     ' Dynamic sound effects.
Public WaterDeepCount As Single         ' Total number of deep water tiles.
Public WaterDeepDistance As Single      ' Total distance of deep water 
tiles.
Public WaterMediumCount As Single       ' Total number of medium water 
tiles.
Public WaterMediumDistance As Single    ' Total distance of medium water 
tiles.
Public WaterShallowCount As Single      ' Total number of shallow water 
tiles.
Public WaterShallowDistance As Single   ' Total distance of shallow 
water tiles.
Public LandCount As Single              ' Total number of land tiles.
Public LandDistance As Single           ' Total distance of land tiles.

Public Sub Update()

   ' Analyze area around player to determine effect amplitudes.

   ' General declarations.
   Dim x As Byte
   Dim y As Byte
   Dim WaterDepth As Short             ' Depth of current cell grid tile.

   ' Reset counters.
   WaterDeepCount = 0
   WaterDeepDistance = 0
   WaterMediumCount = 0
   WaterMediumDistance = 0
   WaterShallowCount = 0
   WaterShallowDistance = 0
   LandCount = 0
   LandDistance = 0

   ' Calculate water and land tile counts and distances from camera.
   For y = Render.ccgy - 32 To Render.ccgy + 31 Step 4
     For x = Render.ccgx - 32 To Render.ccgx + 31 Step 4
       ' Determine if current tile is water or land.
       If Render.delevation[x, y] < 0 Then
         ' Perform water tile calculations.
         WaterDepth = Render.delevation[x, y]
         ' Determine effect type from water depth.
         If WaterDepth >= -32768 And WaterDepth < -2048 Then
           ' Increment deep water count.
           Inc WaterDeepCount
           ' Increment deep water distance.
           WaterDeepDistance = WaterDeepDistance + 
Convert.Distance(Render.ccgx, Render.ccgy, x, y)
         Endif
         If WaterDepth >= -2048 And WaterDepth < -128 Then
           ' Increment medium water.
           Inc WaterMediumCount
           ' Increment medium water distance.
           WaterMediumDistance = WaterMediumDistance + 
Convert.Distance(Render.ccgx, Render.ccgy, x, y)
         Endif
         If WaterDepth >= -128
           ' Increment shallow water.
           Inc WaterShallowCount
           ' Increment shallow water distance.
           WaterShallowDistance = WaterShallowDistance + 
Convert.Distance(Render.ccgx, Render.ccgy, x, y)
         Endif
       Else
         ' Increment land.
         Inc LandCount
         ' Increment land distance.
         LandDistance = LandDistance + Convert.Distance(Render.ccgx, 
Render.ccgy, x, y)
       Endif
     Next
   Next

   ' Convert tile counts to percentages of occurrence (0 - 1).
   WaterDeepCount = WaterDeepCount / 256
   WaterMediumCount = WaterMediumCount / 256
   WaterShallowCount = WaterShallowCount / 256
   LandCount = LandCount / 256

   ' Convert tile distances to percentages of distance (0 - 1).
   WaterDeepDistance = WaterDeepDistance / 6350
   WaterMediumDistance = WaterMediumDistance / 6350
   WaterShallowDistance = WaterShallowDistance / 6350
   LandDistance = LandDistance / 6350

   ' Calculate target water amplitudes.
   Environment[0].AmpTarget = (WaterDeepCount + WaterDeepDistance) / 2 * 
Environment[0].AmpScale
   Environment[1].AmpTarget = (WaterMediumCount + WaterMediumDistance) / 
2 * Environment[1].AmpScale
   Environment[2].AmpTarget = (WaterShallowCount + WaterShallowDistance) 
/ 2 * Environment[2].AmpScale
   Environment[3].AmpTarget = LandCount * Environment[0].AmpTarget * 
Environment[3].AmpScale
   Environment[4].AmpTarget = LandCount * Environment[1].AmpTarget * 
Environment[4].AmpScale
   Environment[5].AmpTarget = LandCount * Environment[2].AmpTarget * 
Environment[5].AmpScale

   ' Adjust current water amplitudes.
   Environment[0].AmpCurrent = (Environment[0].AmpCurrent + 
Environment[0].AmpTarget) / 2 * Environment[0].AmpScale
   Environment[1].AmpCurrent = (Environment[1].AmpCurrent + 
Environment[1].AmpTarget) / 2 * Environment[1].AmpScale
   Environment[2].AmpCurrent = (Environment[2].AmpCurrent + 
Environment[2].AmpTarget) / 2 * Environment[2].AmpScale
   Environment[3].AmpCurrent = (Environment[3].AmpCurrent + 
Environment[3].AmpTarget) / 2 * Environment[3].AmpScale
   Environment[4].AmpCurrent = (Environment[4].AmpCurrent + 
Environment[4].AmpTarget) / 2 * Environment[4].AmpScale
   Environment[5].AmpCurrent = (Environment[5].AmpCurrent + 
Environment[5].AmpTarget) / 2 * Environment[5].AmpScale

   ' Set audible water amplitudes.
   Environment[0].Chan.Volume = Environment[0].AmpCurrent
   Environment[1].Chan.Volume = Environment[1].AmpCurrent
   Environment[2].Chan.Volume = Environment[2].AmpCurrent
   Environment[3].Chan.Volume = Environment[3].AmpCurrent
   Environment[4].Chan.Volume = Environment[4].AmpCurrent
   Environment[5].Chan.Volume = Environment[5].AmpCurrent

   ' Calculate target rain amplitude.
   Environment[6].AmpTarget = Client.CloudCover - 125
   If Environment[6].AmpTarget < 0 Then Environment[6].AmpTarget = 0
   Environment[6].AmpTarget = Convert.Range(Environment[6].AmpTarget, 0, 
31, 0, 1) * Environment[6].AmpScale

   ' Adjust current rain amplitude.
   If Environment[6].AmpCurrent < Environment[6].AmpTarget Then
     Environment[6].AmpCurrent = Environment[6].AmpCurrent + 0.01
   Else
     Environment[6].AmpCurrent = Environment[6].AmpCurrent - 0.01
   Endif

   ' Set audible rain amplitude.
   Environment[6].Chan.Volume = Environment[6].AmpCurrent

   ' Calculate target wind amplitude.
   Environment[7].AmpTarget = Abs(Client.WindSpeed)
   Environment[7].AmpTarget = Convert.Range(Environment[7].AmpTarget, 0, 
31, 0, 1) * Environment[7].AmpScale

   ' Adjust current wind amplitude.
   If Environment[7].AmpCurrent < Environment[7].AmpTarget Then
     Environment[7].AmpCurrent = Environment[7].AmpCurrent + 0.01
   Else
     Environment[7].AmpCurrent = Environment[7].AmpCurrent - 0.01
   Endif

   ' Set audible wind amplitude.
   Environment[7].Chan.Volume = Environment[7].AmpCurrent

   ' Calculate target arctic amplitude.
   If Client.worldz >= 16384 Then
     Environment[8].AmpTarget = Convert.Range(Client.worldz, 16384, 
32767, 0, 1) * Environment[8].AmpScale
   Else
     Environment[8].AmpTarget = 0
   Endif

   ' Adjust current arctic amplitude.
   If Environment[8].AmpCurrent < Environment[8].AmpTarget Then
     Environment[8].AmpCurrent = Environment[8].AmpCurrent + 0.01
   Else
     Environment[8].AmpCurrent = Environment[8].AmpCurrent - 0.01
   Endif

   ' Set audible arctic amplitude.
   Environment[8].Chan.Volume = Environment[8].AmpCurrent

End

Public Sub Initialize()

   ' Start basic environmental sound effects.

   Channels.Count = 32

   ' Start deep water effect.
   Environment[0].Sample = New Sound(GUI.basepath & "/sound/Ocean.wav")
   Environment[0].AmpScale = 1
   Environment[0].Chan = Environment[0].Sample.Play(-1)
   Environment[0].Chan.Volume = 0

   ' Start medium water effect.
   Environment[1].Sample = New Sound(GUI.basepath & "/sound/River.wav")
   Environment[1].AmpScale = 1
   Environment[1].Chan = Environment[1].Sample.Play(-1)
   Environment[1].Chan.Volume = 0

   ' Start shallow water effect.
   Environment[2].Sample = New Sound(GUI.basepath & "/sound/Lake.wav")
   Environment[2].AmpScale = 1
   Environment[2].Chan = Environment[2].Sample.Play(-1)
   Environment[2].Chan.Volume = 0

   ' Start deep seawash effect.
   Environment[3].Sample = New Sound(GUI.basepath & "/sound/Seawash, 
Deep.wav")
   Environment[3].AmpScale = 1
   Environment[3].Chan = Environment[3].Sample.Play(-1)
   Environment[3].Chan.Volume = 0

   ' Start medium seawash effect.
   Environment[4].Sample = New Sound(GUI.basepath & "/sound/Seawash, 
Medium.wav")
   Environment[4].AmpScale = 1
   Environment[4].Chan = Environment[4].Sample.Play(-1)
   Environment[4].Chan.Volume = 0

   ' Start shallow seawash effect.
   Environment[5].Sample = New Sound(GUI.basepath & "/sound/Seawash, 
Shallow.wav")
   Environment[5].AmpScale = 1
   Environment[5].Chan = Environment[5].Sample.Play(-1)
   Environment[5].Chan.Volume = 0

   ' Start rain effect.
   Environment[6].Sample = New Sound(GUI.basepath & "/sound/Rain.wav")
   Environment[6].AmpScale = 1
   Environment[6].Chan = Environment[6].Sample.Play(-1)
   Environment[6].Chan.Volume = 0

   ' Start wind effect.
   Environment[7].Sample = New Sound(GUI.basepath & "/sound/Wind.wav")
   Environment[7].AmpScale = 1
   Environment[7].Chan = Environment[7].Sample.Play(-1)
   Environment[7].Chan.Volume = 0

   ' Start arctic effect.
   Environment[8].Sample = New Sound(GUI.basepath & "/sound/Arctic.wav")
   Environment[8].AmpScale = 1
   Environment[8].Chan = Environment[8].Sample.Play(-1)
   Environment[8].Chan.Volume = 0

End

It dies at the last line, "Environment[8].Chan.Volume = 0". As I said, 
Arctic.wav starts playing, but setting the volume level of its channel 
treats the channel as a null object. Removing a different sound effect 
so that there are only eight instead of nine works fine. Anyone have any 
insight into why this is occurring? The SDL lib says it supports 32 
channels, but it looks like it's just eight with a bug in the way it 
handles more than that.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list