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

View File

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