121 lines
3.2 KiB
C
Raw Normal View History

2020-11-12 00:53:38 +01:00
#pragma once
#include <map>
#include <string>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <dsp/routing.h>
2020-11-30 05:51:33 +01:00
#include <dsp/processing.h>
2020-11-29 20:55:00 +01:00
#include <dsp/sink.h>
2020-11-12 00:53:38 +01:00
#include <mutex>
#include <event.h>
2020-11-22 18:26:48 +01:00
#include <vector>
2020-11-12 00:53:38 +01:00
class SinkManager {
public:
SinkManager();
class Sink {
public:
2020-11-22 18:26:48 +01:00
virtual void start() = 0;
virtual void stop() = 0;
2020-11-12 00:53:38 +01:00
virtual void menuHandler() = 0;
};
class Stream {
public:
2020-11-30 05:51:33 +01:00
Stream() {}
2020-11-12 00:53:38 +01:00
Stream(dsp::stream<dsp::stereo_t>* in, const Event<float>::EventHandler& srChangeHandler, float sampleRate);
2020-11-30 05:51:33 +01:00
void init(dsp::stream<dsp::stereo_t>* in, const Event<float>::EventHandler& srChangeHandler, float sampleRate);
2020-11-22 18:26:48 +01:00
void start();
void stop();
2020-11-30 05:51:33 +01:00
void setVolume(float volume);
float getVolume();
void setSampleRate(float sampleRate);
float getSampleRate();
2020-11-12 00:53:38 +01:00
void setInput(dsp::stream<dsp::stereo_t>* in);
dsp::stream<dsp::stereo_t>* bindStream();
void unbindStream(dsp::stream<dsp::stereo_t>* stream);
friend SinkManager;
friend SinkManager::Sink;
2020-11-30 05:51:33 +01:00
dsp::stream<dsp::stereo_t>* sinkOut;
2020-11-29 20:55:00 +01:00
Event<float> srChange;
2020-11-12 00:53:38 +01:00
private:
dsp::stream<dsp::stereo_t>* _in;
dsp::Splitter<dsp::stereo_t> splitter;
2020-11-29 20:55:00 +01:00
SinkManager::Sink* sink;
2020-11-30 05:51:33 +01:00
dsp::stream<dsp::stereo_t> volumeInput;
dsp::Volume<dsp::stereo_t> volumeAjust;
2020-11-12 00:53:38 +01:00
std::mutex ctrlMtx;
float _sampleRate;
2020-11-29 20:55:00 +01:00
int providerId = 0;
bool running = false;
2020-11-30 05:51:33 +01:00
float guiVolume = 1.0f;
2020-11-12 00:53:38 +01:00
};
struct SinkProvider {
2020-11-29 20:55:00 +01:00
SinkManager::Sink* (*create)(SinkManager::Stream* stream, std::string streamName, void* ctx);
2020-11-12 00:53:38 +01:00
void* ctx;
};
2020-11-29 20:55:00 +01:00
class NullSink : SinkManager::Sink {
public:
2020-11-30 05:51:33 +01:00
NullSink(SinkManager::Stream* stream) {
ns.init(stream->sinkOut);
}
void start() { ns.start(); }
void stop() { ns.stop(); }
2020-11-29 20:55:00 +01:00
void menuHandler() {}
static SinkManager::Sink* create(SinkManager::Stream* stream, std::string streamName, void* ctx) {
2020-11-30 05:51:33 +01:00
stream->srChange.emit(48000);
return new SinkManager::NullSink(stream);
2020-11-29 20:55:00 +01:00
}
2020-11-30 05:51:33 +01:00
private:
dsp::NullSink<dsp::stereo_t> ns;
2020-11-29 20:55:00 +01:00
};
2020-11-12 00:53:38 +01:00
void registerSinkProvider(std::string name, SinkProvider provider);
void registerStream(std::string name, Stream* stream);
void unregisterStream(std::string name);
2020-11-22 18:26:48 +01:00
void startStream(std::string name);
void stopStream(std::string name);
2020-11-30 05:51:33 +01:00
float getStreamSampleRate(std::string name);
2020-11-22 18:26:48 +01:00
void setStreamSink(std::string name, std::string providerName);
void showVolumeSlider(std::string name, std::string prefix, float width, float btnHeight = -1.0f, int btwBorder = 0);
2020-11-30 05:51:33 +01:00
2020-11-12 00:53:38 +01:00
dsp::stream<dsp::stereo_t>* bindStream(std::string name);
void unbindStream(std::string name, dsp::stream<dsp::stereo_t>* stream);
2020-11-30 05:51:33 +01:00
void loadSinksFromConfig();
2020-11-12 00:53:38 +01:00
void showMenu();
2020-11-30 05:51:33 +01:00
std::vector<std::string> getStreamNames();
2020-11-12 00:53:38 +01:00
private:
2020-11-30 05:51:33 +01:00
void loadStreamConfig(std::string name);
void saveStreamConfig(std::string name);
2020-11-12 00:53:38 +01:00
std::map<std::string, SinkProvider> providers;
std::map<std::string, Stream*> streams;
2020-11-22 18:26:48 +01:00
std::vector<std::string> providerNames;
2020-11-30 05:51:33 +01:00
std::vector<std::string> streamNames;
2020-11-12 00:53:38 +01:00
};