forked from MrBesen/soundboard
130 lines
2.4 KiB
C++
130 lines
2.4 KiB
C++
// Diese Datei ist stark an den examples von miniaudio orientiert (vor allem simple_mixing.c). Die Struktur ähnelt sich daher möglicherweise.
|
|
|
|
|
|
//#define MA_DEBUG_OUTPUT
|
|
//#define MA_LOG_LEVEL MA_LOG_LEVEL_VERBOSE
|
|
|
|
#ifdef WIN32
|
|
#ifndef __wtypes_h__
|
|
#include <wtypes.h>
|
|
#endif
|
|
|
|
#ifndef __WINDEF_
|
|
#include <windef.h>
|
|
#endif
|
|
|
|
#ifndef _WINUSER_
|
|
#include <winuser.h>
|
|
#endif
|
|
|
|
#ifndef __RPC_H__
|
|
#include <rpc.h>
|
|
#endif
|
|
#endif
|
|
|
|
#include "sound.h"
|
|
|
|
#define DR_FLAC_IMPLEMENTATION
|
|
#include "extras/dr_flac.h" //flac
|
|
#define DR_MP3_IMPLEMENTATION
|
|
#include "extras/dr_mp3.h" //mp3
|
|
#define DR_WAV_IMPLEMENTATION
|
|
#include "extras/dr_wav.h" //wav
|
|
|
|
#include "extras/stb_vorbis.c" //vorbis
|
|
|
|
#define MINIAUDIO_IMPLEMENTATION
|
|
#include "miniaudio.h"
|
|
|
|
#include <algorithm> //std::max
|
|
#include <cstring>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
|
|
#include <Log.h>
|
|
|
|
Sound* Sound::inst = nullptr;
|
|
const std::string Sound::FOLDER = "/home/yannis/prog/ts3/soundboardsounds/";
|
|
|
|
Sound& Sound::instance() {
|
|
if (!inst) {
|
|
inst = new Sound();
|
|
}
|
|
return *inst;
|
|
}
|
|
|
|
void Sound::deinit() {
|
|
if(inst) {
|
|
inst->stopAll();
|
|
delete inst;
|
|
inst = nullptr;
|
|
}
|
|
|
|
Log::info << "Sound deinited";
|
|
}
|
|
|
|
void Sound::addPlayback(const std::string& name, float volume) {
|
|
for(SoundDevice* sd : devices) {
|
|
sd->addPlayback(name, volume);
|
|
}
|
|
}
|
|
|
|
bool Sound::addDefaultDevice() {
|
|
SoundDevice* sd = SoundDevice::createDevice(&context);
|
|
if(sd) {
|
|
devices.push_back(sd);
|
|
}
|
|
return sd;
|
|
}
|
|
|
|
bool Sound::addDeviceWithName(const std::string& name) {
|
|
SoundDevice* sd = (name.empty() ? SoundDevice::createDevice(&context) : SoundDevice::createDevice(&context, name));
|
|
if(sd) {
|
|
devices.push_back(sd);
|
|
}
|
|
return sd;
|
|
}
|
|
|
|
void Sound::stopAll() {
|
|
for(SoundDevice* sd : devices) {
|
|
sd->stop();
|
|
}
|
|
}
|
|
|
|
void Sound::reset() {
|
|
Log::info << "ResetAudio";
|
|
|
|
// deinit
|
|
stopAll();
|
|
for(SoundDevice* sd : devices) {
|
|
delete sd;
|
|
}
|
|
devices.clear();
|
|
ma_context_uninit(&context);
|
|
|
|
// 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() {
|
|
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);
|
|
}
|
|
|