Sound effect pitch

Hi,

I was wondering is it possible to pitch same instance of Sound effect ?
I’m using cocos2d-x v3.3. Also i’ve noticed that in new AudioEngine there is no option to pitch the sound effect.

BR

Any suggestions ?

I’ve read that it’s hard to support on Android, but I’m definitely still interested in a solution to this.

Is there something broken in SimpleAudioEngine?
I thought I saw somewhere that there is an issue with using SimpleAudioEngine on Android 5 Lollipop, but not sure.

I was able to get pitch setting to work on Android, iOS and WP8 using the SimpleAudioEngine in Cocos2d-x v2.x and v3.x.

Also extended SimpleAudioEngine to be able to modulate the pitch and gain of a looping effect.
Sounds good on iOS and Android, but kind of crappy on WP8.

I’d be interested in seeing your code for extending SimpleAudioEngine if you could share?

Also - can you change the pan value similarly?

I posted a sample project using Cocos2d-x 3.7 rc0 that shows how I modulate gain and pitch.

This same code has been used with previous versions of Cocos2d-x 3.x and 2.x.

@Maxxx It probably would not be too much work to add pan.
The code I posted currently works for iOS, Mac and Android 4.x.
Not sure about Android 5.
There are build issues with WP8.1 Universal mmreg.h. Have not figured that out yet.
https://github.com/jimrange/HeyaldaSimpleAudioEngine

I’m still very curious if there is something wrong with SimpleAudioEngine and why there is a new audio engine.

Hi
I cloned your repository from github & added the panning option - works fine (for IOS at least - Mac panning doesn’t work properly anyway, and I didn’t update other platforms)

I’m a GIT noob, and haven’t figured out how to make a pull request so you can update it if you want (probably a permissions thing?)

This is a very interesting topic since more control over audio is always useful.
There is a C++ library, that does exactly what @maxx requested - SAPL:
http://www.surina.net/soundtouch/

SAPL examples:
http://www.surina.net/soundtouch/soundstretch.html#examples

I wonder if these features can be implemented with Cocos2D-X AudioEngine.
Or does the 3.6 AudioEngine has timestretch, pitch and panning meanwhile?

Found the article about the SimpleAudioEngine performance issue on Android 5: http://blog.cocos2d-x.org/2015/04/performance-issue-caused-by-simpleaudioengine-on-android-5-0/

I have not tested yet to see how bad this performance issue is, but it might mean needing to abandon SimpleAudioEngine.

Edit: It looks like for Android 5 there was a fix added to 3.7 (or at least merged on May 31 into v3) where the new AudioEngine is now used as the implementation behind the SimpleAudioEngine interface for Android.
https://github.com/cocos2d/cocos2d-x/pull/12098
https://github.com/cocos2d/cocos2d-x/issues/9911

Anyway, the code that I pushed to github that I linked above definetly has this issue.

I will probably look into adding the ability to change the pitch, pan and gain of a playing effect to the new audio engine or some other option.

I updated the new AudioEngine in Cocos2d-x 3.7 for iOS, Mac and Android to accept setPitch and setVolume on a currently playing sound effect.

For Win32 it looks like we can use alSourcef to set pitch and gain in AudipPlayer.cpp. I will update that next.
alSourcef(_alSource, AL_PITCH, 1.0f);
alSourcef(_alSource, AL_GAIN, _volume);

WinRT uses XAudio2, so that also can set the pitch or playback rate, just need to update the code to do so.

I will create a pull request with the added features of setting pitch and gain of a playing sound effect sometime in the near future for Mac, iOS, Android, Win32 and WinRT.

Hopefully this will get merged into an upcoming version.

Linux uses FMod, so setting the pitch or playback frequency can be accomplished, but I dont have a dev environment setup in Linux to test.

Pan does not seem to be supported on Android OpenSLES (the underlying code of AudioEngine on Android). http://mobilepearls.com/labs/native-android-api/ndk/docs/opensles/

API Reference for OpenSL. Note that OpenSLES is a subset of OpenSL, but this Khronos doc is useful for OpenSLES as long as you are aware of the limitations of ES.
https://www.khronos.org/registry/sles/api/1.0/OpenSLES.h

I’m looking forward to seeing that - and seeing if I can add panning to it.

I still can’t work out how to push my changes to your simpleAudioEngine update on github, though, so will have to do some testing before I make any changes to the cocos library!

@Maxxx, I am curious what will happen to SimpleAudioEngine, but it looks like it might be on the way out.

Since 3.7 the implementation of SimpleAudioEngine for Android is actually using the new AudioEngine (OpenSLES) if the Android version is 5 (e.g. equal to API 21).

If the Android API is not equal to 21, e.g. greater or less than API 21, then it will use SimpleAudioEngine.

Did Android 5.1 MR1 API 22 fix the lag issue? Not sure. I added that question here: https://github.com/cocos2d/cocos2d-x/pull/12098

But if the new AudioEngine passes the tests that I have planned, then I will probably abandon the SimpleAudioEngine, especially since the version on my website probably has issues since I have not updated it in a while.

I guess I will have to take a look at 3.7 and the new audio engine and see if I can add panning?

So much to do, so little time to do it :smile:

I just opened a pull request for AudioEngine to enable setting pitch of a playing effect. Updated applied to iOS, Mac, Android, WinRT and Win32.

Demo Video of pitch setting / rate playback. Valid values are between half speed and double speed, e.g. [0.5,2.0]. Works the same on all platforms.

4 Likes

Fantastic!

Thanks!

The pull request to add set audio pitch is at risk of not being added to Cocos2d-x.

Unfortunately on Android 5.x the calling setRate on SLPlaybackRateItf seems to go into a black hole.

e.g. getting the OpenSLES interface for SLPlaybackRateItf succeeds. Calling SetRate returns SL_RESULT_SUCCESS. So it should be working, but setRate has no affect on the sound effect.

More Info: https://github.com/cocos2d/cocos2d-x/pull/12780

Would be awesome if someone that knows more about OpenSLES could chime in with some advice.

Docs can be found in your local NDK: android-ndk-r10d/docs/Additional_library_docs/opensles/

The code in the pull request works fine on Android 4.4.4, but not at all on 5.x.

1 Like

Heyalda, Did you solve the pitch issue on Android 5, please?

SimpleAudioEngine does not change sound pitch for me somehow.
Although it works perfectly on iOS and Mac.

OpenSLES SLPlaybackRateItf doesnt work for some reason on Android 5.x. Looks like an Android bug.
You can try using the SimpleAudioEngine and manipulating the pitch or playback rate, but SimpleAudioEngine uses Android SoundPool, which I think had issues with the maximum number of sounds that could be played on android 5.1 (I think it was 5.1, not sure.) Anyway, using SimpleAudioEngine might not be a complete solution for you if you need more than about 5 simultaneous sounds if you care about supporting all recent android versions. But I have not thoroughly tested that it wont work, so it might be worth testing or looking into further what the Cocos2d-x SimpleAudioEngine bugs were with SimpleAudioEngine and android 5.1 (again, I think it was 5.1, but not certain off the top of my head right now.) There is/was some code in Cocos2d-x that would check the version of 5.x that you were using and then automatically choose the experimental audio engine, even if you were using SimpleAudioEngine. Just providing some info that might be useful. Sorry if it is kind of vague.

https://code.google.com/p/android/issues/detail?id=186773&thanks=186773&ts=1442506284

I used fmod instead.