[Android] Audio record using OpenSL

This commit is contained in:
Florin9doi committed 2020-10-24 19:51:30 +03:00
1 parent 254c316b10
commit 9379fe1399
12 files changed
+238 -14

No files matched your search

+39
View File
@@ -18,8 +18,12 @@ AudioContext::AudioContext(AndroidAudioCallback cb, int _FramesPerBuffer, int _S
struct AndroidAudioState {
AudioContext *ctx = nullptr;
AndroidAudioCallback callback = nullptr;
// output
int frames_per_buffer = 0;
int sample_rate = 0;
// input
int input_enable = 0;
int input_sample_rate = 0;
};
AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback callback, int optimalFramesPerBuffer, int optimalSampleRate) {
@@ -30,6 +34,38 @@ AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback callback, int optimalF
return state;
}
bool AndroidAudio_Recording_Start(AndroidAudioState *state, int sampleRate) {
if (!state) {
ERROR_LOG(AUDIO, "AndroidAudioState not initialized, cannot start recording!");
return false;
}
if (!state->ctx) {
ERROR_LOG(AUDIO, "OpenSLContext not initialized, cannot start recording!");
return false;
}
state->input_enable = 1;
state->input_sample_rate = sampleRate;
state->ctx->AudioRecord_Start(sampleRate);
INFO_LOG(AUDIO, "AndroidAudio_Recording_Start");
return true;
}
bool AndroidAudio_Recording_Stop(AndroidAudioState *state) {
if (!state) {
ERROR_LOG(AUDIO, "AndroidAudioState not initialized, cannot stop recording!");
return false;
}
if (!state->ctx) {
ERROR_LOG(AUDIO, "OpenSLContext not initialized, cannot stop recording!");
return false;
}
state->input_enable = 0;
state->input_sample_rate = 0;
state->ctx->AudioRecord_Stop();
INFO_LOG(AUDIO, "AndroidAudio_Recording_Stop");
return true;
}
bool AndroidAudio_Resume(AndroidAudioState *state) {
if (!state) {
ERROR_LOG(AUDIO, "Audio was shutdown, cannot resume!");
@@ -44,6 +80,9 @@ bool AndroidAudio_Resume(AndroidAudioState *state) {
delete state->ctx;
state->ctx = nullptr;
}
if (state->input_enable) {
state->ctx->AudioRecord_Start(state->input_sample_rate);
}
return init_retval;
}
return false;