iOS - ofSoundPlayer causing frame rate to drop

Hi all,

I am working on a cross-platform app that uses, among other things, ofSoundPlayer to trigger sounds. It is working great on Mac OS X, but as we started developing for iOS we noticed that there was a significant drop in the frame rate as sounds were being played. So we came up with the following code to try and isolate the problem.

in ofApp.mm:

void ofApp::setup(){	
	
	ofBackground(127,127,127);
    
    player1.load("switch_on.wav", false);
    player1.setVolume(0.7f);
}

void ofApp::draw(){
    
    cout << ofGetFrameRate() << endl;

}

void ofApp::touchDown(ofTouchEventArgs &touch){
    
        player1.play();
}

So it prints the frame rate to the console on each frame. “switch_on.wav” is a .wav sound in 44 100/16bits, lasting for 0.16 seconds. Here’s what happens (tested with a metronome):

  • When there’s no interaction it prints a frame rate of 60, as you would expect;

  • Clicking once each second first causes it to drop to around 53, then it becomes stable around 58;

  • Clicking four times each second causes it to drop at first to around 48 then it becomes stable around 50. It also causes it not to trigger some of the sounds, now and again;

  • Clicking 8 times each second causes it to drop to 35, and it becomes stable around that value;

  • Clicking as fast as I can causes the frame rate to drop to 11, and no sound is played at all.

I think this may be similar to this topic right here, but I couldn’t get ofxOpenALSoundPlayer to work, with the following error:

Field type 'ofxOpenALSoundPlayer' is an abstract class

Any help will be greatly appreciated!

Edit: forgot to mention. Testing on iPhone 5!

Yeah unfortunately it is a HUGE fps drop.
To explain why, internally iOS is loading the file and creating the buffers on the fly.

I created my own addon just to avoid the core iOS sound player as I am always chasing a 60 FPS world.
Feel free to use my addon which wraps and uses the core sound engine originally created for Cocos2d-x.
What you need is a sound manager that pushes the audio into 2 buffers (music, effects) and then play them from the buffer. ofxCocosDenshion does this perfectly :slightly_smiling:

https://github.com/danoli3/ofxCocosDenshion

It’s pretty well tested but feel free to fix or add things as you see and PR.

Currently used in 2 of my iOS projects: Super Hexagon and Freddy’s Pass Off

1 Like

Thank you very much for your comment @danoli3 :slightly_smiling:

Just tested everything and it’s running a smooth and stable 60 FPS.

By the way, I came across a minor bug in your code and created a PR at GitHub!

1 Like