Cocos Denshion: Play sound effect in sync with music
Asked Answered
B

3

6

I am making a music game and when the user presses a note it will produce a sound. The sound naturally needs to play immediately when the user presses, so they can tell whether they are in time with the music. However, it feels as if the sound is lagging, especially when note presses become quicker.

My background .m4a music file is played with AVAudioPlayer. I chose to use this over Cocos Denshion as I have access to the currentTime property. I may be wrong, but I dont think I can access this with CocosDenshion.

I made a .wav file which is extremely short (less than a second). I preload my sound effect on init:

[[SimpleAudioEngine sharedEngine] preloadEffect:@"Assist.wav"];

Then to play the sound effect, in CCTouchesBegan I call:

[[SimpleAudioEngine sharedEngine] playEffect:@"Assist.wav"];

After that it calls my code to determine the users timing and awards points. Any idea why it might be lagging, or a better way to play sound effects in time with music?

EDIT: Ive tried a few things recently with no results. First I tried playing the sounds automatically as they came up to the appropriate time in the song. Still had the lag, so I dont think it is touch events being slow. I also tried 3 different sound libraries.

However, when I ran in the simulator, it seemed to not be laggy. Does anyone have an idea? Im clueless and its a major feature I cant really take out...

Boreas answered 9/1, 2012 at 10:34 Comment(1)
Please provide information on what devices are you running, and x-code instruments information on cpu load and gpu load, and also an idea of how many sounds at the same time are playing, and in what format is your background music (guessing that there is one).Jeanettajeanette
P
1

you should avoid this code:- [[SimpleAudioEngine sharedEngine] preloadEffect:@"Assist.wav"];

with the start of app you should load your framework SimpleAudioEngine by writing this code :-

//SimpleAudioEngine *palySound; made object in .h file. palySound=[SimpleAudioEngine sharedEngine];

and whenever you want to play sound you can write: [palySound playEffect:@"Assist.wav"];

Precess answered 9/1, 2012 at 11:49 Comment(3)
Thanks for the reply. I tried this, but it doesnt seem to make any different. The sound is still lagging behind.Boreas
Did you load SimpleAudioEngine in appdelegate in didFinishLaunchingWithOptions...Precess
Yeah didnt seem to make a difference. Maybe the touch events are slow?Boreas
A
0

I am not sure what you're doing in your SoundEngine, but in my own experience, the best way to not get lag to play a sound is to assign an AVAudioPlayer for each sound file (unless you want to start messing around with AudioQueues).

Here it is an example:

Let's assume that you have an AVAudioPlayer *assistPlayer; in your current view controller.

In your viewDidLoad initialize it with your sound:

NSURL *wavURL = [[NSBundle mainBundle] URLForResource:@"Assist" withExtension:@"wav"];
assistPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:wavURL error:nil];

Then, in your IBAction where you want to play the file, just do:

[assistPlayer play];

You shouldn't get any lag.

Archdiocese answered 17/2, 2012 at 14:36 Comment(2)
Tried but it didnt seem to make a difference. Actually got less performance. Denshion is a pretty thin wrapper for openAL.Boreas
Whats the sampling frequency and bit depth of your wav file? Have you tried to resample it down and use it just 16 bits?Archdiocese
R
0

Did you try Finch? It claims to play sounds with low latency, and it is also just a wrapper around OpenAL.

Other than that, I'm really not experienced with OpenAL, but can think of two possible reasons for your lag:

  1. The main thread is too busy - Try to offload work from it to other threads.

  2. Perhaps OpenAL is defined with too large of a buffer, so the pipeline loads the entire sound into the buffer (or a big chunk of it), and only afterwards the playback starts.

Relator answered 19/2, 2012 at 21:8 Comment(1)
Yeah I gave Finch a try but had the same results as Denshion. I was also able to replicate this in a very simple sample project github.com/so3arbelnox/soundtestBoreas

© 2022 - 2024 — McMap. All rights reserved.