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@43: #include "data/model/DenseTimeValueModel.h" Chris@43: #include "data/model/SparseOneDimensionalModel.h" Chris@43: Chris@43: #include "plugin/RealTimePluginFactory.h" Chris@43: #include "plugin/RealTimePluginInstance.h" Chris@43: #include "plugin/PluginIdentifier.h" Chris@43: #include "plugin/PluginXml.h" Chris@43: #include "plugin/api/alsa/seq_event.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@43: AudioGenerator::m_pluginBlockSize = 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@108: SIGNAL(playPluginIdChanged(const Playable *, QString)), Chris@43: this, Chris@108: SLOT(playPluginIdChanged(const Playable *, QString))); Chris@108: Chris@108: connect(PlayParameterRepository::getInstance(), Chris@108: SIGNAL(playPluginConfigurationChanged(const Playable *, QString)), Chris@108: this, Chris@108: SLOT(playPluginConfigurationChanged(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@108: std::cerr << "WARNING: AudioGenerator::initialiseSampleDir:" Chris@108: << " Failed to create temporary sample directory" Chris@108: << std::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@108: std::cerr << "WARNING: AudioGenerator::getSampleDir: " Chris@108: << "Unable to copy " << fileName.toStdString() Chris@108: << " into temporary directory \"" Chris@228: << m_sampleDir << "\"" << std::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@43: 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@43: Chris@43: return false; Chris@43: } Chris@43: Chris@43: void Chris@108: AudioGenerator::playPluginIdChanged(const Playable *playable, QString) Chris@43: { Chris@108: const Model *model = dynamic_cast(playable); Chris@108: if (!model) { Chris@108: std::cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable " Chris@108: << playable << " is not a supported model type" Chris@108: << std::endl; Chris@108: return; Chris@108: } Chris@108: Chris@43: if (m_synthMap.find(model) == m_synthMap.end()) return; Chris@43: 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@43: } Chris@43: 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@108: std::cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable " Chris@108: << playable << " is not a supported model type" Chris@108: << std::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@43: pluginId = parameters->getPlayPluginId(); 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@43: parameters->setPlayPluginId(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@43: std::cerr << "Failed to get plugin factory" << std::endl; Chris@274: return 0; Chris@43: } Chris@43: Chris@43: RealTimePluginInstance *instance = Chris@43: factory->instantiatePlugin Chris@43: (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount); Chris@43: Chris@43: if (!instance) { Chris@228: std::cerr << "Failed to instantiate plugin " << pluginId << std::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@276: std::cerr << "first selecting default program " << defaultProgram << std::endl; Chris@43: instance->selectProgram(defaultProgram); Chris@43: } Chris@43: if (program != "") { Chris@276: std::cerr << "now selecting desired program " << program << std::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@43: 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@43: RealTimePluginInstance *instance = m_synthMap[sodm]; Chris@43: 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@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@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::reset() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); 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@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@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@43: } Chris@43: Chris@43: size_t Chris@43: AudioGenerator::getBlockSize() const Chris@43: { Chris@43: return m_pluginBlockSize; 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@43: std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::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@43: std::cout << "AudioGenerator::mixModel(" << model << "): muted" << std::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@43: std::cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << std::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@43: SparseOneDimensionalModel *sodm = dynamic_cast Chris@43: (model); Chris@43: if (sodm) { Chris@275: return mixSyntheticNoteModel(model, startFrame, frameCount, Chris@275: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: Chris@43: NoteModel *nm = dynamic_cast(model); Chris@43: if (nm) { 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@43: size_t blocks = frames / m_pluginBlockSize; 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@43: size_t got = blocks * m_pluginBlockSize; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@275: std::cout << "mixModel [synthetic note]: frames " << frames Chris@43: << ", blocks " << blocks << std::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@43: size_t reqStart = startFrame + i * m_pluginBlockSize; Chris@43: Chris@275: NoteList notes = getNotes(model, Chris@275: reqStart + latency, Chris@275: reqStart + latency + m_pluginBlockSize); Chris@43: Chris@43: Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime Chris@43: (startFrame + i * m_pluginBlockSize, 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@275: noteFrame >= reqStart + m_pluginBlockSize) 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@275: std::cerr << "mixModel [synthetic]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::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@275: std::cerr << "mixModel [synthetic]: non-pitch-quantized notes are not supported [yet], quantizing" << std::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@275: std::cout << "mixModel [synthetic]: note at frame " << noteFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::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@43: startFrame + i * m_pluginBlockSize + m_pluginBlockSize) { 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@275: std::cerr << "mixModel [synthetic]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::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@275: std::cout << "mixModel [synthetic]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::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@43: for (size_t j = 0; j < m_pluginBlockSize; ++j) { Chris@43: buffer[c][i * m_pluginBlockSize + j] += Chris@43: channelGain * outs[sourceChannel][j]; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: return got; Chris@43: } Chris@43: Chris@275: AudioGenerator::NoteList Chris@275: AudioGenerator::getNotes(Model *model, Chris@275: size_t startFrame, Chris@275: size_t endFrame) Chris@43: { Chris@275: NoteList notes; Chris@43: Chris@275: SparseOneDimensionalModel *sodm = Chris@275: qobject_cast(model); Chris@43: Chris@275: if (sodm) { Chris@275: Chris@275: SparseOneDimensionalModel::PointList points = Chris@275: sodm->getPoints(startFrame, endFrame); Chris@43: Chris@275: for (SparseOneDimensionalModel::PointList::iterator pli = Chris@43: points.begin(); pli != points.end(); ++pli) { Chris@43: Chris@275: notes.push_back Chris@275: (NoteData(pli->frame, Chris@275: m_sourceSampleRate / 6, // arbitrary short duration Chris@275: 64, // default pitch Chris@275: 100)); // default velocity Chris@275: } Chris@43: Chris@275: return notes; Chris@275: } Chris@43: Chris@275: NoteModel *nm = qobject_cast(model); Chris@43: Chris@275: if (nm) { Chris@275: Chris@275: NoteModel::PointList points = Chris@275: nm->getPoints(startFrame, endFrame); Chris@43: Chris@275: for (NoteModel::PointList::iterator pli = Chris@275: points.begin(); pli != points.end(); ++pli) { Chris@43: Chris@43: size_t duration = pli->duration; Chris@43: if (duration == 0 || duration == 1) { Chris@43: duration = m_sourceSampleRate / 20; Chris@43: } Chris@179: Chris@275: int pitch = lrintf(pli->value); Chris@43: Chris@275: int velocity = 100; Chris@275: if (pli->level > 0.f && pli->level <= 1.f) { Chris@275: velocity = lrintf(pli->level * 127); Chris@275: } Chris@43: Chris@275: NoteData note(pli->frame, Chris@275: duration, Chris@275: pitch, Chris@275: velocity); Chris@43: Chris@275: if (nm->getScaleUnits() == "Hz") { Chris@275: note.frequency = pli->value; Chris@275: note.isMidiPitchQuantized = false; Chris@275: } Chris@275: Chris@275: notes.push_back(note); Chris@275: } Chris@43: Chris@275: return notes; Chris@43: } Chris@43: Chris@275: return notes; Chris@43: } Chris@43: