amuse/lib/BooBackend.cpp

323 lines
9.1 KiB
C++
Raw Normal View History

#include "amuse/BooBackend.hpp"
#include "amuse/Voice.hpp"
2016-05-07 22:10:57 +00:00
#include "amuse/Submix.hpp"
#include "amuse/Engine.hpp"
namespace amuse
{
size_t BooBackendVoice::VoiceCallback::supplyAudio(boo::IAudioVoice&,
size_t frames, int16_t* data)
{
return m_parent.m_clientVox.supplyAudio(frames, data);
}
BooBackendVoice::BooBackendVoice(boo::IAudioVoiceEngine& engine, Voice& clientVox,
double sampleRate, bool dynamicPitch)
: m_clientVox(clientVox), m_cb(*this),
m_booVoice(engine.allocateNewMonoVoice(sampleRate, &m_cb, dynamicPitch))
{}
2016-05-07 22:10:57 +00:00
BooBackendVoice::BooBackendVoice(boo::IAudioSubmix& submix, Voice& clientVox,
double sampleRate, bool dynamicPitch)
: m_clientVox(clientVox), m_cb(*this),
m_booVoice(submix.allocateNewMonoVoice(sampleRate, &m_cb, dynamicPitch))
{}
void BooBackendVoice::resetSampleRate(double sampleRate)
{
m_booVoice->resetSampleRate(sampleRate);
}
void BooBackendVoice::setMatrixCoefficients(const float coefs[8])
{
m_booVoice->setMonoMatrixCoefficients(coefs);
}
2016-05-17 03:47:12 +00:00
void BooBackendVoice::setPitchRatio(double ratio, bool slew)
{
2016-05-17 03:47:12 +00:00
m_booVoice->setPitchRatio(ratio, slew);
}
void BooBackendVoice::start()
{
m_booVoice->start();
}
void BooBackendVoice::stop()
{
m_booVoice->stop();
}
2016-05-07 22:10:57 +00:00
bool BooBackendSubmix::SubmixCallback::canApplyEffect() const
{
return m_parent.m_clientSmx.canApplyEffect();
}
2016-05-08 05:19:27 +00:00
void BooBackendSubmix::SubmixCallback::applyEffect(int16_t* audio, size_t frameCount,
2016-05-14 04:46:39 +00:00
const boo::ChannelMap& chanMap, double) const
2016-05-07 22:10:57 +00:00
{
2016-05-14 04:46:39 +00:00
return m_parent.m_clientSmx.applyEffect(audio, frameCount, reinterpret_cast<const ChannelMap&>(chanMap));
2016-05-07 22:10:57 +00:00
}
2016-05-08 05:19:27 +00:00
void BooBackendSubmix::SubmixCallback::applyEffect(int32_t* audio, size_t frameCount,
2016-05-14 04:46:39 +00:00
const boo::ChannelMap& chanMap, double) const
2016-05-07 22:10:57 +00:00
{
2016-05-14 04:46:39 +00:00
return m_parent.m_clientSmx.applyEffect(audio, frameCount, reinterpret_cast<const ChannelMap&>(chanMap));
2016-05-07 22:10:57 +00:00
}
2016-05-08 05:19:27 +00:00
void BooBackendSubmix::SubmixCallback::applyEffect(float* audio, size_t frameCount,
2016-05-14 04:46:39 +00:00
const boo::ChannelMap& chanMap, double) const
2016-05-07 22:10:57 +00:00
{
2016-05-14 04:46:39 +00:00
return m_parent.m_clientSmx.applyEffect(audio, frameCount, reinterpret_cast<const ChannelMap&>(chanMap));
2016-05-07 22:10:57 +00:00
}
BooBackendSubmix::BooBackendSubmix(boo::IAudioVoiceEngine& engine, Submix& clientSmx)
: m_clientSmx(clientSmx), m_cb(*this), m_booSubmix(engine.allocateNewSubmix(&m_cb))
{}
BooBackendSubmix::BooBackendSubmix(boo::IAudioSubmix& parent, Submix& clientSmx)
: m_clientSmx(clientSmx), m_cb(*this), m_booSubmix(parent.allocateNewSubmix(&m_cb))
{}
void BooBackendSubmix::setChannelGains(const float gains[8])
{
m_booSubmix->setChannelGains(gains);
}
std::unique_ptr<IBackendVoice>
BooBackendSubmix::allocateVoice(Voice& clientVox, double sampleRate, bool dynamicPitch)
{
return std::make_unique<BooBackendVoice>(*m_booSubmix, clientVox, sampleRate, dynamicPitch);
}
2016-05-14 04:46:39 +00:00
double BooBackendSubmix::getSampleRate() const
{
return m_booSubmix->getSampleRate();
}
SubmixFormat BooBackendSubmix::getSampleFormat() const
{
return SubmixFormat(m_booSubmix->getSampleFormat());
}
std::string BooBackendMIDIReader::description()
{
return m_midiIn->description();
}
2016-05-20 22:56:25 +00:00
BooBackendMIDIReader::~BooBackendMIDIReader() {}
BooBackendMIDIReader::BooBackendMIDIReader(Engine& engine, const char* name)
: m_engine(engine), m_decoder(*this)
{
2016-05-20 22:56:25 +00:00
BooBackendVoiceAllocator& voxAlloc = static_cast<BooBackendVoiceAllocator&>(engine.getBackend());
if (!name)
2016-05-21 01:15:31 +00:00
{
auto devices = voxAlloc.m_booEngine.enumerateMIDIDevices();
for (const auto& dev : devices)
{
m_midiIn = voxAlloc.m_booEngine.newRealMIDIIn(dev.first.c_str(),
std::bind(&BooBackendMIDIReader::_MIDIReceive, this,
std::placeholders::_1));
if (m_midiIn)
return;
}
2016-05-20 22:56:25 +00:00
m_midiIn = voxAlloc.m_booEngine.newVirtualMIDIIn(std::bind(&BooBackendMIDIReader::_MIDIReceive, this,
2016-05-21 01:15:31 +00:00
std::placeholders::_1));
}
2016-05-20 22:56:25 +00:00
else
m_midiIn = voxAlloc.m_booEngine.newRealMIDIIn(name,
2016-05-21 01:15:31 +00:00
std::bind(&BooBackendMIDIReader::_MIDIReceive, this,
std::placeholders::_1));
2016-05-20 22:56:25 +00:00
}
void BooBackendMIDIReader::_MIDIReceive(std::vector<uint8_t>&& bytes)
{
std::unique_lock<std::mutex> lk(m_midiMutex);
m_queue.emplace_back(std::chrono::steady_clock::now(), std::move(bytes));
}
void BooBackendMIDIReader::pumpReader(double dt)
{
dt += 0.001; /* Add 1ms to ensure consumer keeps up with producer */
std::unique_lock<std::mutex> lk(m_midiMutex);
if (m_queue.empty())
return;
/* Determine range of buffer updates within this period */
auto periodEnd = m_queue.cbegin();
std::chrono::steady_clock::time_point startPt = m_queue.front().first;
for (; periodEnd != m_queue.cend() ; ++periodEnd)
{
double delta = std::chrono::duration_cast<std::chrono::microseconds>
(periodEnd->first - startPt).count() / 1000000.0;
if (delta > dt)
break;
}
if (m_queue.cbegin() == periodEnd)
return;
/* Dispatch buffers */
for (auto it = m_queue.begin() ; it != periodEnd ;)
{
m_decoder.receiveBytes(it->second.cbegin(), it->second.cend());
it = m_queue.erase(it);
}
}
void BooBackendMIDIReader::noteOff(uint8_t chan, uint8_t key, uint8_t velocity)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->keyOff(chan, key, velocity);
}
void BooBackendMIDIReader::noteOn(uint8_t chan, uint8_t key, uint8_t velocity)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->keyOn(chan, key, velocity);
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::notePressure(uint8_t /*chan*/, uint8_t /*key*/, uint8_t /*pressure*/)
{
}
void BooBackendMIDIReader::controlChange(uint8_t chan, uint8_t control, uint8_t value)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->setCtrlValue(chan, control, value);
}
void BooBackendMIDIReader::programChange(uint8_t chan, uint8_t program)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->setChanProgram(chan, program);
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::channelPressure(uint8_t /*chan*/, uint8_t /*pressure*/)
{
}
void BooBackendMIDIReader::pitchBend(uint8_t chan, int16_t pitch)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->setPitchWheel(chan, (pitch - 0x2000) / float(0x2000));
}
void BooBackendMIDIReader::allSoundOff(uint8_t chan)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->allOff(chan, true);
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::resetAllControllers(uint8_t /*chan*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::localControl(uint8_t /*chan*/, bool /*on*/)
{
}
void BooBackendMIDIReader::allNotesOff(uint8_t chan)
{
for (std::shared_ptr<Sequencer>& seq : m_engine.getActiveSequencers())
seq->allOff(chan, false);
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::omniMode(uint8_t /*chan*/, bool /*on*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::polyMode(uint8_t /*chan*/, bool /*on*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::sysex(const void* /*data*/, size_t /*len*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::timeCodeQuarterFrame(uint8_t /*message*/, uint8_t /*value*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::songPositionPointer(uint16_t /*pointer*/)
{
}
2016-05-21 01:15:31 +00:00
void BooBackendMIDIReader::songSelect(uint8_t /*song*/)
{
}
void BooBackendMIDIReader::tuneRequest()
{
}
void BooBackendMIDIReader::startSeq()
{
}
void BooBackendMIDIReader::continueSeq()
{
}
void BooBackendMIDIReader::stopSeq()
{
}
void BooBackendMIDIReader::reset()
{
}
BooBackendVoiceAllocator::BooBackendVoiceAllocator(boo::IAudioVoiceEngine& booEngine)
: m_booEngine(booEngine)
{}
std::unique_ptr<IBackendVoice>
BooBackendVoiceAllocator::allocateVoice(Voice& clientVox, double sampleRate, bool dynamicPitch)
{
return std::make_unique<BooBackendVoice>(m_booEngine, clientVox, sampleRate, dynamicPitch);
}
2016-05-07 22:10:57 +00:00
std::unique_ptr<IBackendSubmix> BooBackendVoiceAllocator::allocateSubmix(Submix& clientSmx)
{
return std::make_unique<BooBackendSubmix>(m_booEngine, clientSmx);
}
std::vector<std::pair<std::string, std::string>> BooBackendVoiceAllocator::enumerateMIDIDevices()
{
return m_booEngine.enumerateMIDIDevices();
}
std::unique_ptr<IMIDIReader> BooBackendVoiceAllocator::allocateMIDIReader(Engine& engine, const char* name)
{
2016-05-20 22:56:25 +00:00
std::unique_ptr<IMIDIReader> ret = std::make_unique<BooBackendMIDIReader>(engine, name);
if (!static_cast<BooBackendMIDIReader&>(*ret).m_midiIn)
return {};
2016-05-20 22:56:25 +00:00
return ret;
}
2016-05-19 10:12:32 +00:00
void BooBackendVoiceAllocator::register5MsCallback(std::function<void(double)>&& callback)
{
m_booEngine.register5MsCallback(std::move(callback));
}
AudioChannelSet BooBackendVoiceAllocator::getAvailableSet()
{
return AudioChannelSet(m_booEngine.getAvailableSet());
}
2016-05-14 04:46:39 +00:00
void BooBackendVoiceAllocator::pumpAndMixVoices()
{
m_booEngine.pumpAndMixVoices();
}
}