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@0: 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 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@0: void 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@0: } Chris@0: } Chris@0: Chris@0: SparseOneDimensionalModel *sodm = Chris@0: dynamic_cast(model); Chris@0: if (!sodm) return; // nothing else to initialise Chris@0: 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: QString pluginId = QString("dssi:%1:sample_player"). Chris@0: arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); 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@0: return; 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: m_synthMap[sodm] = instance; Chris@0: for (unsigned int i = 0; i < instance->getParameterCount(); ++i) { Chris@0: instance->setParameterValue(i, instance->getParameterDefault(i)); Chris@0: } Chris@0: QString program = instance->getProgram(0, 0); Chris@0: if (program != "") { Chris@0: std::cerr << "selecting program " << program.toLocal8Bit().data() << std::endl; Chris@0: instance->selectProgram(program); Chris@0: } Chris@0: instance->selectProgram("cowbell"); //!!! Chris@0: instance->setIdealChannelCount(m_targetChannelCount); // reset! Chris@0: } else { Chris@0: std::cerr << "Failed to instantiate plugin" << std::endl; Chris@0: } 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@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@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@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@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@0: PlayParameters *parameters = m_viewManager->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@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@0: Chris@0: for (size_t c = 0; c < m_targetChannelCount && c < dtvm->getChannelCount(); ++c) { Chris@4: Chris@5: if (startFrame >= fadeIn/2) { Chris@5: got = dtvm->getValues Chris@5: (c, startFrame - fadeIn/2, startFrame + frames + fadeOut/2, Chris@5: channelBuffer); Chris@5: } else { Chris@5: size_t missing = fadeIn/2 - startFrame; Chris@5: got = dtvm->getValues Chris@5: (c, 0, startFrame + frames + fadeOut/2, Chris@5: channelBuffer + missing); Chris@5: } 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@4: back[i] += (gain * channelBuffer[i] * i) / fadeIn; Chris@4: } Chris@4: Chris@4: for (size_t i = 0; i < frames + fadeOut/2; ++i) { Chris@4: float mult = gain; 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@0: 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@0: for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++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@0: for (size_t j = 0; j < m_pluginBlockSize; ++j) { Chris@0: buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return got; Chris@0: } Chris@0: