Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@2: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. 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@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@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@0: SparseOneDimensionalModel *sodm = Chris@0: dynamic_cast(model); Chris@11: if (sodm) { Chris@11: QString pluginId = QString("dssi:%1:sample_player"). Chris@11: arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); Chris@11: RealTimePluginInstance *plugin = loadPlugin(pluginId, "cowbell"); Chris@11: if (plugin) { Chris@11: QMutexLocker locker(&m_mutex); Chris@11: m_synthMap[sodm] = plugin; Chris@11: return true; Chris@11: } else { Chris@11: return false; Chris@11: } Chris@11: } Chris@0: Chris@11: NoteModel *nm = dynamic_cast(model); Chris@11: if (nm) { Chris@11: QString pluginId = QString("dssi:%1:sample_player"). Chris@11: arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); Chris@11: RealTimePluginInstance *plugin = loadPlugin(pluginId, "piano"); Chris@11: if (plugin) { Chris@11: QMutexLocker locker(&m_mutex); Chris@11: m_synthMap[nm] = plugin; Chris@11: return true; Chris@11: } else { Chris@11: return false; Chris@11: } Chris@11: } Chris@11: Chris@11: Chris@11: return false; Chris@11: } Chris@11: Chris@11: RealTimePluginInstance * Chris@11: AudioGenerator::loadPlugin(QString pluginId, QString program) Chris@11: { Chris@0: // QString pluginId = "dssi:/usr/lib/dssi/dssi-vst.so:FEARkILLERrev1.dll"; Chris@0: // QString pluginId = "dssi:/usr/lib/dssi/hexter.so:hexter"; Chris@0: // QString pluginId = "dssi:/usr/lib/dssi/sineshaper.so:sineshaper"; Chris@0: // QString pluginId = "dssi:/usr/local/lib/dssi/xsynth-dssi.so:Xsynth"; Chris@0: // QString pluginId = "dssi:/usr/local/lib/dssi/trivial_synth.so:TS"; 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@11: QString defaultProgram = instance->getProgram(0, 0); Chris@11: if (defaultProgram != "") { Chris@11: std::cerr << "first selecting default program " << defaultProgram.toLocal8Bit().data() << std::endl; Chris@11: instance->selectProgram(defaultProgram); Chris@11: } Chris@0: if (program != "") { Chris@11: std::cerr << "now selecting desired program " << program.toLocal8Bit().data() << std::endl; Chris@0: instance->selectProgram(program); 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@0: RealTime blockTime = 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@0: RealTime eventTime = 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@0: RealTime eventTime = 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@0: RealTime eventTime = 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@11: RealTime blockTime = 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@11: RealTime eventTime = 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@11: RealTime eventTime = 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@11: RealTime eventTime = 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: