From aaa97c924237f60aabfe9e24f2bc6f6cee888da7 Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Thu, 29 Jul 2021 01:19:03 +0200
Subject: only open microphone when actually needed. fixes #1165

---
 src/frontend/qt_sdl/main.cpp | 66 ++++++++++++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 20 deletions(-)

(limited to 'src/frontend/qt_sdl')

diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp
index d7751b7..01e5e75 100644
--- a/src/frontend/qt_sdl/main.cpp
+++ b/src/frontend/qt_sdl/main.cpp
@@ -106,6 +106,8 @@ u32 micExtBufferWritePos;
 u32 micWavLength;
 s16* micWavBuffer;
 
+void micCallback(void* data, Uint8* stream, int len);
+
 
 void audioCallback(void* data, Uint8* stream, int len)
 {
@@ -143,6 +145,40 @@ void audioCallback(void* data, Uint8* stream, int len)
 }
 
 
+void micOpen()
+{
+    if (Config::MicInputType != 1)
+    {
+        micDevice = 0;
+        return;
+    }
+
+    SDL_AudioSpec whatIwant, whatIget;
+    memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
+    whatIwant.freq = 44100;
+    whatIwant.format = AUDIO_S16LSB;
+    whatIwant.channels = 1;
+    whatIwant.samples = 1024;
+    whatIwant.callback = micCallback;
+    micDevice = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
+    if (!micDevice)
+    {
+        printf("Mic init failed: %s\n", SDL_GetError());
+    }
+    else
+    {
+        SDL_PauseAudioDevice(micDevice, 0);
+    }
+}
+
+void micClose()
+{
+    if (micDevice)
+        SDL_CloseAudioDevice(micDevice);
+
+    micDevice = 0;
+}
+
 void micLoadWav(const char* name)
 {
     SDL_AudioSpec format;
@@ -623,7 +659,7 @@ void EmuThread::emuRun()
     // checkme
     emit windowEmuStart();
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 0);
-    if (micDevice)   SDL_PauseAudioDevice(micDevice, 0);
+    micOpen();
 }
 
 void EmuThread::emuPause()
@@ -636,7 +672,7 @@ void EmuThread::emuPause()
     while (EmuStatus != 2);
 
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 1);
-    if (micDevice)   SDL_PauseAudioDevice(micDevice, 1);
+    micClose();
 }
 
 void EmuThread::emuUnpause()
@@ -649,7 +685,7 @@ void EmuThread::emuUnpause()
     EmuRunning = PrevEmuStatus;
 
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 0);
-    if (micDevice)   SDL_PauseAudioDevice(micDevice, 0);
+    micOpen();
 }
 
 void EmuThread::emuStop()
@@ -658,7 +694,7 @@ void EmuThread::emuStop()
     EmuPause = 0;
 
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 1);
-    if (micDevice)   SDL_PauseAudioDevice(micDevice, 1);
+    micClose();
 }
 
 void EmuThread::emuFrameStep()
@@ -2393,6 +2429,8 @@ void MainWindow::onOpenAudioSettings()
 
 void MainWindow::onAudioSettingsFinished(int res)
 {
+    micClose();
+
     if (Config::MicInputType == 3)
     {
         micLoadWav(Config::MicWavPath);
@@ -2408,6 +2446,8 @@ void MainWindow::onAudioSettingsFinished(int res)
         else
             Frontend::Mic_SetExternalBuffer(NULL, 0);
     }
+
+    micOpen();
 }
 
 void MainWindow::onOpenWifiSettings()
@@ -2751,21 +2791,7 @@ int main(int argc, char** argv)
         SDL_PauseAudioDevice(audioDevice, 1);
     }
 
-    memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
-    whatIwant.freq = 44100;
-    whatIwant.format = AUDIO_S16LSB;
-    whatIwant.channels = 1;
-    whatIwant.samples = 1024;
-    whatIwant.callback = micCallback;
-    micDevice = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
-    if (!micDevice)
-    {
-        printf("Mic init failed: %s\n", SDL_GetError());
-    }
-    else
-    {
-        SDL_PauseAudioDevice(micDevice, 1);
-    }
+    micDevice = 0;
 
 
     memset(micExtBuffer, 0, sizeof(micExtBuffer));
@@ -2836,7 +2862,7 @@ int main(int argc, char** argv)
     Frontend::DeInit_ROM();
 
     if (audioDevice) SDL_CloseAudioDevice(audioDevice);
-    if (micDevice)   SDL_CloseAudioDevice(micDevice);
+    micClose();
 
     SDL_DestroyCond(audioSync);
     SDL_DestroyMutex(audioSyncLock);
-- 
cgit v1.2.3