4

When using AudioTrack in Android you can call attachAuxEffect to attach an audio effect. Is there a similar method or approach when using OpenSL ES for audio playback? I can't seem to find a similar method.

William Seemann
  • 3,440
  • 10
  • 44
  • 78

1 Answers1

0

There's an example of how to use OpenSL ES native audio in your NDK-home/samples/native-audio directory.

Take a look at this snippet of code (we are applying a reverb effect here):

...
// create the engine and output mix objects
void Java_com_example_nativeaudio_NativeAudio_createEngine(JNIEnv* env, jclass clazz)
{
    SLresult result;

    // create engine
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    assert(SL_RESULT_SUCCESS == result);
    (void)result;

    // realize the engine
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    assert(SL_RESULT_SUCCESS == result);
    (void)result;

    // get the engine interface, which is needed in order to create other objects
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
    assert(SL_RESULT_SUCCESS == result);
    (void)result;

    // create output mix, with environmental reverb specified as a non-required interface
    const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB};
    const SLboolean req[1] = {SL_BOOLEAN_FALSE};
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req);
    assert(SL_RESULT_SUCCESS == result);
    (void)result;

    // realize the output mix
    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
    assert(SL_RESULT_SUCCESS == result);
    (void)result;

    // get the environmental reverb interface
    // this could fail if the environmental reverb effect is not available,
    // either because the feature is not present, excessive CPU load, or
    // the required MODIFY_AUDIO_SETTINGS permission was not requested and granted
    result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB,
            &outputMixEnvironmentalReverb);
    if (SL_RESULT_SUCCESS == result) {
        result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
                outputMixEnvironmentalReverb, &reverbSettings);
        (void)result;
    }
    // ignore unsuccessful result codes for environmental reverb, as it is optional for this example

}

Please note the Android OpenSL ES implementation is a subset of OpenSL ES which is a subset of OpenSL: some effects may not be available on all devices. A very brief explanation of this aspect can be found here.

bonnyz
  • 13,458
  • 5
  • 46
  • 70
  • 1
    I'm familiar with this code, however the snippet you provided doesn't accomplish the same task as calling "attachAuxEffect" and passing an effect id. – William Seemann Mar 19 '15 at 02:52
  • @WilliamSeemann I don't think so. The AudioTrack is an high-level API witch masks a lot of the complexity of the underlying audio framework (like the source-to-sink paths); this means that the OpensSL ES API may be "different" but accomplishes the same thing. – bonnyz Mar 19 '15 at 09:21