Code archives/Audio/Sound Sorter

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

Download source code

Sound Sorter by Rick2002
This sets up 16 channels (editable by changing the channel constant) that will manage your sound calls to help avoid sounds that interfere with one another. It will first look for an unused channel and if all are in use will look to see if the newly called sound's priority overides that of a currently playing sound and will replace it if it will.

This also detects if sound effects are on or off with the SoundOff variable. If SoundOff is true then it will not assign sounds. If you wish to have a seperate MusicOff variable then I would advise making our MusicOff check before calling the RequestSound function. If MusicOff is True then don't let it call the function.

You need only to call RequestSound once to play your sound, but multiple calls to it will ignore sounds already played - however if you call the same sound twice with different parameters (volume for example) then it will update the playing sound to change its parameters. In this way you can have sound increase or decrease, change the pitch of a sound, or even continue to hold the priority of a sound. You can also easily have a sound decrease by simply supplying a value in the "silence" parameter.

Make calls to the UpdateSound() function with every pass thru your program. It will degrade the priority so that new soundcalls will naturally have priority over older ones. This also searches out used channels that have finished playing sounds and turns them off so that future soundrequests can access the unused channels.

To kill a sound simply call a currently playing sound with the job attribute set to "1". To loop a sound have the job attribute set to "2".

The sound you send in the RequestSound function should be a previously preloaded sound. Example :

Global sndMySound=LoadSound"sound\mySound.wav"

The call would look like :

RequestSound(sndMySound,100,1,0,-1,0)

If you have any questions you can contact me thru http://dewstation.tripod.com
Const Channels = 15

Type Sound_Struc
    Field Channel        ;Channel used to play
    Field Sound            ;Sound to be played
    Field Priority#        ;Importance Rating of Sound
    Field Volume#        ;How loud to play Sound
    Field silence#        ;How fast Volume decreases
    Field Pitch            ;Htz pitch to play sound
End Type
    Global Play.Sound_Struc = New Sound_Struc

SeedRnd(MilliSecs())
For a = 0 To channels        ;As program starts or reloops we want
Delete play                    ;to make sure all Channels are cleared
Next                        ;of possible old sounds

;Create 16 Empty Channels
For a = 0 To channels
Play.Sound_Struc = New Sound_Struc
Play\Channel    =0            ;what sound is being played
play\sound        =0            ;what sound is remembered
play\priority =0            ;sounds priority
play\volume        =0            ;Sound Volume
play\silence    =0            ;Volume decrease rate
play\Pitch        =0            ;Hertz Range
Next

;XXXXXXXXXXXXXXXXXXXXX
;Main Program with all sound calls
;XXXXXXXXXXXXXXXXXXXXX

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Function UpdateSound()
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
;Update the channel information

For a.sound_struc = Each sound_struc
    If ChannelPlaying (a\channel) Then
        a\volume=a\volume-(a\silence*.00001)
        If a\volume<0 Then a\volume=0
        ChannelVolume a\channel,a\volume
        If a\pitch <> -1 Then ChannelPitch a\channel,a\pitch
        a\priority=a\priority-.01
            If a\priority<1 Then a\priority=1
    Else
        a\sound=0
        a\priority=0
        a\volume=0
        a\silence=0
        a\pitch=0
    EndIf
    If a\volume=0 Or a\priority<0 And a\sound <> 0 Then StopChannel a\channel
Next

End Function


;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Function RequestSound(Sound,priority,volume#,silence,pitch,job)
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    ;sound,priority,volume,silence,pitch,job
    
    ;sound - Any sound variable loaded with a sound
    ;priority - importance of sound in the event that it needs to overide another
    ;volume - how loud to play the sound
    ;silence - how fast volume is decreased *.00001
    ;pitch - what hertz to play sound at
    ;job - 0=None 1=kill 2=Loop (only loops new sounds)

If (Not soundoff) Then         ;Global Sound Flag
    SoundPlaced=False
    ;Determine if requested sound already exhists
    For play.sound_struc = Each sound_struc
        If play\sound = sound Then
            SoundPlaced=True
            If job = 1 Then            ;Do we kill the current sound?
                If ChannelPlaying (play\channel) Then StopChannel play\channel
                play\priority=0
                play\volume=0
                play\silence=0
                play\pitch=0
            Else                    ;If not a kill then update Sound Variables
                play\priority=priority
                play\volume=volume
                play\silence=silence
                play\pitch=pitch
                If play\pitch <> -1 Then ChannelPitch play\channel,play\pitch
            EndIf
        EndIf
    Next
    
    ;Is there an open channel?
    If (Not SoundPlaced) And job <> 1 Then
        For play.sound_struc = Each sound_struc
            If (Not ChannelPlaying (play\channel)) And (Not SoundPlaced) Then
                SoundPlaced = True
                play\sound = sound
                play\priority=priority
                play\volume=volume
                play\silence=silence
                play\pitch=pitch
            ;    If play\pitch <> -1 Then ChannelPitch play\channel,play\pitch
                If job = 2 Then LoopSound(sound)
                play\channel = PlaySound(sound)
                
            EndIf
        Next
    EndIf
    ;Is the priority higher than exhisting Sound?
    If (Not soundplaced) And job <> 1 Then
        For play.sound_struc = Each sound_struc
            If priority >= play\priority Then
                StopChannel play\channel
                play\sound = sound
                play\priority=priority
                play\volume=volume
                play\silence=silence
                play\pitch=pitch
            ;    If play\pitch <> -1 Then ChannelPitch play\channel,play\pitch
                If job = 2 Then LoopSound(sound)
                play\channel = PlaySound(sound)
                
            EndIf
        Next
    EndIf
EndIf
End Function

Comments

-Rick-2009
How does one claim this as public domain? I put it on here, and I see that there are better examples available, but I have not qualms of anyone using it if they find it useful.


plash2009
As soon as you post the code you claim it as public domain.

.. I would assume it applies to all that were made before the license agreement was changed as well.


xlsior2009
.. I would assume it applies to all that were made before the license agreement was changed as well.


Kind of a grey area, since the poster didn't explicitly agree to it at that time.


Perturbatio2009
.. I would assume it applies to all that were made before the license agreement was changed as well.


Kind of a grey area, since the poster didn't explicitly agree to it at that time.

All code archive posters were notified and asked to give their permission when this changed, the ones that didn't agree were removed (from what I recall).


Code Archives Forum