muzgen/lib/AudioEngine.hpp

74 lines
1.4 KiB
C++
Raw Normal View History

#ifndef muz_AUDIOENGINE_HPP
#define muz_AUDIOENGINE_HPP
#include <portaudio.h>
#include "commons.hpp"
#include "AudioConf.hpp"
namespace muz
{
class Signal;
MUZ_ERROR(audio_error);
/**
* Make sound from signals.
* @see Signal
**/
class AudioEngine
{
public:
explicit AudioEngine(AudioConf const& conf);
virtual ~AudioEngine();
/**
* Initialize the engine opening audio device.
**/
void init();
/**
* Run the engine, making sound.
**/
void run();
/**
* Push a new signal in the signals queue.
*/
void push_signal(std::unique_ptr<Signal> signal);
/**
* Pop the current signal or does nothing if the queue is empty.
**/
void pop_signal();
private:
AudioConf m_conf;
PaStream* m_stream = nullptr;
std::vector<std::unique_ptr<Signal>> m_sig_queue;
std::mutex m_sig_mtx;
/**
* Throws an audio_error exception if err is an error.
**/
void check_error(PaError err);
/**
* Gives the next sample of the current signal.
* @see Signal
**/
std::vector<float> next();
/**
* Portaudio audio callback.
**/
static int callback(void const* input, void* output,
unsigned long frames_per_buffer,
PaStreamCallbackTimeInfo const* time,
PaStreamCallbackFlags flags,
void* data);
};
}
#endif