Skip to content

Commit

Permalink
functions for modify effect sound attribute while playing, just work …
Browse files Browse the repository at this point in the history
…with ios/mac/android
  • Loading branch information
Jennal committed Jun 8, 2015
1 parent 8ad4764 commit 97f39c0
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 59 deletions.
2 changes: 2 additions & 0 deletions cocos/audio/android/cddSimpleAudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ namespace CocosDenshion {
float pan,
float gain) {
return 0; }
void SimpleAudioEngine::setEffectAttr(unsigned int id, float pitch, float pan, float gain) { }
void SimpleAudioEngine::getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain) { }
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId) { }
void SimpleAudioEngine::pauseAllEffects() { }
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId) { }
Expand Down
45 changes: 45 additions & 0 deletions cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ namespace CocosDenshion {
paramCode);
}

float AndroidJavaEngine::getFloatField(jobject obj,
const char* className,
const char* fieldName)
{
JNIEnv* env = cocos2d::JniHelper::getEnv();

jclass clazz = env->FindClass(className);
jfieldID field = env->GetFieldID( clazz, fieldName, "F");
float result = (float)env->GetFloatField(obj, field);

return result;
}

AndroidJavaEngine::~AndroidJavaEngine() {
cocos2d::JniMethodInfo methodInfo;

Expand Down Expand Up @@ -225,6 +238,38 @@ namespace CocosDenshion {
return (unsigned int)ret;
}

void AndroidJavaEngine::setEffectAttr(unsigned int id, float pitch, float pan, float gain)
{
cocos2d::JniMethodInfo methodInfo;
if (! getJNIStaticMethodInfo(methodInfo, "setEffect", "(IFFF)V")) {
return;
}

methodInfo.env->CallStaticVoidMethod(methodInfo.classID,
methodInfo.methodID,
id,
pitch, pan, gain);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}

void AndroidJavaEngine::getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain)
{
cocos2d::JniMethodInfo methodInfo;
if (! getJNIStaticMethodInfo(methodInfo, "getEffect", "(I)Lorg/cocos2dx/lib/Cocos2dxSound/SoundAttr;")) {
return;
}

jobject obj = methodInfo.env->CallStaticObjectMethod(methodInfo.classID,
methodInfo.methodID,
id);
methodInfo.env->DeleteLocalRef(methodInfo.classID);

const char* className = "org/cocos2dx/lib/Cocos2dxSound/SoundAttr";
*pitch = getFloatField(obj, className, "pitch");
*pan = getFloatField(obj, className, "pan");
*gain = getFloatField(obj, className, "gain");
}

void AndroidJavaEngine::pauseEffect(unsigned int nSoundId) {
cocos2d::JniMethodInfo methodInfo;

Expand Down
3 changes: 3 additions & 0 deletions cocos/audio/android/jni/cddandroidAndroidJavaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace CocosDenshion {
void setEffectsVolume(float volume);
unsigned int playEffect(const char* pszFilePath, bool bLoop = false,
float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
void setEffectAttr(unsigned int id, float pitch, float pan = 0.0f, float gain = 1.0f);
void getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain);
void pauseEffect(unsigned int nSoundId);
void pauseAllEffects();
void resumeEffect(unsigned int nSoundId);
Expand All @@ -60,6 +62,7 @@ namespace CocosDenshion {
static bool getJNIStaticMethodInfo(cocos2d::JniMethodInfo &methodinfo,
const char *methodName,
const char *paramCode);
static float getFloatField(jobject, const char* className, const char* fieldName);
};
}
}
Expand Down
15 changes: 14 additions & 1 deletion cocos/audio/include/SimpleAudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,20 @@ class EXPORT_DLL SimpleAudioEngine
*/
virtual unsigned int playEffect(const char* pszFilePath, bool bLoop = false,
float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);

/**
@brief Set sound effect with pitch, pan and gain while sound is playing
@param id the OpenAL source id
@param pitch Frequency, normal value is 1.0. Will also change effect play time.
@param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
@param gain Volume, in the range of [0..1]. The normal value is 1.
@note Full support is under development, now there are limitations:
- no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
- no pitch/pan/gain on emscrippten, win32, marmalade.
*/
virtual void setEffectAttr(unsigned int id, float pitch, float pan = 0.0f, float gain = 1.0f);
virtual void getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain);

/**
@brief Pause playing sound effect
@param nSoundId The return value of function playEffect
Expand Down
2 changes: 2 additions & 0 deletions cocos/audio/ios/CocosDenshion.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ typedef struct _sourceInfo {

/** Plays a sound in a channel group with a pitch, pan and gain. The sound could played looped or not */
-(ALuint) playSound:(int) soundId sourceGroupId:(int)sourceGroupId pitch:(float) pitch pan:(float) pan gain:(float) gain loop:(BOOL) loop;
-(void) setSound:(ALuint)sourceId pitch:(float)pitch pan:(float)pan gain:(float)gain;
-(void) getSound:(ALuint)sourceId pitch:(float*)pitch pan:(float*)pan gain:(float*)gain;

/** Creates and returns a sound source object for the specified sound within the specified source group.
*/
Expand Down
79 changes: 79 additions & 0 deletions cocos/audio/ios/CocosDenshion.m
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,85 @@ - (ALuint)playSound:(int) soundId sourceGroupId:(int)sourceGroupId pitch:(float)
}
}

-(void) setSound:(ALuint)source pitch:(float)pitch pan:(float)pan gain:(float)gain
{
#ifdef CD_DEBUG
//Sanity check parameters - only in DEBUG
NSAssert(pitch > 0, @"pitch must be greater than zero");
NSAssert(pan >= -1 && pan <= 1, @"pan must be between -1 and 1");
NSAssert(gain >= 0, @"gain can not be negative");
#endif
//If mute or initialisation has failed or buffer is not loaded then do nothing
if (!enabled_ || !functioning_) {
return;
}

ALint state;
// ALuint buffer = _buffers[soundId].bufferId;
alGetError();//Clear the error code
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) {
return;
// alSourceStop(source);
}
// alSourcei(source, AL_BUFFER, buffer);//Attach to sound
alSourcef(source, AL_PITCH, pitch);//Set pitch
// alSourcei(source, AL_LOOPING, loop);//Set looping
alSourcef(source, AL_GAIN, gain);//Set gain/volume
float sourcePosAL[] = {pan, 0.0f, 0.0f};//Set position - just using left and right panning
alSourcefv(source, AL_POSITION, sourcePosAL);
alGetError();//Clear the error code
// alSourcePlay(source);
if((lastErrorCode_ = alGetError()) == AL_NO_ERROR) {
//Everything was okay
// _sources[sourceIndex].attachedBufferId = buffer;
return;
} else {
if (alcGetCurrentContext() == NULL) {
CDLOGINFO(@"Denshion::CDSoundEngine - posting bad OpenAL context message");
[[NSNotificationCenter defaultCenter] postNotificationName:kCDN_BadAlContext object:nil];
}
return;
}
}

-(void) getSound:(ALuint)source pitch:(float*)pitch pan:(float*)pan gain:(float*)gain
{
//If mute or initialisation has failed or buffer is not loaded then do nothing
if (!enabled_ || !functioning_) {
return;
}

ALint state;
// ALuint buffer = _buffers[soundId].bufferId;
alGetError();//Clear the error code
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) {
return;
// alSourceStop(source);
}
// alSourcei(source, AL_BUFFER, buffer);//Attach to sound
alGetSourcef(source, AL_PITCH, pitch);//Set pitch
// alSourcei(source, AL_LOOPING, loop);//Set looping
alGetSourcef(source, AL_GAIN, gain);//Set gain/volume
float sourcePosAL[] = {0.0f, 0.0f, 0.0f};//Set position - just using left and right panning
alGetSourcefv(source, AL_POSITION, sourcePosAL);
*pan = sourcePosAL[0];
alGetError();//Clear the error code
// alSourcePlay(source);
if((lastErrorCode_ = alGetError()) == AL_NO_ERROR) {
//Everything was okay
// _sources[sourceIndex].attachedBufferId = buffer;
return;
} else {
if (alcGetCurrentContext() == NULL) {
CDLOGINFO(@"Denshion::CDSoundEngine - posting bad OpenAL context message");
[[NSNotificationCenter defaultCenter] postNotificationName:kCDN_BadAlContext object:nil];
}
return;
}
}

-(BOOL) _soundSourceAttachToBuffer:(CDSoundSource*) soundSource soundId:(int) soundId {
//Attach the source to the buffer
ALint state;
Expand Down
22 changes: 21 additions & 1 deletion cocos/audio/ios/SimpleAudioEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ static unsigned int static_playEffect(const char* pszFilePath, bool bLoop, Float
{
return [[SimpleAudioEngine sharedEngine] playEffect:[NSString stringWithUTF8String: pszFilePath] loop:bLoop pitch:pszPitch pan: pszPan gain:pszGain];
}


static void static_setEffect(unsigned int soundId, Float32 pszPitch, Float32 pszPan, Float32 pszGain)
{
return [[SimpleAudioEngine sharedEngine] setEffect:soundId pitch:pszPitch pan: pszPan gain:pszGain ];
}

static void static_getEffect(unsigned int soundId, Float32* pszPitch, Float32* pszPan, Float32* pszGain)
{
return [[SimpleAudioEngine sharedEngine] getEffect:soundId pitch:pszPitch pan: pszPan gain:pszGain ];
}

static void static_stopEffect(int nSoundId)
{
[[SimpleAudioEngine sharedEngine] stopEffect: nSoundId];
Expand Down Expand Up @@ -246,6 +256,16 @@ static void static_stopAllEffects()
return static_playEffect(fullPath.c_str(), bLoop, pitch, pan, gain);
}

void SimpleAudioEngine::setEffectAttr(unsigned int id, float pitch, float pan, float gain)
{
static_setEffect(id, pitch, pan, gain);
}

void SimpleAudioEngine::getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain)
{
static_getEffect(id, pitch, pan, gain);
}

void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{
static_stopEffect(nSoundId);
Expand Down
3 changes: 3 additions & 0 deletions cocos/audio/ios/SimpleAudioEngine_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
-(void) stopEffect:(ALuint) soundId;
/** plays an audio effect with a file path, pitch, pan and gain */
-(ALuint) playEffect:(NSString*) filePath loop:(BOOL)loop pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain;
/** set an audio effect with pitch, pan and gain by soundId */
-(void) setEffect:(ALuint)soundId pitch:(Float32)pitch pan:(Float32)pan gain:(Float32)gain;
-(void) getEffect:(ALuint)soundId pitch:(Float32*)pitch pan:(Float32*)pan gain:(Float32*)gain;
/** pause an audio */
-(void) pauseEffect:(ALuint) soundId;
/** pause all audioes */
Expand Down
10 changes: 10 additions & 0 deletions cocos/audio/ios/SimpleAudioEngine_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop pitch:(Float32) pitch
}
}

-(void) setEffect:(ALuint)soundId pitch:(Float32)pitch pan:(Float32)pan gain:(Float32)gain
{
[soundEngine setSound:soundId pitch:pitch pan:pan gain:gain];
}

-(void) getEffect:(ALuint)soundId pitch:(Float32*)pitch pan:(Float32*)pan gain:(Float32*)gain
{
[soundEngine getSound:soundId pitch:pitch pan:pan gain:gain];
}

-(void) stopEffect:(ALuint) soundId {
[soundEngine stopSound:soundId];
}
Expand Down
2 changes: 2 additions & 0 deletions cocos/audio/mac/CocosDenshion.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ typedef struct _sourceInfo {

/** Plays a sound in a channel group with a pitch, pan and gain. The sound could played looped or not */
-(ALuint) playSound:(int) soundId sourceGroupId:(int)sourceGroupId pitch:(float) pitch pan:(float) pan gain:(float) gain loop:(BOOL) loop;
-(void) setSound:(ALuint)sourceId pitch:(float)pitch pan:(float)pan gain:(float)gain;
-(void) getSound:(ALuint)sourceId pitch:(float*)pitch pan:(float*)pan gain:(float*)gain;

/** Creates and returns a sound source object for the specified sound within the specified source group.
*/
Expand Down
79 changes: 79 additions & 0 deletions cocos/audio/mac/CocosDenshion.m
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,85 @@ - (ALuint)playSound:(int) soundId sourceGroupId:(int)sourceGroupId pitch:(float)
}
}

-(void) setSound:(ALuint)source pitch:(float)pitch pan:(float)pan gain:(float)gain
{
#ifdef CD_DEBUG
//Sanity check parameters - only in DEBUG
NSAssert(pitch > 0, @"pitch must be greater than zero");
NSAssert(pan >= -1 && pan <= 1, @"pan must be between -1 and 1");
NSAssert(gain >= 0, @"gain can not be negative");
#endif
//If mute or initialisation has failed or buffer is not loaded then do nothing
if (!enabled_ || !functioning_) {
return;
}

ALint state;
// ALuint buffer = _buffers[soundId].bufferId;
alGetError();//Clear the error code
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) {
return;
// alSourceStop(source);
}
// alSourcei(source, AL_BUFFER, buffer);//Attach to sound
alSourcef(source, AL_PITCH, pitch);//Set pitch
// alSourcei(source, AL_LOOPING, loop);//Set looping
alSourcef(source, AL_GAIN, gain);//Set gain/volume
float sourcePosAL[] = {pan, 0.0f, 0.0f};//Set position - just using left and right panning
alSourcefv(source, AL_POSITION, sourcePosAL);
alGetError();//Clear the error code
// alSourcePlay(source);
if((lastErrorCode_ = alGetError()) == AL_NO_ERROR) {
//Everything was okay
// _sources[sourceIndex].attachedBufferId = buffer;
return;
} else {
if (alcGetCurrentContext() == NULL) {
CDLOGINFO(@"Denshion::CDSoundEngine - posting bad OpenAL context message");
[[NSNotificationCenter defaultCenter] postNotificationName:kCDN_BadAlContext object:nil];
}
return;
}
}

-(void) getSound:(ALuint)source pitch:(float*)pitch pan:(float*)pan gain:(float*)gain
{
//If mute or initialisation has failed or buffer is not loaded then do nothing
if (!enabled_ || !functioning_) {
return;
}

ALint state;
// ALuint buffer = _buffers[soundId].bufferId;
alGetError();//Clear the error code
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state != AL_PLAYING) {
return;
// alSourceStop(source);
}
// alSourcei(source, AL_BUFFER, buffer);//Attach to sound
alGetSourcef(source, AL_PITCH, pitch);//Set pitch
// alSourcei(source, AL_LOOPING, loop);//Set looping
alGetSourcef(source, AL_GAIN, gain);//Set gain/volume
float sourcePosAL[] = {0.0f, 0.0f, 0.0f};//Set position - just using left and right panning
alGetSourcefv(source, AL_POSITION, sourcePosAL);
*pan = sourcePosAL[0];
alGetError();//Clear the error code
// alSourcePlay(source);
if((lastErrorCode_ = alGetError()) == AL_NO_ERROR) {
//Everything was okay
// _sources[sourceIndex].attachedBufferId = buffer;
return;
} else {
if (alcGetCurrentContext() == NULL) {
CDLOGINFO(@"Denshion::CDSoundEngine - posting bad OpenAL context message");
[[NSNotificationCenter defaultCenter] postNotificationName:kCDN_BadAlContext object:nil];
}
return;
}
}

-(BOOL) _soundSourceAttachToBuffer:(CDSoundSource*) soundSource soundId:(int) soundId {
//Attach the source to the buffer
ALint state;
Expand Down
22 changes: 21 additions & 1 deletion cocos/audio/mac/SimpleAudioEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ static unsigned int static_playEffect(const char* pszFilePath, bool bLoop, Float
{
return [[SimpleAudioEngine sharedEngine] playEffect:[NSString stringWithUTF8String: pszFilePath] loop:bLoop pitch:pszPitch pan: pszPan gain:pszGain ];
}


static void static_setEffect(unsigned int soundId, Float32 pszPitch, Float32 pszPan, Float32 pszGain)
{
return [[SimpleAudioEngine sharedEngine] setEffect:soundId pitch:pszPitch pan: pszPan gain:pszGain ];
}

static void static_getEffect(unsigned int soundId, Float32* pszPitch, Float32* pszPan, Float32* pszGain)
{
return [[SimpleAudioEngine sharedEngine] getEffect:soundId pitch:pszPitch pan: pszPan gain:pszGain ];
}

static void static_stopEffect(int nSoundId)
{
[[SimpleAudioEngine sharedEngine] stopEffect: nSoundId];
Expand Down Expand Up @@ -248,6 +258,16 @@ static void static_stopAllEffects()
return static_playEffect(fullPath.c_str(), bLoop, pitch, pan, gain);
}

void SimpleAudioEngine::setEffectAttr(unsigned int id, float pitch, float pan, float gain)
{
static_setEffect(id, pitch, pan, gain);
}

void SimpleAudioEngine::getEffectAttr(unsigned int id, float* pitch, float* pan, float* gain)
{
static_getEffect(id, pitch, pan, gain);
}

void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{
static_stopEffect(nSoundId);
Expand Down
Loading

0 comments on commit 97f39c0

Please sign in to comment.