From 2d3f850ca0597b764753f300f50a62e7961dd988 Mon Sep 17 00:00:00 2001 From: paolo mac Date: Fri, 2 Feb 2024 17:47:50 +0100 Subject: [PATCH] same fix for android: when there are 0 in/out audio channels, use ma anyways with a fake output channel --- templates/android/src/jni.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/templates/android/src/jni.cpp b/templates/android/src/jni.cpp index 510d4ad..cde4110 100644 --- a/templates/android/src/jni.cpp +++ b/templates/android/src/jni.cpp @@ -16,23 +16,21 @@ #if PARAMETERS_N > 0 # include #endif -#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 + # define MINIAUDIO_IMPLEMENTATION # define MA_ENABLE_ONLY_SPECIFIC_BACKENDS # define MA_ENABLE_AAUDIO # include # define BLOCK_SIZE 32 -#endif + #if NUM_MIDI_INPUTS > 0 # include # include #endif -#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 static ma_device device; -#endif static plugin instance; static void * mem; #if (NUM_NON_OPT_CHANNELS_IN > NUM_CHANNELS_IN) || (NUM_NON_OPT_CHANNELS_OUT > NUM_CHANNELS_OUT) @@ -103,6 +101,7 @@ static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, const float * in_buf = reinterpret_cast(pInput); float * out_buf = reinterpret_cast(pOutput); +#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 ma_uint32 i = 0; #if NUM_CHANNELS_IN > 0 size_t ix = 0; @@ -133,6 +132,10 @@ static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, i += n; } +#else + for (ma_uint32 i = 0; i < frameCount; i++) + out_buf[i] = 0.f; // Unique fake channel +#endif } extern "C" @@ -143,12 +146,16 @@ JNI_FUNC(nativeAudioStart)(JNIEnv* env, jobject thiz) { #if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 # if NUM_CHANNELS_IN == 0 - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); # elif NUM_CHANNELS_OUT == 0 - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture); + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture); # else - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_duplex); + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_duplex); # endif +#else + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); +#endif + deviceConfig.periodSizeInFrames = BLOCK_SIZE; deviceConfig.periods = 1; deviceConfig.performanceProfile = ma_performance_profile_low_latency; @@ -163,12 +170,15 @@ JNI_FUNC(nativeAudioStart)(JNIEnv* env, jobject thiz) { deviceConfig.capture.shareMode = ma_share_mode_shared; deviceConfig.playback.pDeviceID = NULL; deviceConfig.playback.format = ma_format_f32; - deviceConfig.playback.channels = NUM_CHANNELS_OUT; +#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 + deviceConfig.playback.channels = NUM_CHANNELS_OUT; +#else + deviceConfig.playback.channels = 1; // Fake & muted +#endif deviceConfig.playback.shareMode = ma_share_mode_shared; if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) return false; -#endif plugin_init(&instance); @@ -186,9 +196,7 @@ JNI_FUNC(nativeAudioStart)(JNIEnv* env, jobject thiz) { mem = malloc(req); if (mem == NULL) { plugin_fini(&instance); -#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 ma_device_uninit(&device); -#endif return false; } plugin_mem_set(&instance, mem); @@ -250,14 +258,12 @@ JNI_FUNC(nativeAudioStart)(JNIEnv* env, jobject thiz) { y = NULL; #endif -#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 if (ma_device_start(&device) != MA_SUCCESS) { if (mem != NULL) free(mem); ma_device_uninit(&device); return false; } -#endif return true; } @@ -267,10 +273,8 @@ JNIEXPORT void JNICALL JNI_FUNC(nativeAudioStop)(JNIEnv* env, jobject thiz) { (void)env; (void)thiz; -#if NUM_CHANNELS_IN + NUM_CHANNELS_OUT > 0 ma_device_stop(&device); ma_device_uninit(&device); -#endif if (mem != NULL) free(mem); plugin_fini(&instance);