From 9b9b826a841799252e0d707f19ab1b10faca17cf Mon Sep 17 00:00:00 2001
From: SeeLook <seelook@gmail.com>
Date: Wed, 3 Mar 2021 13:40:15 +0100
Subject: [PATCH] [Android] Handle when app is going background and release
 input

- otherwise Nootka steals mic
- and pitch detection makes no sense then
- but stopListening() was not enough, input device
  has to be stopped so apparent methods were added
---
 changes                       |  3 ++-
 src/libs/sound/tqtaudioin.cpp | 31 ++++++++++++++++++++++++++-----
 src/libs/sound/tqtaudioin.h   |  5 ++++-
 src/libs/sound/tsound.cpp     | 16 ++++++++++++++--
 4 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/changes b/changes
index 8e26da78d..249e2d984 100644
--- a/changes
+++ b/changes
@@ -7,7 +7,8 @@
      - no more jumping UI when some of buttons was pressed
      - fixed pitch detection for low-pitched instruments (bass)
     ANDROID
-     - Animation about how score edition with touch works
+     - animation about how score edition with touch works
+     - do not block mic when app is send to background
 
 1.7.3 beta3
      - new, better way of detecting note/rest duration (rhythms)
diff --git a/src/libs/sound/tqtaudioin.cpp b/src/libs/sound/tqtaudioin.cpp
index fbfc0c373..ce813085d 100644
--- a/src/libs/sound/tqtaudioin.cpp
+++ b/src/libs/sound/tqtaudioin.cpp
@@ -70,13 +70,15 @@ TaudioIN::TaudioIN(TaudioParams* params, QObject *parent) :
 TaudioIN::~TaudioIN() {
   stopListening();
   delete m_touchHandler;
-  m_audioIN->blockSignals(true); // otherwise stop() will activate it again
-  m_audioIN->stop();
+  if (m_audioIN) {
+    m_audioIN->blockSignals(true); // otherwise stop() will activate it again
+    m_audioIN->stop();
+  }
 
   m_audioParams->INdevName = m_deviceName; // store device name at the app exit
   m_deviceName = QStringLiteral("anything");
 
-  m_instance = 0;
+  m_instance = nullptr;
 
   if (m_audioIN)
     delete m_audioIN;
@@ -90,8 +92,28 @@ void TaudioIN::updateAudioParams() {
 }
 
 
+void TaudioIN::stopDevice() {
+  if (m_audioIN) {
+      disconnect(m_audioIN, &QAudioInput::stateChanged, this, &TaudioIN::stateChangedSlot);
+      m_audioIN->stop();
+  } else
+      qDebug() << "[tqtaudioin] Input device doesn't exist! Nothing to stop!";
+}
+
+
+void TaudioIN::startDevice() {
+  if (m_audioIN) {
+      m_audioIN->start(m_inBuffer);
+      connect(m_audioIN, &QAudioInput::stateChanged, this, &TaudioIN::stateChangedSlot, Qt::UniqueConnection);
+  } else
+      qDebug() << "[tqtaudioin] Input device doesn't exist! Nothing to start!";
+}
+
+
 void TaudioIN::startListening() {
   m_touchHandler->skip();
+  if (m_audioIN && m_audioIN->state() == QAudio::StoppedState)
+    startDevice();
   if (detectingState() == e_stopped) {
     resetVolume();
     resetChunkPitch();
@@ -169,8 +191,7 @@ void TaudioIN::createInputDevice() {
   m_inBuffer->setBufferSize(0); // respect amount of data send by input device, otherwise it will be overwritten
   connect(m_inBuffer, SIGNAL(readAudio(const char*, qint64&)), this, SLOT(bufferReady(const char*, qint64&)));
 
-  m_audioIN->start(m_inBuffer);
-  connect(m_audioIN, &QAudioInput::stateChanged, this, &TaudioIN::stateChangedSlot);
+  startDevice();
 //   qDebug() << "setAudioInParams" << "\nrate:" << finder()->aGl()->rate << m_audioIN->format().sampleRate() << "\nmethod:"
 //           << m_audioParams->detectMethod
 //           << "\nmin duration" << m_audioParams->minDuration << "\nmin volume" << m_audioParams->minimalVol
diff --git a/src/libs/sound/tqtaudioin.h b/src/libs/sound/tqtaudioin.h
index edc0b4661..9b1f933d0 100644
--- a/src/libs/sound/tqtaudioin.h
+++ b/src/libs/sound/tqtaudioin.h
@@ -69,7 +69,7 @@ class NOOTKASOUND_EXPORT TaudioIN : public TcommonListener
   Q_OBJECT
 
 public:
-  explicit TaudioIN(TaudioParams* params, QObject *parent = 0);
+  explicit TaudioIN(TaudioParams* params, QObject *parent = nullptr);
   ~TaudioIN();
 
       /**
@@ -85,6 +85,9 @@ public:
 
   void updateAudioParams();
 
+  void stopDevice();
+  void startDevice();
+
       /**
        * Stops handling touch events  to suspend sniffing
        */
diff --git a/src/libs/sound/tsound.cpp b/src/libs/sound/tsound.cpp
index cc84b37e7..7d435c073 100644
--- a/src/libs/sound/tsound.cpp
+++ b/src/libs/sound/tsound.cpp
@@ -92,9 +92,9 @@ void Tsound::init() {
       TrtAudio::initJACKorASIO(GLOB->A->JACKorASIO);
 #endif
       if (GLOB->A->OUTenabled)
-          createPlayer();
+        createPlayer();
       if (GLOB->A->INenabled)
-          createSniffer();
+        createSniffer();
 
       connect(NOO, &TnootkaQML::playNote, [=](const Tnote& n){ play(n); });
       setDefaultAmbitus();
@@ -107,6 +107,18 @@ void Tsound::init() {
   m_maxVol = maxVolRange();
   qApp->installEventFilter(this);
   m_volKeyTimer.start();
+  connect(qApp, &QGuiApplication::applicationStateChanged, this, [=]{
+    if (qApp->applicationState() == Qt::ApplicationActive)
+        startListen();
+    else if (qApp->applicationState() == Qt::ApplicationInactive)
+        stop();
+    else {
+        stop();
+        auto qtSniff = qobject_cast<TaudioIN*>(sniffer);
+        if (qtSniff)
+          qtSniff->stopDevice();
+    }
+  });
 #endif
 }
 
-- 
GitLab