From 28fb75d41e2a3f708496f1865c9183e98ac19590 Mon Sep 17 00:00:00 2001 From: Louis-Joseph Fournier Date: Tue, 5 Jan 2016 16:02:03 +0100 Subject: [PATCH] Stop pulseaudio after 2sec of inactivity - Add pause management on page click - Start pulseaudio every time it's needed - Stop pulseaudio after 2sec of inactivity --- qml/Desktop.qml | 5 +++++ qml/Sailfish.qml | 8 ++++++- qml/TunerScreen.qml | 7 ------ src/TunerWorker.cpp | 52 ++++++++++++++++++++++++++++----------------- src/TunerWorker.hpp | 7 +++++- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/qml/Desktop.qml b/qml/Desktop.qml index 3f15519..84cf56a 100644 --- a/qml/Desktop.qml +++ b/qml/Desktop.qml @@ -36,4 +36,9 @@ Item { theme: theme tuner: tuner } + + MouseArea { + anchors.fill: parent + onClicked: tuner.running = (tuner.running ^ true) + } } diff --git a/qml/Sailfish.qml b/qml/Sailfish.qml index a2b5c3f..e0be184 100644 --- a/qml/Sailfish.qml +++ b/qml/Sailfish.qml @@ -36,6 +36,7 @@ ApplicationWindow { Page { id: page allowedOrientations: Orientation.All + signal togglePause() SilicaFlickable { anchors.fill: parent @@ -53,9 +54,14 @@ ApplicationWindow { running: Qt.application.active && app.userRunning } + MouseArea { + anchors.fill: parent + onClicked: togglePause() + } + Component.onCompleted: { app.tuner = tunerObject - //togglePause.connect(app.togglePause) + togglePause.connect(app.togglePause) } } } diff --git a/qml/TunerScreen.qml b/qml/TunerScreen.qml index fd88b6c..8b3b927 100644 --- a/qml/TunerScreen.qml +++ b/qml/TunerScreen.qml @@ -151,11 +151,4 @@ Item { index: tuner.octave } } - - MouseArea { - anchors.fill: parent - onClicked: { - tuner.running = tuner.running ^ true - } - } } diff --git a/src/TunerWorker.cpp b/src/TunerWorker.cpp index daa23bf..1257e67 100644 --- a/src/TunerWorker.cpp +++ b/src/TunerWorker.cpp @@ -110,9 +110,9 @@ void TunerWorker::Entry() { cerr << __func__ << endl; - int nbSamplePreventRunning = nbSecPreventRunning * PitchDetection::rate; + int nbSamplePreventBlanking = nbSecPreventBlanking * PitchDetection::rate; int nb_sample_running = 0; - bool new_stream = true; + bool new_stream = true, waked; int16_t *buffer = new int16_t[nbSampleBuffer]; @@ -125,31 +125,26 @@ void TunerWorker::Entry() emit temperamentListUpdated(pitchDetection->GetTemperamentList()); // pulseaudio - pa_simple *p_simple; + pa_simple *p_simple = NULL; pa_sample_spec p_spec; p_spec.format = PA_SAMPLE_S16NE; p_spec.channels = 1; p_spec.rate = PitchDetection::rate; - p_simple = pa_simple_new( - NULL, - NAME, - PA_STREAM_RECORD, - NULL, - "Mic", - &p_spec, - NULL, - NULL, - NULL - ); - while (1) { // wait for running mutex.lock(); if (!running) { blank_prevent(false); - while (!running && !quit) condition.wait(&mutex); + while (!running && !quit) { + waked = condition.wait(&mutex, p_simple ? stopPulseAfterMs : ULONG_MAX); + if (!waked && p_simple) { + // stop pulseaudio after a delay if not running + pa_simple_free(p_simple); + p_simple = NULL; + } + } cerr << "wake-up" << endl; // reset operations on start new_stream = true; @@ -169,12 +164,29 @@ void TunerWorker::Entry() } mutex.unlock(); + if (!p_simple) { + // start pulseaudio if stopped + p_simple = pa_simple_new( + NULL, + NAME, + PA_STREAM_RECORD, + NULL, + "Mic", + &p_spec, + NULL, + NULL, + NULL + ); + } + else if (new_stream) { + // flush pulseaudio if paused + pa_simple_flush(p_simple, NULL); + } + // if srteam was stopped, reset analyse if (new_stream) { pitchDetection->Reset(); nb_sample_running = 0; - // flush audio - pa_simple_flush(p_simple, NULL); new_stream = false; } @@ -198,13 +210,13 @@ void TunerWorker::Entry() // prevent screen blanking nb_sample_running += nbSampleBuffer; - if (nb_sample_running >= nbSamplePreventRunning && running) { + if (nb_sample_running >= nbSamplePreventBlanking && running) { nb_sample_running = 0; blank_prevent(true); } } - pa_simple_free(p_simple); + if (p_simple) pa_simple_free(p_simple); delete pitchDetection; delete buffer; diff --git a/src/TunerWorker.hpp b/src/TunerWorker.hpp index 6131817..eec2d87 100644 --- a/src/TunerWorker.hpp +++ b/src/TunerWorker.hpp @@ -35,8 +35,13 @@ class TunerWorker : public QObject { Q_OBJECT private: - static const int nbSecPreventRunning = 40; + /// time beetween dbus signals to prevent screen blanking + static const int nbSecPreventBlanking = 40; + /// nb of audio sample to read from pulseaudio at once static const int nbSampleBuffer = 512; + /// stop pulseaudio after time if not running + static const int stopPulseAfterMs = 2000; // 2 sec + static const char *filename_record; QMutex mutex;