116 lines
2.1 KiB
C++
116 lines
2.1 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>
|
|
|
|
|
|
Sound* Sound::inst = nullptr;
|
|
const std::string Sound::FOLDER = "/home/yannis/prog/ts3/soundboardsounds/";
|
|
|
|
Sound& Sound::instance() {
|
|
if (!inst) {
|
|
inst = new Sound(); //TODO: needs delete
|
|
inst->init();
|
|
}
|
|
return *inst;
|
|
}
|
|
|
|
void Sound::deinit() {
|
|
if(inst) {
|
|
inst->stopAll();
|
|
delete inst;
|
|
inst = nullptr;
|
|
}
|
|
|
|
std::cout << "Sound deinited" << std::endl;
|
|
}
|
|
|
|
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 = SoundDevice::createDevice(&context, name);
|
|
if(sd) {
|
|
devices.push_back(sd);
|
|
}
|
|
return sd;
|
|
}
|
|
|
|
void Sound::stopAll() {
|
|
for(SoundDevice* sd : devices) {
|
|
sd->stop();
|
|
}
|
|
}
|
|
|
|
Sound::Sound() {
|
|
}
|
|
|
|
Sound::~Sound() {
|
|
stopAll();
|
|
for(SoundDevice* sd : devices) {
|
|
delete sd;
|
|
}
|
|
devices.clear();
|
|
ma_context_uninit(&context);
|
|
}
|
|
|
|
void Sound::init() {
|
|
//dies wird getrennt vom ctor, da es beim ma_device_start sonst zu einer race condition kommen kann
|
|
|
|
// enumerate devices
|
|
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
|
|
std::cout << "Failed to initialize Sound context." << std::endl;
|
|
throw new std::exception();
|
|
}
|
|
}
|