RecorderObject in OpenSL does not implement the interface to set the volume or configure on Android

StackOverflow https://stackoverflow.com/questions/10996402

  •  14-06-2021
  •  | 
  •  

Question

I tried to get the SLDeviceVolumeItf interface of the RecorderObject on Android but I got the error: SL_RESULT_FEATURE_UNSUPPORTED.

I read that the Android implementation of OpenSL ES does not support volume setting for the AudioRecorder. Is that true?

If yes is there a workaround? I have a VOIP application that does not worl well on Galaxy Nexus because of the very high mic gain.

I also tried to get the SL_IID_ANDROIDCONFIGURATION to set the streamType to the new VOICE_COMMUNINCATION audio-source but again I get error 12 (not supported).

   // create audio recorder
const SLInterfaceID id[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
const SLboolean    req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

result = (*engine)->CreateAudioRecorder(engine, &recorderObject, &audioSrc,  &audioSnk, 2, id, req);
if (SL_RESULT_SUCCESS != result) {
    return false;
}

SLAndroidConfigurationItf recorderConfig;   
result = (*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDCONFIGURATION, &recorderConfig);
if(result != SL_RESULT_SUCCESS) {
    error("failed to get SL_IID_ANDROIDCONFIGURATION interface. e == %d", result);
}

The recorderObject is created but I can't get the SL_IID_ANDROIDCONFIGURATION interface. I tried it on Galaxy Nexus (ICS), HTC sense (ICS) and Motorola Blur (Gingerbread). I'm using NDK version 6.

Was it helpful?

Solution

Now I can get the interface. I had to use NDK 8 and target-14. When I tried to use 10 as a target, I had an error compiling the native code (dirent.h was not found). I had to use target-platform-14.

OTHER TIPS

I ran into a similar problem. My results were returning the error code for not implemented. However, my problem was that I wasn't creating the recorder with the SL_IID_ANDROIDCONFIGURATION interface flag.

    apiLvl = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);

SLint32 streamType = SL_ANDROID_RECORDING_PRESET_GENERIC;
    if(apiLvl > 10){
        streamType = SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION;
        I("set SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION");
    }

    result = (*recorderConfig)->SetConfiguration(recorderConfig, SL_ANDROID_KEY_RECORDING_PRESET, &streamType, sizeof(SLint32));
    if (SL_RESULT_SUCCESS != result) {
        return 0;
    }

Even i tried to find a way to change the gain in OpenSL, looks like there is no api/interface for that. i implemented a work around by implementing a simple shift gain multiplier

void multiply_gain(void *buffer, int bytes, int gain_val) { int i = 0, j = 0;

    short *buffer_samples = (short*)buffer;

    for(i = 0, j = 0; i < bytes; i+=2,j++)
    {
       buffer_samples[j] = (buffer_samples[j] >> gain_val);
    }

}

But here the gain is multiplied/divided (based on << or >>) by a factor or 2. if you need a smoother gain curve, you need to write a more complex digital gain function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top