From 811917b33447ad227428795c3c618c4a19c7b2f3 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Sun, 11 Oct 2015 19:15:50 +0200 Subject: [PATCH] Asynchronous Wave playback --- doorlockd/client/wave.cpp | 9 +++++++++ doorlockd/client/wave.h | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doorlockd/client/wave.cpp b/doorlockd/client/wave.cpp index d7cfbe0..25e4558 100644 --- a/doorlockd/client/wave.cpp +++ b/doorlockd/client/wave.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -76,6 +77,7 @@ Wave Wave::fromFile(const std::string &filename) void Wave::play() const { + std::lock_guard l(_playMutex); if (_device == nullptr) { return; } @@ -84,3 +86,10 @@ void Wave::play() const (char*)&_data.front(), _data.size()); } + +void Wave::playAsync() const +{ + std::thread([this] () { + play(); + }).detach(); +} diff --git a/doorlockd/client/wave.h b/doorlockd/client/wave.h index b60560c..548dbc2 100644 --- a/doorlockd/client/wave.h +++ b/doorlockd/client/wave.h @@ -1,9 +1,10 @@ #ifndef WAVE_H #define WAVE_H +#include +#include #include #include -#include #include @@ -23,9 +24,12 @@ public: Wave &operator=(const Wave &rhs); void play() const; + void playAsync() const; private: + mutable std::mutex _playMutex = { }; + std::unique_ptr _format; ao_device* _device = { nullptr };