CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/android/jni/AndroidAudio.cpp
Views: 1401
#include "Common/Log.h"12#include "android/jni/AndroidAudio.h"3#include "android/jni/OpenSLContext.h"45std::string g_error;6std::mutex g_errorMutex;78AudioContext::AudioContext(AndroidAudioCallback cb, int _FramesPerBuffer, int _SampleRate)9: audioCallback(cb), framesPerBuffer(_FramesPerBuffer), sampleRate(_SampleRate) {10if (framesPerBuffer == 0)11framesPerBuffer = 256;12if (framesPerBuffer < 32)13framesPerBuffer = 32;14if (framesPerBuffer > 4096)15framesPerBuffer = 4096;1617sampleRate = _SampleRate;18g_error = "";19}2021void AudioContext::SetErrorString(const std::string &error) {22std::unique_lock<std::mutex> lock(g_errorMutex);23g_error = error;24}2526struct AndroidAudioState {27AudioContext *ctx = nullptr;28AndroidAudioCallback callback = nullptr;29// output30int frames_per_buffer = 0;31int sample_rate = 0;32// input33int input_enable = 0;34int input_sample_rate = 0;35};3637AndroidAudioState *AndroidAudio_Init(AndroidAudioCallback callback, int optimalFramesPerBuffer, int optimalSampleRate) {38AndroidAudioState *state = new AndroidAudioState();39state->callback = callback;40state->frames_per_buffer = optimalFramesPerBuffer ? optimalFramesPerBuffer : 256;41state->sample_rate = optimalSampleRate ? optimalSampleRate : 44100;42return state;43}4445bool AndroidAudio_Recording_SetSampleRate(AndroidAudioState *state, int sampleRate) {46if (!state) {47ERROR_LOG(Log::Audio, "AndroidAudioState not initialized, cannot set recording sample rate");48return false;49}50state->input_sample_rate = sampleRate;51INFO_LOG(Log::Audio, "AndroidAudio_Recording_SetSampleRate=%d", sampleRate);52return true;53}5455bool AndroidAudio_Recording_Start(AndroidAudioState *state) {56if (!state) {57ERROR_LOG(Log::Audio, "AndroidAudioState not initialized, cannot start recording!");58return false;59}60state->input_enable = 1;61if (!state->ctx) {62ERROR_LOG(Log::Audio, "OpenSLContext not initialized, cannot start recording!");63return false;64}65state->ctx->AudioRecord_Start(state->input_sample_rate);66INFO_LOG(Log::Audio, "AndroidAudio_Recording_Start");67return true;68}6970bool AndroidAudio_Recording_Stop(AndroidAudioState *state) {71if (!state) {72ERROR_LOG(Log::Audio, "AndroidAudioState not initialized, cannot stop recording!");73return false;74}75if (!state->ctx) {76ERROR_LOG(Log::Audio, "OpenSLContext not initialized, cannot stop recording!");77return false;78}79state->input_enable = 0;80state->input_sample_rate = 0;81state->ctx->AudioRecord_Stop();82INFO_LOG(Log::Audio, "AndroidAudio_Recording_Stop");83return true;84}8586bool AndroidAudio_Recording_State(AndroidAudioState *state) {87if (!state) {88return false;89}90return state->input_enable;91}9293bool AndroidAudio_Resume(AndroidAudioState *state) {94if (!state) {95ERROR_LOG(Log::Audio, "Audio was shutdown, cannot resume!");96return false;97}98if (!state->ctx) {99INFO_LOG(Log::Audio, "Calling OpenSLWrap_Init_T...");100state->ctx = new OpenSLContext(state->callback, state->frames_per_buffer, state->sample_rate);101INFO_LOG(Log::Audio, "Returned from OpenSLWrap_Init_T");102bool init_retval = state->ctx->Init();103if (!init_retval) {104delete state->ctx;105state->ctx = nullptr;106}107if (state->input_enable) {108state->ctx->AudioRecord_Start(state->input_sample_rate);109}110return init_retval;111}112return false;113}114115bool AndroidAudio_Pause(AndroidAudioState *state) {116if (!state) {117ERROR_LOG(Log::Audio, "Audio was shutdown, cannot pause!");118return false;119}120if (state->ctx) {121INFO_LOG(Log::Audio, "Calling OpenSLWrap_Shutdown_T...");122delete state->ctx;123state->ctx = nullptr;124INFO_LOG(Log::Audio, "Returned from OpenSLWrap_Shutdown_T ...");125return true;126}127return false;128}129130bool AndroidAudio_Shutdown(AndroidAudioState *state) {131if (!state) {132ERROR_LOG(Log::Audio, "Audio already shutdown!");133return false;134}135if (state->ctx) {136ERROR_LOG(Log::Audio, "Should not shut down when playing! Something is wrong!");137return false;138}139delete state;140INFO_LOG(Log::Audio, "OpenSLWrap completely unloaded.");141return true;142}143144const std::string AndroidAudio_GetErrorString(AndroidAudioState *state) {145if (!state) {146return "No state";147}148std::unique_lock<std::mutex> lock(g_errorMutex);149return g_error;150}151152153