Chris@19: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@21: Sonic Visualiser Chris@21: An audio file viewer and annotation editor. Chris@21: Centre for Digital Music, Queen Mary, University of London. Chris@21: This file copyright 2006 Chris Cannam. Chris@0: Chris@21: This program is free software; you can redistribute it and/or Chris@21: modify it under the terms of the GNU General Public License as Chris@21: published by the Free Software Foundation; either version 2 of the Chris@21: License, or (at your option) any later version. See the file Chris@21: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "AudioGenerator.h" Chris@0: Chris@0: #include "base/ViewManager.h" Chris@0: #include "base/PlayParameters.h" Chris@11: #include "base/PlayParameterRepository.h" Chris@0: Chris@11: #include "model/NoteModel.h" Chris@0: #include "model/DenseTimeValueModel.h" Chris@0: #include "model/SparseOneDimensionalModel.h" Chris@0: Chris@0: #include "plugin/RealTimePluginFactory.h" Chris@0: #include "plugin/RealTimePluginInstance.h" Chris@0: #include "plugin/PluginIdentifier.h" Chris@26: #include "plugin/PluginXml.h" Chris@0: #include "plugin/api/alsa/seq_event.h" Chris@0: Chris@0: #include Martin@14: #include Chris@0: Chris@0: const size_t Chris@0: AudioGenerator::m_pluginBlockSize = 2048; Chris@0: Chris@5: //#define DEBUG_AUDIO_GENERATOR 1 Chris@0: Chris@0: AudioGenerator::AudioGenerator(ViewManager *manager) : Chris@0: m_viewManager(manager), Chris@0: m_sourceSampleRate(0), Chris@0: m_targetChannelCount(1) Chris@0: { Chris@22: connect(PlayParameterRepository::instance(), Chris@22: SIGNAL(playPluginIdChanged(const Model *, QString)), Chris@22: this, Chris@22: SLOT(playPluginIdChanged(const Model *, QString))); Chris@22: Chris@22: connect(PlayParameterRepository::instance(), Chris@22: SIGNAL(playPluginConfigurationChanged(const Model *, QString)), Chris@22: this, Chris@22: SLOT(playPluginConfigurationChanged(const Model *, QString))); Chris@0: } Chris@0: Chris@0: AudioGenerator::~AudioGenerator() Chris@0: { Chris@0: } Chris@0: Chris@10: bool Chris@13: AudioGenerator::canPlay(const Model *model) Chris@13: { Chris@13: if (dynamic_cast(model) || Chris@13: dynamic_cast(model) || Chris@13: dynamic_cast(model)) { Chris@13: return true; Chris@13: } else { Chris@13: return false; Chris@13: } Chris@13: } Chris@13: Chris@13: bool Chris@0: AudioGenerator::addModel(Model *model) Chris@0: { Chris@0: if (m_sourceSampleRate == 0) { Chris@0: Chris@0: m_sourceSampleRate = model->getSampleRate(); Chris@0: Chris@0: } else { Chris@0: Chris@0: DenseTimeValueModel *dtvm = Chris@0: dynamic_cast(model); Chris@0: Chris@0: if (dtvm) { Chris@0: m_sourceSampleRate = model->getSampleRate(); Chris@10: return true; Chris@0: } Chris@0: } Chris@0: Chris@22: RealTimePluginInstance *plugin = loadPluginFor(model); Chris@22: if (plugin) { Chris@22: QMutexLocker locker(&m_mutex); Chris@22: m_synthMap[model] = plugin; Chris@22: return true; Chris@11: } Chris@0: Chris@22: return false; Chris@22: } Chris@22: Chris@22: void Chris@22: AudioGenerator::playPluginIdChanged(const Model *model, QString) Chris@22: { Chris@22: if (m_synthMap.find(model) == m_synthMap.end()) return; Chris@22: Chris@22: RealTimePluginInstance *plugin = loadPluginFor(model); Chris@22: if (plugin) { Chris@22: QMutexLocker locker(&m_mutex); Chris@22: delete m_synthMap[model]; Chris@22: m_synthMap[model] = plugin; Chris@22: } Chris@22: } Chris@22: Chris@22: void Chris@22: AudioGenerator::playPluginConfigurationChanged(const Model *model, Chris@22: QString configurationXml) Chris@22: { Chris@22: if (m_synthMap.find(model) == m_synthMap.end()) return; Chris@22: Chris@22: RealTimePluginInstance *plugin = m_synthMap[model]; Chris@22: if (plugin) { Chris@26: PluginXml(plugin).setParametersFromXml(configurationXml); Chris@22: } Chris@22: } Chris@22: Chris@22: QString Chris@22: AudioGenerator::getDefaultPlayPluginId(const Model *model) Chris@22: { Chris@22: const SparseOneDimensionalModel *sodm = Chris@22: dynamic_cast(model); Chris@22: if (sodm) { Chris@22: return QString("dssi:%1:sample_player"). Chris@22: arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); Chris@22: } Chris@22: Chris@22: const NoteModel *nm = dynamic_cast(model); Chris@11: if (nm) { Chris@22: return QString("dssi:%1:sample_player"). Chris@22: arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); Chris@22: } Chris@22: Chris@22: return ""; Chris@22: } Chris@22: Chris@22: QString Chris@22: AudioGenerator::getDefaultPlayPluginConfiguration(const Model *model) Chris@22: { Chris@22: const SparseOneDimensionalModel *sodm = Chris@22: dynamic_cast(model); Chris@22: if (sodm) { Chris@22: return ""; Chris@11: } Chris@11: Chris@22: const NoteModel *nm = dynamic_cast(model); Chris@22: if (nm) { Chris@22: return ""; Chris@22: } Chris@22: Chris@22: return ""; Chris@22: } Chris@22: Chris@22: RealTimePluginInstance * Chris@22: AudioGenerator::loadPluginFor(const Model *model) Chris@22: { Chris@22: QString pluginId, configurationXml; Chris@22: Chris@22: PlayParameters *parameters = Chris@22: PlayParameterRepository::instance()->getPlayParameters(model); Chris@22: if (parameters) { Chris@22: pluginId = parameters->getPlayPluginId(); Chris@22: configurationXml = parameters->getPlayPluginConfiguration(); Chris@22: } Chris@22: Chris@22: if (pluginId == "") { Chris@22: pluginId = getDefaultPlayPluginId(model); Chris@22: configurationXml = getDefaultPlayPluginConfiguration(model); Chris@22: } Chris@22: Chris@22: if (pluginId == "") return 0; Chris@22: Chris@22: RealTimePluginInstance *plugin = loadPlugin(pluginId, ""); Chris@22: if (configurationXml != "") { Chris@26: PluginXml(plugin).setParametersFromXml(configurationXml); Chris@22: } Chris@22: Chris@22: if (parameters) { Chris@22: parameters->setPlayPluginId(pluginId); Chris@22: parameters->setPlayPluginConfiguration(configurationXml); Chris@22: } Chris@22: Chris@22: return plugin; Chris@11: } Chris@11: Chris@11: RealTimePluginInstance * Chris@11: AudioGenerator::loadPlugin(QString pluginId, QString program) Chris@11: { Chris@0: RealTimePluginFactory *factory = Chris@0: RealTimePluginFactory::instanceFor(pluginId); Chris@0: Chris@0: if (!factory) { Chris@0: std::cerr << "Failed to get plugin factory" << std::endl; Chris@10: return false; Chris@0: } Chris@0: Chris@0: RealTimePluginInstance *instance = Chris@0: factory->instantiatePlugin Chris@0: (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount); Chris@0: Chris@0: if (instance) { Chris@0: for (unsigned int i = 0; i < instance->getParameterCount(); ++i) { Chris@0: instance->setParameterValue(i, instance->getParameterDefault(i)); Chris@0: } Chris@20: std::string defaultProgram = instance->getProgram(0, 0); Chris@11: if (defaultProgram != "") { Chris@20: std::cerr << "first selecting default program " << defaultProgram << std::endl; Chris@11: instance->selectProgram(defaultProgram); Chris@11: } Chris@0: if (program != "") { Chris@20: std::cerr << "now selecting desired program " << program.toStdString() << std::endl; Chris@20: instance->selectProgram(program.toStdString()); Chris@0: } Chris@0: instance->setIdealChannelCount(m_targetChannelCount); // reset! Chris@0: } else { Chris@0: std::cerr << "Failed to instantiate plugin" << std::endl; Chris@0: } Chris@10: Chris@11: return instance; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioGenerator::removeModel(Model *model) Chris@0: { Chris@0: SparseOneDimensionalModel *sodm = Chris@0: dynamic_cast(model); Chris@0: if (!sodm) return; // nothing to do Chris@0: Chris@8: QMutexLocker locker(&m_mutex); Chris@8: Chris@0: if (m_synthMap.find(sodm) == m_synthMap.end()) return; Chris@0: Chris@0: RealTimePluginInstance *instance = m_synthMap[sodm]; Chris@0: m_synthMap.erase(sodm); Chris@0: delete instance; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioGenerator::clearModels() Chris@0: { Chris@8: QMutexLocker locker(&m_mutex); Chris@0: while (!m_synthMap.empty()) { Chris@0: RealTimePluginInstance *instance = m_synthMap.begin()->second; Chris@0: m_synthMap.erase(m_synthMap.begin()); Chris@0: delete instance; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: AudioGenerator::reset() Chris@0: { Chris@8: QMutexLocker locker(&m_mutex); Chris@0: for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { Chris@0: if (i->second) { Chris@0: i->second->silence(); Chris@0: i->second->discardEvents(); Chris@0: } Chris@0: } Chris@0: Chris@0: m_noteOffs.clear(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioGenerator::setTargetChannelCount(size_t targetChannelCount) Chris@0: { Chris@13: if (m_targetChannelCount == targetChannelCount) return; Chris@13: Chris@13: std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::endl; Chris@13: Chris@8: QMutexLocker locker(&m_mutex); Chris@0: m_targetChannelCount = targetChannelCount; Chris@0: Chris@0: for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { Chris@0: if (i->second) i->second->setIdealChannelCount(targetChannelCount); Chris@0: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioGenerator::getBlockSize() const Chris@0: { Chris@0: return m_pluginBlockSize; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount, Chris@4: float **buffer, size_t fadeIn, size_t fadeOut) Chris@0: { Chris@0: if (m_sourceSampleRate == 0) { Chris@0: std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl; Chris@0: return frameCount; Chris@0: } Chris@0: Chris@8: QMutexLocker locker(&m_mutex); Chris@8: Chris@11: PlayParameters *parameters = Chris@11: PlayParameterRepository::instance()->getPlayParameters(model); Chris@0: if (!parameters) return frameCount; Chris@0: Chris@0: bool playing = !parameters->isPlayMuted(); Chris@0: if (!playing) return frameCount; Chris@0: Chris@0: float gain = parameters->getPlayGain(); Chris@0: float pan = parameters->getPlayPan(); Chris@0: Chris@0: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@0: if (dtvm) { Chris@0: return mixDenseTimeValueModel(dtvm, startFrame, frameCount, Chris@4: buffer, gain, pan, fadeIn, fadeOut); Chris@0: } Chris@0: Chris@0: SparseOneDimensionalModel *sodm = dynamic_cast Chris@0: (model); Chris@0: if (sodm) { Chris@0: return mixSparseOneDimensionalModel(sodm, startFrame, frameCount, Chris@4: buffer, gain, pan, fadeIn, fadeOut); Chris@0: } Chris@0: Chris@11: NoteModel *nm = dynamic_cast(model); Chris@11: if (nm) { Chris@11: return mixNoteModel(nm, startFrame, frameCount, Chris@11: buffer, gain, pan, fadeIn, fadeOut); Chris@11: } Chris@11: Chris@0: return frameCount; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm, Chris@0: size_t startFrame, size_t frames, Chris@4: float **buffer, float gain, float pan, Chris@4: size_t fadeIn, size_t fadeOut) Chris@0: { Chris@0: static float *channelBuffer = 0; Chris@0: static size_t channelBufSiz = 0; Chris@5: Chris@4: size_t totalFrames = frames + fadeIn/2 + fadeOut/2; Chris@4: Chris@4: if (channelBufSiz < totalFrames) { Chris@0: delete[] channelBuffer; Chris@4: channelBuffer = new float[totalFrames]; Chris@4: channelBufSiz = totalFrames; Chris@0: } Chris@0: Chris@0: size_t got = 0; Chris@13: size_t prevChannel = 999; Chris@0: Chris@13: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@4: Chris@13: size_t sourceChannel = (c % dtvm->getChannelCount()); Chris@13: Chris@13: // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::endl; Chris@13: Chris@13: float channelGain = gain; Chris@13: if (pan != 0.0) { Chris@13: if (c == 0) { Chris@13: if (pan > 0.0) channelGain *= 1.0 - pan; Chris@13: } else { Chris@13: if (pan < 0.0) channelGain *= pan + 1.0; Chris@13: } Chris@13: } Chris@13: Chris@13: if (prevChannel != sourceChannel) { Chris@13: if (startFrame >= fadeIn/2) { Chris@13: got = dtvm->getValues Chris@13: (sourceChannel, Chris@13: startFrame - fadeIn/2, startFrame + frames + fadeOut/2, Chris@13: channelBuffer); Chris@13: } else { Chris@13: size_t missing = fadeIn/2 - startFrame; Chris@13: got = dtvm->getValues Chris@13: (sourceChannel, Chris@13: 0, startFrame + frames + fadeOut/2, Chris@13: channelBuffer + missing); Chris@13: } Chris@13: } Chris@13: prevChannel = sourceChannel; Chris@4: Chris@4: for (size_t i = 0; i < fadeIn/2; ++i) { Chris@4: float *back = buffer[c]; Chris@4: back -= fadeIn/2; Chris@13: back[i] += (channelGain * channelBuffer[i] * i) / fadeIn; Chris@4: } Chris@4: Chris@4: for (size_t i = 0; i < frames + fadeOut/2; ++i) { Chris@13: float mult = channelGain; Chris@4: if (i < fadeIn/2) { Chris@4: mult = (mult * i) / fadeIn; Chris@4: } Chris@4: if (i > frames - fadeOut/2) { Chris@4: mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut; Chris@4: } Chris@4: buffer[c][i] += mult * channelBuffer[i]; Chris@0: } Chris@0: } Chris@0: Chris@0: return got; Chris@0: } Chris@11: Chris@0: size_t Chris@0: AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm, Chris@0: size_t startFrame, size_t frames, Chris@4: float **buffer, float gain, float pan, Chris@4: size_t /* fadeIn */, Chris@4: size_t /* fadeOut */) Chris@0: { Chris@0: RealTimePluginInstance *plugin = m_synthMap[sodm]; Chris@0: if (!plugin) return 0; Chris@0: Chris@0: size_t latency = plugin->getLatency(); Chris@0: size_t blocks = frames / m_pluginBlockSize; Chris@0: Chris@0: //!!! hang on -- the fact that the audio callback play source's Chris@0: //buffer is a multiple of the plugin's buffer size doesn't mean Chris@0: //that we always get called for a multiple of it here (because it Chris@0: //also depends on the JACK block size). how should we ensure that Chris@0: //all models write the same amount in to the mix, and that we Chris@0: //always have a multiple of the plugin buffer size? I guess this Chris@0: //class has to be queryable for the plugin buffer size & the Chris@0: //callback play source has to use that as a multiple for all the Chris@0: //calls to mixModel Chris@0: Chris@0: size_t got = blocks * m_pluginBlockSize; Chris@0: Chris@0: #ifdef DEBUG_AUDIO_GENERATOR Chris@0: std::cout << "mixModel [sparse]: frames " << frames Chris@0: << ", blocks " << blocks << std::endl; Chris@0: #endif Chris@0: Chris@0: snd_seq_event_t onEv; Chris@0: onEv.type = SND_SEQ_EVENT_NOTEON; Chris@0: onEv.data.note.channel = 0; Chris@0: onEv.data.note.note = 64; Chris@0: onEv.data.note.velocity = 127; Chris@0: Chris@0: snd_seq_event_t offEv; Chris@0: offEv.type = SND_SEQ_EVENT_NOTEOFF; Chris@0: offEv.data.note.channel = 0; Chris@0: offEv.data.note.velocity = 0; Chris@0: Chris@0: NoteOffSet ¬eOffs = m_noteOffs[sodm]; Chris@0: Chris@0: for (size_t i = 0; i < blocks; ++i) { Chris@0: Chris@0: size_t reqStart = startFrame + i * m_pluginBlockSize; Chris@0: Chris@0: SparseOneDimensionalModel::PointList points = Chris@5: sodm->getPoints(reqStart + latency, Chris@0: reqStart + latency + m_pluginBlockSize); Chris@0: Chris@26: Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime Chris@0: (startFrame + i * m_pluginBlockSize, m_sourceSampleRate); Chris@0: Chris@0: for (SparseOneDimensionalModel::PointList::iterator pli = Chris@0: points.begin(); pli != points.end(); ++pli) { Chris@0: Chris@0: size_t pliFrame = pli->frame; Chris@5: Chris@0: if (pliFrame >= latency) pliFrame -= latency; Chris@0: Chris@5: if (pliFrame < reqStart || Chris@5: pliFrame >= reqStart + m_pluginBlockSize) continue; Chris@5: Chris@0: while (noteOffs.begin() != noteOffs.end() && Chris@0: noteOffs.begin()->frame <= pliFrame) { Chris@0: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@0: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@0: Chris@0: offEv.data.note.note = noteOffs.begin()->pitch; Chris@5: Chris@5: #ifdef DEBUG_AUDIO_GENERATOR Chris@5: std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl; Chris@5: #endif Chris@5: Chris@0: plugin->sendEvent(eventTime, &offEv); Chris@0: noteOffs.erase(noteOffs.begin()); Chris@0: } Chris@0: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@0: (pliFrame, m_sourceSampleRate); Chris@0: Chris@0: plugin->sendEvent(eventTime, &onEv); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_GENERATOR Chris@0: std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; Chris@0: #endif Chris@0: Chris@0: size_t duration = 7000; // frames [for now] Chris@0: NoteOff noff; Chris@0: noff.pitch = onEv.data.note.note; Chris@0: noff.frame = pliFrame + duration; Chris@0: noteOffs.insert(noff); Chris@0: } Chris@0: Chris@0: while (noteOffs.begin() != noteOffs.end() && Chris@0: noteOffs.begin()->frame <= Chris@0: startFrame + i * m_pluginBlockSize + m_pluginBlockSize) { Chris@0: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@0: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@0: Chris@0: offEv.data.note.note = noteOffs.begin()->pitch; Chris@5: Chris@5: #ifdef DEBUG_AUDIO_GENERATOR Chris@5: std::cerr << "mixModel [sparse]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl; Chris@5: #endif Chris@5: Chris@0: plugin->sendEvent(eventTime, &offEv); Chris@0: noteOffs.erase(noteOffs.begin()); Chris@0: } Chris@0: Chris@0: plugin->run(blockTime); Chris@0: float **outs = plugin->getAudioOutputBuffers(); Chris@0: Chris@13: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@0: #ifdef DEBUG_AUDIO_GENERATOR Chris@0: std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; Chris@0: #endif Chris@0: Chris@13: size_t sourceChannel = (c % plugin->getAudioOutputCount()); Chris@13: Chris@13: float channelGain = gain; Chris@13: if (pan != 0.0) { Chris@13: if (c == 0) { Chris@13: if (pan > 0.0) channelGain *= 1.0 - pan; Chris@13: } else { Chris@13: if (pan < 0.0) channelGain *= pan + 1.0; Chris@13: } Chris@13: } Chris@13: Chris@0: for (size_t j = 0; j < m_pluginBlockSize; ++j) { Chris@13: buffer[c][i * m_pluginBlockSize + j] += Chris@13: channelGain * outs[sourceChannel][j]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return got; Chris@0: } Chris@0: Chris@11: Chris@11: //!!! mucho duplication with above -- refactor Chris@11: size_t Chris@11: AudioGenerator::mixNoteModel(NoteModel *nm, Chris@11: size_t startFrame, size_t frames, Chris@11: float **buffer, float gain, float pan, Chris@11: size_t /* fadeIn */, Chris@11: size_t /* fadeOut */) Chris@11: { Chris@11: RealTimePluginInstance *plugin = m_synthMap[nm]; Chris@11: if (!plugin) return 0; Chris@11: Chris@11: size_t latency = plugin->getLatency(); Chris@11: size_t blocks = frames / m_pluginBlockSize; Chris@11: Chris@11: //!!! hang on -- the fact that the audio callback play source's Chris@11: //buffer is a multiple of the plugin's buffer size doesn't mean Chris@11: //that we always get called for a multiple of it here (because it Chris@11: //also depends on the JACK block size). how should we ensure that Chris@11: //all models write the same amount in to the mix, and that we Chris@11: //always have a multiple of the plugin buffer size? I guess this Chris@11: //class has to be queryable for the plugin buffer size & the Chris@11: //callback play source has to use that as a multiple for all the Chris@11: //calls to mixModel Chris@11: Chris@11: size_t got = blocks * m_pluginBlockSize; Chris@11: Chris@11: #ifdef DEBUG_AUDIO_GENERATOR Chris@11: std::cout << "mixModel [note]: frames " << frames Chris@11: << ", blocks " << blocks << std::endl; Chris@11: #endif Chris@11: Chris@11: snd_seq_event_t onEv; Chris@11: onEv.type = SND_SEQ_EVENT_NOTEON; Chris@11: onEv.data.note.channel = 0; Chris@11: onEv.data.note.note = 64; Chris@11: onEv.data.note.velocity = 127; Chris@11: Chris@11: snd_seq_event_t offEv; Chris@11: offEv.type = SND_SEQ_EVENT_NOTEOFF; Chris@11: offEv.data.note.channel = 0; Chris@11: offEv.data.note.velocity = 0; Chris@11: Chris@11: NoteOffSet ¬eOffs = m_noteOffs[nm]; Chris@11: Chris@11: for (size_t i = 0; i < blocks; ++i) { Chris@11: Chris@11: size_t reqStart = startFrame + i * m_pluginBlockSize; Chris@11: Chris@11: NoteModel::PointList points = Chris@11: nm->getPoints(reqStart + latency, Chris@11: reqStart + latency + m_pluginBlockSize); Chris@11: Chris@26: Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime Chris@11: (startFrame + i * m_pluginBlockSize, m_sourceSampleRate); Chris@11: Chris@11: for (NoteModel::PointList::iterator pli = Chris@11: points.begin(); pli != points.end(); ++pli) { Chris@11: Chris@11: size_t pliFrame = pli->frame; Chris@11: Chris@11: if (pliFrame >= latency) pliFrame -= latency; Chris@11: Chris@11: if (pliFrame < reqStart || Chris@11: pliFrame >= reqStart + m_pluginBlockSize) continue; Chris@11: Chris@11: while (noteOffs.begin() != noteOffs.end() && Chris@11: noteOffs.begin()->frame <= pliFrame) { Chris@11: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@11: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@11: Chris@11: offEv.data.note.note = noteOffs.begin()->pitch; Chris@11: Chris@11: #ifdef DEBUG_AUDIO_GENERATOR Chris@11: std::cerr << "mixModel [note]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl; Chris@11: #endif Chris@11: Chris@11: plugin->sendEvent(eventTime, &offEv); Chris@11: noteOffs.erase(noteOffs.begin()); Chris@11: } Chris@11: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@11: (pliFrame, m_sourceSampleRate); Chris@11: Chris@11: onEv.data.note.note = lrintf(pli->value); Chris@11: Chris@11: plugin->sendEvent(eventTime, &onEv); Chris@11: Chris@11: #ifdef DEBUG_AUDIO_GENERATOR Chris@11: std::cout << "mixModel [note]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; Chris@11: #endif Chris@11: Chris@11: size_t duration = pli->duration; Chris@11: NoteOff noff; Chris@11: noff.pitch = onEv.data.note.note; Chris@11: noff.frame = pliFrame + duration; Chris@11: noteOffs.insert(noff); Chris@11: } Chris@11: Chris@11: while (noteOffs.begin() != noteOffs.end() && Chris@11: noteOffs.begin()->frame <= Chris@11: startFrame + i * m_pluginBlockSize + m_pluginBlockSize) { Chris@11: Chris@26: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@11: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@11: Chris@11: offEv.data.note.note = noteOffs.begin()->pitch; Chris@11: Chris@11: #ifdef DEBUG_AUDIO_GENERATOR Chris@11: std::cerr << "mixModel [note]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl; Chris@11: #endif Chris@11: Chris@11: plugin->sendEvent(eventTime, &offEv); Chris@11: noteOffs.erase(noteOffs.begin()); Chris@11: } Chris@11: Chris@11: plugin->run(blockTime); Chris@11: float **outs = plugin->getAudioOutputBuffers(); Chris@11: Chris@13: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@11: #ifdef DEBUG_AUDIO_GENERATOR Chris@11: std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; Chris@11: #endif Chris@11: Chris@13: size_t sourceChannel = (c % plugin->getAudioOutputCount()); Chris@13: Chris@13: float channelGain = gain; Chris@13: if (pan != 0.0) { Chris@13: if (c == 0) { Chris@13: if (pan > 0.0) channelGain *= 1.0 - pan; Chris@13: } else { Chris@13: if (pan < 0.0) channelGain *= pan + 1.0; Chris@13: } Chris@13: } Chris@13: Chris@11: for (size_t j = 0; j < m_pluginBlockSize; ++j) { Chris@13: buffer[c][i * m_pluginBlockSize + j] += Chris@13: channelGain * outs[sourceChannel][j]; Chris@11: } Chris@11: } Chris@11: } Chris@11: Chris@11: return got; Chris@11: } Chris@11: