Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change OpenSL ES input stream from stereo to mono #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions Android/app/src/main/jni/SuperpoweredLatency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ static latencyMeasurer *measurer;
static bool hasAAudio = false;
static bool isRunning = false;

constexpr int kChannelCountMono = 1;
constexpr int kChannelCountStereo = 2;
constexpr int kBytesIn16BitSample = 2;
constexpr int kBytesPerStereoAudioFrame = kChannelCountStereo * kBytesIn16BitSample;


// ---------
// OPENSL ES
// ---------
Expand All @@ -28,14 +34,26 @@ static struct {
SLAndroidSimpleBufferQueueItf outputBufferQueueInterface, inputBufferQueueInterface;
short int *inputBuffers[NUMOPENSLESBUFFERS], *outputBuffers[NUMOPENSLESBUFFERS];
int samplerateFromJava, buffersizeFromJava, inputBufferWriteIndex, inputBufferReadIndex, inputBuffersAvailable, outputBufferWriteIndex;
SLuint32 inputChannelCount = kChannelCountStereo;
} openSLES;

// Note: input array must be at least 2 * numFrames in size
static void convertBufferToStereo(short int *buffer, const int numFrames){
for (int i = numFrames - 1; i >=0; --i) {
buffer[i*2] = buffer[i];
buffer[(i*2)+1] = buffer[i];
}
}

// Audio input comes here.
static void openSLESInputCallback(SLAndroidSimpleBufferQueueItf caller, __unused void *pContext) {
__sync_fetch_and_add(&openSLES.inputBuffersAvailable, 1);
short int *inputBuffer = openSLES.inputBuffers[openSLES.inputBufferWriteIndex];
if (openSLES.inputBufferWriteIndex < NUMOPENSLESBUFFERS - 1) openSLES.inputBufferWriteIndex++; else openSLES.inputBufferWriteIndex = 0;
(*caller)->Enqueue(caller, inputBuffer, (SLuint32)buffersize * 4);
(*caller)->Enqueue(caller, inputBuffer, (SLuint32)buffersize * openSLES.inputChannelCount * kBytesIn16BitSample);
if (openSLES.inputChannelCount == kChannelCountMono){
convertBufferToStereo(inputBuffer, (SLuint32)buffersize);
}
}

// Audio output must be provided here.
Expand All @@ -50,28 +68,33 @@ static void openSLESOutputCallback(SLAndroidSimpleBufferQueueItf caller, __unuse

measurer->processInput(inputBuffer, samplerate, buffersize);
measurer->processOutput(outputBuffer);
if (measurer->state == -1) memcpy(outputBuffer, inputBuffer, (size_t)buffersize * 4);
} else memset(outputBuffer, 0, (size_t)buffersize * 4);
if (measurer->state == -1) memcpy(outputBuffer, inputBuffer, (size_t)buffersize * kChannelCountStereo * kBytesIn16BitSample);
} else memset(outputBuffer, 0, (size_t)buffersize * kChannelCountStereo * kBytesIn16BitSample);

(*caller)->Enqueue(caller, outputBuffer, (SLuint32)buffersize * 4);
(*caller)->Enqueue(caller, outputBuffer, (SLuint32)buffersize * kChannelCountStereo * kBytesIn16BitSample);
}

static void startOpenSLES() {
hasAAudio = false;
samplerate = openSLES.samplerateFromJava;
buffersize = openSLES.buffersizeFromJava;

// If on Android 8.0 then use a mono input stream
if (__ANDROID_API__ == __ANDROID_API_O__) openSLES.inputChannelCount = kChannelCountMono;

openSLES.inputBufferWriteIndex = 1; // Start from 1, because we enqueue one buffer when we start the input buffer queue.
openSLES.inputBufferReadIndex = 0;
openSLES.inputBuffersAvailable = 0;
openSLES.outputBufferWriteIndex = 1; // Start from 1, because we enqueue one buffer when we start the output buffer queue.

// Allocating audio buffers for input and output.
// Note that regardless of the input channel count we still allocate enough for stereo audio
// frames so we can do the mono->stereo conversion.
for (int n = 0; n < NUMOPENSLESBUFFERS; n++) {
openSLES.inputBuffers[n] = (short int *)malloc(((size_t)buffersize + 16) * 4);
openSLES.outputBuffers[n] = (short int *)malloc(((size_t)buffersize + 16) * 4);
memset(openSLES.inputBuffers[n], 0, (size_t)buffersize * 4);
memset(openSLES.outputBuffers[n], 0, (size_t)buffersize * 4);
openSLES.inputBuffers[n] = (short int *)malloc(((size_t)buffersize + 16) * kBytesPerStereoAudioFrame);
openSLES.outputBuffers[n] = (short int *)malloc(((size_t)buffersize + 16) * kBytesPerStereoAudioFrame);
memset(openSLES.inputBuffers[n], 0, (size_t)buffersize * kBytesPerStereoAudioFrame);
memset(openSLES.outputBuffers[n], 0, (size_t)buffersize * kBytesPerStereoAudioFrame);
};

const SLboolean requireds[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE };
Expand All @@ -88,7 +111,7 @@ static void startOpenSLES() {

// Create the output buffer queue.
SLDataLocator_AndroidSimpleBufferQueue outputLocator = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1 };
SLDataFormat_PCM outputFormat = { SL_DATAFORMAT_PCM, 2, (SLuint32)samplerate * 1000, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN };
SLDataFormat_PCM outputFormat = { SL_DATAFORMAT_PCM, kChannelCountStereo, (SLuint32)samplerate * 1000, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN };
SLDataSource outputSource = { &outputLocator, &outputFormat };
const SLInterfaceID outputInterfaces[1] = { SL_IID_BUFFERQUEUE };
SLDataSink outputSink = { &outputMixLocator, NULL };
Expand All @@ -99,7 +122,7 @@ static void startOpenSLES() {
SLDataLocator_IODevice deviceInputLocator = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL };
SLDataSource inputSource = { &deviceInputLocator, NULL };
SLDataLocator_AndroidSimpleBufferQueue inputLocator = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1 };
SLDataFormat_PCM inputFormat = { SL_DATAFORMAT_PCM, 2, (SLuint32)samplerate * 1000, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN };
SLDataFormat_PCM inputFormat = { SL_DATAFORMAT_PCM, openSLES.inputChannelCount, (SLuint32)samplerate * 1000, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN };
SLDataSink inputSink = { &inputLocator, &inputFormat };
const SLInterfaceID inputInterfaces[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
(*openSLEngineInterface)->CreateAudioRecorder(openSLEngineInterface, &openSLES.inputBufferQueue, &inputSource, &inputSink, 2, inputInterfaces, requireds);
Expand Down