Chris@43: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@43: Chris@43: /* Chris@43: Sonic Visualiser Chris@43: An audio file viewer and annotation editor. Chris@43: Centre for Digital Music, Queen Mary, University of London. Chris@43: This file copyright 2006 Chris Cannam. Chris@43: Chris@43: This program is free software; you can redistribute it and/or Chris@43: modify it under the terms of the GNU General Public License as Chris@43: published by the Free Software Foundation; either version 2 of the Chris@43: License, or (at your option) any later version. See the file Chris@43: COPYING included with this distribution for more information. Chris@43: */ Chris@43: Chris@43: #include "AudioGenerator.h" Chris@43: Chris@43: #include "base/TempDirectory.h" Chris@43: #include "base/PlayParameters.h" Chris@43: #include "base/PlayParameterRepository.h" Chris@43: #include "base/Pitch.h" Chris@43: #include "base/Exceptions.h" Chris@43: Chris@43: #include "data/model/NoteModel.h" Chris@278: #include "data/model/FlexiNoteModel.h" Chris@43: #include "data/model/DenseTimeValueModel.h" Chris@43: #include "data/model/SparseOneDimensionalModel.h" Chris@299: #include "data/model/NoteData.h" Chris@43: Chris@43: #include Chris@167: #include Chris@43: Chris@43: #include Chris@43: #include Chris@43: Chris@43: const size_t Chris@305: AudioGenerator::m_processingBlockSize = 2048; Chris@43: Chris@43: QString Chris@43: AudioGenerator::m_sampleDir = ""; Chris@43: Chris@43: //#define DEBUG_AUDIO_GENERATOR 1 Chris@43: Chris@43: AudioGenerator::AudioGenerator() : Chris@43: m_sourceSampleRate(0), Chris@43: m_targetChannelCount(1), Chris@43: m_soloing(false) Chris@43: { Chris@108: initialiseSampleDir(); Chris@43: Chris@43: connect(PlayParameterRepository::getInstance(), Chris@305: SIGNAL(playSampleIdChanged(const Playable *, QString)), Chris@43: this, Chris@305: SLOT(playSampleIdChanged(const Playable *, QString))); Chris@43: } Chris@43: Chris@43: AudioGenerator::~AudioGenerator() Chris@43: { Chris@177: #ifdef DEBUG_AUDIO_GENERATOR Chris@233: SVDEBUG << "AudioGenerator::~AudioGenerator" << endl; Chris@177: #endif Chris@43: } Chris@43: Chris@108: void Chris@108: AudioGenerator::initialiseSampleDir() Chris@43: { Chris@108: if (m_sampleDir != "") return; Chris@108: Chris@108: try { Chris@108: m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples"); Chris@108: } catch (DirectoryCreationFailed f) { Chris@293: cerr << "WARNING: AudioGenerator::initialiseSampleDir:" Chris@108: << " Failed to create temporary sample directory" Chris@293: << endl; Chris@108: m_sampleDir = ""; Chris@108: return; Chris@108: } Chris@108: Chris@108: QDir sampleResourceDir(":/samples", "*.wav"); Chris@108: Chris@108: for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) { Chris@108: Chris@108: QString fileName(sampleResourceDir[i]); Chris@108: QFile file(sampleResourceDir.filePath(fileName)); Chris@151: QString target = QDir(m_sampleDir).filePath(fileName); Chris@108: Chris@151: if (!file.copy(target)) { Chris@293: cerr << "WARNING: AudioGenerator::getSampleDir: " Chris@294: << "Unable to copy " << fileName Chris@108: << " into temporary directory \"" Chris@293: << m_sampleDir << "\"" << endl; Chris@151: } else { Chris@151: QFile tf(target); Chris@151: tf.setPermissions(tf.permissions() | Chris@151: QFile::WriteOwner | Chris@151: QFile::WriteUser); Chris@108: } Chris@43: } Chris@43: } Chris@43: Chris@43: bool Chris@43: AudioGenerator::addModel(Model *model) Chris@43: { Chris@43: if (m_sourceSampleRate == 0) { Chris@43: Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: Chris@43: } else { Chris@43: Chris@43: DenseTimeValueModel *dtvm = Chris@43: dynamic_cast(model); Chris@43: Chris@43: if (dtvm) { Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: return true; Chris@43: } Chris@43: } Chris@305: /*!!! Chris@43: RealTimePluginInstance *plugin = loadPluginFor(model); Chris@43: if (plugin) { Chris@43: QMutexLocker locker(&m_mutex); Chris@43: m_synthMap[model] = plugin; Chris@43: return true; Chris@43: } Chris@305: */ Chris@43: return false; Chris@43: } Chris@43: Chris@43: void Chris@305: AudioGenerator::playSampleIdChanged(const Playable *playable, QString) Chris@43: { Chris@108: const Model *model = dynamic_cast(playable); Chris@108: if (!model) { Chris@305: cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable " Chris@108: << playable << " is not a supported model type" Chris@293: << endl; Chris@108: return; Chris@108: } Chris@108: Chris@43: if (m_synthMap.find(model) == m_synthMap.end()) return; Chris@305: /*!!! Chris@43: RealTimePluginInstance *plugin = loadPluginFor(model); Chris@43: if (plugin) { Chris@43: QMutexLocker locker(&m_mutex); Chris@43: delete m_synthMap[model]; Chris@43: m_synthMap[model] = plugin; Chris@43: } Chris@305: */ Chris@43: } Chris@305: /*!!! Chris@43: void Chris@108: AudioGenerator::playPluginConfigurationChanged(const Playable *playable, Chris@43: QString configurationXml) Chris@43: { Chris@233: // SVDEBUG << "AudioGenerator::playPluginConfigurationChanged" << endl; Chris@43: Chris@108: const Model *model = dynamic_cast(playable); Chris@108: if (!model) { Chris@305: cerr << "WARNING: AudioGenerator::playSampleIdChanged: playable " Chris@108: << playable << " is not a supported model type" Chris@293: << endl; Chris@108: return; Chris@108: } Chris@108: Chris@43: if (m_synthMap.find(model) == m_synthMap.end()) { Chris@233: SVDEBUG << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << endl; Chris@43: return; Chris@43: } Chris@43: Chris@43: RealTimePluginInstance *plugin = m_synthMap[model]; Chris@43: if (plugin) { Chris@43: PluginXml(plugin).setParametersFromXml(configurationXml); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::setSampleDir(RealTimePluginInstance *plugin) Chris@43: { Chris@108: if (m_sampleDir != "") { Chris@108: plugin->configure("sampledir", m_sampleDir.toStdString()); Chris@108: } Chris@43: } Chris@43: Chris@43: RealTimePluginInstance * Chris@43: AudioGenerator::loadPluginFor(const Model *model) Chris@43: { Chris@43: QString pluginId, configurationXml; Chris@43: Chris@108: const Playable *playable = model; Chris@108: if (!playable || !playable->canPlay()) return 0; Chris@108: Chris@43: PlayParameters *parameters = Chris@108: PlayParameterRepository::getInstance()->getPlayParameters(playable); Chris@43: if (parameters) { Chris@305: pluginId = parameters->getPlaySampleId(); Chris@43: configurationXml = parameters->getPlayPluginConfiguration(); Chris@43: } Chris@43: Chris@276: std::cerr << "AudioGenerator::loadPluginFor(" << model << "): id = " << pluginId << std::endl; Chris@276: Chris@276: if (pluginId == "") { Chris@276: SVDEBUG << "AudioGenerator::loadPluginFor(" << model << "): parameters contain empty plugin ID, skipping" << endl; Chris@276: return 0; Chris@276: } Chris@43: Chris@43: RealTimePluginInstance *plugin = loadPlugin(pluginId, ""); Chris@43: if (!plugin) return 0; Chris@43: Chris@276: std::cerr << "AudioGenerator::loadPluginFor(" << model << "): loaded plugin " Chris@276: << plugin << std::endl; Chris@276: Chris@43: if (configurationXml != "") { Chris@43: PluginXml(plugin).setParametersFromXml(configurationXml); Chris@179: setSampleDir(plugin); Chris@43: } Chris@43: Chris@108: configurationXml = PluginXml(plugin).toXmlString(); Chris@108: Chris@43: if (parameters) { Chris@305: parameters->setPlaySampleId(pluginId); Chris@43: parameters->setPlayPluginConfiguration(configurationXml); Chris@43: } Chris@43: Chris@43: return plugin; Chris@43: } Chris@43: Chris@43: RealTimePluginInstance * Chris@43: AudioGenerator::loadPlugin(QString pluginId, QString program) Chris@43: { Chris@43: RealTimePluginFactory *factory = Chris@43: RealTimePluginFactory::instanceFor(pluginId); Chris@43: Chris@43: if (!factory) { Chris@293: cerr << "Failed to get plugin factory" << endl; Chris@274: return 0; Chris@43: } Chris@43: Chris@43: RealTimePluginInstance *instance = Chris@43: factory->instantiatePlugin Chris@305: (pluginId, 0, 0, m_sourceSampleRate, m_processingBlockSize, m_targetChannelCount); Chris@43: Chris@43: if (!instance) { Chris@293: cerr << "Failed to instantiate plugin " << pluginId << endl; Chris@43: return 0; Chris@43: } Chris@43: Chris@43: setSampleDir(instance); Chris@43: Chris@43: for (unsigned int i = 0; i < instance->getParameterCount(); ++i) { Chris@43: instance->setParameterValue(i, instance->getParameterDefault(i)); Chris@43: } Chris@43: std::string defaultProgram = instance->getProgram(0, 0); Chris@43: if (defaultProgram != "") { Chris@295: cerr << "first selecting default program " << defaultProgram << endl; Chris@43: instance->selectProgram(defaultProgram); Chris@43: } Chris@43: if (program != "") { Chris@295: cerr << "now selecting desired program " << program << endl; Chris@43: instance->selectProgram(program.toStdString()); Chris@43: } Chris@43: instance->setIdealChannelCount(m_targetChannelCount); // reset! Chris@43: Chris@43: return instance; Chris@43: } Chris@305: */ Chris@43: void Chris@43: AudioGenerator::removeModel(Model *model) Chris@43: { Chris@43: SparseOneDimensionalModel *sodm = Chris@43: dynamic_cast(model); Chris@43: if (!sodm) return; // nothing to do Chris@43: Chris@43: QMutexLocker locker(&m_mutex); Chris@43: Chris@43: if (m_synthMap.find(sodm) == m_synthMap.end()) return; Chris@43: Chris@305: //!!! RealTimePluginInstance *instance = m_synthMap[sodm]; Chris@305: // m_synthMap.erase(sodm); Chris@43: delete instance; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::clearModels() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@305: /*!!! Chris@43: while (!m_synthMap.empty()) { Chris@43: RealTimePluginInstance *instance = m_synthMap.begin()->second; Chris@43: m_synthMap.erase(m_synthMap.begin()); Chris@43: delete instance; Chris@43: } Chris@305: */ Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::reset() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@305: /*!!! Chris@43: for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { Chris@43: if (i->second) { Chris@43: i->second->silence(); Chris@43: i->second->discardEvents(); Chris@43: } Chris@43: } Chris@305: */ Chris@43: Chris@43: m_noteOffs.clear(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::setTargetChannelCount(size_t targetChannelCount) Chris@43: { Chris@43: if (m_targetChannelCount == targetChannelCount) return; Chris@43: Chris@233: // SVDEBUG << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << endl; Chris@43: Chris@43: QMutexLocker locker(&m_mutex); Chris@43: m_targetChannelCount = targetChannelCount; Chris@43: Chris@305: /*!!! Chris@43: for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { Chris@43: if (i->second) i->second->setIdealChannelCount(targetChannelCount); Chris@43: } Chris@305: */ Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioGenerator::getBlockSize() const Chris@43: { Chris@305: return m_processingBlockSize; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::setSoloModelSet(std::set s) Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@43: Chris@43: m_soloModelSet = s; Chris@43: m_soloing = true; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::clearSoloModelSet() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@43: Chris@43: m_soloModelSet.clear(); Chris@43: m_soloing = false; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount, Chris@43: float **buffer, size_t fadeIn, size_t fadeOut) Chris@43: { Chris@43: if (m_sourceSampleRate == 0) { Chris@293: cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl; Chris@43: return frameCount; Chris@43: } Chris@43: Chris@43: QMutexLocker locker(&m_mutex); Chris@43: Chris@108: Playable *playable = model; Chris@108: if (!playable || !playable->canPlay()) return frameCount; Chris@108: Chris@43: PlayParameters *parameters = Chris@108: PlayParameterRepository::getInstance()->getPlayParameters(playable); Chris@43: if (!parameters) return frameCount; Chris@43: Chris@43: bool playing = !parameters->isPlayMuted(); Chris@43: if (!playing) { Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cout << "AudioGenerator::mixModel(" << model << "): muted" << endl; Chris@43: #endif Chris@43: return frameCount; Chris@43: } Chris@43: Chris@43: if (m_soloing) { Chris@43: if (m_soloModelSet.find(model) == m_soloModelSet.end()) { Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << endl; Chris@43: #endif Chris@43: return frameCount; Chris@43: } Chris@43: } Chris@43: Chris@43: float gain = parameters->getPlayGain(); Chris@43: float pan = parameters->getPlayPan(); Chris@43: Chris@43: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@43: if (dtvm) { Chris@43: return mixDenseTimeValueModel(dtvm, startFrame, frameCount, Chris@43: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: Chris@279: bool synthetic = Chris@279: (qobject_cast(model) || Chris@278: qobject_cast(model) || Chris@278: qobject_cast(model)); Chris@43: Chris@279: if (synthetic) { Chris@275: return mixSyntheticNoteModel(model, startFrame, frameCount, Chris@275: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: Chris@276: std::cerr << "AudioGenerator::mixModel: WARNING: Model " << model << " of type " << model->getTypeName() << " is marked as playable, but I have no mechanism to play it" << std::endl; Chris@276: Chris@43: return frameCount; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm, Chris@43: size_t startFrame, size_t frames, Chris@43: float **buffer, float gain, float pan, Chris@43: size_t fadeIn, size_t fadeOut) Chris@43: { Chris@80: static float **channelBuffer = 0; Chris@80: static size_t channelBufSiz = 0; Chris@80: static size_t channelBufCount = 0; Chris@43: Chris@43: size_t totalFrames = frames + fadeIn/2 + fadeOut/2; Chris@43: Chris@80: size_t modelChannels = dtvm->getChannelCount(); Chris@80: Chris@80: if (channelBufSiz < totalFrames || channelBufCount < modelChannels) { Chris@80: Chris@80: for (size_t c = 0; c < channelBufCount; ++c) { Chris@80: delete[] channelBuffer[c]; Chris@80: } Chris@80: Chris@43: delete[] channelBuffer; Chris@80: channelBuffer = new float *[modelChannels]; Chris@80: Chris@80: for (size_t c = 0; c < modelChannels; ++c) { Chris@80: channelBuffer[c] = new float[totalFrames]; Chris@80: } Chris@80: Chris@80: channelBufCount = modelChannels; Chris@43: channelBufSiz = totalFrames; Chris@43: } Chris@80: Chris@43: size_t got = 0; Chris@80: Chris@80: if (startFrame >= fadeIn/2) { Chris@80: got = dtvm->getData(0, modelChannels - 1, Chris@80: startFrame - fadeIn/2, Chris@80: frames + fadeOut/2 + fadeIn/2, Chris@80: channelBuffer); Chris@80: } else { Chris@80: size_t missing = fadeIn/2 - startFrame; Chris@80: Chris@80: for (size_t c = 0; c < modelChannels; ++c) { Chris@80: channelBuffer[c] += missing; Chris@80: } Chris@80: Chris@80: got = dtvm->getData(0, modelChannels - 1, Chris@80: startFrame, Chris@80: frames + fadeOut/2, Chris@80: channelBuffer); Chris@80: Chris@80: for (size_t c = 0; c < modelChannels; ++c) { Chris@80: channelBuffer[c] -= missing; Chris@80: } Chris@80: Chris@80: got += missing; Chris@80: } Chris@43: Chris@43: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@43: Chris@80: size_t sourceChannel = (c % modelChannels); Chris@43: Chris@233: // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl; Chris@43: Chris@43: float channelGain = gain; Chris@43: if (pan != 0.0) { Chris@43: if (c == 0) { Chris@43: if (pan > 0.0) channelGain *= 1.0 - pan; Chris@43: } else { Chris@43: if (pan < 0.0) channelGain *= pan + 1.0; Chris@43: } Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i < fadeIn/2; ++i) { Chris@43: float *back = buffer[c]; Chris@43: back -= fadeIn/2; Chris@80: back[i] += (channelGain * channelBuffer[sourceChannel][i] * i) / fadeIn; Chris@43: } Chris@43: Chris@43: for (size_t i = 0; i < frames + fadeOut/2; ++i) { Chris@43: float mult = channelGain; Chris@43: if (i < fadeIn/2) { Chris@43: mult = (mult * i) / fadeIn; Chris@43: } Chris@43: if (i > frames - fadeOut/2) { Chris@43: mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut; Chris@43: } Chris@80: float val = channelBuffer[sourceChannel][i]; Chris@80: if (i >= got) val = 0.f; Chris@80: buffer[c][i] += mult * val; Chris@43: } Chris@43: } Chris@43: Chris@43: return got; Chris@43: } Chris@43: Chris@43: size_t Chris@275: AudioGenerator::mixSyntheticNoteModel(Model *model, Chris@275: size_t startFrame, size_t frames, Chris@275: float **buffer, float gain, float pan, Chris@275: size_t /* fadeIn */, Chris@275: size_t /* fadeOut */) Chris@43: { Chris@275: RealTimePluginInstance *plugin = m_synthMap[model]; Chris@43: if (!plugin) return 0; Chris@43: Chris@43: size_t latency = plugin->getLatency(); Chris@305: size_t blocks = frames / m_processingBlockSize; Chris@43: Chris@43: //!!! hang on -- the fact that the audio callback play source's Chris@43: //buffer is a multiple of the plugin's buffer size doesn't mean Chris@43: //that we always get called for a multiple of it here (because it Chris@43: //also depends on the JACK block size). how should we ensure that Chris@43: //all models write the same amount in to the mix, and that we Chris@43: //always have a multiple of the plugin buffer size? I guess this Chris@43: //class has to be queryable for the plugin buffer size & the Chris@43: //callback play source has to use that as a multiple for all the Chris@43: //calls to mixModel Chris@43: Chris@305: size_t got = blocks * m_processingBlockSize; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cout << "mixModel [synthetic note]: frames " << frames Chris@293: << ", blocks " << blocks << endl; Chris@43: #endif Chris@43: Chris@43: snd_seq_event_t onEv; Chris@43: onEv.type = SND_SEQ_EVENT_NOTEON; Chris@43: onEv.data.note.channel = 0; Chris@43: Chris@43: snd_seq_event_t offEv; Chris@43: offEv.type = SND_SEQ_EVENT_NOTEOFF; Chris@43: offEv.data.note.channel = 0; Chris@43: offEv.data.note.velocity = 0; Chris@43: Chris@275: NoteOffSet ¬eOffs = m_noteOffs[model]; Chris@43: Chris@43: for (size_t i = 0; i < blocks; ++i) { Chris@43: Chris@305: size_t reqStart = startFrame + i * m_processingBlockSize; Chris@43: Chris@299: NoteList notes; Chris@299: NoteExportable *exportable = dynamic_cast(model); Chris@299: if (exportable) { Chris@299: notes = exportable->getNotes(reqStart + latency, Chris@305: reqStart + latency + m_processingBlockSize); Chris@299: } Chris@43: Chris@43: Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime Chris@305: (startFrame + i * m_processingBlockSize, m_sourceSampleRate); Chris@43: Chris@275: for (NoteList::const_iterator ni = notes.begin(); Chris@275: ni != notes.end(); ++ni) { Chris@43: Chris@275: size_t noteFrame = ni->start; Chris@43: Chris@275: if (noteFrame >= latency) noteFrame -= latency; Chris@43: Chris@275: if (noteFrame < reqStart || Chris@305: noteFrame >= reqStart + m_processingBlockSize) continue; Chris@43: Chris@43: while (noteOffs.begin() != noteOffs.end() && Chris@275: noteOffs.begin()->frame <= noteFrame) { Chris@43: Chris@43: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@43: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@43: Chris@43: offEv.data.note.note = noteOffs.begin()->pitch; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cerr << "mixModel [synthetic]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl; Chris@43: #endif Chris@43: Chris@43: plugin->sendEvent(eventTime, &offEv); Chris@43: noteOffs.erase(noteOffs.begin()); Chris@43: } Chris@43: Chris@43: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@275: (noteFrame, m_sourceSampleRate); Chris@43: Chris@275: if (ni->isMidiPitchQuantized) { Chris@275: onEv.data.note.note = ni->midiPitch; Chris@275: } else { Chris@275: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cerr << "mixModel [synthetic]: non-pitch-quantized notes are not supported [yet], quantizing" << endl; Chris@275: #endif Chris@275: onEv.data.note.note = Pitch::getPitchForFrequency(ni->frequency); Chris@275: } Chris@275: Chris@275: onEv.data.note.velocity = ni->velocity; Chris@275: Chris@43: plugin->sendEvent(eventTime, &onEv); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@305: cout << "mixModel [synthetic]: note at frame " << noteFrame << ", block start " << (startFrame + i * m_processingBlockSize) << ", resulting time " << eventTime << endl; Chris@43: #endif Chris@43: Chris@275: noteOffs.insert Chris@275: (NoteOff(onEv.data.note.note, noteFrame + ni->duration)); Chris@43: } Chris@43: Chris@43: while (noteOffs.begin() != noteOffs.end() && Chris@43: noteOffs.begin()->frame <= Chris@305: startFrame + i * m_processingBlockSize + m_processingBlockSize) { Chris@43: Chris@43: Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime Chris@43: (noteOffs.begin()->frame, m_sourceSampleRate); Chris@43: Chris@43: offEv.data.note.note = noteOffs.begin()->pitch; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@293: cerr << "mixModel [synthetic]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl; Chris@43: #endif Chris@43: Chris@43: plugin->sendEvent(eventTime, &offEv); Chris@43: noteOffs.erase(noteOffs.begin()); Chris@43: } Chris@43: Chris@43: plugin->run(blockTime); Chris@43: float **outs = plugin->getAudioOutputBuffers(); Chris@43: Chris@43: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@305: cout << "mixModel [synthetic]: adding " << m_processingBlockSize << " samples from plugin output " << c << endl; Chris@43: #endif Chris@43: Chris@43: size_t sourceChannel = (c % plugin->getAudioOutputCount()); Chris@43: Chris@43: float channelGain = gain; Chris@43: if (pan != 0.0) { Chris@43: if (c == 0) { Chris@43: if (pan > 0.0) channelGain *= 1.0 - pan; Chris@43: } else { Chris@43: if (pan < 0.0) channelGain *= pan + 1.0; Chris@43: } Chris@43: } Chris@43: Chris@305: for (size_t j = 0; j < m_processingBlockSize; ++j) { Chris@305: buffer[c][i * m_processingBlockSize + j] += Chris@43: channelGain * outs[sourceChannel][j]; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: return got; Chris@43: }