From 5c8ca78c99dae2a193284a03693ea4968b59f475 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Sun, 11 Oct 2015 19:33:19 +0200 Subject: [PATCH] Cached playback of audio files doorlock_client now caches wave files in memory for faster playback. LibAO is used for audio playback and libsndfile for wave file header parsing. --- doorlockd/client/doorlock-client.cpp | 17 +++++++++++++++-- doorlockd/client/mainwindow.cpp | 24 +++++++++++++----------- doorlockd/client/mainwindow.h | 9 +++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/doorlockd/client/doorlock-client.cpp b/doorlockd/client/doorlock-client.cpp index a12de78..d2693b2 100644 --- a/doorlockd/client/doorlock-client.cpp +++ b/doorlockd/client/doorlock-client.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include #include @@ -164,8 +166,17 @@ int main(int argc, char** argv) app.setOrganizationName("Binary Kitchen"); app.setApplicationName("doorlock-client"); - mainWindow = std::unique_ptr(new MainWindow); - mainWindow->showFullScreen(); + ao_initialize(); + + try { + mainWindow = std::unique_ptr(new MainWindow); + mainWindow->showFullScreen(); + } + catch(const std::exception &e) + { + l(LogLevel::error, e.what()); + exit(-1); + } // Start the TCP client as thread std::thread clientThread = std::thread([&] () { @@ -199,6 +210,8 @@ int main(int argc, char** argv) if (mainWindow) mainWindow.reset(); + ao_shutdown(); + l(LogLevel::notice, "Stopping doorlock-client"); return 0; } diff --git a/doorlockd/client/mainwindow.cpp b/doorlockd/client/mainwindow.cpp index 8409f6b..ca9d134 100644 --- a/doorlockd/client/mainwindow.cpp +++ b/doorlockd/client/mainwindow.cpp @@ -3,15 +3,17 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#define PLAY(file) \ - system("play -q " file " &") - MainWindow::MainWindow(QWidget *parent) : QWidget(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + _soundLock(Wave::fromFile(SOUND_LOCK)), + _soundUnlock(Wave::fromFile(SOUND_UNLOCK)), + _soundEmergencyUnlock(Wave::fromFile(SOUND_EMERGENCY_UNLOCK)), + _soundZonk(Wave::fromFile(SOUND_ZONK)), + _soundLockButton(Wave::fromFile(SOUND_LOCK_BUTTON)), + _soundUnlockButton(Wave::fromFile(SOUND_UNLOCK_BUTTON)) { ui->setupUi(this); - _LED(false); } @@ -31,23 +33,23 @@ void MainWindow::setClientmessage(const Clientmessage &msg) if (_oldMessage.isOpen() && !msg.isOpen()) { // regular close - PLAY(SOUND_LOCK); + _soundLock.playAsync(); } else if (!_oldMessage.isOpen() && msg.isOpen()) { // regular open - PLAY(SOUND_UNLOCK); + _soundUnlock.playAsync(); } else { // no change } if (doormsg.isEmergencyUnlock) { - PLAY(SOUND_EMERGENCY_UNLOCK); + _soundEmergencyUnlock.playAsync(); } else if (doormsg.isLockButton) { - PLAY(SOUND_LOCK_BUTTON); + _soundLockButton.playAsync(); } else if (doormsg.isUnlockButton) { if (msg.isOpen()) { - PLAY(SOUND_ZONK); + _soundZonk.playAsync(); } else { - PLAY(SOUND_UNLOCK_BUTTON); + _soundUnlockButton.playAsync(); } } diff --git a/doorlockd/client/mainwindow.h b/doorlockd/client/mainwindow.h index 186f80d..6d0612e 100644 --- a/doorlockd/client/mainwindow.h +++ b/doorlockd/client/mainwindow.h @@ -2,7 +2,9 @@ #define MAINWINDOW_H #include + #include "../lib/clientmessage.h" +#include "wave.h" namespace Ui { class MainWindow; @@ -27,6 +29,13 @@ private: Clientmessage _oldMessage = { }; + const Wave _soundLock; + const Wave _soundUnlock; + const Wave _soundEmergencyUnlock; + const Wave _soundZonk; + const Wave _soundLockButton; + const Wave _soundUnlockButton; + void _LED(const bool on); };