1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-06-13 11:42:35 +02:00

Use aplay command for playing sounds

This basically reverts 5c8ca78c99.
Using libao + sndfile was a bad idea...

Signed-off-by: Ralf Ramsauer <ralf@ramses-pyramidenbau.de>
This commit is contained in:
Ralf Ramsauer 2016-04-24 22:02:49 +02:00
parent 88926613e7
commit 99feb14036
5 changed files with 17 additions and 102 deletions

View File

@ -110,7 +110,7 @@ add_executable(doorlockd ${_DOORLOCKD_SRCS})
target_link_libraries(doorlockd doorlock) target_link_libraries(doorlockd doorlock)
add_executable(doorlock-client ${_DOORLOCK_CLIENT_SRCS}) 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}) target_include_directories(doorlock-client PRIVATE ${CMAKE_SOURCE_DIR})
mark_as_advanced( mark_as_advanced(

View File

@ -3,8 +3,6 @@
#include <string> #include <string>
#include <thread> #include <thread>
#include <ao/ao.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
@ -166,8 +164,6 @@ int main(int argc, char** argv)
app.setOrganizationName("Binary Kitchen"); app.setOrganizationName("Binary Kitchen");
app.setApplicationName("doorlock-client"); app.setApplicationName("doorlock-client");
ao_initialize();
try { try {
mainWindow = std::unique_ptr<MainWindow>(new MainWindow); mainWindow = std::unique_ptr<MainWindow>(new MainWindow);
mainWindow->showFullScreen(); mainWindow->showFullScreen();
@ -210,8 +206,6 @@ int main(int argc, char** argv)
if (mainWindow) if (mainWindow)
mainWindow.reset(); mainWindow.reset();
ao_shutdown();
l(LogLevel::notice, "Stopping doorlock-client"); l(LogLevel::notice, "Stopping doorlock-client");
return 0; return 0;
} }

View File

@ -6,12 +6,12 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::MainWindow), ui(new Ui::MainWindow),
_soundLock(Wave::fromFile(SOUND_LOCK)), _soundLock(Wave(SOUND_LOCK)),
_soundUnlock(Wave::fromFile(SOUND_UNLOCK)), _soundUnlock(Wave(SOUND_UNLOCK)),
_soundEmergencyUnlock(Wave::fromFile(SOUND_EMERGENCY_UNLOCK)), _soundEmergencyUnlock(Wave(SOUND_EMERGENCY_UNLOCK)),
_soundZonk(Wave::fromFile(SOUND_ZONK)), _soundZonk(Wave(SOUND_ZONK)),
_soundLockButton(Wave::fromFile(SOUND_LOCK_BUTTON)), _soundLockButton(Wave(SOUND_LOCK_BUTTON)),
_soundUnlockButton(Wave::fromFile(SOUND_UNLOCK_BUTTON)) _soundUnlockButton(Wave(SOUND_UNLOCK_BUTTON))
{ {
ui->setupUi(this); ui->setupUi(this);
_LED(false); _LED(false);

View File

@ -1,90 +1,25 @@
#include <cstring> #include <cstring>
#include <fstream>
#include <stdexcept> #include <stdexcept>
#include <thread> #include <thread>
#include <sndfile.h>
#include "wave.h" #include "wave.h"
Wave::Wave(int bits, std::mutex Wave::_playMutex = {};
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;
int default_driver; Wave::Wave(const std::string &filename):
default_driver = ao_default_driver_id(); _filename(filename),
_device = ao_open_live(default_driver, _command("aplay " + _filename)
_format.get(), {
nullptr);
} }
Wave::~Wave() 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 void Wave::play() const
{ {
std::lock_guard<std::mutex> l(_playMutex); std::lock_guard<std::mutex> l(_playMutex);
if (_device == nullptr) { system(_command.c_str());
return;
}
ao_play(_device,
(char*)&_data.front(),
_data.size());
} }
void Wave::playAsync() const void Wave::playAsync() const

View File

@ -1,23 +1,12 @@
#ifndef WAVE_H #ifndef WAVE_H
#define WAVE_H #define WAVE_H
#include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <vector>
#include <ao/ao.h>
class Wave { class Wave {
public: public:
Wave(const std::string &filename);
using Raw = std::vector<char>;
static Wave fromFile(const std::string &filename);
Wave(int bits,
int channels,
int rate,
Raw data);
Wave(const Wave &rhs); Wave(const Wave &rhs);
~Wave(); ~Wave();
@ -27,13 +16,10 @@ public:
void playAsync() const; void playAsync() const;
private: private:
static std::mutex _playMutex;
mutable std::mutex _playMutex = { }; const std::string _filename;
const std::string _command;
std::unique_ptr<ao_sample_format> _format;
ao_device* _device = { nullptr };
const Raw _data;
}; };
#endif #endif