Cocos2D Audio – CocosDenshion

Introduction

In this iDevBlogADay tutorial I will show you how to add music and effects to your iPhone game. You will learn how to play effects passing pan, pitch & gain parameters, preload background music & adding steps effects. We will use CocosDenshion to achieve this. This audio library is already included in Cocos2D, so you will not need to download or configure anything! For a complete overview of what CocosDenshion is please read its FAQ at http://cocos2d-iphone.org/wiki/doku.php/cocosdenshion:faq

Supported formats

CocosDenshion supports the following audio formats:

MP3 (IOS 3.0 onward)
WAV
IMA4 (1/4 the size of a WAV file)

To convert a WAV file to IMA4:

Open Terminal and type:
afconvert -f caff -d ima4 mysound.wav

The code

Add this line at the beginning of your implementation file (below #import “HelloWorldLayer.h”)

#import "SimpleAudioEngine.h"

Add these lines inside your init method

//Preloading background music
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"background.mp3"];

//Preloading effects
[[SimpleAudioEngine sharedEngine] preloadEffect:@"asphalt1.mp3"];

//Play background music
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.mp3"];

//Play effect
[[SimpleAudioEngine sharedEngine] playEffect:@"effect.wav"];

//Play effect advanced settings
[[SimpleAudioEngine sharedEngine] playEffect:stepEffect pitch:1.0f pan:0.0f gain:0.6f];

Values:
pitch: 0.5f (half the speed) to 2.0f (twice the speed). 1.0f to play normal speed
pan: -1.0f fully left, 1.0f fully right. 0.0f centered.
pitch: 1.0f unchanged. Each division by 2 equals a -6dB reduction

//Pause background music
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];

// Background music volume
[CDAudioManager sharedManager].backgroundMusic.volume = 1.0f;

// Get noticed when background music stops
[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self selector:@selector(backgroundMusicFinished)]; 

// Loop background music
[[CDAudioManager sharedManager] playBackgroundMusic:@"background.mp3" loop:YES];
[CDAudioManager sharedManager].backgroundMusic.numberOfLoops = 3; //To loop 3 times

Adding Steps:

Use this code to add steps sounds to your characters.

Add this line to your interface file:

CCLabelTTF *label;
BOOL bStepEffect;

Add these functions above your init method:

-(void) backgroundMusicFinished{

    CCLOG(@"Background Music Completed");
    [label setString:@"Music completed"];

}

-(void) playStepEffect {

    [[SimpleAudioEngine sharedEngine] playEffect:@"asphalt1.mp3" pitch:1.0f pan:0.0f gain:0.6f];

}

- (void) playSteps: (ccTime) dt{

    [self playStepEffect];

}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

    CCLOG(@"Start/Stop Steps");

    if(bStepEffect == FALSE){

        [self playStepEffect];
        [self schedule:@selector(playSteps:) interval:0.3f];
        bStepEffect = TRUE;

    }else{

        [self unschedule:@selector(playSteps:)];
        bStepEffect = FALSE;

    }

    return TRUE;

}

Finally, add the following lines inside your init method:

bStepEffect = FALSE;

self.isTouchEnabled = YES;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

Positional Audio:

@Crocodella has an excellent article explaining what positional audio is and its benefits.
“The term itself is pretty self-explanatory, in that positional audio is the simulation of the ability that we humans have to judge the approximate distance and direction of a sound source, and also the physical properties of sound based on the environment.” You can find the complete tutorial here:
http://www.crocodella.com.br/2011/05/positional-audio-with-cocos2d-and-cocosdenshion/

Professional Music:

Our game music is being composed by @wblackall, he’s doing an amazing job, so I recommend him if you plan on adding background music to your game.

Sources
Click here to download the source code of this tutorial

Posted By admin

    2 Responses to “Cocos2D Audio – CocosDenshion”

  1. griotspeak says:

    How accurately can you schedule audio events with this? I have not used cocos2d but am considering attending a workshop. One of my biggest concerns is whether or not there is an ‘easy’ way to approach sample accurate audio for something like a rhythm game.

  2. admin says:

    You can schedule different audio effects of background music playing all at the same time. You can even control when one song has finished playing to schedule another, or set timers to overlap them. If you are new to game development I recommend Cocos2D, there are many developers willing to help out there.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>