Large number of Sound Effects with SimpleAudioEngine

1.8k views Asked by At

Background: My background music is being played as a sound effect because I want to change the pitch between each repetition of the tune.

The Issue: While the background music is playing there's a lot of other short sound effects happening. After a certain number of sound effects play, my background music (also a sound effect) cuts out. The queue seems to cycle so that after say 50 sound effects have played, when the 51st plays, the 1st will be released whether it has completed or not.

Request for direction: There are two directions I can see possibly going with this issue. 1. Not play the background music as an effect and figure out how to change the pitch as background music instead of an effect 2. Some how make sure that the effect will be retained until it has completed.

Thanks

1

There are 1 answers

6
CodeSmile On BEST ANSWER

The SimpleAudioEngine doesn't allow you to control channels, so it'll just play your sound effects in one of the channels it has allocated internally. Eventually all channels are playing audio. In that case, when a new audio file is to be played and all channels are already playing, SimpleAudioEngine will cancel one of the existing audio files. On occasion this will be your background music. Dang.

What you normally do to fix this is to assign (allocate) one audio channel specifically for the background music. No other audio will ever attempt to play audio on that channel. You will have to use the "regular" CocosDenshion API. It looks like you either need to use CDAudioManager or CDSoundEngine, but I have no experience with using channels with CocosDenshion.

Personally I never used CocosDenshion, whenever I needed more control I found the ObjectAL API to be easier to understand and it has excellent documentation. I made the following helper function for ObjectAL that plays a sound on a particular channel. I simply instantiated a ALChannelSource for my background music, and only played background music through that channel. The returned ALSoundSource even allows you to change pitch, pan, etc. while the audio is playing.

+(id<ALSoundSource>) playEffect:(NSString*)effect channel:(ALChannelSource*)channel loop:(bool)loop
{
    id<ALSoundSource> soundSource = nil;

    if (channel)
    {
        ALBuffer* buffer = [[OALAudioSupport sharedInstance] bufferFromFile:effect];
        soundSource = [channel play:buffer loop:loop];
    }

    return soundSource;
}

All other audio files I'm playing through the OALSimpleAudio class.