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 };