From 99feb1403695e193b322bc5524861b14ebfa001c Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Sun, 24 Apr 2016 22:02:49 +0200 Subject: [PATCH] Use aplay command for playing sounds This basically reverts 5c8ca78c99dae2a193284a03693ea4968b59f475. Using libao + sndfile was a bad idea... Signed-off-by: Ralf Ramsauer --- doorlockd/CMakeLists.txt | 2 +- doorlockd/client/doorlock-client.cpp | 6 --- doorlockd/client/mainwindow.cpp | 12 ++--- doorlockd/client/wave.cpp | 77 +++------------------------- doorlockd/client/wave.h | 22 ++------ 5 files changed, 17 insertions(+), 102 deletions(-) diff --git a/doorlockd/CMakeLists.txt b/doorlockd/CMakeLists.txt index 5086a0b..907b995 100644 --- a/doorlockd/CMakeLists.txt +++ b/doorlockd/CMakeLists.txt @@ -110,7 +110,7 @@ add_executable(doorlockd ${_DOORLOCKD_SRCS}) target_link_libraries(doorlockd doorlock) add_executable(doorlock-client ${_DOORLOCK_CLIENT_SRCS}) -target_link_libraries(doorlock-client doorlock qrencode ao sndfile Qt5::Widgets) +target_link_libraries(doorlock-client doorlock qrencode Qt5::Widgets) target_include_directories(doorlock-client PRIVATE ${CMAKE_SOURCE_DIR}) mark_as_advanced( diff --git a/doorlockd/client/doorlock-client.cpp b/doorlockd/client/doorlock-client.cpp index d2693b2..78f155f 100644 --- a/doorlockd/client/doorlock-client.cpp +++ b/doorlockd/client/doorlock-client.cpp @@ -3,8 +3,6 @@ #include #include -#include - #include #include @@ -166,8 +164,6 @@ int main(int argc, char** argv) app.setOrganizationName("Binary Kitchen"); app.setApplicationName("doorlock-client"); - ao_initialize(); - try { mainWindow = std::unique_ptr(new MainWindow); mainWindow->showFullScreen(); @@ -210,8 +206,6 @@ 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 0f66aae..c822c9c 100644 --- a/doorlockd/client/mainwindow.cpp +++ b/doorlockd/client/mainwindow.cpp @@ -6,12 +6,12 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent), 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)) + _soundLock(Wave(SOUND_LOCK)), + _soundUnlock(Wave(SOUND_UNLOCK)), + _soundEmergencyUnlock(Wave(SOUND_EMERGENCY_UNLOCK)), + _soundZonk(Wave(SOUND_ZONK)), + _soundLockButton(Wave(SOUND_LOCK_BUTTON)), + _soundUnlockButton(Wave(SOUND_UNLOCK_BUTTON)) { ui->setupUi(this); _LED(false); diff --git a/doorlockd/client/wave.cpp b/doorlockd/client/wave.cpp index 25e4558..26ee6bf 100644 --- a/doorlockd/client/wave.cpp +++ b/doorlockd/client/wave.cpp @@ -1,90 +1,25 @@ #include -#include #include #include -#include - #include "wave.h" -Wave::Wave(int bits, - int channels, - int rate, - Raw data) : - _format(new ao_sample_format), - _data(data) -{ - _format->bits = bits; - _format->rate = rate; - _format->channels = channels; - _format->byte_format = AO_FMT_LITTLE; - _format->matrix = nullptr; +std::mutex Wave::_playMutex = {}; - int default_driver; - default_driver = ao_default_driver_id(); - _device = ao_open_live(default_driver, - _format.get(), - nullptr); +Wave::Wave(const std::string &filename): + _filename(filename), + _command("aplay " + _filename) +{ } Wave::~Wave() { - if (_device != nullptr) - ao_close(_device); -} - -Wave Wave::fromFile(const std::string &filename) -{ - SF_INFO sfinfo; - SNDFILE *file = sf_open(filename.c_str(), SFM_READ, &sfinfo); - if (file == nullptr) - throw std::runtime_error("Unable to open soundfile " + filename); - - size_t rawSize = sfinfo.channels * sfinfo.frames * sizeof(short); - Raw data; - data.resize(rawSize); - - sf_read_raw(file, &data.front(), data.size()); - sf_close(file); - - int bits; - switch (sfinfo.format & SF_FORMAT_SUBMASK) { - case SF_FORMAT_PCM_16: - bits = 16; - break; - case SF_FORMAT_PCM_24: - bits = 24; - break; - case SF_FORMAT_PCM_32: - bits = 32; - break; - case SF_FORMAT_PCM_S8: - bits = 8; - break; - case SF_FORMAT_PCM_U8: - bits = 8; - break; - default: - bits = 16; - break; - } - - return Wave(bits, - sfinfo.channels, - sfinfo.samplerate, - data); } void Wave::play() const { std::lock_guard l(_playMutex); - if (_device == nullptr) { - return; - } - - ao_play(_device, - (char*)&_data.front(), - _data.size()); + system(_command.c_str()); } void Wave::playAsync() const diff --git a/doorlockd/client/wave.h b/doorlockd/client/wave.h index 548dbc2..ef6d15e 100644 --- a/doorlockd/client/wave.h +++ b/doorlockd/client/wave.h @@ -1,23 +1,12 @@ #ifndef WAVE_H #define WAVE_H -#include #include #include -#include - -#include class Wave { public: - - using Raw = std::vector; - - static Wave fromFile(const std::string &filename); - Wave(int bits, - int channels, - int rate, - Raw data); + Wave(const std::string &filename); Wave(const Wave &rhs); ~Wave(); @@ -27,13 +16,10 @@ public: void playAsync() const; private: + static std::mutex _playMutex; - mutable std::mutex _playMutex = { }; - - std::unique_ptr _format; - ao_device* _device = { nullptr }; - - const Raw _data; + const std::string _filename; + const std::string _command; }; #endif