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 and QMUL. 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 "AudioCallbackPlaySource.h" Chris@43: Chris@43: #include "AudioGenerator.h" Chris@43: Chris@43: #include "data/model/Model.h" Chris@43: #include "view/ViewManager.h" Chris@43: #include "base/PlayParameterRepository.h" Chris@43: #include "base/Preferences.h" Chris@43: #include "data/model/DenseTimeValueModel.h" Chris@43: #include "data/model/WaveFileModel.h" Chris@43: #include "data/model/SparseOneDimensionalModel.h" Chris@43: #include "plugin/RealTimePluginInstance.h" Chris@62: Chris@91: #include "AudioCallbackPlayTarget.h" Chris@91: Chris@62: #include Chris@62: using namespace RubberBand; Chris@43: Chris@43: #include Chris@43: #include Chris@43: Chris@43: //#define DEBUG_AUDIO_PLAY_SOURCE 1 Chris@43: //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1 Chris@43: Chris@43: const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071; Chris@43: Chris@57: AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager, Chris@57: QString clientName) : Chris@43: m_viewManager(manager), Chris@43: m_audioGenerator(new AudioGenerator()), Chris@57: m_clientName(clientName), Chris@43: m_readBuffers(0), Chris@43: m_writeBuffers(0), Chris@43: m_readBufferFill(0), Chris@43: m_writeBufferFill(0), Chris@43: m_bufferScavenger(1), Chris@43: m_sourceChannelCount(0), Chris@43: m_blockSize(1024), Chris@43: m_sourceSampleRate(0), Chris@43: m_targetSampleRate(0), Chris@43: m_playLatency(0), Chris@91: m_target(0), Chris@91: m_lastRetrievalTimestamp(0.0), Chris@91: m_lastRetrievedBlockSize(0), Chris@43: m_playing(false), Chris@43: m_exiting(false), Chris@43: m_lastModelEndFrame(0), Chris@43: m_outputLeft(0.0), Chris@43: m_outputRight(0.0), Chris@43: m_auditioningPlugin(0), Chris@43: m_auditioningPluginBypassed(false), Chris@94: m_playStartFrame(0), Chris@94: m_playStartFramePassed(false), Chris@43: m_timeStretcher(0), Chris@91: m_stretchRatio(1.0), Chris@91: m_stretcherInputCount(0), Chris@91: m_stretcherInputs(0), Chris@91: m_stretcherInputSizes(0), Chris@43: m_fillThread(0), Chris@43: m_converter(0), Chris@43: m_crapConverter(0), Chris@43: m_resampleQuality(Preferences::getInstance()->getResampleQuality()) Chris@43: { Chris@43: m_viewManager->setAudioPlaySource(this); Chris@43: Chris@43: connect(m_viewManager, SIGNAL(selectionChanged()), Chris@43: this, SLOT(selectionChanged())); Chris@43: connect(m_viewManager, SIGNAL(playLoopModeChanged()), Chris@43: this, SLOT(playLoopModeChanged())); Chris@43: connect(m_viewManager, SIGNAL(playSelectionModeChanged()), Chris@43: this, SLOT(playSelectionModeChanged())); Chris@43: Chris@43: connect(PlayParameterRepository::getInstance(), Chris@43: SIGNAL(playParametersChanged(PlayParameters *)), Chris@43: this, SLOT(playParametersChanged(PlayParameters *))); Chris@43: Chris@43: connect(Preferences::getInstance(), Chris@43: SIGNAL(propertyChanged(PropertyContainer::PropertyName)), Chris@43: this, SLOT(preferenceChanged(PropertyContainer::PropertyName))); Chris@43: } Chris@43: Chris@43: AudioCallbackPlaySource::~AudioCallbackPlaySource() Chris@43: { Chris@43: m_exiting = true; Chris@43: Chris@43: if (m_fillThread) { Chris@43: m_condition.wakeAll(); Chris@43: m_fillThread->wait(); Chris@43: delete m_fillThread; Chris@43: } Chris@43: Chris@43: clearModels(); Chris@43: Chris@43: if (m_readBuffers != m_writeBuffers) { Chris@43: delete m_readBuffers; Chris@43: } Chris@43: Chris@43: delete m_writeBuffers; Chris@43: Chris@43: delete m_audioGenerator; Chris@43: Chris@91: for (size_t i = 0; i < m_stretcherInputCount; ++i) { Chris@91: delete[] m_stretcherInputs[i]; Chris@91: } Chris@91: delete[] m_stretcherInputSizes; Chris@91: delete[] m_stretcherInputs; Chris@91: Chris@43: m_bufferScavenger.scavenge(true); Chris@43: m_pluginScavenger.scavenge(true); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::addModel(Model *model) Chris@43: { Chris@43: if (m_models.find(model) != m_models.end()) return; Chris@43: Chris@43: bool canPlay = m_audioGenerator->addModel(model); Chris@43: Chris@43: m_mutex.lock(); Chris@43: Chris@43: m_models.insert(model); Chris@43: if (model->getEndFrame() > m_lastModelEndFrame) { Chris@43: m_lastModelEndFrame = model->getEndFrame(); Chris@43: } Chris@43: Chris@43: bool buffersChanged = false, srChanged = false; Chris@43: Chris@43: size_t modelChannels = 1; Chris@43: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@43: if (dtvm) modelChannels = dtvm->getChannelCount(); Chris@43: if (modelChannels > m_sourceChannelCount) { Chris@43: m_sourceChannelCount = modelChannels; Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Adding model with " << modelChannels << " channels " << std::endl; Chris@43: #endif Chris@43: Chris@43: if (m_sourceSampleRate == 0) { Chris@43: Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: srChanged = true; Chris@43: Chris@43: } else if (model->getSampleRate() != m_sourceSampleRate) { Chris@43: Chris@43: // If this is a dense time-value model and we have no other, we Chris@43: // can just switch to this model's sample rate Chris@43: Chris@43: if (dtvm) { Chris@43: Chris@43: bool conflicting = false; Chris@43: Chris@43: for (std::set::const_iterator i = m_models.begin(); Chris@43: i != m_models.end(); ++i) { Chris@43: // Only wave file models can be considered conflicting -- Chris@43: // writable wave file models are derived and we shouldn't Chris@43: // take their rates into account. Also, don't give any Chris@43: // particular weight to a file that's already playing at Chris@43: // the wrong rate anyway Chris@43: WaveFileModel *wfm = dynamic_cast(*i); Chris@43: if (wfm && wfm != dtvm && Chris@43: wfm->getSampleRate() != model->getSampleRate() && Chris@43: wfm->getSampleRate() == m_sourceSampleRate) { Chris@43: std::cerr << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << *i << " found" << std::endl; Chris@43: conflicting = true; Chris@43: break; Chris@43: } Chris@43: } Chris@43: Chris@43: if (conflicting) { Chris@43: Chris@43: std::cerr << "AudioCallbackPlaySource::addModel: ERROR: " Chris@43: << "New model sample rate does not match" << std::endl Chris@43: << "existing model(s) (new " << model->getSampleRate() Chris@43: << " vs " << m_sourceSampleRate Chris@43: << "), playback will be wrong" Chris@43: << std::endl; Chris@43: Chris@43: emit sampleRateMismatch(model->getSampleRate(), Chris@43: m_sourceSampleRate, Chris@43: false); Chris@43: } else { Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: srChanged = true; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) { Chris@43: clearRingBuffers(true, getTargetChannelCount()); Chris@43: buffersChanged = true; Chris@43: } else { Chris@43: if (canPlay) clearRingBuffers(true); Chris@43: } Chris@43: Chris@43: if (buffersChanged || srChanged) { Chris@43: if (m_converter) { Chris@43: src_delete(m_converter); Chris@43: src_delete(m_crapConverter); Chris@43: m_converter = 0; Chris@43: m_crapConverter = 0; Chris@43: } Chris@43: } Chris@43: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: m_audioGenerator->setTargetChannelCount(getTargetChannelCount()); Chris@43: Chris@43: if (!m_fillThread) { Chris@43: m_fillThread = new FillThread(*this); Chris@43: m_fillThread->start(); Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s) -- emitting modelReplaced" << std::endl; Chris@43: #endif Chris@43: Chris@43: if (buffersChanged || srChanged) { Chris@43: emit modelReplaced(); Chris@43: } Chris@43: Chris@43: connect(model, SIGNAL(modelChanged(size_t, size_t)), Chris@43: this, SLOT(modelChanged(size_t, size_t))); Chris@43: Chris@43: m_condition.wakeAll(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::modelChanged(size_t startFrame, size_t endFrame) Chris@43: { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cerr << "AudioCallbackPlaySource::modelChanged(" << startFrame << "," << endFrame << ")" << std::endl; Chris@43: #endif Chris@93: if (endFrame > m_lastModelEndFrame) { Chris@93: m_lastModelEndFrame = endFrame; Chris@93: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::removeModel(Model *model) Chris@43: { Chris@43: m_mutex.lock(); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << std::endl; Chris@43: #endif Chris@43: Chris@43: disconnect(model, SIGNAL(modelChanged(size_t, size_t)), Chris@43: this, SLOT(modelChanged(size_t, size_t))); Chris@43: Chris@43: m_models.erase(model); Chris@43: Chris@43: if (m_models.empty()) { Chris@43: if (m_converter) { Chris@43: src_delete(m_converter); Chris@43: src_delete(m_crapConverter); Chris@43: m_converter = 0; Chris@43: m_crapConverter = 0; Chris@43: } Chris@43: m_sourceSampleRate = 0; Chris@43: } Chris@43: Chris@43: size_t lastEnd = 0; Chris@43: for (std::set::const_iterator i = m_models.begin(); Chris@43: i != m_models.end(); ++i) { Chris@43: // std::cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl; Chris@43: if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame(); Chris@43: // std::cout << "(done, lastEnd now " << lastEnd << ")" << std::endl; Chris@43: } Chris@43: m_lastModelEndFrame = lastEnd; Chris@43: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: m_audioGenerator->removeModel(model); Chris@43: Chris@43: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::clearModels() Chris@43: { Chris@43: m_mutex.lock(); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySource::clearModels()" << std::endl; Chris@43: #endif Chris@43: Chris@43: m_models.clear(); Chris@43: Chris@43: if (m_converter) { Chris@43: src_delete(m_converter); Chris@43: src_delete(m_crapConverter); Chris@43: m_converter = 0; Chris@43: m_crapConverter = 0; Chris@43: } Chris@43: Chris@43: m_lastModelEndFrame = 0; Chris@43: Chris@43: m_sourceSampleRate = 0; Chris@43: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: m_audioGenerator->clearModels(); Chris@93: Chris@93: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count) Chris@43: { Chris@43: if (!haveLock) m_mutex.lock(); Chris@43: Chris@93: rebuildRangeLists(); Chris@93: Chris@43: if (count == 0) { Chris@43: if (m_writeBuffers) count = m_writeBuffers->size(); Chris@43: } Chris@43: Chris@93: m_writeBufferFill = getCurrentBufferedFrame(); Chris@43: Chris@43: if (m_readBuffers != m_writeBuffers) { Chris@43: delete m_writeBuffers; Chris@43: } Chris@43: Chris@43: m_writeBuffers = new RingBufferVector; Chris@43: Chris@43: for (size_t i = 0; i < count; ++i) { Chris@43: m_writeBuffers->push_back(new RingBuffer(m_ringBufferSize)); Chris@43: } Chris@43: Chris@43: // std::cout << "AudioCallbackPlaySource::clearRingBuffers: Created " Chris@43: // << count << " write buffers" << std::endl; Chris@43: Chris@43: if (!haveLock) { Chris@43: m_mutex.unlock(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::play(size_t startFrame) Chris@43: { Chris@43: if (m_viewManager->getPlaySelectionMode() && Chris@43: !m_viewManager->getSelections().empty()) { Chris@60: Chris@94: std::cerr << "AudioCallbackPlaySource::play: constraining frame " << startFrame << " to selection = "; Chris@94: Chris@60: startFrame = m_viewManager->constrainFrameToSelection(startFrame); Chris@60: Chris@94: std::cerr << startFrame << std::endl; Chris@94: Chris@43: } else { Chris@43: if (startFrame >= m_lastModelEndFrame) { Chris@43: startFrame = 0; Chris@43: } Chris@43: } Chris@43: Chris@60: std::cerr << "play(" << startFrame << ") -> playback model "; Chris@60: Chris@60: startFrame = m_viewManager->alignReferenceToPlaybackFrame(startFrame); Chris@60: Chris@60: std::cerr << startFrame << std::endl; Chris@60: Chris@43: // The fill thread will automatically empty its buffers before Chris@43: // starting again if we have not so far been playing, but not if Chris@43: // we're just re-seeking. Chris@43: Chris@43: m_mutex.lock(); Chris@91: if (m_timeStretcher) { Chris@91: m_timeStretcher->reset(); Chris@91: } Chris@43: if (m_playing) { Chris@93: std::cerr << "playing already, resetting" << std::endl; Chris@43: m_readBufferFill = m_writeBufferFill = startFrame; Chris@43: if (m_readBuffers) { Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *rb = getReadRingBuffer(c); Chris@93: std::cerr << "reset ring buffer for channel " << c << std::endl; Chris@43: if (rb) rb->reset(); Chris@43: } Chris@43: } Chris@43: if (m_converter) src_reset(m_converter); Chris@43: if (m_crapConverter) src_reset(m_crapConverter); Chris@43: } else { Chris@43: if (m_converter) src_reset(m_converter); Chris@43: if (m_crapConverter) src_reset(m_crapConverter); Chris@43: m_readBufferFill = m_writeBufferFill = startFrame; Chris@43: } Chris@43: m_mutex.unlock(); Chris@43: Chris@43: m_audioGenerator->reset(); Chris@43: Chris@94: m_playStartFrame = startFrame; Chris@94: m_playStartFramePassed = false; Chris@94: m_playStartedAt = RealTime::zeroTime; Chris@94: if (m_target) { Chris@94: m_playStartedAt = RealTime::fromSeconds(m_target->getCurrentTime()); Chris@94: } Chris@94: Chris@43: bool changed = !m_playing; Chris@91: m_lastRetrievalTimestamp = 0; Chris@43: m_playing = true; Chris@43: m_condition.wakeAll(); Chris@43: if (changed) emit playStatusChanged(m_playing); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::stop() Chris@43: { Chris@43: bool changed = m_playing; Chris@43: m_playing = false; Chris@43: m_condition.wakeAll(); Chris@91: m_lastRetrievalTimestamp = 0; Chris@43: if (changed) emit playStatusChanged(m_playing); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::selectionChanged() Chris@43: { Chris@43: if (m_viewManager->getPlaySelectionMode()) { Chris@43: clearRingBuffers(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::playLoopModeChanged() Chris@43: { Chris@43: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::playSelectionModeChanged() Chris@43: { Chris@43: if (!m_viewManager->getSelections().empty()) { Chris@43: clearRingBuffers(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::playParametersChanged(PlayParameters *) Chris@43: { Chris@43: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n) Chris@43: { Chris@43: if (n == "Resample Quality") { Chris@43: setResampleQuality(Preferences::getInstance()->getResampleQuality()); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::audioProcessingOverload() Chris@43: { Chris@43: RealTimePluginInstance *ap = m_auditioningPlugin; Chris@43: if (ap && m_playing && !m_auditioningPluginBypassed) { Chris@43: m_auditioningPluginBypassed = true; Chris@43: emit audioOverloadPluginDisabled(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@91: AudioCallbackPlaySource::setTarget(AudioCallbackPlayTarget *target, size_t size) Chris@43: { Chris@91: m_target = target; Chris@43: // std::cout << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl; Chris@43: assert(size < m_ringBufferSize); Chris@43: m_blockSize = size; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getTargetBlockSize() const Chris@43: { Chris@43: // std::cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl; Chris@43: return m_blockSize; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setTargetPlayLatency(size_t latency) Chris@43: { Chris@43: m_playLatency = latency; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getTargetPlayLatency() const Chris@43: { Chris@43: return m_playLatency; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getCurrentPlayingFrame() Chris@43: { Chris@91: // This method attempts to estimate which audio sample frame is Chris@91: // "currently coming through the speakers". Chris@91: Chris@93: size_t targetRate = getTargetSampleRate(); Chris@93: size_t latency = m_playLatency; // at target rate Chris@93: RealTime latency_t = RealTime::frame2RealTime(latency, targetRate); Chris@93: Chris@93: return getCurrentFrame(latency_t); Chris@93: } Chris@93: Chris@93: size_t Chris@93: AudioCallbackPlaySource::getCurrentBufferedFrame() Chris@93: { Chris@93: return getCurrentFrame(RealTime::zeroTime); Chris@93: } Chris@93: Chris@93: size_t Chris@93: AudioCallbackPlaySource::getCurrentFrame(RealTime latency_t) Chris@93: { Chris@43: bool resample = false; Chris@91: double resampleRatio = 1.0; Chris@43: Chris@91: // We resample when filling the ring buffer, and time-stretch when Chris@91: // draining it. The buffer contains data at the "target rate" and Chris@91: // the latency provided by the target is also at the target rate. Chris@91: // Because of the multiple rates involved, we do the actual Chris@91: // calculation using RealTime instead. Chris@43: Chris@91: size_t sourceRate = getSourceSampleRate(); Chris@91: size_t targetRate = getTargetSampleRate(); Chris@91: Chris@91: if (sourceRate == 0 || targetRate == 0) return 0; Chris@91: Chris@91: size_t inbuffer = 0; // at target rate Chris@91: Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *rb = getReadRingBuffer(c); Chris@43: if (rb) { Chris@91: size_t here = rb->getReadSpace(); Chris@91: if (c == 0 || here < inbuffer) inbuffer = here; Chris@43: } Chris@43: } Chris@43: Chris@91: size_t readBufferFill = m_readBufferFill; Chris@91: size_t lastRetrievedBlockSize = m_lastRetrievedBlockSize; Chris@91: double lastRetrievalTimestamp = m_lastRetrievalTimestamp; Chris@91: double currentTime = 0.0; Chris@91: if (m_target) currentTime = m_target->getCurrentTime(); Chris@91: Chris@91: RealTime inbuffer_t = RealTime::frame2RealTime(inbuffer, targetRate); Chris@91: Chris@91: size_t stretchlat = 0; Chris@91: double timeRatio = 1.0; Chris@91: Chris@91: if (m_timeStretcher) { Chris@91: stretchlat = m_timeStretcher->getLatency(); Chris@91: timeRatio = m_timeStretcher->getTimeRatio(); Chris@43: } Chris@43: Chris@91: RealTime stretchlat_t = RealTime::frame2RealTime(stretchlat, targetRate); Chris@43: Chris@91: // When the target has just requested a block from us, the last Chris@91: // sample it obtained was our buffer fill frame count minus the Chris@91: // amount of read space (converted back to source sample rate) Chris@91: // remaining now. That sample is not expected to be played until Chris@91: // the target's play latency has elapsed. By the time the Chris@91: // following block is requested, that sample will be at the Chris@91: // target's play latency minus the last requested block size away Chris@91: // from being played. Chris@91: Chris@91: RealTime sincerequest_t = RealTime::zeroTime; Chris@91: RealTime lastretrieved_t = RealTime::zeroTime; Chris@91: Chris@91: if (m_target && lastRetrievalTimestamp != 0.0) { Chris@91: Chris@91: lastretrieved_t = RealTime::frame2RealTime Chris@91: (lastRetrievedBlockSize, targetRate); Chris@91: Chris@91: // calculate number of frames at target rate that have elapsed Chris@91: // since the end of the last call to getSourceSamples Chris@91: Chris@91: double elapsed = currentTime - lastRetrievalTimestamp; Chris@91: Chris@91: if (elapsed > 0.0) { Chris@91: sincerequest_t = RealTime::fromSeconds(elapsed); Chris@91: } Chris@91: Chris@91: } else { Chris@91: Chris@91: lastretrieved_t = RealTime::frame2RealTime Chris@91: (getTargetBlockSize(), targetRate); Chris@62: } Chris@91: Chris@91: RealTime bufferedto_t = RealTime::frame2RealTime(readBufferFill, sourceRate); Chris@91: Chris@91: if (timeRatio != 1.0) { Chris@91: lastretrieved_t = lastretrieved_t / timeRatio; Chris@91: sincerequest_t = sincerequest_t / timeRatio; Chris@43: } Chris@43: Chris@43: bool looping = m_viewManager->getPlayLoopMode(); Chris@43: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@91: std::cerr << "\nbuffered to: " << bufferedto_t << ", in buffer: " << inbuffer_t << ", time ratio " << timeRatio << "\n stretcher latency: " << stretchlat_t << ", device latency: " << latency_t << "\n since request: " << sincerequest_t << ", last retrieved: " << lastretrieved_t << std::endl; Chris@91: #endif Chris@43: Chris@91: RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate); Chris@60: Chris@93: // Normally the range lists should contain at least one item each Chris@93: // -- if playback is unconstrained, that item should report the Chris@93: // entire source audio duration. Chris@43: Chris@93: if (m_rangeStarts.empty()) { Chris@93: rebuildRangeLists(); Chris@93: } Chris@92: Chris@93: if (m_rangeStarts.empty()) { Chris@93: // this code is only used in case of error in rebuildRangeLists Chris@93: RealTime playing_t = bufferedto_t Chris@93: - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t Chris@93: + sincerequest_t; Chris@93: size_t frame = RealTime::realTime2Frame(playing_t, sourceRate); Chris@93: return m_viewManager->alignPlaybackFrameToReference(frame); Chris@93: } Chris@43: Chris@91: int inRange = 0; Chris@91: int index = 0; Chris@91: Chris@93: for (size_t i = 0; i < m_rangeStarts.size(); ++i) { Chris@93: if (bufferedto_t >= m_rangeStarts[i]) { Chris@93: inRange = index; Chris@93: } else { Chris@93: break; Chris@93: } Chris@93: ++index; Chris@93: } Chris@93: Chris@93: if (inRange >= m_rangeStarts.size()) inRange = m_rangeStarts.size()-1; Chris@93: Chris@94: RealTime playing_t = bufferedto_t; Chris@93: Chris@93: playing_t = playing_t Chris@93: - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t Chris@93: + sincerequest_t; Chris@94: Chris@94: // This rather gross little hack is used to ensure that latency Chris@94: // compensation doesn't result in the playback pointer appearing Chris@94: // to start earlier than the actual playback does. It doesn't Chris@94: // work properly (hence the bail-out in the middle) because if we Chris@94: // are playing a relatively short looped region, the playing time Chris@94: // estimated from the buffer fill frame may have wrapped around Chris@94: // the region boundary and end up being much smaller than the Chris@94: // theoretical play start frame, perhaps even for the entire Chris@94: // duration of playback! Chris@94: Chris@94: if (!m_playStartFramePassed) { Chris@94: RealTime playstart_t = RealTime::frame2RealTime(m_playStartFrame, Chris@94: sourceRate); Chris@94: if (playing_t < playstart_t) { Chris@94: // std::cerr << "playing_t " << playing_t << " < playstart_t " Chris@94: // << playstart_t << std::endl; Chris@94: if (sincerequest_t > RealTime::zeroTime && Chris@94: m_playStartedAt + latency_t + stretchlat_t < Chris@94: RealTime::fromSeconds(currentTime)) { Chris@94: // std::cerr << "but we've been playing for long enough that I think we should disregard it (it probably results from loop wrapping)" << std::endl; Chris@94: m_playStartFramePassed = true; Chris@94: } else { Chris@94: playing_t = playstart_t; Chris@94: } Chris@94: } else { Chris@94: m_playStartFramePassed = true; Chris@94: } Chris@94: } Chris@94: Chris@94: playing_t = playing_t - m_rangeStarts[inRange]; Chris@93: Chris@93: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@93: std::cerr << "playing_t as offset into range " << inRange << " (with start = " << m_rangeStarts[inRange] << ") = " << playing_t << std::endl; Chris@93: #endif Chris@93: Chris@93: while (playing_t < RealTime::zeroTime) { Chris@93: Chris@93: if (inRange == 0) { Chris@93: if (looping) { Chris@93: inRange = m_rangeStarts.size() - 1; Chris@93: } else { Chris@93: break; Chris@93: } Chris@93: } else { Chris@93: --inRange; Chris@93: } Chris@93: Chris@93: playing_t = playing_t + m_rangeDurations[inRange]; Chris@93: } Chris@93: Chris@93: playing_t = playing_t + m_rangeStarts[inRange]; Chris@93: Chris@93: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@93: std::cerr << " playing time: " << playing_t << std::endl; Chris@93: #endif Chris@93: Chris@93: if (!looping) { Chris@93: if (inRange == m_rangeStarts.size()-1 && Chris@93: playing_t >= m_rangeStarts[inRange] + m_rangeDurations[inRange]) { Chris@93: stop(); Chris@93: } Chris@93: } Chris@93: Chris@93: if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime; Chris@93: Chris@93: size_t frame = RealTime::realTime2Frame(playing_t, sourceRate); Chris@93: return m_viewManager->alignPlaybackFrameToReference(frame); Chris@93: } Chris@93: Chris@93: void Chris@93: AudioCallbackPlaySource::rebuildRangeLists() Chris@93: { Chris@93: bool constrained = (m_viewManager->getPlaySelectionMode()); Chris@93: Chris@93: m_rangeStarts.clear(); Chris@93: m_rangeDurations.clear(); Chris@93: Chris@93: size_t sourceRate = getSourceSampleRate(); Chris@93: if (sourceRate == 0) return; Chris@93: Chris@93: RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate); Chris@93: if (end == RealTime::zeroTime) return; Chris@93: Chris@93: if (!constrained) { Chris@93: m_rangeStarts.push_back(RealTime::zeroTime); Chris@93: m_rangeDurations.push_back(end); Chris@93: return; Chris@93: } Chris@93: Chris@93: MultiSelection::SelectionList selections = m_viewManager->getSelections(); Chris@93: MultiSelection::SelectionList::const_iterator i; Chris@93: Chris@93: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@93: std::cerr << "AudioCallbackPlaySource::rebuildRangeLists" << std::endl; Chris@93: #endif Chris@93: Chris@93: if (!selections.empty()) { Chris@91: Chris@91: for (i = selections.begin(); i != selections.end(); ++i) { Chris@91: Chris@91: RealTime start = Chris@91: (RealTime::frame2RealTime Chris@91: (m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()), Chris@91: sourceRate)); Chris@91: RealTime duration = Chris@91: (RealTime::frame2RealTime Chris@91: (m_viewManager->alignReferenceToPlaybackFrame(i->getEndFrame()) - Chris@91: m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()), Chris@91: sourceRate)); Chris@91: Chris@93: m_rangeStarts.push_back(start); Chris@93: m_rangeDurations.push_back(duration); Chris@91: } Chris@93: } else { Chris@93: m_rangeStarts.push_back(RealTime::zeroTime); Chris@93: m_rangeDurations.push_back(end); Chris@43: } Chris@43: Chris@93: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@93: std::cerr << "Now have " << m_rangeStarts.size() << " play ranges" << std::endl; Chris@91: #endif Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setOutputLevels(float left, float right) Chris@43: { Chris@43: m_outputLeft = left; Chris@43: m_outputRight = right; Chris@43: } Chris@43: Chris@43: bool Chris@43: AudioCallbackPlaySource::getOutputLevels(float &left, float &right) Chris@43: { Chris@43: left = m_outputLeft; Chris@43: right = m_outputRight; Chris@43: return true; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setTargetSampleRate(size_t sr) Chris@43: { Chris@43: m_targetSampleRate = sr; Chris@43: initialiseConverter(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::initialiseConverter() Chris@43: { Chris@43: m_mutex.lock(); Chris@43: Chris@43: if (m_converter) { Chris@43: src_delete(m_converter); Chris@43: src_delete(m_crapConverter); Chris@43: m_converter = 0; Chris@43: m_crapConverter = 0; Chris@43: } Chris@43: Chris@43: if (getSourceSampleRate() != getTargetSampleRate()) { Chris@43: Chris@43: int err = 0; Chris@43: Chris@43: m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY : Chris@43: m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY : Chris@43: m_resampleQuality == 0 ? SRC_SINC_FASTEST : Chris@43: SRC_SINC_MEDIUM_QUALITY, Chris@43: getTargetChannelCount(), &err); Chris@43: Chris@43: if (m_converter) { Chris@43: m_crapConverter = src_new(SRC_LINEAR, Chris@43: getTargetChannelCount(), Chris@43: &err); Chris@43: } Chris@43: Chris@43: if (!m_converter || !m_crapConverter) { Chris@43: std::cerr Chris@43: << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: " Chris@43: << src_strerror(err) << std::endl; Chris@43: Chris@43: if (m_converter) { Chris@43: src_delete(m_converter); Chris@43: m_converter = 0; Chris@43: } Chris@43: Chris@43: if (m_crapConverter) { Chris@43: src_delete(m_crapConverter); Chris@43: m_crapConverter = 0; Chris@43: } Chris@43: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: emit sampleRateMismatch(getSourceSampleRate(), Chris@43: getTargetSampleRate(), Chris@43: false); Chris@43: } else { Chris@43: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: emit sampleRateMismatch(getSourceSampleRate(), Chris@43: getTargetSampleRate(), Chris@43: true); Chris@43: } Chris@43: } else { Chris@43: m_mutex.unlock(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setResampleQuality(int q) Chris@43: { Chris@43: if (q == m_resampleQuality) return; Chris@43: m_resampleQuality = q; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cerr << "AudioCallbackPlaySource::setResampleQuality: setting to " Chris@43: << m_resampleQuality << std::endl; Chris@43: #endif Chris@43: Chris@43: initialiseConverter(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setAuditioningPlugin(RealTimePluginInstance *plugin) Chris@43: { Chris@43: RealTimePluginInstance *formerPlugin = m_auditioningPlugin; Chris@43: m_auditioningPlugin = plugin; Chris@43: m_auditioningPluginBypassed = false; Chris@43: if (formerPlugin) m_pluginScavenger.claim(formerPlugin); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setSoloModelSet(std::set s) Chris@43: { Chris@43: m_audioGenerator->setSoloModelSet(s); Chris@43: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::clearSoloModelSet() Chris@43: { Chris@43: m_audioGenerator->clearSoloModelSet(); Chris@43: clearRingBuffers(); Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getTargetSampleRate() const Chris@43: { Chris@43: if (m_targetSampleRate) return m_targetSampleRate; Chris@43: else return getSourceSampleRate(); Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getSourceChannelCount() const Chris@43: { Chris@43: return m_sourceChannelCount; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getTargetChannelCount() const Chris@43: { Chris@43: if (m_sourceChannelCount < 2) return 2; Chris@43: return m_sourceChannelCount; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getSourceSampleRate() const Chris@43: { Chris@43: return m_sourceSampleRate; Chris@43: } Chris@43: Chris@43: void Chris@91: AudioCallbackPlaySource::setTimeStretch(float factor) Chris@43: { Chris@91: m_stretchRatio = factor; Chris@91: Chris@91: if (m_timeStretcher || (factor == 1.f)) { Chris@91: // stretch ratio will be set in next process call if appropriate Chris@62: return; Chris@62: } else { Chris@91: m_stretcherInputCount = getTargetChannelCount(); Chris@62: RubberBandStretcher *stretcher = new RubberBandStretcher Chris@62: (getTargetSampleRate(), Chris@91: m_stretcherInputCount, Chris@62: RubberBandStretcher::OptionProcessRealTime, Chris@62: factor); Chris@91: m_stretcherInputs = new float *[m_stretcherInputCount]; Chris@91: m_stretcherInputSizes = new size_t[m_stretcherInputCount]; Chris@91: for (size_t c = 0; c < m_stretcherInputCount; ++c) { Chris@91: m_stretcherInputSizes[c] = 16384; Chris@91: m_stretcherInputs[c] = new float[m_stretcherInputSizes[c]]; Chris@91: } Chris@62: m_timeStretcher = stretcher; Chris@62: return; Chris@62: } Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer) Chris@43: { Chris@43: if (!m_playing) { Chris@43: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@43: for (size_t i = 0; i < count; ++i) { Chris@43: buffer[ch][i] = 0.0; Chris@43: } Chris@43: } Chris@43: return 0; Chris@43: } Chris@43: Chris@43: // Ensure that all buffers have at least the amount of data we Chris@43: // need -- else reduce the size of our requests correspondingly Chris@43: Chris@43: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@43: Chris@43: RingBuffer *rb = getReadRingBuffer(ch); Chris@43: Chris@43: if (!rb) { Chris@43: std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@43: << "No ring buffer available for channel " << ch Chris@43: << ", returning no data here" << std::endl; Chris@43: count = 0; Chris@43: break; Chris@43: } Chris@43: Chris@43: size_t rs = rb->getReadSpace(); Chris@43: if (rs < count) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@43: << "Ring buffer for channel " << ch << " has only " Chris@43: << rs << " (of " << count << ") samples available, " Chris@43: << "reducing request size" << std::endl; Chris@43: #endif Chris@43: count = rs; Chris@43: } Chris@43: } Chris@43: Chris@43: if (count == 0) return 0; Chris@43: Chris@62: RubberBandStretcher *ts = m_timeStretcher; Chris@62: float ratio = ts ? ts->getTimeRatio() : 1.f; Chris@91: Chris@91: if (ratio != m_stretchRatio) { Chris@91: if (!ts) { Chris@91: std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: Time ratio change to " << m_stretchRatio << " is pending, but no stretcher is set" << std::endl; Chris@91: m_stretchRatio = 1.f; Chris@91: } else { Chris@91: ts->setTimeRatio(m_stretchRatio); Chris@91: } Chris@91: } Chris@91: Chris@91: if (m_target) { Chris@91: m_lastRetrievedBlockSize = count; Chris@91: m_lastRetrievalTimestamp = m_target->getCurrentTime(); Chris@91: } Chris@43: Chris@62: if (!ts || ratio == 1.f) { Chris@43: Chris@43: size_t got = 0; Chris@43: Chris@43: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@43: Chris@43: RingBuffer *rb = getReadRingBuffer(ch); Chris@43: Chris@43: if (rb) { Chris@43: Chris@43: // this is marginally more likely to leave our channels in Chris@43: // sync after a processing failure than just passing "count": Chris@43: size_t request = count; Chris@43: if (ch > 0) request = got; Chris@43: Chris@43: got = rb->read(buffer[ch], request); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@43: std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << std::endl; Chris@43: #endif Chris@43: } Chris@43: Chris@43: for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) { Chris@43: for (size_t i = got; i < count; ++i) { Chris@43: buffer[ch][i] = 0.0; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: applyAuditioningEffect(count, buffer); Chris@43: Chris@43: m_condition.wakeAll(); Chris@91: Chris@43: return got; Chris@43: } Chris@43: Chris@62: size_t channels = getTargetChannelCount(); Chris@91: size_t available; Chris@91: int warned = 0; Chris@91: size_t fedToStretcher = 0; Chris@43: Chris@91: // The input block for a given output is approx output / ratio, Chris@91: // but we can't predict it exactly, for an adaptive timestretcher. Chris@91: Chris@91: while ((available = ts->available()) < count) { Chris@91: Chris@91: size_t reqd = lrintf((count - available) / ratio); Chris@91: reqd = std::max(reqd, ts->getSamplesRequired()); Chris@91: if (reqd == 0) reqd = 1; Chris@91: Chris@91: size_t got = reqd; Chris@91: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@91: std::cerr << "reqd = " <= m_stretcherInputCount) continue; Chris@91: RingBuffer *rb = getReadRingBuffer(c); Chris@91: if (rb) { Chris@91: size_t gotHere = rb->read(m_stretcherInputs[c], got); Chris@91: if (gotHere < got) got = gotHere; Chris@91: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@91: if (c == 0) { Chris@91: std::cerr << "feeding stretcher: got " << gotHere Chris@91: << ", " << rb->getReadSpace() << " remain" << std::endl; Chris@91: } Chris@62: #endif Chris@43: Chris@91: } else { Chris@91: std::cerr << "WARNING: No ring buffer available for channel " << c << " in stretcher input block" << std::endl; Chris@43: } Chris@43: } Chris@43: Chris@43: if (got < reqd) { Chris@43: std::cerr << "WARNING: Read underrun in playback (" Chris@43: << got << " < " << reqd << ")" << std::endl; Chris@43: } Chris@43: Chris@91: ts->process(m_stretcherInputs, got, false); Chris@91: Chris@91: fedToStretcher += got; Chris@43: Chris@43: if (got == 0) break; Chris@43: Chris@62: if (ts->available() == available) { Chris@43: std::cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << std::endl; Chris@43: if (++warned == 5) break; Chris@43: } Chris@43: } Chris@43: Chris@62: ts->retrieve(buffer, count); Chris@43: Chris@43: applyAuditioningEffect(count, buffer); Chris@43: Chris@43: m_condition.wakeAll(); Chris@43: Chris@43: return count; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::applyAuditioningEffect(size_t count, float **buffers) Chris@43: { Chris@43: if (m_auditioningPluginBypassed) return; Chris@43: RealTimePluginInstance *plugin = m_auditioningPlugin; Chris@43: if (!plugin) return; Chris@43: Chris@43: if (plugin->getAudioInputCount() != getTargetChannelCount()) { Chris@43: // std::cerr << "plugin input count " << plugin->getAudioInputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@43: // << std::endl; Chris@43: return; Chris@43: } Chris@43: if (plugin->getAudioOutputCount() != getTargetChannelCount()) { Chris@43: // std::cerr << "plugin output count " << plugin->getAudioOutputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@43: // << std::endl; Chris@43: return; Chris@43: } Chris@43: if (plugin->getBufferSize() != count) { Chris@43: // std::cerr << "plugin buffer size " << plugin->getBufferSize() Chris@43: // << " != our block size " << count Chris@43: // << std::endl; Chris@43: return; Chris@43: } Chris@43: Chris@43: float **ib = plugin->getAudioInputBuffers(); Chris@43: float **ob = plugin->getAudioOutputBuffers(); Chris@43: Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: for (size_t i = 0; i < count; ++i) { Chris@43: ib[c][i] = buffers[c][i]; Chris@43: } Chris@43: } Chris@43: Chris@43: plugin->run(Vamp::RealTime::zeroTime); Chris@43: Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: for (size_t i = 0; i < count; ++i) { Chris@43: buffers[c][i] = ob[c][i]; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: // Called from fill thread, m_playing true, mutex held Chris@43: bool Chris@43: AudioCallbackPlaySource::fillBuffers() Chris@43: { Chris@43: static float *tmp = 0; Chris@43: static size_t tmpSize = 0; Chris@43: Chris@43: size_t space = 0; Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@43: size_t spaceHere = wb->getWriteSpace(); Chris@43: if (c == 0 || spaceHere < space) space = spaceHere; Chris@43: } Chris@43: } Chris@43: Chris@43: if (space == 0) return false; Chris@43: Chris@43: size_t f = m_writeBufferFill; Chris@43: Chris@43: bool readWriteEqual = (m_readBuffers == m_writeBuffers); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl; Chris@43: #endif Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "buffered to " << f << " already" << std::endl; Chris@43: #endif Chris@43: Chris@43: bool resample = (getSourceSampleRate() != getTargetSampleRate()); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl; Chris@43: #endif Chris@43: Chris@43: size_t channels = getTargetChannelCount(); Chris@43: Chris@43: size_t orig = space; Chris@43: size_t got = 0; Chris@43: Chris@43: static float **bufferPtrs = 0; Chris@43: static size_t bufferPtrCount = 0; Chris@43: Chris@43: if (bufferPtrCount < channels) { Chris@43: if (bufferPtrs) delete[] bufferPtrs; Chris@43: bufferPtrs = new float *[channels]; Chris@43: bufferPtrCount = channels; Chris@43: } Chris@43: Chris@43: size_t generatorBlockSize = m_audioGenerator->getBlockSize(); Chris@43: Chris@43: if (resample && !m_converter) { Chris@43: static bool warned = false; Chris@43: if (!warned) { Chris@43: std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl; Chris@43: warned = true; Chris@43: } Chris@43: } Chris@43: Chris@43: if (resample && m_converter) { Chris@43: Chris@43: double ratio = Chris@43: double(getTargetSampleRate()) / double(getSourceSampleRate()); Chris@43: orig = size_t(orig / ratio + 0.1); Chris@43: Chris@43: // orig must be a multiple of generatorBlockSize Chris@43: orig = (orig / generatorBlockSize) * generatorBlockSize; Chris@43: if (orig == 0) return false; Chris@43: Chris@43: size_t work = std::max(orig, space); Chris@43: Chris@43: // We only allocate one buffer, but we use it in two halves. Chris@43: // We place the non-interleaved values in the second half of Chris@43: // the buffer (orig samples for channel 0, orig samples for Chris@43: // channel 1 etc), and then interleave them into the first Chris@43: // half of the buffer. Then we resample back into the second Chris@43: // half (interleaved) and de-interleave the results back to Chris@43: // the start of the buffer for insertion into the ringbuffers. Chris@43: // What a faff -- especially as we've already de-interleaved Chris@43: // the audio data from the source file elsewhere before we Chris@43: // even reach this point. Chris@43: Chris@43: if (tmpSize < channels * work * 2) { Chris@43: delete[] tmp; Chris@43: tmp = new float[channels * work * 2]; Chris@43: tmpSize = channels * work * 2; Chris@43: } Chris@43: Chris@43: float *nonintlv = tmp + channels * work; Chris@43: float *intlv = tmp; Chris@43: float *srcout = tmp + channels * work; Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: for (size_t i = 0; i < orig; ++i) { Chris@43: nonintlv[channels * i + c] = 0.0f; Chris@43: } Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: bufferPtrs[c] = nonintlv + c * orig; Chris@43: } Chris@43: Chris@43: got = mixModels(f, orig, bufferPtrs); Chris@43: Chris@43: // and interleave into first half Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: for (size_t i = 0; i < got; ++i) { Chris@43: float sample = nonintlv[c * got + i]; Chris@43: intlv[channels * i + c] = sample; Chris@43: } Chris@43: } Chris@43: Chris@43: SRC_DATA data; Chris@43: data.data_in = intlv; Chris@43: data.data_out = srcout; Chris@43: data.input_frames = got; Chris@43: data.output_frames = work; Chris@43: data.src_ratio = ratio; Chris@43: data.end_of_input = 0; Chris@43: Chris@43: int err = 0; Chris@43: Chris@62: if (m_timeStretcher && m_timeStretcher->getTimeRatio() < 0.4) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Using crappy converter" << std::endl; Chris@43: #endif Chris@43: err = src_process(m_crapConverter, &data); Chris@43: } else { Chris@43: err = src_process(m_converter, &data); Chris@43: } Chris@43: Chris@43: size_t toCopy = size_t(got * ratio + 0.1); Chris@43: Chris@43: if (err) { Chris@43: std::cerr Chris@43: << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: " Chris@43: << src_strerror(err) << std::endl; Chris@43: //!!! Then what? Chris@43: } else { Chris@43: got = data.input_frames_used; Chris@43: toCopy = data.output_frames_gen; Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl; Chris@43: #endif Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: for (size_t i = 0; i < toCopy; ++i) { Chris@43: tmp[i] = srcout[channels * i + c]; Chris@43: } Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) wb->write(tmp, toCopy); Chris@43: } Chris@43: Chris@43: m_writeBufferFill = f; Chris@43: if (readWriteEqual) m_readBufferFill = f; Chris@43: Chris@43: } else { Chris@43: Chris@43: // space must be a multiple of generatorBlockSize Chris@43: space = (space / generatorBlockSize) * generatorBlockSize; Chris@91: if (space == 0) { Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@91: std::cout << "requested fill is less than generator block size of " Chris@91: << generatorBlockSize << ", leaving it" << std::endl; Chris@91: #endif Chris@91: return false; Chris@91: } Chris@43: Chris@43: if (tmpSize < channels * space) { Chris@43: delete[] tmp; Chris@43: tmp = new float[channels * space]; Chris@43: tmpSize = channels * space; Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: Chris@43: bufferPtrs[c] = tmp + c * space; Chris@43: Chris@43: for (size_t i = 0; i < space; ++i) { Chris@43: tmp[c * space + i] = 0.0f; Chris@43: } Chris@43: } Chris@43: Chris@43: size_t got = mixModels(f, space, bufferPtrs); Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@43: size_t actual = wb->write(bufferPtrs[c], got); Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Wrote " << actual << " samples for ch " << c << ", now " Chris@43: << wb->getReadSpace() << " to read" Chris@43: << std::endl; Chris@43: #endif Chris@43: if (actual < got) { Chris@43: std::cerr << "WARNING: Buffer overrun in channel " << c Chris@43: << ": wrote " << actual << " of " << got Chris@43: << " samples" << std::endl; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@43: m_writeBufferFill = f; Chris@43: if (readWriteEqual) m_readBufferFill = f; Chris@43: Chris@43: //!!! 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@43: } Chris@43: Chris@43: return true; Chris@43: } Chris@43: Chris@43: size_t Chris@43: AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers) Chris@43: { Chris@43: size_t processed = 0; Chris@43: size_t chunkStart = frame; Chris@43: size_t chunkSize = count; Chris@43: size_t selectionSize = 0; Chris@43: size_t nextChunkStart = chunkStart + chunkSize; Chris@43: Chris@43: bool looping = m_viewManager->getPlayLoopMode(); Chris@43: bool constrained = (m_viewManager->getPlaySelectionMode() && Chris@43: !m_viewManager->getSelections().empty()); Chris@43: Chris@43: static float **chunkBufferPtrs = 0; Chris@43: static size_t chunkBufferPtrCount = 0; Chris@43: size_t channels = getTargetChannelCount(); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl; Chris@43: #endif Chris@43: Chris@43: if (chunkBufferPtrCount < channels) { Chris@43: if (chunkBufferPtrs) delete[] chunkBufferPtrs; Chris@43: chunkBufferPtrs = new float *[channels]; Chris@43: chunkBufferPtrCount = channels; Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: chunkBufferPtrs[c] = buffers[c]; Chris@43: } Chris@43: Chris@43: while (processed < count) { Chris@43: Chris@43: chunkSize = count - processed; Chris@43: nextChunkStart = chunkStart + chunkSize; Chris@43: selectionSize = 0; Chris@43: Chris@43: size_t fadeIn = 0, fadeOut = 0; Chris@43: Chris@43: if (constrained) { Chris@60: Chris@60: size_t rChunkStart = Chris@60: m_viewManager->alignPlaybackFrameToReference(chunkStart); Chris@43: Chris@43: Selection selection = Chris@60: m_viewManager->getContainingSelection(rChunkStart, true); Chris@43: Chris@43: if (selection.isEmpty()) { Chris@43: if (looping) { Chris@43: selection = *m_viewManager->getSelections().begin(); Chris@60: chunkStart = m_viewManager->alignReferenceToPlaybackFrame Chris@60: (selection.getStartFrame()); Chris@43: fadeIn = 50; Chris@43: } Chris@43: } Chris@43: Chris@43: if (selection.isEmpty()) { Chris@43: Chris@43: chunkSize = 0; Chris@43: nextChunkStart = chunkStart; Chris@43: Chris@43: } else { Chris@43: Chris@60: size_t sf = m_viewManager->alignReferenceToPlaybackFrame Chris@60: (selection.getStartFrame()); Chris@60: size_t ef = m_viewManager->alignReferenceToPlaybackFrame Chris@60: (selection.getEndFrame()); Chris@43: Chris@60: selectionSize = ef - sf; Chris@60: Chris@60: if (chunkStart < sf) { Chris@60: chunkStart = sf; Chris@43: fadeIn = 50; Chris@43: } Chris@43: Chris@43: nextChunkStart = chunkStart + chunkSize; Chris@43: Chris@60: if (nextChunkStart >= ef) { Chris@60: nextChunkStart = ef; Chris@43: fadeOut = 50; Chris@43: } Chris@43: Chris@43: chunkSize = nextChunkStart - chunkStart; Chris@43: } Chris@43: Chris@43: } else if (looping && m_lastModelEndFrame > 0) { Chris@43: Chris@43: if (chunkStart >= m_lastModelEndFrame) { Chris@43: chunkStart = 0; Chris@43: } Chris@43: if (chunkSize > m_lastModelEndFrame - chunkStart) { Chris@43: chunkSize = m_lastModelEndFrame - chunkStart; Chris@43: } Chris@43: nextChunkStart = chunkStart + chunkSize; Chris@43: } Chris@43: Chris@43: // std::cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl; Chris@43: Chris@43: if (!chunkSize) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Ending selection playback at " << nextChunkStart << std::endl; Chris@43: #endif Chris@43: // We need to maintain full buffers so that the other Chris@43: // thread can tell where it's got to in the playback -- so Chris@43: // return the full amount here Chris@43: frame = frame + count; Chris@43: return count; Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl; Chris@43: #endif Chris@43: Chris@43: size_t got = 0; Chris@43: Chris@43: if (selectionSize < 100) { Chris@43: fadeIn = 0; Chris@43: fadeOut = 0; Chris@43: } else if (selectionSize < 300) { Chris@43: if (fadeIn > 0) fadeIn = 10; Chris@43: if (fadeOut > 0) fadeOut = 10; Chris@43: } Chris@43: Chris@43: if (fadeIn > 0) { Chris@43: if (processed * 2 < fadeIn) { Chris@43: fadeIn = processed * 2; Chris@43: } Chris@43: } Chris@43: Chris@43: if (fadeOut > 0) { Chris@43: if ((count - processed - chunkSize) * 2 < fadeOut) { Chris@43: fadeOut = (count - processed - chunkSize) * 2; Chris@43: } Chris@43: } Chris@43: Chris@43: for (std::set::iterator mi = m_models.begin(); Chris@43: mi != m_models.end(); ++mi) { Chris@43: Chris@43: got = m_audioGenerator->mixModel(*mi, chunkStart, Chris@43: chunkSize, chunkBufferPtrs, Chris@43: fadeIn, fadeOut); Chris@43: } Chris@43: Chris@43: for (size_t c = 0; c < channels; ++c) { Chris@43: chunkBufferPtrs[c] += chunkSize; Chris@43: } Chris@43: Chris@43: processed += chunkSize; Chris@43: chunkStart = nextChunkStart; Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl; Chris@43: #endif Chris@43: Chris@43: frame = nextChunkStart; Chris@43: return processed; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::unifyRingBuffers() Chris@43: { Chris@43: if (m_readBuffers == m_writeBuffers) return; Chris@43: Chris@43: // only unify if there will be something to read Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@43: if (wb->getReadSpace() < m_blockSize * 2) { Chris@43: if ((m_writeBufferFill + m_blockSize * 2) < Chris@43: m_lastModelEndFrame) { Chris@43: // OK, we don't have enough and there's more to Chris@43: // read -- don't unify until we can do better Chris@43: return; Chris@43: } Chris@43: } Chris@43: break; Chris@43: } Chris@43: } Chris@43: Chris@43: size_t rf = m_readBufferFill; Chris@43: RingBuffer *rb = getReadRingBuffer(0); Chris@43: if (rb) { Chris@43: size_t rs = rb->getReadSpace(); Chris@43: //!!! incorrect when in non-contiguous selection, see comments elsewhere Chris@43: // std::cout << "rs = " << rs << std::endl; Chris@43: if (rs < rf) rf -= rs; Chris@43: else rf = 0; Chris@43: } Chris@43: Chris@43: //std::cout << "m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl; Chris@43: Chris@43: size_t wf = m_writeBufferFill; Chris@43: size_t skip = 0; Chris@43: for (size_t c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@43: if (c == 0) { Chris@43: Chris@43: size_t wrs = wb->getReadSpace(); Chris@43: // std::cout << "wrs = " << wrs << std::endl; Chris@43: Chris@43: if (wrs < wf) wf -= wrs; Chris@43: else wf = 0; Chris@43: // std::cout << "wf = " << wf << std::endl; Chris@43: Chris@43: if (wf < rf) skip = rf - wf; Chris@43: if (skip == 0) break; Chris@43: } Chris@43: Chris@43: // std::cout << "skipping " << skip << std::endl; Chris@43: wb->skip(skip); Chris@43: } Chris@43: } Chris@43: Chris@43: m_bufferScavenger.claim(m_readBuffers); Chris@43: m_readBuffers = m_writeBuffers; Chris@43: m_readBufferFill = m_writeBufferFill; Chris@43: // std::cout << "unified" << std::endl; Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::FillThread::run() Chris@43: { Chris@43: AudioCallbackPlaySource &s(m_source); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySourceFillThread starting" << std::endl; Chris@43: #endif Chris@43: Chris@43: s.m_mutex.lock(); Chris@43: Chris@43: bool previouslyPlaying = s.m_playing; Chris@43: bool work = false; Chris@43: Chris@43: while (!s.m_exiting) { Chris@43: Chris@43: s.unifyRingBuffers(); Chris@43: s.m_bufferScavenger.scavenge(); Chris@43: s.m_pluginScavenger.scavenge(); Chris@43: Chris@43: if (work && s.m_playing && s.getSourceSampleRate()) { Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl; Chris@43: #endif Chris@43: Chris@43: s.m_mutex.unlock(); Chris@43: s.m_mutex.lock(); Chris@43: Chris@43: } else { Chris@43: Chris@43: float ms = 100; Chris@43: if (s.getSourceSampleRate() > 0) { Chris@43: ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0; Chris@43: } Chris@43: Chris@43: if (s.m_playing) ms /= 10; Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: if (!s.m_playing) std::cout << std::endl; Chris@43: std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl; Chris@43: #endif Chris@43: Chris@43: s.m_condition.wait(&s.m_mutex, size_t(ms)); Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl; Chris@43: #endif Chris@43: Chris@43: work = false; Chris@43: Chris@43: if (!s.getSourceSampleRate()) continue; Chris@43: Chris@43: bool playing = s.m_playing; Chris@43: Chris@43: if (playing && !previouslyPlaying) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@43: std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl; Chris@43: #endif Chris@43: for (size_t c = 0; c < s.getTargetChannelCount(); ++c) { Chris@43: RingBuffer *rb = s.getReadRingBuffer(c); Chris@43: if (rb) rb->reset(); Chris@43: } Chris@43: } Chris@43: previouslyPlaying = playing; Chris@43: Chris@43: work = s.fillBuffers(); Chris@43: } Chris@43: Chris@43: s.m_mutex.unlock(); Chris@43: } Chris@43: