Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@77: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "AudioCallbackPlaySource.h" Chris@0: Chris@0: #include "AudioGenerator.h" Chris@0: Chris@1: #include "data/model/Model.h" Chris@1: #include "view/ViewManager.h" Chris@0: #include "base/PlayParameterRepository.h" Chris@32: #include "base/Preferences.h" Chris@1: #include "data/model/DenseTimeValueModel.h" Chris@139: #include "data/model/WaveFileModel.h" Chris@1: #include "data/model/SparseOneDimensionalModel.h" Chris@41: #include "plugin/RealTimePluginInstance.h" Chris@14: #include "PhaseVocoderTimeStretcher.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: //#define DEBUG_AUDIO_PLAY_SOURCE 1 Chris@14: //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1 Chris@0: Chris@0: const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071; Chris@0: Chris@0: AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) : Chris@0: m_viewManager(manager), Chris@0: m_audioGenerator(new AudioGenerator()), Chris@0: m_readBuffers(0), Chris@0: m_writeBuffers(0), Chris@0: m_readBufferFill(0), Chris@0: m_writeBufferFill(0), Chris@0: m_bufferScavenger(1), Chris@0: m_sourceChannelCount(0), Chris@0: m_blockSize(1024), Chris@0: m_sourceSampleRate(0), Chris@0: m_targetSampleRate(0), Chris@0: m_playLatency(0), Chris@0: m_playing(false), Chris@0: m_exiting(false), Chris@0: m_lastModelEndFrame(0), Chris@0: m_outputLeft(0.0), Chris@0: m_outputRight(0.0), Chris@41: m_auditioningPlugin(0), Chris@42: m_auditioningPluginBypassed(false), Chris@0: m_timeStretcher(0), Chris@0: m_fillThread(0), Chris@32: m_converter(0), Chris@32: m_crapConverter(0), Chris@32: m_resampleQuality(Preferences::getInstance()->getResampleQuality()) Chris@0: { Chris@0: m_viewManager->setAudioPlaySource(this); Chris@0: Chris@0: connect(m_viewManager, SIGNAL(selectionChanged()), Chris@0: this, SLOT(selectionChanged())); Chris@0: connect(m_viewManager, SIGNAL(playLoopModeChanged()), Chris@0: this, SLOT(playLoopModeChanged())); Chris@0: connect(m_viewManager, SIGNAL(playSelectionModeChanged()), Chris@0: this, SLOT(playSelectionModeChanged())); Chris@0: Chris@0: connect(PlayParameterRepository::getInstance(), Chris@0: SIGNAL(playParametersChanged(PlayParameters *)), Chris@0: this, SLOT(playParametersChanged(PlayParameters *))); Chris@32: Chris@32: connect(Preferences::getInstance(), Chris@32: SIGNAL(propertyChanged(PropertyContainer::PropertyName)), Chris@32: this, SLOT(preferenceChanged(PropertyContainer::PropertyName))); Chris@0: } Chris@0: Chris@0: AudioCallbackPlaySource::~AudioCallbackPlaySource() Chris@0: { Chris@0: m_exiting = true; Chris@0: Chris@0: if (m_fillThread) { Chris@0: m_condition.wakeAll(); Chris@0: m_fillThread->wait(); Chris@0: delete m_fillThread; Chris@0: } Chris@0: Chris@0: clearModels(); Chris@0: Chris@0: if (m_readBuffers != m_writeBuffers) { Chris@0: delete m_readBuffers; Chris@0: } Chris@0: Chris@0: delete m_writeBuffers; Chris@0: Chris@0: delete m_audioGenerator; Chris@0: Chris@0: m_bufferScavenger.scavenge(true); Chris@41: m_pluginScavenger.scavenge(true); Chris@41: m_timeStretcherScavenger.scavenge(true); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::addModel(Model *model) Chris@0: { Chris@0: if (m_models.find(model) != m_models.end()) return; Chris@0: Chris@0: bool canPlay = m_audioGenerator->addModel(model); Chris@0: Chris@0: m_mutex.lock(); Chris@0: Chris@0: m_models.insert(model); Chris@0: if (model->getEndFrame() > m_lastModelEndFrame) { Chris@0: m_lastModelEndFrame = model->getEndFrame(); Chris@0: } Chris@0: Chris@0: bool buffersChanged = false, srChanged = false; Chris@0: Chris@0: size_t modelChannels = 1; Chris@0: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@0: if (dtvm) modelChannels = dtvm->getChannelCount(); Chris@0: if (modelChannels > m_sourceChannelCount) { Chris@0: m_sourceChannelCount = modelChannels; Chris@0: } Chris@0: Chris@118: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@118: std::cout << "Adding model with " << modelChannels << " channels " << std::endl; Chris@118: #endif Chris@0: Chris@0: if (m_sourceSampleRate == 0) { Chris@0: Chris@0: m_sourceSampleRate = model->getSampleRate(); Chris@0: srChanged = true; Chris@0: Chris@0: } else if (model->getSampleRate() != m_sourceSampleRate) { Chris@0: Chris@0: // If this is a dense time-value model and we have no other, we Chris@0: // can just switch to this model's sample rate Chris@0: Chris@0: if (dtvm) { Chris@0: Chris@0: bool conflicting = false; Chris@0: Chris@0: for (std::set::const_iterator i = m_models.begin(); Chris@0: i != m_models.end(); ++i) { Chris@139: // Only wave file models can be considered conflicting -- Chris@139: // writable wave file models are derived and we shouldn't Chris@139: // take their rates into account. Also, don't give any Chris@139: // particular weight to a file that's already playing at Chris@139: // the wrong rate anyway Chris@139: WaveFileModel *wfm = dynamic_cast(*i); Chris@139: if (wfm && wfm != dtvm && Chris@139: wfm->getSampleRate() != model->getSampleRate() && Chris@139: wfm->getSampleRate() == m_sourceSampleRate) { Chris@139: std::cerr << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << *i << " found" << std::endl; Chris@0: conflicting = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (conflicting) { Chris@0: Chris@0: std::cerr << "AudioCallbackPlaySource::addModel: ERROR: " Chris@0: << "New model sample rate does not match" << std::endl Chris@0: << "existing model(s) (new " << model->getSampleRate() Chris@0: << " vs " << m_sourceSampleRate Chris@0: << "), playback will be wrong" Chris@0: << std::endl; Chris@0: Chris@139: emit sampleRateMismatch(model->getSampleRate(), Chris@139: m_sourceSampleRate, Chris@0: false); Chris@0: } else { Chris@0: m_sourceSampleRate = model->getSampleRate(); Chris@0: srChanged = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) { Chris@0: clearRingBuffers(true, getTargetChannelCount()); Chris@0: buffersChanged = true; Chris@0: } else { Chris@0: if (canPlay) clearRingBuffers(true); Chris@0: } Chris@0: Chris@0: if (buffersChanged || srChanged) { Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@32: src_delete(m_crapConverter); Chris@0: m_converter = 0; Chris@32: m_crapConverter = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: m_mutex.unlock(); Chris@0: Chris@0: m_audioGenerator->setTargetChannelCount(getTargetChannelCount()); Chris@0: Chris@0: if (!m_fillThread) { Chris@127: m_fillThread = new FillThread(*this); Chris@0: m_fillThread->start(); Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@118: std::cout << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s) -- emitting modelReplaced" << std::endl; Chris@0: #endif Chris@0: Chris@0: if (buffersChanged || srChanged) { Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@148: connect(model, SIGNAL(modelChanged(size_t, size_t)), Chris@148: this, SLOT(modelChanged(size_t, size_t))); Chris@148: Chris@0: m_condition.wakeAll(); Chris@0: } Chris@0: Chris@0: void Chris@148: AudioCallbackPlaySource::modelChanged(size_t startFrame, size_t endFrame) Chris@148: { Chris@149: // std::cerr << "AudioCallbackPlaySource::modelChanged(" << startFrame << "," << endFrame << ")" << std::endl; Chris@148: if (endFrame > m_lastModelEndFrame) m_lastModelEndFrame = endFrame; Chris@148: } Chris@148: Chris@148: void Chris@0: AudioCallbackPlaySource::removeModel(Model *model) Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@118: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@118: std::cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << std::endl; Chris@118: #endif Chris@118: Chris@148: disconnect(model, SIGNAL(modelChanged(size_t, size_t)), Chris@148: this, SLOT(modelChanged(size_t, size_t))); Chris@148: Chris@0: m_models.erase(model); Chris@0: Chris@0: if (m_models.empty()) { Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@32: src_delete(m_crapConverter); Chris@0: m_converter = 0; Chris@32: m_crapConverter = 0; Chris@0: } Chris@0: m_sourceSampleRate = 0; Chris@0: } Chris@0: Chris@0: size_t lastEnd = 0; Chris@0: for (std::set::const_iterator i = m_models.begin(); Chris@0: i != m_models.end(); ++i) { Chris@106: // std::cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl; Chris@0: if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame(); Chris@106: // std::cout << "(done, lastEnd now " << lastEnd << ")" << std::endl; Chris@0: } Chris@0: m_lastModelEndFrame = lastEnd; Chris@0: Chris@0: m_mutex.unlock(); Chris@0: Chris@0: m_audioGenerator->removeModel(model); Chris@0: Chris@0: clearRingBuffers(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::clearModels() Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@118: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@118: std::cout << "AudioCallbackPlaySource::clearModels()" << std::endl; Chris@118: #endif Chris@118: Chris@0: m_models.clear(); Chris@0: Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@32: src_delete(m_crapConverter); Chris@0: m_converter = 0; Chris@32: m_crapConverter = 0; Chris@0: } Chris@0: Chris@0: m_lastModelEndFrame = 0; Chris@0: Chris@0: m_sourceSampleRate = 0; Chris@0: Chris@0: m_mutex.unlock(); Chris@0: Chris@0: m_audioGenerator->clearModels(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count) Chris@0: { Chris@0: if (!haveLock) m_mutex.lock(); Chris@0: Chris@0: if (count == 0) { Chris@0: if (m_writeBuffers) count = m_writeBuffers->size(); Chris@0: } Chris@0: Chris@0: size_t sf = m_readBufferFill; Chris@0: RingBuffer *rb = getReadRingBuffer(0); Chris@0: if (rb) { Chris@0: //!!! This is incorrect if we're in a non-contiguous selection Chris@0: //Same goes for all related code (subtracting the read space Chris@0: //from the fill frame to try to establish where the effective Chris@0: //pre-resample/timestretch read pointer is) Chris@0: size_t rs = rb->getReadSpace(); Chris@0: if (rs < sf) sf -= rs; Chris@0: else sf = 0; Chris@0: } Chris@0: m_writeBufferFill = sf; Chris@0: Chris@0: if (m_readBuffers != m_writeBuffers) { Chris@0: delete m_writeBuffers; Chris@0: } Chris@0: Chris@0: m_writeBuffers = new RingBufferVector; Chris@0: Chris@0: for (size_t i = 0; i < count; ++i) { Chris@0: m_writeBuffers->push_back(new RingBuffer(m_ringBufferSize)); Chris@0: } Chris@0: Chris@106: // std::cout << "AudioCallbackPlaySource::clearRingBuffers: Created " Chris@0: // << count << " write buffers" << std::endl; Chris@0: Chris@0: if (!haveLock) { Chris@0: m_mutex.unlock(); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::play(size_t startFrame) Chris@0: { Chris@0: if (m_viewManager->getPlaySelectionMode() && Chris@0: !m_viewManager->getSelections().empty()) { Chris@0: MultiSelection::SelectionList selections = m_viewManager->getSelections(); Chris@0: MultiSelection::SelectionList::iterator i = selections.begin(); Chris@0: if (i != selections.end()) { Chris@0: if (startFrame < i->getStartFrame()) { Chris@0: startFrame = i->getStartFrame(); Chris@0: } else { Chris@0: MultiSelection::SelectionList::iterator j = selections.end(); Chris@0: --j; Chris@0: if (startFrame >= j->getEndFrame()) { Chris@0: startFrame = i->getStartFrame(); Chris@0: } Chris@0: } Chris@0: } Chris@0: } else { Chris@0: if (startFrame >= m_lastModelEndFrame) { Chris@0: startFrame = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: // The fill thread will automatically empty its buffers before Chris@0: // starting again if we have not so far been playing, but not if Chris@0: // we're just re-seeking. Chris@0: Chris@0: m_mutex.lock(); Chris@0: if (m_playing) { Chris@0: m_readBufferFill = m_writeBufferFill = startFrame; Chris@0: if (m_readBuffers) { Chris@0: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@0: RingBuffer *rb = getReadRingBuffer(c); Chris@0: if (rb) rb->reset(); Chris@0: } Chris@0: } Chris@0: if (m_converter) src_reset(m_converter); Chris@32: if (m_crapConverter) src_reset(m_crapConverter); Chris@0: } else { Chris@0: if (m_converter) src_reset(m_converter); Chris@32: if (m_crapConverter) src_reset(m_crapConverter); Chris@0: m_readBufferFill = m_writeBufferFill = startFrame; Chris@0: } Chris@0: m_mutex.unlock(); Chris@0: Chris@0: m_audioGenerator->reset(); Chris@0: Chris@0: bool changed = !m_playing; Chris@0: m_playing = true; Chris@0: m_condition.wakeAll(); Chris@0: if (changed) emit playStatusChanged(m_playing); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::stop() Chris@0: { Chris@0: bool changed = m_playing; Chris@0: m_playing = false; Chris@0: m_condition.wakeAll(); Chris@0: if (changed) emit playStatusChanged(m_playing); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::selectionChanged() Chris@0: { Chris@0: if (m_viewManager->getPlaySelectionMode()) { Chris@0: clearRingBuffers(); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::playLoopModeChanged() Chris@0: { Chris@0: clearRingBuffers(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::playSelectionModeChanged() Chris@0: { Chris@0: if (!m_viewManager->getSelections().empty()) { Chris@0: clearRingBuffers(); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@137: AudioCallbackPlaySource::playParametersChanged(PlayParameters *) Chris@0: { Chris@0: clearRingBuffers(); Chris@0: } Chris@0: Chris@0: void Chris@32: AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n) Chris@32: { Chris@32: if (n == "Resample Quality") { Chris@32: setResampleQuality(Preferences::getInstance()->getResampleQuality()); Chris@32: } Chris@32: } Chris@32: Chris@32: void Chris@42: AudioCallbackPlaySource::audioProcessingOverload() Chris@42: { Chris@42: RealTimePluginInstance *ap = m_auditioningPlugin; Chris@42: if (ap && m_playing && !m_auditioningPluginBypassed) { Chris@42: m_auditioningPluginBypassed = true; Chris@42: emit audioOverloadPluginDisabled(); Chris@42: } Chris@42: } Chris@42: Chris@42: void Chris@0: AudioCallbackPlaySource::setTargetBlockSize(size_t size) Chris@0: { Chris@106: // std::cout << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl; Chris@0: assert(size < m_ringBufferSize); Chris@0: m_blockSize = size; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetBlockSize() const Chris@0: { Chris@106: // std::cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl; Chris@0: return m_blockSize; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setTargetPlayLatency(size_t latency) Chris@0: { Chris@0: m_playLatency = latency; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetPlayLatency() const Chris@0: { Chris@0: return m_playLatency; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getCurrentPlayingFrame() Chris@0: { Chris@0: bool resample = false; Chris@0: double ratio = 1.0; Chris@0: Chris@0: if (getSourceSampleRate() != getTargetSampleRate()) { Chris@0: resample = true; Chris@0: ratio = double(getSourceSampleRate()) / double(getTargetSampleRate()); Chris@0: } Chris@0: Chris@0: size_t readSpace = 0; Chris@0: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@0: RingBuffer *rb = getReadRingBuffer(c); Chris@0: if (rb) { Chris@0: size_t spaceHere = rb->getReadSpace(); Chris@0: if (c == 0 || spaceHere < readSpace) readSpace = spaceHere; Chris@0: } Chris@0: } Chris@0: Chris@0: if (resample) { Chris@0: readSpace = size_t(readSpace * ratio + 0.1); Chris@0: } Chris@0: Chris@0: size_t latency = m_playLatency; Chris@0: if (resample) latency = size_t(m_playLatency * ratio + 0.1); Chris@16: Chris@16: PhaseVocoderTimeStretcher *timeStretcher = m_timeStretcher; Chris@0: if (timeStretcher) { Chris@16: latency += timeStretcher->getProcessingLatency(); Chris@0: } Chris@0: Chris@0: latency += readSpace; Chris@0: size_t bufferedFrame = m_readBufferFill; Chris@0: Chris@0: bool looping = m_viewManager->getPlayLoopMode(); Chris@0: bool constrained = (m_viewManager->getPlaySelectionMode() && Chris@0: !m_viewManager->getSelections().empty()); Chris@0: Chris@0: size_t framePlaying = bufferedFrame; Chris@0: Chris@0: if (looping && !constrained) { Chris@0: while (framePlaying < latency) framePlaying += m_lastModelEndFrame; Chris@0: } Chris@0: Chris@0: if (framePlaying > latency) framePlaying -= latency; Chris@0: else framePlaying = 0; Chris@0: Chris@0: if (!constrained) { Chris@0: if (!looping && framePlaying > m_lastModelEndFrame) { Chris@0: framePlaying = m_lastModelEndFrame; Chris@0: stop(); Chris@0: } Chris@0: return framePlaying; Chris@0: } Chris@0: Chris@0: MultiSelection::SelectionList selections = m_viewManager->getSelections(); Chris@0: MultiSelection::SelectionList::const_iterator i; Chris@0: Chris@137: // i = selections.begin(); Chris@137: // size_t rangeStart = i->getStartFrame(); Chris@0: Chris@0: i = selections.end(); Chris@0: --i; Chris@0: size_t rangeEnd = i->getEndFrame(); Chris@0: Chris@0: for (i = selections.begin(); i != selections.end(); ++i) { Chris@0: if (i->contains(bufferedFrame)) break; Chris@0: } Chris@0: Chris@0: size_t f = bufferedFrame; Chris@0: Chris@106: // std::cout << "getCurrentPlayingFrame: f=" << f << ", latency=" << latency << ", rangeEnd=" << rangeEnd << std::endl; Chris@0: Chris@0: if (i == selections.end()) { Chris@0: --i; Chris@0: if (i->getEndFrame() + latency < f) { Chris@106: // std::cout << "framePlaying = " << framePlaying << ", rangeEnd = " << rangeEnd << std::endl; Chris@0: Chris@0: if (!looping && (framePlaying > rangeEnd)) { Chris@106: // std::cout << "STOPPING" << std::endl; Chris@0: stop(); Chris@0: return rangeEnd; Chris@0: } else { Chris@0: return framePlaying; Chris@0: } Chris@0: } else { Chris@106: // std::cout << "latency <- " << latency << "-(" << f << "-" << i->getEndFrame() << ")" << std::endl; Chris@0: latency -= (f - i->getEndFrame()); Chris@0: f = i->getEndFrame(); Chris@0: } Chris@0: } Chris@0: Chris@106: // std::cout << "i=(" << i->getStartFrame() << "," << i->getEndFrame() << ") f=" << f << ", latency=" << latency << std::endl; Chris@0: Chris@0: while (latency > 0) { Chris@0: size_t offset = f - i->getStartFrame(); Chris@0: if (offset >= latency) { Chris@0: if (f > latency) { Chris@0: framePlaying = f - latency; Chris@0: } else { Chris@0: framePlaying = 0; Chris@0: } Chris@0: break; Chris@0: } else { Chris@0: if (i == selections.begin()) { Chris@0: if (looping) { Chris@0: i = selections.end(); Chris@0: } Chris@0: } Chris@0: latency -= offset; Chris@0: --i; Chris@0: f = i->getEndFrame(); Chris@0: } Chris@0: } Chris@0: Chris@0: return framePlaying; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setOutputLevels(float left, float right) Chris@0: { Chris@0: m_outputLeft = left; Chris@0: m_outputRight = right; Chris@0: } Chris@0: Chris@0: bool Chris@0: AudioCallbackPlaySource::getOutputLevels(float &left, float &right) Chris@0: { Chris@0: left = m_outputLeft; Chris@0: right = m_outputRight; Chris@0: return true; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setTargetSampleRate(size_t sr) Chris@0: { Chris@0: m_targetSampleRate = sr; Chris@32: initialiseConverter(); Chris@32: } Chris@32: Chris@32: void Chris@32: AudioCallbackPlaySource::initialiseConverter() Chris@32: { Chris@32: m_mutex.lock(); Chris@32: Chris@32: if (m_converter) { Chris@32: src_delete(m_converter); Chris@32: src_delete(m_crapConverter); Chris@32: m_converter = 0; Chris@32: m_crapConverter = 0; Chris@32: } Chris@0: Chris@0: if (getSourceSampleRate() != getTargetSampleRate()) { Chris@0: Chris@0: int err = 0; Chris@32: Chris@32: m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY : Chris@32: m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY : Chris@32: m_resampleQuality == 0 ? SRC_SINC_FASTEST : Chris@32: SRC_SINC_MEDIUM_QUALITY, Chris@0: getTargetChannelCount(), &err); Chris@32: Chris@32: if (m_converter) { Chris@32: m_crapConverter = src_new(SRC_LINEAR, Chris@32: getTargetChannelCount(), Chris@32: &err); Chris@32: } Chris@32: Chris@32: if (!m_converter || !m_crapConverter) { Chris@0: std::cerr Chris@0: << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: " Chris@0: << src_strerror(err) << std::endl; Chris@0: Chris@32: if (m_converter) { Chris@32: src_delete(m_converter); Chris@32: m_converter = 0; Chris@32: } Chris@32: Chris@32: if (m_crapConverter) { Chris@32: src_delete(m_crapConverter); Chris@32: m_crapConverter = 0; Chris@32: } Chris@32: Chris@32: m_mutex.unlock(); Chris@32: Chris@0: emit sampleRateMismatch(getSourceSampleRate(), Chris@0: getTargetSampleRate(), Chris@0: false); Chris@0: } else { Chris@0: Chris@32: m_mutex.unlock(); Chris@32: Chris@0: emit sampleRateMismatch(getSourceSampleRate(), Chris@0: getTargetSampleRate(), Chris@0: true); Chris@0: } Chris@32: } else { Chris@32: m_mutex.unlock(); Chris@0: } Chris@0: } Chris@0: Chris@32: void Chris@32: AudioCallbackPlaySource::setResampleQuality(int q) Chris@32: { Chris@32: if (q == m_resampleQuality) return; Chris@32: m_resampleQuality = q; Chris@32: Chris@32: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@32: std::cerr << "AudioCallbackPlaySource::setResampleQuality: setting to " Chris@32: << m_resampleQuality << std::endl; Chris@32: #endif Chris@32: Chris@32: initialiseConverter(); Chris@32: } Chris@32: Chris@41: void Chris@41: AudioCallbackPlaySource::setAuditioningPlugin(RealTimePluginInstance *plugin) Chris@41: { Chris@41: RealTimePluginInstance *formerPlugin = m_auditioningPlugin; Chris@41: m_auditioningPlugin = plugin; Chris@42: m_auditioningPluginBypassed = false; Chris@41: if (formerPlugin) m_pluginScavenger.claim(formerPlugin); Chris@41: } Chris@41: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetSampleRate() const Chris@0: { Chris@0: if (m_targetSampleRate) return m_targetSampleRate; Chris@0: else return getSourceSampleRate(); Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceChannelCount() const Chris@0: { Chris@0: return m_sourceChannelCount; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetChannelCount() const Chris@0: { Chris@0: if (m_sourceChannelCount < 2) return 2; Chris@0: return m_sourceChannelCount; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceSampleRate() const Chris@0: { Chris@0: return m_sourceSampleRate; Chris@0: } Chris@0: Chris@0: void Chris@26: AudioCallbackPlaySource::setTimeStretch(float factor, bool sharpen, bool mono) Chris@0: { Chris@0: // Avoid locks -- create, assign, mark old one for scavenging Chris@0: // later (as a call to getSourceSamples may still be using it) Chris@0: Chris@16: PhaseVocoderTimeStretcher *existingStretcher = m_timeStretcher; Chris@0: Chris@26: size_t channels = getTargetChannelCount(); Chris@26: if (mono) channels = 1; Chris@26: Chris@16: if (existingStretcher && Chris@16: existingStretcher->getRatio() == factor && Chris@26: existingStretcher->getSharpening() == sharpen && Chris@26: existingStretcher->getChannelCount() == channels) { Chris@0: return; Chris@0: } Chris@0: Chris@12: if (factor != 1) { Chris@25: Chris@25: if (existingStretcher && Chris@26: existingStretcher->getSharpening() == sharpen && Chris@26: existingStretcher->getChannelCount() == channels) { Chris@25: existingStretcher->setRatio(factor); Chris@25: return; Chris@25: } Chris@25: Chris@16: PhaseVocoderTimeStretcher *newStretcher = new PhaseVocoderTimeStretcher Chris@22: (getTargetSampleRate(), Chris@26: channels, Chris@16: factor, Chris@16: sharpen, Chris@31: getTargetBlockSize()); Chris@26: Chris@0: m_timeStretcher = newStretcher; Chris@26: Chris@0: } else { Chris@0: m_timeStretcher = 0; Chris@0: } Chris@0: Chris@0: if (existingStretcher) { Chris@0: m_timeStretcherScavenger.claim(existingStretcher); Chris@0: } Chris@0: } Chris@26: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer) Chris@0: { Chris@0: if (!m_playing) { Chris@0: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@0: for (size_t i = 0; i < count; ++i) { Chris@0: buffer[ch][i] = 0.0; Chris@0: } Chris@0: } Chris@0: return 0; Chris@0: } Chris@0: Chris@106: // Ensure that all buffers have at least the amount of data we Chris@106: // need -- else reduce the size of our requests correspondingly Chris@106: Chris@106: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@106: Chris@106: RingBuffer *rb = getReadRingBuffer(ch); Chris@106: Chris@106: if (!rb) { Chris@106: std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@106: << "No ring buffer available for channel " << ch Chris@106: << ", returning no data here" << std::endl; Chris@106: count = 0; Chris@106: break; Chris@106: } Chris@106: Chris@106: size_t rs = rb->getReadSpace(); Chris@106: if (rs < count) { Chris@106: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@106: << "Ring buffer for channel " << ch << " has only " Chris@106: << rs << " (of " << count << ") samples available, " Chris@106: << "reducing request size" << std::endl; Chris@106: #endif Chris@106: count = rs; Chris@106: } Chris@106: } Chris@106: Chris@106: if (count == 0) return 0; Chris@106: Chris@16: PhaseVocoderTimeStretcher *ts = m_timeStretcher; Chris@0: Chris@16: if (!ts || ts->getRatio() == 1) { Chris@0: Chris@0: size_t got = 0; Chris@0: Chris@0: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@0: Chris@0: RingBuffer *rb = getReadRingBuffer(ch); Chris@0: Chris@0: if (rb) { Chris@0: Chris@0: // this is marginally more likely to leave our channels in Chris@0: // sync after a processing failure than just passing "count": Chris@0: size_t request = count; Chris@0: if (ch > 0) request = got; Chris@0: Chris@0: got = rb->read(buffer[ch], request); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@106: std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@0: for (size_t i = got; i < count; ++i) { Chris@0: buffer[ch][i] = 0.0; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@41: applyAuditioningEffect(count, buffer); Chris@41: Chris@0: m_condition.wakeAll(); Chris@0: return got; Chris@0: } Chris@0: Chris@16: float ratio = ts->getRatio(); Chris@0: Chris@16: // std::cout << "ratio = " << ratio << std::endl; Chris@0: Chris@26: size_t channels = getTargetChannelCount(); Chris@26: bool mix = (channels > 1 && ts->getChannelCount() == 1); Chris@26: Chris@16: size_t available; Chris@0: Chris@31: int warned = 0; Chris@31: Chris@31: // We want output blocks of e.g. 1024 (probably fixed, certainly Chris@31: // bounded). We can provide input blocks of any size (unbounded) Chris@31: // at the timestretcher's request. The input block for a given Chris@31: // output is approx output / ratio, but we can't predict it Chris@31: // exactly, for an adaptive timestretcher. The stretcher will Chris@56: // need some additional buffer space. See the time stretcher code Chris@56: // and comments. Chris@31: Chris@16: while ((available = ts->getAvailableOutputSamples()) < count) { Chris@0: Chris@16: size_t reqd = lrintf((count - available) / ratio); Chris@16: reqd = std::max(reqd, ts->getRequiredInputSamples()); Chris@16: if (reqd == 0) reqd = 1; Chris@16: Chris@16: float *ib[channels]; Chris@0: Chris@16: size_t got = reqd; Chris@0: Chris@26: if (mix) { Chris@26: for (size_t c = 0; c < channels; ++c) { Chris@26: if (c == 0) ib[c] = new float[reqd]; //!!! fix -- this is a rt function Chris@26: else ib[c] = 0; Chris@26: RingBuffer *rb = getReadRingBuffer(c); Chris@26: if (rb) { Chris@26: size_t gotHere; Chris@26: if (c > 0) gotHere = rb->readAdding(ib[0], got); Chris@26: else gotHere = rb->read(ib[0], got); Chris@26: if (gotHere < got) got = gotHere; Chris@26: } Chris@26: } Chris@26: } else { Chris@26: for (size_t c = 0; c < channels; ++c) { Chris@26: ib[c] = new float[reqd]; //!!! fix -- this is a rt function Chris@26: RingBuffer *rb = getReadRingBuffer(c); Chris@26: if (rb) { Chris@26: size_t gotHere = rb->read(ib[c], got); Chris@26: if (gotHere < got) got = gotHere; Chris@26: } Chris@16: } Chris@16: } Chris@0: Chris@16: if (got < reqd) { Chris@16: std::cerr << "WARNING: Read underrun in playback (" Chris@16: << got << " < " << reqd << ")" << std::endl; Chris@16: } Chris@16: Chris@16: ts->putInput(ib, got); Chris@16: Chris@16: for (size_t c = 0; c < channels; ++c) { Chris@16: delete[] ib[c]; Chris@16: } Chris@16: Chris@16: if (got == 0) break; Chris@16: Chris@16: if (ts->getAvailableOutputSamples() == available) { Chris@31: std::cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << std::endl; Chris@31: if (++warned == 5) break; Chris@16: } Chris@0: } Chris@0: Chris@16: ts->getOutput(buffer, count); Chris@0: Chris@26: if (mix) { Chris@26: for (size_t c = 1; c < channels; ++c) { Chris@26: for (size_t i = 0; i < count; ++i) { Chris@26: buffer[c][i] = buffer[0][i] / channels; Chris@26: } Chris@26: } Chris@26: for (size_t i = 0; i < count; ++i) { Chris@26: buffer[0][i] /= channels; Chris@26: } Chris@26: } Chris@26: Chris@41: applyAuditioningEffect(count, buffer); Chris@41: Chris@16: m_condition.wakeAll(); Chris@12: Chris@0: return count; Chris@0: } Chris@0: Chris@41: void Chris@41: AudioCallbackPlaySource::applyAuditioningEffect(size_t count, float **buffers) Chris@41: { Chris@42: if (m_auditioningPluginBypassed) return; Chris@41: RealTimePluginInstance *plugin = m_auditioningPlugin; Chris@41: if (!plugin) return; Chris@41: Chris@41: if (plugin->getAudioInputCount() != getTargetChannelCount()) { Chris@43: // std::cerr << "plugin input count " << plugin->getAudioInputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@43: // << std::endl; Chris@41: return; Chris@41: } Chris@41: if (plugin->getAudioOutputCount() != getTargetChannelCount()) { Chris@43: // std::cerr << "plugin output count " << plugin->getAudioOutputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@43: // << std::endl; Chris@41: return; Chris@41: } Chris@41: if (plugin->getBufferSize() != count) { Chris@43: // std::cerr << "plugin buffer size " << plugin->getBufferSize() Chris@43: // << " != our block size " << count Chris@43: // << std::endl; Chris@41: return; Chris@41: } Chris@41: Chris@41: float **ib = plugin->getAudioInputBuffers(); Chris@41: float **ob = plugin->getAudioOutputBuffers(); Chris@41: Chris@41: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@41: for (size_t i = 0; i < count; ++i) { Chris@41: ib[c][i] = buffers[c][i]; Chris@41: } Chris@41: } Chris@41: Chris@41: plugin->run(Vamp::RealTime::zeroTime); Chris@41: Chris@41: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@41: for (size_t i = 0; i < count; ++i) { Chris@41: buffers[c][i] = ob[c][i]; Chris@41: } Chris@41: } Chris@41: } Chris@41: Chris@0: // Called from fill thread, m_playing true, mutex held Chris@0: bool Chris@0: AudioCallbackPlaySource::fillBuffers() Chris@0: { Chris@0: static float *tmp = 0; Chris@0: static size_t tmpSize = 0; Chris@0: Chris@0: size_t space = 0; Chris@0: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@0: RingBuffer *wb = getWriteRingBuffer(c); Chris@0: if (wb) { Chris@0: size_t spaceHere = wb->getWriteSpace(); Chris@0: if (c == 0 || spaceHere < space) space = spaceHere; Chris@0: } Chris@0: } Chris@0: Chris@0: if (space == 0) return false; Chris@0: Chris@0: size_t f = m_writeBufferFill; Chris@0: Chris@0: bool readWriteEqual = (m_readBuffers == m_writeBuffers); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl; Chris@0: #endif Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "buffered to " << f << " already" << std::endl; Chris@0: #endif Chris@0: Chris@0: bool resample = (getSourceSampleRate() != getTargetSampleRate()); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl; Chris@0: #endif Chris@0: Chris@0: size_t channels = getTargetChannelCount(); Chris@0: Chris@0: size_t orig = space; Chris@0: size_t got = 0; Chris@0: Chris@0: static float **bufferPtrs = 0; Chris@0: static size_t bufferPtrCount = 0; Chris@0: Chris@0: if (bufferPtrCount < channels) { Chris@0: if (bufferPtrs) delete[] bufferPtrs; Chris@0: bufferPtrs = new float *[channels]; Chris@0: bufferPtrCount = channels; Chris@0: } Chris@0: Chris@0: size_t generatorBlockSize = m_audioGenerator->getBlockSize(); Chris@0: Chris@0: if (resample && !m_converter) { Chris@0: static bool warned = false; Chris@0: if (!warned) { Chris@0: std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl; Chris@0: warned = true; Chris@0: } Chris@0: } Chris@0: Chris@0: if (resample && m_converter) { Chris@0: Chris@0: double ratio = Chris@0: double(getTargetSampleRate()) / double(getSourceSampleRate()); Chris@0: orig = size_t(orig / ratio + 0.1); Chris@0: Chris@0: // orig must be a multiple of generatorBlockSize Chris@0: orig = (orig / generatorBlockSize) * generatorBlockSize; Chris@0: if (orig == 0) return false; Chris@0: Chris@0: size_t work = std::max(orig, space); Chris@0: Chris@0: // We only allocate one buffer, but we use it in two halves. Chris@0: // We place the non-interleaved values in the second half of Chris@0: // the buffer (orig samples for channel 0, orig samples for Chris@0: // channel 1 etc), and then interleave them into the first Chris@0: // half of the buffer. Then we resample back into the second Chris@0: // half (interleaved) and de-interleave the results back to Chris@0: // the start of the buffer for insertion into the ringbuffers. Chris@0: // What a faff -- especially as we've already de-interleaved Chris@0: // the audio data from the source file elsewhere before we Chris@0: // even reach this point. Chris@0: Chris@0: if (tmpSize < channels * work * 2) { Chris@0: delete[] tmp; Chris@0: tmp = new float[channels * work * 2]; Chris@0: tmpSize = channels * work * 2; Chris@0: } Chris@0: Chris@0: float *nonintlv = tmp + channels * work; Chris@0: float *intlv = tmp; Chris@0: float *srcout = tmp + channels * work; Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < orig; ++i) { Chris@0: nonintlv[channels * i + c] = 0.0f; Chris@0: } Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: bufferPtrs[c] = nonintlv + c * orig; Chris@0: } Chris@0: Chris@0: got = mixModels(f, orig, bufferPtrs); Chris@0: Chris@0: // and interleave into first half Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < got; ++i) { Chris@0: float sample = nonintlv[c * got + i]; Chris@0: intlv[channels * i + c] = sample; Chris@0: } Chris@0: } Chris@0: Chris@0: SRC_DATA data; Chris@0: data.data_in = intlv; Chris@0: data.data_out = srcout; Chris@0: data.input_frames = got; Chris@0: data.output_frames = work; Chris@0: data.src_ratio = ratio; Chris@0: data.end_of_input = 0; Chris@0: Chris@32: int err = 0; Chris@32: Chris@32: if (m_timeStretcher && m_timeStretcher->getRatio() < 0.4) { Chris@32: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Using crappy converter" << std::endl; Chris@32: #endif Chris@32: src_process(m_crapConverter, &data); Chris@32: } else { Chris@32: src_process(m_converter, &data); Chris@32: } Chris@32: Chris@0: size_t toCopy = size_t(got * ratio + 0.1); Chris@0: Chris@0: if (err) { Chris@0: std::cerr Chris@0: << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: " Chris@0: << src_strerror(err) << std::endl; Chris@0: //!!! Then what? Chris@0: } else { Chris@0: got = data.input_frames_used; Chris@0: toCopy = data.output_frames_gen; Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < toCopy; ++i) { Chris@0: tmp[i] = srcout[channels * i + c]; Chris@0: } Chris@0: RingBuffer *wb = getWriteRingBuffer(c); Chris@0: if (wb) wb->write(tmp, toCopy); Chris@0: } Chris@0: Chris@0: m_writeBufferFill = f; Chris@0: if (readWriteEqual) m_readBufferFill = f; Chris@0: Chris@0: } else { Chris@0: Chris@0: // space must be a multiple of generatorBlockSize Chris@0: space = (space / generatorBlockSize) * generatorBlockSize; Chris@0: if (space == 0) return false; Chris@0: Chris@0: if (tmpSize < channels * space) { Chris@0: delete[] tmp; Chris@0: tmp = new float[channels * space]; Chris@0: tmpSize = channels * space; Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: Chris@0: bufferPtrs[c] = tmp + c * space; Chris@0: Chris@0: for (size_t i = 0; i < space; ++i) { Chris@0: tmp[c * space + i] = 0.0f; Chris@0: } Chris@0: } Chris@0: Chris@0: size_t got = mixModels(f, space, bufferPtrs); Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: Chris@0: RingBuffer *wb = getWriteRingBuffer(c); Chris@106: if (wb) { Chris@106: size_t actual = wb->write(bufferPtrs[c], got); Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Wrote " << actual << " samples for ch " << c << ", now " Chris@0: << wb->getReadSpace() << " to read" Chris@0: << std::endl; Chris@0: #endif Chris@106: if (actual < got) { Chris@106: std::cerr << "WARNING: Buffer overrun in channel " << c Chris@106: << ": wrote " << actual << " of " << got Chris@106: << " samples" << std::endl; Chris@106: } Chris@106: } Chris@0: } Chris@0: Chris@0: m_writeBufferFill = f; Chris@0: if (readWriteEqual) m_readBufferFill = f; Chris@0: Chris@0: //!!! how do we know when ended? need to mark up a fully-buffered flag and check this if we find the buffers empty in getSourceSamples Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers) Chris@0: { Chris@0: size_t processed = 0; Chris@0: size_t chunkStart = frame; Chris@0: size_t chunkSize = count; Chris@0: size_t selectionSize = 0; Chris@0: size_t nextChunkStart = chunkStart + chunkSize; Chris@0: Chris@0: bool looping = m_viewManager->getPlayLoopMode(); Chris@0: bool constrained = (m_viewManager->getPlaySelectionMode() && Chris@0: !m_viewManager->getSelections().empty()); Chris@0: Chris@0: static float **chunkBufferPtrs = 0; Chris@0: static size_t chunkBufferPtrCount = 0; Chris@0: size_t channels = getTargetChannelCount(); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl; Chris@0: #endif Chris@0: Chris@0: if (chunkBufferPtrCount < channels) { Chris@0: if (chunkBufferPtrs) delete[] chunkBufferPtrs; Chris@0: chunkBufferPtrs = new float *[channels]; Chris@0: chunkBufferPtrCount = channels; Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: chunkBufferPtrs[c] = buffers[c]; Chris@0: } Chris@0: Chris@0: while (processed < count) { Chris@0: Chris@0: chunkSize = count - processed; Chris@0: nextChunkStart = chunkStart + chunkSize; Chris@0: selectionSize = 0; Chris@0: Chris@0: size_t fadeIn = 0, fadeOut = 0; Chris@0: Chris@0: if (constrained) { Chris@0: Chris@0: Selection selection = Chris@0: m_viewManager->getContainingSelection(chunkStart, true); Chris@0: Chris@0: if (selection.isEmpty()) { Chris@0: if (looping) { Chris@0: selection = *m_viewManager->getSelections().begin(); Chris@0: chunkStart = selection.getStartFrame(); Chris@0: fadeIn = 50; Chris@0: } Chris@0: } Chris@0: Chris@0: if (selection.isEmpty()) { Chris@0: Chris@0: chunkSize = 0; Chris@0: nextChunkStart = chunkStart; Chris@0: Chris@0: } else { Chris@0: Chris@0: selectionSize = Chris@0: selection.getEndFrame() - Chris@0: selection.getStartFrame(); Chris@0: Chris@0: if (chunkStart < selection.getStartFrame()) { Chris@0: chunkStart = selection.getStartFrame(); Chris@0: fadeIn = 50; Chris@0: } Chris@0: Chris@0: nextChunkStart = chunkStart + chunkSize; Chris@0: Chris@0: if (nextChunkStart >= selection.getEndFrame()) { Chris@0: nextChunkStart = selection.getEndFrame(); Chris@0: fadeOut = 50; Chris@0: } Chris@0: Chris@0: chunkSize = nextChunkStart - chunkStart; Chris@0: } Chris@0: Chris@0: } else if (looping && m_lastModelEndFrame > 0) { Chris@0: Chris@0: if (chunkStart >= m_lastModelEndFrame) { Chris@0: chunkStart = 0; Chris@0: } Chris@0: if (chunkSize > m_lastModelEndFrame - chunkStart) { Chris@0: chunkSize = m_lastModelEndFrame - chunkStart; Chris@0: } Chris@0: nextChunkStart = chunkStart + chunkSize; Chris@0: } Chris@0: Chris@106: // std::cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl; Chris@0: Chris@0: if (!chunkSize) { Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Ending selection playback at " << nextChunkStart << std::endl; Chris@0: #endif Chris@0: // We need to maintain full buffers so that the other Chris@0: // thread can tell where it's got to in the playback -- so Chris@0: // return the full amount here Chris@0: frame = frame + count; Chris@0: return count; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl; Chris@0: #endif Chris@0: Chris@0: size_t got = 0; Chris@0: Chris@0: if (selectionSize < 100) { Chris@0: fadeIn = 0; Chris@0: fadeOut = 0; Chris@0: } else if (selectionSize < 300) { Chris@0: if (fadeIn > 0) fadeIn = 10; Chris@0: if (fadeOut > 0) fadeOut = 10; Chris@0: } Chris@0: Chris@0: if (fadeIn > 0) { Chris@0: if (processed * 2 < fadeIn) { Chris@0: fadeIn = processed * 2; Chris@0: } Chris@0: } Chris@0: Chris@0: if (fadeOut > 0) { Chris@0: if ((count - processed - chunkSize) * 2 < fadeOut) { Chris@0: fadeOut = (count - processed - chunkSize) * 2; Chris@0: } Chris@0: } Chris@0: Chris@0: for (std::set::iterator mi = m_models.begin(); Chris@0: mi != m_models.end(); ++mi) { Chris@0: Chris@0: got = m_audioGenerator->mixModel(*mi, chunkStart, Chris@0: chunkSize, chunkBufferPtrs, Chris@0: fadeIn, fadeOut); Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: chunkBufferPtrs[c] += chunkSize; Chris@0: } Chris@0: Chris@0: processed += chunkSize; Chris@0: chunkStart = nextChunkStart; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl; Chris@0: #endif Chris@0: Chris@0: frame = nextChunkStart; Chris@0: return processed; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::unifyRingBuffers() Chris@0: { Chris@0: if (m_readBuffers == m_writeBuffers) return; Chris@0: Chris@0: // only unify if there will be something to read Chris@0: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@0: RingBuffer *wb = getWriteRingBuffer(c); Chris@0: if (wb) { Chris@0: if (wb->getReadSpace() < m_blockSize * 2) { Chris@0: if ((m_writeBufferFill + m_blockSize * 2) < Chris@0: m_lastModelEndFrame) { Chris@0: // OK, we don't have enough and there's more to Chris@0: // read -- don't unify until we can do better Chris@0: return; Chris@0: } Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: size_t rf = m_readBufferFill; Chris@0: RingBuffer *rb = getReadRingBuffer(0); Chris@0: if (rb) { Chris@0: size_t rs = rb->getReadSpace(); Chris@0: //!!! incorrect when in non-contiguous selection, see comments elsewhere Chris@106: // std::cout << "rs = " << rs << std::endl; Chris@0: if (rs < rf) rf -= rs; Chris@0: else rf = 0; Chris@0: } Chris@0: Chris@106: //std::cout << "m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl; Chris@0: Chris@0: size_t wf = m_writeBufferFill; Chris@0: size_t skip = 0; Chris@0: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@0: RingBuffer *wb = getWriteRingBuffer(c); Chris@0: if (wb) { Chris@0: if (c == 0) { Chris@0: Chris@0: size_t wrs = wb->getReadSpace(); Chris@106: // std::cout << "wrs = " << wrs << std::endl; Chris@0: Chris@0: if (wrs < wf) wf -= wrs; Chris@0: else wf = 0; Chris@106: // std::cout << "wf = " << wf << std::endl; Chris@0: Chris@0: if (wf < rf) skip = rf - wf; Chris@0: if (skip == 0) break; Chris@0: } Chris@0: Chris@106: // std::cout << "skipping " << skip << std::endl; Chris@0: wb->skip(skip); Chris@0: } Chris@0: } Chris@0: Chris@0: m_bufferScavenger.claim(m_readBuffers); Chris@0: m_readBuffers = m_writeBuffers; Chris@0: m_readBufferFill = m_writeBufferFill; Chris@106: // std::cout << "unified" << std::endl; Chris@0: } Chris@0: Chris@0: void Chris@127: AudioCallbackPlaySource::FillThread::run() Chris@0: { Chris@0: AudioCallbackPlaySource &s(m_source); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: std::cout << "AudioCallbackPlaySourceFillThread starting" << std::endl; Chris@0: #endif Chris@0: Chris@0: s.m_mutex.lock(); Chris@0: Chris@0: bool previouslyPlaying = s.m_playing; Chris@0: bool work = false; Chris@0: Chris@0: while (!s.m_exiting) { Chris@0: Chris@0: s.unifyRingBuffers(); Chris@0: s.m_bufferScavenger.scavenge(); Chris@41: s.m_pluginScavenger.scavenge(); Chris@0: s.m_timeStretcherScavenger.scavenge(); Chris@0: Chris@0: if (work && s.m_playing && s.getSourceSampleRate()) { Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl; Chris@0: #endif Chris@0: Chris@0: s.m_mutex.unlock(); Chris@0: s.m_mutex.lock(); Chris@0: Chris@0: } else { Chris@0: Chris@0: float ms = 100; Chris@0: if (s.getSourceSampleRate() > 0) { Chris@0: ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0; Chris@0: } Chris@0: Chris@0: if (s.m_playing) ms /= 10; Chris@106: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@106: if (!s.m_playing) std::cout << std::endl; Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl; Chris@0: #endif Chris@0: Chris@0: s.m_condition.wait(&s.m_mutex, size_t(ms)); Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl; Chris@0: #endif Chris@0: Chris@0: work = false; Chris@0: Chris@0: if (!s.getSourceSampleRate()) continue; Chris@0: Chris@0: bool playing = s.m_playing; Chris@0: Chris@0: if (playing && !previouslyPlaying) { Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl; Chris@0: #endif Chris@0: for (size_t c = 0; c < s.getTargetChannelCount(); ++c) { Chris@0: RingBuffer *rb = s.getReadRingBuffer(c); Chris@0: if (rb) rb->reset(); Chris@0: } Chris@0: } Chris@0: previouslyPlaying = playing; Chris@0: Chris@0: work = s.fillBuffers(); Chris@0: } Chris@0: Chris@0: s.m_mutex.unlock(); Chris@0: } Chris@0: