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@177: std::cerr << "AudioGenerator::~AudioGenerator" << std::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@43: // std::cerr << "AudioGenerator::playPluginConfigurationChanged" << std::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@43: std::cerr << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << std::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@43: if (pluginId == "") return 0; Chris@43: Chris@43: RealTimePluginInstance *plugin = loadPlugin(pluginId, ""); Chris@43: if (!plugin) return 0; Chris@43: 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@43: return false; 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@43: // std::cerr << "first selecting default program " << defaultProgram << std::endl; Chris@43: instance->selectProgram(defaultProgram); Chris@43: } Chris@43: if (program != "") { Chris@228: // 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@43: // std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::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@43: return mixSparseOneDimensionalModel(sodm, startFrame, frameCount, Chris@43: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: Chris@43: NoteModel *nm = dynamic_cast(model); Chris@43: if (nm) { Chris@43: return mixNoteModel(nm, startFrame, frameCount, Chris@43: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: 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@43: // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::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@43: AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm, Chris@43: size_t startFrame, size_t frames, Chris@43: float **buffer, float gain, float pan, Chris@43: size_t /* fadeIn */, Chris@43: size_t /* fadeOut */) Chris@43: { Chris@43: RealTimePluginInstance *plugin = m_synthMap[sodm]; 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@43: std::cout << "mixModel [sparse]: 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: onEv.data.note.note = 64; Chris@43: onEv.data.note.velocity = 100; 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@43: NoteOffSet ¬eOffs = m_noteOffs[sodm]; 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@43: SparseOneDimensionalModel::PointList points = Chris@43: sodm->getPoints(reqStart + latency, Chris@43: 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@43: for (SparseOneDimensionalModel::PointList::iterator pli = Chris@43: points.begin(); pli != points.end(); ++pli) { Chris@43: Chris@43: size_t pliFrame = pli->frame; Chris@43: Chris@43: if (pliFrame >= latency) pliFrame -= latency; Chris@43: Chris@43: if (pliFrame < reqStart || Chris@43: pliFrame >= reqStart + m_pluginBlockSize) continue; Chris@43: Chris@43: while (noteOffs.begin() != noteOffs.end() && Chris@43: noteOffs.begin()->frame <= pliFrame) { 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@179: std::cerr << "mixModel [sparse]: 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@43: (pliFrame, m_sourceSampleRate); Chris@43: Chris@43: plugin->sendEvent(eventTime, &onEv); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@43: std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; Chris@43: #endif Chris@43: Chris@43: size_t duration = 7000; // frames [for now] Chris@43: NoteOff noff; Chris@43: noff.pitch = onEv.data.note.note; Chris@43: noff.frame = pliFrame + duration; Chris@43: noteOffs.insert(noff); 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@179: std::cerr << "mixModel [sparse]: 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@43: std::cout << "mixModel [sparse]: 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@43: Chris@43: //!!! mucho duplication with above -- refactor Chris@43: size_t Chris@43: AudioGenerator::mixNoteModel(NoteModel *nm, Chris@43: size_t startFrame, size_t frames, Chris@43: float **buffer, float gain, float pan, Chris@43: size_t /* fadeIn */, Chris@43: size_t /* fadeOut */) Chris@43: { Chris@43: RealTimePluginInstance *plugin = m_synthMap[nm]; 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@179: Vamp::RealTime startTime = Vamp::RealTime::frame2RealTime Chris@179: (startFrame, m_sourceSampleRate); Chris@179: Chris@179: std::cout << "mixModel [note]: frames " << frames << " from " << startFrame Chris@179: << " (time " << startTime << "), 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: onEv.data.note.note = 64; Chris@43: onEv.data.note.velocity = 100; 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@43: NoteOffSet ¬eOffs = m_noteOffs[nm]; 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@43: NoteModel::PointList points = Chris@43: nm->getPoints(reqStart + latency, Chris@43: 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@43: for (NoteModel::PointList::iterator pli = Chris@43: points.begin(); pli != points.end(); ++pli) { Chris@43: Chris@43: size_t pliFrame = pli->frame; Chris@43: Chris@43: if (pliFrame >= latency) pliFrame -= latency; Chris@43: Chris@43: if (pliFrame < reqStart || Chris@43: pliFrame >= reqStart + m_pluginBlockSize) continue; Chris@43: Chris@43: while (noteOffs.begin() != noteOffs.end() && Chris@43: noteOffs.begin()->frame <= pliFrame) { 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@179: std::cerr << "mixModel [note]: 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@43: (pliFrame, m_sourceSampleRate); Chris@43: Chris@43: if (nm->getScaleUnits() == "Hz") { Chris@43: onEv.data.note.note = Pitch::getPitchForFrequency(pli->value); Chris@43: } else { Chris@43: onEv.data.note.note = lrintf(pli->value); Chris@43: } Chris@43: Chris@61: if (pli->level > 0.f && pli->level <= 1.f) { Chris@61: onEv.data.note.velocity = lrintf(pli->level * 127); Chris@61: } else { Chris@61: onEv.data.note.velocity = 100; Chris@61: } Chris@61: Chris@43: plugin->sendEvent(eventTime, &onEv); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@179: std::cout << "mixModel [note]: point at frame " << pliFrame << ", pitch " << (int)onEv.data.note.note << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; Chris@43: #endif 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@43: NoteOff noff; Chris@43: noff.pitch = onEv.data.note.note; Chris@43: noff.frame = pliFrame + duration; Chris@43: noteOffs.insert(noff); Chris@179: Chris@179: #ifdef DEBUG_AUDIO_GENERATOR Chris@179: std::cout << "mixModel [note]: recording note off at " << noff.frame << std::endl; Chris@179: #endif 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@179: std::cerr << "mixModel [note]: 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@43: std::cout << "mixModel [note]: 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: