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@313: #include "data/model/SparseTimeValueModel.h" Chris@43: #include "data/model/SparseOneDimensionalModel.h" Chris@299: #include "data/model/NoteData.h" Chris@43: Chris@307: #include "ClipMixer.h" Chris@313: #include "ContinuousSynth.h" Chris@307: Chris@43: #include Chris@167: #include Chris@43: Chris@43: #include Chris@43: #include Chris@43: Chris@436: const sv_frame_t Chris@315: AudioGenerator::m_processingBlockSize = 1024; 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@348: m_waveType(0), Chris@382: m_soloing(false), Chris@382: m_channelBuffer(0), Chris@382: m_channelBufSiz(0), Chris@382: m_channelBufCount(0) 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@596: cerr << "AudioGenerator::~AudioGenerator" << endl; Chris@177: #endif Chris@593: Chris@593: for (int i = 0; i < m_channelBufCount; ++i) { Chris@593: delete[] m_channelBuffer[i]; Chris@593: } Chris@593: delete[] m_channelBuffer; 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@598: } catch (const 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@595: m_sourceSampleRate = model->getSampleRate(); Chris@43: Chris@43: } else { Chris@43: Chris@595: DenseTimeValueModel *dtvm = Chris@595: dynamic_cast(model); Chris@43: Chris@595: if (dtvm) { Chris@595: m_sourceSampleRate = model->getSampleRate(); Chris@595: return true; Chris@595: } Chris@43: } Chris@307: Chris@418: const Playable *playable = model; Chris@418: if (!playable || !playable->canPlay()) return 0; Chris@418: Chris@418: PlayParameters *parameters = Chris@595: PlayParameterRepository::getInstance()->getPlayParameters(playable); Chris@418: Chris@418: bool willPlay = !parameters->isPlayMuted(); Chris@418: Chris@313: if (usesClipMixer(model)) { Chris@313: ClipMixer *mixer = makeClipMixerFor(model); Chris@313: if (mixer) { Chris@313: QMutexLocker locker(&m_mutex); Chris@616: m_clipMixerMap[model->getId()] = mixer; Chris@418: return willPlay; Chris@313: } Chris@313: } Chris@313: Chris@313: if (usesContinuousSynth(model)) { Chris@313: ContinuousSynth *synth = makeSynthFor(model); Chris@313: if (synth) { Chris@313: QMutexLocker locker(&m_mutex); Chris@616: m_continuousSynthMap[model->getId()] = synth; Chris@418: return willPlay; Chris@313: } 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@616: if (m_clipMixerMap.find(model->getId()) == m_clipMixerMap.end()) { Chris@616: return; Chris@616: } Chris@307: Chris@307: ClipMixer *mixer = makeClipMixerFor(model); Chris@307: if (mixer) { Chris@43: QMutexLocker locker(&m_mutex); Chris@616: m_clipMixerMap[model->getId()] = mixer; Chris@43: } Chris@43: } Chris@308: Chris@313: bool Chris@313: AudioGenerator::usesClipMixer(const Model *model) Chris@43: { Chris@313: bool clip = Chris@313: (qobject_cast(model) || Chris@313: qobject_cast(model) || Chris@313: qobject_cast(model)); Chris@313: return clip; Chris@43: } Chris@43: Chris@313: bool Chris@349: AudioGenerator::wantsQuieterClips(const Model *model) Chris@349: { Chris@349: // basically, anything that usually has sustain (like notes) or Chris@349: // often has multiple sounds at once (like notes) wants to use a Chris@349: // quieter level than simple click tracks Chris@349: bool does = Chris@349: (qobject_cast(model) || Chris@349: qobject_cast(model)); Chris@349: return does; Chris@349: } Chris@349: Chris@349: bool Chris@313: AudioGenerator::usesContinuousSynth(const Model *model) Chris@43: { Chris@313: bool cont = Chris@313: (qobject_cast(model)); Chris@313: return cont; Chris@313: } Chris@313: 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@595: PlayParameterRepository::getInstance()->getPlayParameters(playable); Chris@43: if (parameters) { Chris@309: clipId = parameters->getPlayClipId(); Chris@43: } Chris@43: Chris@445: #ifdef DEBUG_AUDIO_GENERATOR Chris@309: std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): sample id = " << clipId << std::endl; Chris@445: #endif 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@436: double clipF0 = Pitch::getFrequencyForPitch(60, 0, 440.0); // required Chris@307: Chris@309: QString clipPath = QString("%1/%2.wav").arg(m_sampleDir).arg(clipId); Chris@307: Chris@436: double level = wantsQuieterClips(model) ? 0.5 : 1.0; Chris@349: if (!mixer->loadClipData(clipPath, clipF0, level)) { Chris@308: delete mixer; Chris@43: return 0; Chris@43: } Chris@43: Chris@445: #ifdef DEBUG_AUDIO_GENERATOR Chris@309: std::cerr << "AudioGenerator::makeClipMixerFor(" << model << "): loaded clip " << clipId << std::endl; Chris@445: #endif Chris@43: Chris@308: return mixer; Chris@308: } Chris@43: Chris@313: ContinuousSynth * Chris@313: AudioGenerator::makeSynthFor(const Model *model) Chris@313: { Chris@313: const Playable *playable = model; Chris@313: if (!playable || !playable->canPlay()) return 0; Chris@313: Chris@313: ContinuousSynth *synth = new ContinuousSynth(m_targetChannelCount, Chris@313: m_sourceSampleRate, rmb456@323: m_processingBlockSize, rmb456@323: m_waveType); Chris@313: Chris@445: #ifdef DEBUG_AUDIO_GENERATOR Chris@313: std::cerr << "AudioGenerator::makeSynthFor(" << model << "): created synth" << std::endl; Chris@445: #endif Chris@313: Chris@313: return synth; Chris@313: } Chris@313: Chris@43: void Chris@43: AudioGenerator::removeModel(Model *model) Chris@43: { Chris@43: SparseOneDimensionalModel *sodm = Chris@595: dynamic_cast(model); Chris@43: if (!sodm) return; // nothing to do Chris@43: Chris@43: QMutexLocker locker(&m_mutex); Chris@43: Chris@616: if (m_clipMixerMap.find(sodm->getId()) == m_clipMixerMap.end()) { Chris@616: return; Chris@616: } Chris@43: Chris@616: ClipMixer *mixer = m_clipMixerMap[sodm->getId()]; Chris@616: m_clipMixerMap.erase(sodm->getId()); 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@595: m_clipMixerMap.erase(m_clipMixerMap.begin()); Chris@595: 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@445: #ifdef DEBUG_AUDIO_GENERATOR Chris@397: cerr << "AudioGenerator::reset()" << endl; Chris@445: #endif Chris@397: Chris@616: for (ClipMixerMap::iterator i = m_clipMixerMap.begin(); Chris@616: i != m_clipMixerMap.end(); ++i) { Chris@595: if (i->second) { Chris@595: i->second->reset(); Chris@595: } Chris@43: } Chris@43: Chris@43: m_noteOffs.clear(); Chris@43: } Chris@43: Chris@43: void Chris@366: AudioGenerator::setTargetChannelCount(int 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@595: if (i->second) i->second->setChannelCount(targetChannelCount); Chris@43: } Chris@43: } Chris@43: Chris@436: sv_frame_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@436: sv_frame_t Chris@613: AudioGenerator::mixModel(Model *model, Chris@613: sv_frame_t startFrame, sv_frame_t frameCount, Chris@613: float **buffer, Chris@613: sv_frame_t fadeIn, sv_frame_t fadeOut) Chris@43: { Chris@43: if (m_sourceSampleRate == 0) { Chris@595: cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl; Chris@595: 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@595: 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@595: return mixDenseTimeValueModel(dtvm, startFrame, frameCount, Chris@595: buffer, gain, pan, fadeIn, fadeOut); Chris@43: } Chris@43: Chris@313: if (usesClipMixer(model)) { Chris@313: return mixClipModel(model, startFrame, frameCount, Chris@313: buffer, gain, pan); Chris@313: } Chris@43: Chris@313: if (usesContinuousSynth(model)) { Chris@313: return mixContinuousSynthModel(model, startFrame, frameCount, Chris@313: buffer, gain, pan); 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@436: sv_frame_t Chris@43: AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm, Chris@595: sv_frame_t startFrame, sv_frame_t frames, Chris@595: float **buffer, float gain, float pan, Chris@595: sv_frame_t fadeIn, sv_frame_t fadeOut) Chris@43: { Chris@436: sv_frame_t maxFrames = frames + std::max(fadeIn, fadeOut); Chris@43: Chris@366: int modelChannels = dtvm->getChannelCount(); Chris@80: Chris@382: if (m_channelBufSiz < maxFrames || m_channelBufCount < modelChannels) { Chris@80: Chris@382: for (int c = 0; c < m_channelBufCount; ++c) { Chris@382: delete[] m_channelBuffer[c]; Chris@80: } Chris@80: Chris@595: delete[] m_channelBuffer; Chris@382: m_channelBuffer = new float *[modelChannels]; Chris@80: Chris@366: for (int c = 0; c < modelChannels; ++c) { Chris@382: m_channelBuffer[c] = new float[maxFrames]; Chris@80: } Chris@80: Chris@382: m_channelBufCount = modelChannels; Chris@595: m_channelBufSiz = maxFrames; Chris@43: } Chris@80: Chris@436: sv_frame_t got = 0; Chris@80: Chris@80: if (startFrame >= fadeIn/2) { Chris@460: Chris@460: auto data = dtvm->getMultiChannelData(0, modelChannels - 1, Chris@460: startFrame - fadeIn/2, Chris@460: frames + fadeOut/2 + fadeIn/2); Chris@460: Chris@460: for (int c = 0; c < modelChannels; ++c) { Chris@460: copy(data[c].begin(), data[c].end(), m_channelBuffer[c]); Chris@460: } Chris@460: Chris@461: got = data[0].size(); Chris@460: Chris@80: } else { Chris@436: sv_frame_t missing = fadeIn/2 - startFrame; Chris@80: Chris@382: if (missing > 0) { Chris@382: cerr << "note: channelBufSiz = " << m_channelBufSiz Chris@382: << ", frames + fadeOut/2 = " << frames + fadeOut/2 Chris@382: << ", startFrame = " << startFrame Chris@382: << ", missing = " << missing << endl; Chris@80: } Chris@80: Chris@460: auto data = dtvm->getMultiChannelData(0, modelChannels - 1, Chris@460: startFrame, Chris@460: frames + fadeOut/2); Chris@366: for (int c = 0; c < modelChannels; ++c) { Chris@460: copy(data[c].begin(), data[c].end(), m_channelBuffer[c] + missing); Chris@80: } Chris@80: Chris@461: got = data[0].size() + missing; Chris@595: } Chris@43: Chris@366: for (int c = 0; c < m_targetChannelCount; ++c) { Chris@43: Chris@595: int sourceChannel = (c % modelChannels); Chris@43: Chris@595: // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl; Chris@43: Chris@595: float channelGain = gain; Chris@595: if (pan != 0.0) { Chris@595: if (c == 0) { Chris@595: if (pan > 0.0) channelGain *= 1.0f - pan; Chris@595: } else { Chris@595: if (pan < 0.0) channelGain *= pan + 1.0f; Chris@595: } Chris@595: } Chris@43: Chris@595: for (sv_frame_t i = 0; i < fadeIn/2; ++i) { Chris@595: float *back = buffer[c]; Chris@595: back -= fadeIn/2; Chris@595: back[i] += Chris@436: (channelGain * m_channelBuffer[sourceChannel][i] * float(i)) Chris@436: / float(fadeIn); Chris@595: } Chris@43: Chris@595: for (sv_frame_t i = 0; i < frames + fadeOut/2; ++i) { Chris@595: float mult = channelGain; Chris@595: if (i < fadeIn/2) { Chris@595: mult = (mult * float(i)) / float(fadeIn); Chris@595: } Chris@595: if (i > frames - fadeOut/2) { Chris@595: mult = (mult * float((frames + fadeOut/2) - i)) / float(fadeOut); Chris@595: } Chris@382: float val = m_channelBuffer[sourceChannel][i]; Chris@80: if (i >= got) val = 0.f; Chris@595: buffer[c][i] += mult * val; Chris@595: } Chris@43: } Chris@43: Chris@43: return got; Chris@43: } Chris@43: Chris@436: sv_frame_t Chris@313: AudioGenerator::mixClipModel(Model *model, Chris@436: sv_frame_t startFrame, sv_frame_t frames, Chris@313: float **buffer, float gain, float pan) Chris@43: { Chris@616: ClipMixer *clipMixer = m_clipMixerMap[model->getId()]; Chris@308: if (!clipMixer) return 0; Chris@43: Chris@436: int blocks = int(frames / m_processingBlockSize); Chris@43: Chris@313: //!!! todo: the below -- it matters Chris@313: 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@436: sv_frame_t got = blocks * m_processingBlockSize; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@442: cout << "mixModel [clip]: start " << startFrame << ", frames " << frames Chris@442: << ", blocks " << blocks << ", have " << m_noteOffs.size() Chris@442: << " note-offs" << endl; Chris@43: #endif Chris@43: Chris@308: ClipMixer::NoteStart on; Chris@308: ClipMixer::NoteEnd off; Chris@43: Chris@616: NoteOffSet ¬eOffs = m_noteOffs[model->getId()]; Chris@43: Chris@308: float **bufferIndexes = new float *[m_targetChannelCount]; Chris@308: Chris@366: for (int i = 0; i < blocks; ++i) { Chris@43: Chris@595: sv_frame_t reqStart = startFrame + i * m_processingBlockSize; Chris@43: Chris@299: NoteList notes; Chris@299: NoteExportable *exportable = dynamic_cast(model); Chris@299: if (exportable) { Chris@366: notes = exportable->getNotesWithin(reqStart, Chris@366: reqStart + m_processingBlockSize); Chris@299: } Chris@43: Chris@308: std::vector starts; Chris@308: std::vector ends; Chris@43: Chris@615: while (noteOffs.begin() != noteOffs.end() && Chris@615: noteOffs.begin()->onFrame > reqStart) { Chris@615: Chris@615: // We must have jumped back in time, as there is a Chris@615: // note-off pending for a note that hasn't begun yet. Emit Chris@615: // the note-off now and discard Chris@615: Chris@615: off.frameOffset = 0; Chris@615: off.frequency = noteOffs.begin()->frequency; Chris@615: Chris@615: #ifdef DEBUG_AUDIO_GENERATOR Chris@615: cerr << "mixModel [clip]: adding rewind-caused note-off at frame offset 0 frequency " << off.frequency << endl; Chris@615: #endif Chris@615: Chris@615: ends.push_back(off); Chris@615: noteOffs.erase(noteOffs.begin()); Chris@615: } Chris@615: Chris@595: for (NoteList::const_iterator ni = notes.begin(); Chris@275: ni != notes.end(); ++ni) { Chris@43: Chris@595: sv_frame_t noteFrame = ni->start; Chris@596: sv_frame_t noteDuration = ni->duration; Chris@43: Chris@595: if (noteFrame < reqStart || Chris@596: noteFrame >= reqStart + m_processingBlockSize) { Chris@596: continue; Chris@596: } Chris@596: Chris@596: if (noteDuration == 0) { Chris@596: // If we have a note-off and a note-on with the same Chris@596: // time, then the note-off will be assumed (in the Chris@596: // logic below that deals with two-point note-on/off Chris@596: // events) to be switching off an earlier note before Chris@596: // this one begins -- that's necessary in order to Chris@596: // support adjoining notes of equal pitch. But it does Chris@596: // mean we have to explicitly ignore zero-duration Chris@596: // notes, otherwise they'll be played without end Chris@596: #ifdef DEBUG_AUDIO_GENERATOR Chris@596: cerr << "mixModel [clip]: zero-duration note found at frame " << noteFrame << ", skipping it" << endl; Chris@596: #endif Chris@596: continue; Chris@596: } Chris@43: Chris@595: while (noteOffs.begin() != noteOffs.end() && Chris@615: noteOffs.begin()->offFrame <= noteFrame) { Chris@43: Chris@615: sv_frame_t eventFrame = noteOffs.begin()->offFrame; 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@595: cerr << "mixModel [clip]: 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@595: noteOffs.erase(noteOffs.begin()); Chris@595: } Chris@43: Chris@308: on.frameOffset = noteFrame - reqStart; Chris@308: on.frequency = ni->getFrequency(); Chris@436: on.level = float(ni->velocity) / 127.0f; Chris@308: on.pan = pan; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_GENERATOR Chris@595: cout << "mixModel [clip]: adding note at frame " << noteFrame << ", frame offset " << on.frameOffset << " frequency " << on.frequency << ", level " << on.level << endl; Chris@43: #endif Chris@595: Chris@308: starts.push_back(on); Chris@595: noteOffs.insert Chris@615: (NoteOff(on.frequency, noteFrame + noteDuration, noteFrame)); Chris@595: } Chris@43: Chris@595: while (noteOffs.begin() != noteOffs.end() && Chris@615: noteOffs.begin()->offFrame <= Chris@615: reqStart + m_processingBlockSize) { Chris@43: Chris@615: sv_frame_t eventFrame = noteOffs.begin()->offFrame; 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@313: cerr << "mixModel [clip]: 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@595: } Chris@43: Chris@595: for (int 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: } Chris@313: Chris@436: sv_frame_t Chris@313: AudioGenerator::mixContinuousSynthModel(Model *model, Chris@436: sv_frame_t startFrame, Chris@436: sv_frame_t frames, Chris@313: float **buffer, Chris@313: float gain, Chris@313: float pan) Chris@313: { Chris@616: ContinuousSynth *synth = m_continuousSynthMap[model->getId()]; Chris@313: if (!synth) return 0; Chris@313: Chris@313: // only type we support here at the moment Chris@313: SparseTimeValueModel *stvm = qobject_cast(model); Chris@313: if (stvm->getScaleUnits() != "Hz") return 0; Chris@313: Chris@436: int blocks = int(frames / m_processingBlockSize); Chris@313: Chris@313: //!!! todo: see comment in mixClipModel Chris@313: Chris@436: sv_frame_t got = blocks * m_processingBlockSize; Chris@313: Chris@313: #ifdef DEBUG_AUDIO_GENERATOR Chris@313: cout << "mixModel [synth]: frames " << frames Chris@595: << ", blocks " << blocks << endl; Chris@313: #endif Chris@313: Chris@313: float **bufferIndexes = new float *[m_targetChannelCount]; Chris@313: Chris@366: for (int i = 0; i < blocks; ++i) { Chris@313: Chris@595: sv_frame_t reqStart = startFrame + i * m_processingBlockSize; Chris@313: Chris@595: for (int c = 0; c < m_targetChannelCount; ++c) { Chris@313: bufferIndexes[c] = buffer[c] + i * m_processingBlockSize; Chris@313: } Chris@313: Chris@313: SparseTimeValueModel::PointList points = Chris@313: stvm->getPoints(reqStart, reqStart + m_processingBlockSize); Chris@313: Chris@313: // by default, repeat last frequency Chris@313: float f0 = 0.f; Chris@313: Chris@313: // go straight to the last freq that is genuinely in this range Chris@313: for (SparseTimeValueModel::PointList::const_iterator itr = points.end(); Chris@313: itr != points.begin(); ) { Chris@313: --itr; Chris@313: if (itr->frame >= reqStart && Chris@313: itr->frame < reqStart + m_processingBlockSize) { Chris@313: f0 = itr->value; Chris@313: break; Chris@313: } Chris@313: } Chris@313: Chris@314: // if we found no such frequency and the next point is further Chris@314: // away than twice the model resolution, go silent (same Chris@314: // criterion TimeValueLayer uses for ending a discrete curve Chris@314: // segment) Chris@314: if (f0 == 0.f) { Chris@314: SparseTimeValueModel::PointList nextPoints = Chris@314: stvm->getNextPoints(reqStart + m_processingBlockSize); Chris@314: if (nextPoints.empty() || Chris@314: nextPoints.begin()->frame > reqStart + 2 * stvm->getResolution()) { Chris@314: f0 = -1.f; Chris@314: } Chris@314: } Chris@314: Chris@315: // cerr << "f0 = " << f0 << endl; Chris@313: Chris@313: synth->mix(bufferIndexes, Chris@313: gain, Chris@313: pan, Chris@313: f0); Chris@313: } Chris@313: Chris@313: delete[] bufferIndexes; Chris@313: Chris@313: return got; Chris@313: } Chris@313: