soundboard/include/sound.h

65 lines
1.4 KiB
C
Raw Normal View History

2021-12-13 00:28:04 +01:00
#pragma once
#include "miniaudio.h"
2021-12-20 02:13:26 +01:00
#include <functional>
2021-12-13 00:28:04 +01:00
#include <string>
#include <list>
#include <mutex>
2021-12-18 15:34:27 +01:00
#include <thread>
2021-12-13 00:28:04 +01:00
#include <vector>
#include "sounddevice.h"
2021-12-20 00:19:35 +01:00
#include "samplereader.h"
#include "sound.h"
2021-12-13 00:28:04 +01:00
class Sound {
public:
2021-12-20 02:13:26 +01:00
using sndcb_func = std::function<void(uint64_t)>;
2021-12-13 00:28:04 +01:00
static Sound& instance();
2021-12-18 15:34:27 +01:00
static void deinitInstance();
2021-12-13 00:28:04 +01:00
2021-12-22 12:52:25 +01:00
void addPlayback(const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0);
void addPlayback(const std::string& audioDeviceName, const std::string& name, float volume = 1.f, uint64_t beginms = 0, uint64_t length = 0, sndcb_func callback = {});
2021-12-13 00:28:04 +01:00
bool addDefaultDevice();
bool addDeviceWithName(const std::string& name);
void stopAll();
2021-12-13 14:48:43 +01:00
void reset();
2021-12-13 00:28:04 +01:00
2021-12-20 00:19:35 +01:00
SampleReader* openFile(const std::string& name);
2021-12-20 01:30:04 +01:00
std::vector<std::string> getOutputs();
2021-12-20 00:19:35 +01:00
2022-04-02 23:42:56 +02:00
void setMasterVolume(float m);
float getMasterVolume() const;
2021-12-13 17:28:32 +01:00
static std::string FOLDER;
2021-12-20 02:13:26 +01:00
2021-12-13 00:28:04 +01:00
private:
Sound();
~Sound();
2021-12-18 15:34:27 +01:00
void deinit();
void init();
void backgroundThreadLoop();
void cleanAllDecoders();
void startBackgroundThread();
void stopBackgroundThread();
2021-12-13 00:28:04 +01:00
ma_context context;
2021-12-18 15:34:27 +01:00
bool threadShouldrun = false;
std::thread backgroundCleanup;
2021-12-13 00:28:04 +01:00
2021-12-18 15:34:27 +01:00
std::mutex devicesMutex;
2021-12-13 00:28:04 +01:00
std::vector<SoundDevice*> devices;
2022-04-02 23:42:56 +02:00
float masterVolume = 1.0;
2021-12-13 00:28:04 +01:00
static Sound* inst;
friend void sound_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
};