1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 02:14:26 +01: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)
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(

View File

@ -3,8 +3,6 @@
#include <string>
#include <thread>
#include <ao/ao.h>
#include <boost/asio.hpp>
#include <boost/program_options.hpp>
@ -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<MainWindow>(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;
}

View File

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

View File

@ -1,90 +1,25 @@
#include <cstring>
#include <fstream>
#include <stdexcept>
#include <thread>
#include <sndfile.h>
#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<std::mutex> l(_playMutex);
if (_device == nullptr) {
return;
}
ao_play(_device,
(char*)&_data.front(),
_data.size());
system(_command.c_str());
}
void Wave::playAsync() const

View File

@ -1,23 +1,12 @@
#ifndef WAVE_H
#define WAVE_H
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <ao/ao.h>
class Wave {
public:
using Raw = std::vector<char>;
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<ao_sample_format> _format;
ao_device* _device = { nullptr };
const Raw _data;
const std::string _filename;
const std::string _command;
};
#endif