Skip to content

Instantly share code, notes, and snippets.

@reuniware
Last active August 20, 2020 17:03
Show Gist options
  • Save reuniware/db9a5c2db06840f3d97fafce788fbd1a to your computer and use it in GitHub Desktop.
Save reuniware/db9a5c2db06840f3d97fafce788fbd1a to your computer and use it in GitHub Desktop.
Google Oboe Audio Recorder
//
// Created by Reuniware on 29/06/2020.
//
#include <jni.h>
#include <string>
#include <oboe/Oboe.h>
#include "OboeAudioRecorder.h"
#include "oboe/samples/debug-utils/logging_macros.h"
class OboeAudioRecorder: public oboe::AudioStreamCallback {
public:
bool isRecording = true;
explicit OboeAudioRecorder() {
oboe::AudioStreamBuilder builder;
builder.setDirection(oboe::Direction::Input);
builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);
builder.setFormat(oboe::AudioFormat::Float);
builder.setChannelCount(oboe::ChannelCount::Mono);
builder.setInputPreset(oboe::InputPreset::Unprocessed);
builder.setSharingMode(oboe::SharingMode::Shared);
builder.setSampleRate(48000);
builder.setAudioApi(oboe::AudioApi::OpenSLES);
//builder.setCallback(this);
oboe::Result r = builder.openStream(&stream);
if (r != oboe::Result::OK) {
return;
}
r = stream->requestStart();
if (r != oboe::Result::OK) {
return;
}
auto a = stream->getState();
if (a == oboe::StreamState::Started) {
constexpr int kMillisecondsToRecord = 2;
const int32_t requestedFrames = (int32_t) (kMillisecondsToRecord * (stream->getSampleRate() / oboe::kMillisPerSecond));
__android_log_print(ANDROID_LOG_INFO, "OboeAudioRecorder", "requestedFrames = %d", requestedFrames);
float_t mybuffer[requestedFrames];
constexpr int64_t kTimeoutValue = 3 * oboe::kNanosPerMillisecond;
int framesRead = 0;
do {
auto result = stream->read(mybuffer, requestedFrames, 0);
if (result != oboe::Result::OK) {
break;
}
framesRead = result.value();
__android_log_print(ANDROID_LOG_INFO, "OboeAudioRecorder", "framesRead = %d", framesRead);
if (framesRead > 0) {
break;
}
} while (framesRead != 0);
while (isRecording) {
auto result = stream->read(mybuffer, requestedFrames, kTimeoutValue * 1000);
if (result == oboe::Result::OK) {
auto nbFramesRead = result.value();
__android_log_print(ANDROID_LOG_INFO, "OboeAudioRecorder", "nbFramesRead = %d", nbFramesRead);
for (int i=0; i<nbFramesRead; i++) {
__android_log_print(ANDROID_LOG_INFO, "OboeAudioRecorder", "nbFramesRead[%d] = %f", i, mybuffer[i]);
}
} else {
auto error = convertToText(result.error());
__android_log_print(ANDROID_LOG_INFO, "OboeAudioRecorder", "error = %s", error);
}
}
stream->requestStop();
stream->close();
}
}
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
LOGE("onAudioReady");
}
private:
oboe::ManagedStream outStream;
oboe::AudioStream *stream;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment