#pragma once
#include "ofConstants.h"
#include <functional>
class ofSoundBuffer;
class ofBaseSoundInput{
public:
virtual ~ofBaseSoundInput() {};
virtual void audioIn( ofSoundBuffer& buffer );
virtual void audioIn( float * input, int bufferSize, int nChannels, int deviceID, long unsigned long tickCount );
virtual void audioIn( float * input, int bufferSize, int nChannels );
virtual void audioReceived( float * input, int bufferSize, int nChannels ){}
};
class ofBaseSoundOutput{
public:
virtual ~ofBaseSoundOutput() {};
virtual void audioOut( ofSoundBuffer& buffer );
virtual void audioOut( float * output, int bufferSize, int nChannels, int deviceID, long unsigned long tickCount );
virtual void audioOut( float * output, int bufferSize, int nChannels );
virtual void audioRequested( float * output, int bufferSize, int nChannels ){}
};
class ofSoundDevice {
public:
enum Api {
UNSPECIFIED,
DEFAULT,
ALSA,
PULSE,
OSS,
JACK,
OSX_CORE,
MS_WASAPI,
MS_ASIO,
MS_DS,
NUM_APIS
} api = UNSPECIFIED;
friend std::ostream& operator << (std::ostream& os, const ofSoundDevice& dev);
friend std::ostream& operator << (std::ostream& os, const std::vector<ofSoundDevice>& devs);
std::string name{"Unknown"};
int deviceID = -1;
unsigned int inputChannels = 0;
unsigned int outputChannels = 0;
bool isDefaultInput = false;
bool isDefaultOutput = false;
std::vector<unsigned int> sampleRates;
};
class ofSoundStreamSettings {
public:
virtual ~ofSoundStreamSettings() {}
size_t sampleRate = 44100;
size_t bufferSize = 256;
size_t numBuffers = 4;
size_t numInputChannels = 0;
size_t numOutputChannels = 0;
virtual bool setInDevice(const ofSoundDevice & device);
virtual bool setOutDevice(const ofSoundDevice & device);
virtual bool setApi(ofSoundDevice::Api api);
virtual const ofSoundDevice * getInDevice() const;
virtual const ofSoundDevice * getOutDevice() const;
virtual ofSoundDevice::Api getApi() const;
template<typename Listener>
void setInListener(Listener * inListener){
inCallback = std::bind(static_cast<void(Listener::*)(ofSoundBuffer &)>(&Listener::audioIn), inListener, std::placeholders::_1);
}
template<typename Listener>
void setOutListener(Listener * outListener){
outCallback = std::bind(static_cast<void(Listener::*)(ofSoundBuffer &)>(&Listener::audioOut), outListener, std::placeholders::_1);
}
std::function<void(ofSoundBuffer &)> inCallback;
std::function<void(ofSoundBuffer &)> outCallback;
private:
ofSoundDevice inDevice;
ofSoundDevice outDevice;
ofSoundDevice::Api api = ofSoundDevice::Api::UNSPECIFIED;
};
class ofBaseSoundStream {
public:
virtual ~ofBaseSoundStream() {}
virtual bool setup(const ofSoundStreamSettings & settings) = 0;
virtual void setInput(ofBaseSoundInput * soundInput) = 0;
virtual void setOutput(ofBaseSoundOutput * soundOutput) = 0;
virtual std::vector<ofSoundDevice> getDeviceList(ofSoundDevice::Api api) const = 0;
virtual void printDeviceList() const;
virtual void start() = 0;
virtual void stop() = 0;
virtual void close() = 0;
virtual uint64_t getTickCount() const = 0;
virtual int getNumInputChannels() const = 0;
virtual int getNumOutputChannels() const = 0;
virtual int getSampleRate() const = 0;
virtual int getBufferSize() const = 0;
virtual ofSoundDevice getInDevice() const = 0;
virtual ofSoundDevice getOutDevice() const = 0;
};
std::string toString(ofSoundDevice::Api api);
class ofBaseSoundPlayer {
public:
ofBaseSoundPlayer(){};
virtual ~ofBaseSoundPlayer(){};
virtual bool load(const std::filesystem::path& fileName, bool stream = false)=0;
virtual void unload()=0;
virtual void play() = 0;
virtual void stop() = 0;
virtual void setVolume(float vol) = 0;
virtual void setPan(float vol) = 0;
virtual void setSpeed(float spd) = 0;
virtual void setPaused(bool bP) = 0;
virtual void setLoop(bool bLp) = 0;
virtual void setMultiPlay(bool bMp) = 0;
virtual void setPosition(float pct) = 0;
virtual void setPositionMS(int ms) = 0;
virtual float getPosition() const = 0;
virtual int getPositionMS() const = 0;
virtual bool isPlaying() const = 0;
virtual float getSpeed() const = 0;
virtual float getPan() const = 0;
virtual bool isLoaded() const = 0;
virtual float getVolume() const = 0;
};
Comments