1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00

Asynchronous Wave playback

This commit is contained in:
Ralf Ramsauer 2015-10-11 19:15:50 +02:00
parent 4865a6bb9f
commit 811917b334
2 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,7 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <stdexcept> #include <stdexcept>
#include <thread>
#include <sndfile.h> #include <sndfile.h>
@ -76,6 +77,7 @@ Wave Wave::fromFile(const std::string &filename)
void Wave::play() const void Wave::play() const
{ {
std::lock_guard<std::mutex> l(_playMutex);
if (_device == nullptr) { if (_device == nullptr) {
return; return;
} }
@ -84,3 +86,10 @@ void Wave::play() const
(char*)&_data.front(), (char*)&_data.front(),
_data.size()); _data.size());
} }
void Wave::playAsync() const
{
std::thread([this] () {
play();
}).detach();
}

View File

@ -1,9 +1,10 @@
#ifndef WAVE_H #ifndef WAVE_H
#define WAVE_H #define WAVE_H
#include <memory>
#include <mutex>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include <ao/ao.h> #include <ao/ao.h>
@ -23,9 +24,12 @@ public:
Wave &operator=(const Wave &rhs); Wave &operator=(const Wave &rhs);
void play() const; void play() const;
void playAsync() const;
private: private:
mutable std::mutex _playMutex = { };
std::unique_ptr<ao_sample_format> _format; std::unique_ptr<ao_sample_format> _format;
ao_device* _device = { nullptr }; ao_device* _device = { nullptr };