mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-10-31 22:47:05 +01:00
Asynchronous Wave playback
This commit is contained in:
parent
4865a6bb9f
commit
811917b334
@ -1,6 +1,7 @@
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
|
||||
#include <sndfile.h>
|
||||
|
||||
@ -76,6 +77,7 @@ Wave Wave::fromFile(const std::string &filename)
|
||||
|
||||
void Wave::play() const
|
||||
{
|
||||
std::lock_guard<std::mutex> 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();
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#ifndef WAVE_H
|
||||
#define WAVE_H
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <ao/ao.h>
|
||||
|
||||
@ -23,9 +24,12 @@ public:
|
||||
Wave &operator=(const Wave &rhs);
|
||||
|
||||
void play() const;
|
||||
void playAsync() const;
|
||||
|
||||
private:
|
||||
|
||||
mutable std::mutex _playMutex = { };
|
||||
|
||||
std::unique_ptr<ao_sample_format> _format;
|
||||
ao_device* _device = { nullptr };
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user