I'm currently working on a project where I need low-latency audio, so I decided to use Oboe (google library) to work with it.
I've read the documentation but multiple questions arise from it.
First, once I create my stream using the builder pattern, open the stream and request the stream to start, how can I capture the data coming in from the mic?
I know there are two ways: using the read function or a callback.
QUESTIONS:
But if I use the read function how can I make sure it is being call as long as the stream is started?
How can I use the callback to obtain the data and process it?
I'll leave my code below
void HelloOboeEngine::start() {
isRecording = true;
oboe::Result result = createStream();
if (result != oboe::Result::OK) {
closeStream(mRecordingStream);
}
result = mRecordingStream->requestStart();
if (result != oboe::Result::OK) {
closeStream(mRecordingStream);
}
}
oboe::Result HelloOboeEngine::createStream() {
oboe::AudioStreamBuilder builder;
return builder.setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setDirection(oboe::Direction::Input)
->setFormat(mFormat)
->setFormatConversionAllowed(true)
->setChannelConversionAllowed(true)
->setSampleRateConversionQuality(oboe::SampleRateConversionQuality::Medium)
->openStream(mRecordingStream);
}
void HelloOboeEngine::closeStream(std::shared_ptr<oboe::AudioStream> &stream) {
if (stream) {
oboe::Result result = stream->stop();
if (result != oboe::Result::OK) {
LOGW("Error stopping stream: %s", oboe::convertToText(result));
}
result = stream->close();
if (result != oboe::Result::OK) {
LOGE("Error closing stream: %s", oboe::convertToText(result));
} else {
LOGW("Successfully closed stream");
}
stream.reset();
}
}
You need first extend your class to override and get the data from the callback in your header file, like this:
Set the callback in the builder in your class like this:
And write your implementation, overriding the method: