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@307: #include "ClipMixer.h" Chris@307: 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@309: SIGNAL(playClipIdChanged(const Playable *, QString)), Chris@43: this, Chris@309: SLOT(playClipIdChanged(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@307: Chris@307: ClipMixer *mixer = makeClipMixerFor(model); Chris@307: if (mixer) { Chris@43: QMutexLocker locker(&m_mutex); Chris@307: m_clipMixerMap[model] = mixer; Chris@43: return true; Chris@43: } Chris@307: Chris@43: return false; Chris@43: } Chris@43: Chris@43: void Chris@309: AudioGenerator::playClipIdChanged(const Playable *playable, QString) Chris@43: { Chris@108: const Model *model = dynamic_cast(playable); Chris@108: if (!model) { Chris@309: cerr << "WARNING: AudioGenerator::playClipIdChanged: playable " Chris@108: << playable << " is not a supported model type" Chris@293: << endl; Chris@108: return; Chris@108: } Chris@108: Chris@307: if (m_clipMixerMap.find(model) == m_clipMixerMap.end()) return; Chris@307: Chris@307: ClipMixer *mixer = makeClipMixerFor(model); Chris@307: if (mixer) { Chris@43: QMutexLocker locker(&m_mutex); Chris@307: m_clipMixerMap[model] = mixer; Chris@43: } Chris@43: } Chris@308: 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@309: cerr << "WARNING: AudioGenerator::playClipIdChanged: 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@307: */ Chris@307: ClipMixer * Chris@307: AudioGenerator::makeClipMixerFor(const Model *model) Chris@43: { Chris@309: QString clipId; 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@309: clipId = parameters->getPlayClipId(); Chris@43: } Chris@43: Chris@309: std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): sample id = " << clipId << std::endl; Chris@276: Chris@309: if (clipId == "") { Chris@308: SVDEBUG << "AudioGenerator::makeClipMixerFor(" << model << "): no sample, skipping" << endl; Chris@276: return 0; Chris@276: } Chris@43: Chris@308: ClipMixer *mixer = new ClipMixer(m_targetChannelCount, Chris@308: m_sourceSampleRate, Chris@308: m_processingBlockSize); Chris@307: Chris@308: float clipF0 = Pitch::getFrequencyForPitch(60, 0, 440.0f); // required Chris@307: Chris@309: QString clipPath = QString("%1/%2.wav").arg(m_sampleDir).arg(clipId); Chris@307: Chris@308: if (!mixer->loadClipData(clipPath, clipF0)) { Chris@308: delete mixer; Chris@43: return 0; Chris@43: } Chris@43: Chris@309: std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): loaded clip " << clipId << std::endl; Chris@43: Chris@308: return mixer; Chris@308: } 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@308: if (m_clipMixerMap.find(sodm) == m_clipMixerMap.end()) return; Chris@43: Chris@308: ClipMixer *mixer = m_clipMixerMap[sodm]; Chris@308: m_clipMixerMap.erase(sodm); Chris@308: delete mixer; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::clearModels() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@308: Chris@308: while (!m_clipMixerMap.empty()) { Chris@308: ClipMixer *mixer = m_clipMixerMap.begin()->second; Chris@308: m_clipMixerMap.erase(m_clipMixerMap.begin()); Chris@308: delete mixer; Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioGenerator::reset() Chris@43: { Chris@43: QMutexLocker locker(&m_mutex); Chris@308: Chris@308: for (ClipMixerMap::iterator i = m_clipMixerMap.begin(); i != m_clipMixerMap.end(); ++i) { Chris@43: if (i->second) { Chris@308: i->second->reset(); 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@308: for (ClipMixerMap::iterator i = m_clipMixerMap.begin(); i != m_clipMixerMap.end(); ++i) { Chris@308: if (i->second) i->second->setChannelCount(targetChannelCount); Chris@43: } 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@308: ClipMixer *clipMixer = m_clipMixerMap[model]; Chris@308: if (!clipMixer) return 0; Chris@43: 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@308: ClipMixer::NoteStart on; Chris@308: ClipMixer::NoteEnd off; Chris@43: Chris@275: NoteOffSet ¬eOffs = m_noteOffs[model]; Chris@43: Chris@308: float **bufferIndexes = new float *[m_targetChannelCount]; Chris@308: 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@308: notes = exportable->getNotes(reqStart, Chris@308: reqStart + m_processingBlockSize); Chris@299: } Chris@43: Chris@308: std::vector starts; Chris@308: std::vector ends; 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 < 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@308: size_t eventFrame = noteOffs.begin()->frame; Chris@308: if (eventFrame < reqStart) eventFrame = reqStart; Chris@43: Chris@308: off.frameOffset = eventFrame - reqStart; Chris@308: off.frequency = noteOffs.begin()->frequency; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@308: cerr << "mixModel [synthetic]: adding note-off at frame " << eventFrame << " frame offset " << off.frameOffset << " frequency " << off.frequency << endl; Chris@43: #endif Chris@43: Chris@308: ends.push_back(off); Chris@43: noteOffs.erase(noteOffs.begin()); Chris@43: } Chris@43: Chris@308: on.frameOffset = noteFrame - reqStart; Chris@308: on.frequency = ni->getFrequency(); Chris@308: on.level = float(ni->velocity) / 127.0; Chris@308: on.pan = pan; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@308: cout << "mixModel [synthetic]: adding note at frame " << noteFrame << ", frame offset " << on.frameOffset << " frequency " << on.frequency << endl; Chris@43: #endif Chris@43: Chris@308: starts.push_back(on); Chris@275: noteOffs.insert Chris@308: (NoteOff(on.frequency, noteFrame + ni->duration)); Chris@43: } Chris@43: Chris@43: while (noteOffs.begin() != noteOffs.end() && Chris@308: noteOffs.begin()->frame <= reqStart + m_processingBlockSize) { Chris@43: Chris@308: size_t eventFrame = noteOffs.begin()->frame; Chris@308: if (eventFrame < reqStart) eventFrame = reqStart; Chris@43: Chris@308: off.frameOffset = eventFrame - reqStart; Chris@308: off.frequency = noteOffs.begin()->frequency; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@308: cerr << "mixModel [synthetic]: adding leftover note-off at frame " << eventFrame << " frame offset " << off.frameOffset << " frequency " << off.frequency << endl; Chris@43: #endif Chris@43: Chris@308: ends.push_back(off); Chris@308: noteOffs.erase(noteOffs.begin()); Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < m_targetChannelCount; ++c) { Chris@308: bufferIndexes[c] = buffer[c] + i * m_processingBlockSize; Chris@308: } Chris@43: Chris@308: clipMixer->mix(bufferIndexes, gain, starts, ends); Chris@308: } Chris@43: Chris@308: delete[] bufferIndexes; Chris@43: Chris@43: return got; Chris@43: }