forked from MrBesen/soundboard
background Thread for soundcleanup
This commit is contained in:
parent
5d2beeb643
commit
f92d6c07d8
|
@ -5,6 +5,7 @@
|
|||
#include <string>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "sounddevice.h"
|
||||
|
@ -12,7 +13,7 @@
|
|||
class Sound {
|
||||
public:
|
||||
static Sound& instance();
|
||||
static void deinit();
|
||||
static void deinitInstance();
|
||||
|
||||
void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t endms = 0);
|
||||
bool addDefaultDevice();
|
||||
|
@ -25,8 +26,20 @@ private:
|
|||
Sound();
|
||||
~Sound();
|
||||
|
||||
ma_context context;
|
||||
void deinit();
|
||||
void init();
|
||||
|
||||
void backgroundThreadLoop();
|
||||
void cleanAllDecoders();
|
||||
|
||||
void startBackgroundThread();
|
||||
void stopBackgroundThread();
|
||||
|
||||
ma_context context;
|
||||
bool threadShouldrun = false;
|
||||
std::thread backgroundCleanup;
|
||||
|
||||
std::mutex devicesMutex;
|
||||
std::vector<SoundDevice*> devices;
|
||||
|
||||
static Sound* inst;
|
||||
|
|
|
@ -44,7 +44,7 @@ MainWindow::~MainWindow() {
|
|||
delete right;
|
||||
delete play;
|
||||
|
||||
Sound::deinit();
|
||||
Sound::deinitInstance();
|
||||
}
|
||||
|
||||
void MainWindow::reloadConfig() {
|
||||
|
|
|
@ -54,7 +54,7 @@ Sound& Sound::instance() {
|
|||
return *inst;
|
||||
}
|
||||
|
||||
void Sound::deinit() {
|
||||
void Sound::deinitInstance() {
|
||||
if(inst) {
|
||||
inst->stopAll();
|
||||
delete inst;
|
||||
|
@ -68,6 +68,7 @@ void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms,
|
|||
if(volume < 0.00001f)
|
||||
volume = 0.00001f;
|
||||
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
for(SoundDevice* sd : devices) {
|
||||
sd->addPlayback(name, volume, beginms, endms);
|
||||
}
|
||||
|
@ -76,6 +77,7 @@ void Sound::addPlayback(const std::string& name, float volume, uint64_t beginms,
|
|||
bool Sound::addDefaultDevice() {
|
||||
SoundDevice* sd = SoundDevice::createDevice(&context);
|
||||
if(sd) {
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
devices.push_back(sd);
|
||||
}
|
||||
return sd;
|
||||
|
@ -84,12 +86,15 @@ bool Sound::addDefaultDevice() {
|
|||
bool Sound::addDeviceWithName(const std::string& name) {
|
||||
SoundDevice* sd = (name.empty() ? SoundDevice::createDevice(&context) : SoundDevice::createDevice(&context, name));
|
||||
if(sd) {
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
devices.push_back(sd);
|
||||
}
|
||||
return sd;
|
||||
}
|
||||
|
||||
void Sound::stopAll() {
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
|
||||
for(SoundDevice* sd : devices) {
|
||||
sd->stop();
|
||||
}
|
||||
|
@ -98,41 +103,64 @@ void Sound::stopAll() {
|
|||
void Sound::reset() {
|
||||
Log::info << "ResetAudio";
|
||||
|
||||
// deinit
|
||||
stopAll();
|
||||
for(SoundDevice* sd : devices) {
|
||||
delete sd;
|
||||
}
|
||||
devices.clear();
|
||||
ma_context_uninit(&context);
|
||||
deinit();
|
||||
init();
|
||||
|
||||
// reinit
|
||||
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
|
||||
Log::error << "Failed to initialize Sound context.";
|
||||
throw new std::exception();
|
||||
}
|
||||
Log::info << "ResetAudio done";
|
||||
}
|
||||
|
||||
Sound::Sound() {
|
||||
init();
|
||||
startBackgroundThread();
|
||||
}
|
||||
|
||||
Sound::~Sound() {
|
||||
deinit();
|
||||
stopBackgroundThread();
|
||||
}
|
||||
|
||||
void Sound::deinit() {
|
||||
stopAll();
|
||||
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
for(SoundDevice* sd : devices) {
|
||||
delete sd;
|
||||
}
|
||||
devices.clear();
|
||||
|
||||
ma_context_uninit(&context);
|
||||
}
|
||||
|
||||
void Sound::init() {
|
||||
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
|
||||
Log::error << "Failed to initialize Sound context.";
|
||||
throw new std::exception();
|
||||
}
|
||||
}
|
||||
|
||||
Sound::~Sound() {
|
||||
stopAll();
|
||||
for(SoundDevice* sd : devices) {
|
||||
delete sd;
|
||||
}
|
||||
devices.clear();
|
||||
ma_context_uninit(&context);
|
||||
}
|
||||
|
||||
void Sound::backgroundThreadLoop() {
|
||||
while(threadShouldrun) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
cleanAllDecoders();
|
||||
}
|
||||
}
|
||||
|
||||
void Sound::cleanAllDecoders() {
|
||||
std::lock_guard<std::mutex> lock(devicesMutex);
|
||||
|
||||
for(SoundDevice* sd : devices) {
|
||||
sd->cleanupDecoders();
|
||||
}
|
||||
}
|
||||
|
||||
void Sound::startBackgroundThread() {
|
||||
threadShouldrun = true;
|
||||
backgroundCleanup = std::thread(&Sound::backgroundThreadLoop, this);
|
||||
}
|
||||
|
||||
void Sound::stopBackgroundThread() {
|
||||
threadShouldrun = false;
|
||||
if(backgroundCleanup.joinable()) {
|
||||
backgroundCleanup.join();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue