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
This commit is contained in:
Louis-Joseph Fournier 2016-01-05 16:02:03 +01:00
parent 497b6911a8
commit 28fb75d41e
5 changed files with 50 additions and 29 deletions

View file

@ -36,4 +36,9 @@ Item {
theme: theme
tuner: tuner
}
MouseArea {
anchors.fill: parent
onClicked: tuner.running = (tuner.running ^ true)
}
}

View file

@ -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)
}
}
}

View file

@ -151,11 +151,4 @@ Item {
index: tuner.octave
}
}
MouseArea {
anchors.fill: parent
onClicked: {
tuner.running = tuner.running ^ true
}
}
}

View file

@ -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;

View file

@ -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;