From a2ffd4611b5325d9dccb7887206660a39ce8efaf Mon Sep 17 00:00:00 2001 From: Stefano D'Angelo Date: Thu, 11 Apr 2024 10:44:27 +0200 Subject: [PATCH] tentative ios fix background audio pause/resume --- templates/ios/src/app-Bridging-Header.h | 2 ++ templates/ios/src/app.swift | 6 +++++ templates/ios/src/native.mm | 30 +++++++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/templates/ios/src/app-Bridging-Header.h b/templates/ios/src/app-Bridging-Header.h index e232263..0e78a53 100644 --- a/templates/ios/src/app-Bridging-Header.h +++ b/templates/ios/src/app-Bridging-Header.h @@ -20,5 +20,7 @@ char audioStart(); void audioStop(); +void audioPause(); +void audioResume(); void setParameter(int i, float v); float getParameter(int i); diff --git a/templates/ios/src/app.swift b/templates/ios/src/app.swift index a71bed9..d92db0c 100644 --- a/templates/ios/src/app.swift +++ b/templates/ios/src/app.swift @@ -97,6 +97,12 @@ struct templateApp: App { var body: some Scene { WindowGroup { ContentView() + .onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in + audioPause() + } + .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in + audioResume() + } } } } diff --git a/templates/ios/src/native.mm b/templates/ios/src/native.mm index 5a1c570..779b058 100644 --- a/templates/ios/src/native.mm +++ b/templates/ios/src/native.mm @@ -36,6 +36,8 @@ #define BLOCK_SIZE 32 static ma_device device; +static ma_device_config deviceConfig; +char device_inited = 0; static plugin instance; static void * mem; #if NUM_NON_OPT_CHANNELS_IN > NUM_CHANNELS_IN @@ -189,14 +191,14 @@ extern "C" char audioStart() { #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); + 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); + deviceConfig = ma_device_config_init(ma_device_type_capture); # else - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_duplex); + deviceConfig = ma_device_config_init(ma_device_type_duplex); # endif #else - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig = ma_device_config_init(ma_device_type_playback); #endif deviceConfig.periodSizeInFrames = BLOCK_SIZE; @@ -328,6 +330,9 @@ char audioStart() { ma_device_uninit(&device); return false; } + + device_inited = 1; + return true; } @@ -341,6 +346,23 @@ void audioStop() { // No need to close MIDI connections (e.g. via MIDIClientDispose), the system terminates them when the app terminates. } +extern "C" +void audioPause() { + if (device_inited) { + ma_device_stop(&device); + ma_device_uninit(&device); + } +} + +extern "C" +void audioResume() { + // TODO: couldn't this could fail?... + if (device_inited) { + ma_device_init(NULL, &deviceConfig, &device); + ma_device_start(&device); + } +} + extern "C" float getParameter(int i) { #if PARAMETERS_N > 0