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@105: #include "base/ViewManagerBase.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@506: #include "data/model/ReadOnlyWaveFileModel.h" Chris@43: #include "data/model/SparseOneDimensionalModel.h" Chris@43: #include "plugin/RealTimePluginInstance.h" Chris@62: Chris@468: #include "bqaudioio/SystemPlaybackTarget.h" Chris@551: #include "bqaudioio/ResamplerWrapper.h" Chris@91: Chris@559: #include "bqvec/VectorOps.h" Chris@559: Chris@62: #include Chris@62: using namespace RubberBand; Chris@43: Chris@559: using breakfastquay::v_zero_channels; Chris@559: Chris@43: #include Chris@43: #include Chris@43: Chris@510: //#define DEBUG_AUDIO_PLAY_SOURCE 1 Chris@43: //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1 Chris@43: Chris@366: static const int DEFAULT_RING_BUFFER_SIZE = 131071; Chris@43: Chris@105: AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManagerBase *manager, Chris@57: QString clientName) : Chris@43: m_viewManager(manager), Chris@43: m_audioGenerator(new AudioGenerator()), Chris@468: m_clientName(clientName.toUtf8().data()), 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@553: m_deviceSampleRate(0), Chris@559: m_deviceChannelCount(0), Chris@43: m_playLatency(0), Chris@91: m_target(0), Chris@91: m_lastRetrievalTimestamp(0.0), Chris@91: m_lastRetrievedBlockSize(0), Chris@102: m_trustworthyTimestamps(true), Chris@102: m_lastCurrentFrame(0), Chris@43: m_playing(false), Chris@43: m_exiting(false), Chris@43: m_lastModelEndFrame(0), Chris@193: m_ringBufferSize(DEFAULT_RING_BUFFER_SIZE), 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@130: m_monoStretcher(0), Chris@91: m_stretchRatio(1.0), Chris@405: m_stretchMono(false), Chris@91: m_stretcherInputCount(0), Chris@91: m_stretcherInputs(0), Chris@91: m_stretcherInputSizes(0), Chris@551: m_fillThread(0), Chris@551: m_resamplerWrapper(0) 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@300: connect(this, SIGNAL(playStatusChanged(bool)), Chris@300: m_viewManager, SLOT(playStatusChanged(bool))); Chris@300: 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@177: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@233: SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource entering" << endl; Chris@177: #endif Chris@43: m_exiting = true; Chris@43: Chris@43: if (m_fillThread) { Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource dtor: awakening thread" << endl; Chris@212: #endif Chris@212: 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@366: for (int 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@130: delete m_timeStretcher; Chris@130: delete m_monoStretcher; Chris@130: Chris@43: m_bufferScavenger.scavenge(true); Chris@43: m_pluginScavenger.scavenge(true); Chris@177: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@233: SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource finishing" << endl; Chris@177: #endif 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@418: bool willPlay = 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@559: bool buffersIncreased = false, srChanged = false; Chris@43: Chris@366: int modelChannels = 1; Chris@506: ReadOnlyWaveFileModel *rowfm = qobject_cast(model); Chris@506: if (rowfm) modelChannels = rowfm->getChannelCount(); Chris@43: if (modelChannels > m_sourceChannelCount) { Chris@43: m_sourceChannelCount = modelChannels; Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@295: cout << "AudioCallbackPlaySource: Adding model with " << modelChannels << " channels at rate " << model->getSampleRate() << endl; Chris@43: #endif Chris@43: Chris@43: if (m_sourceSampleRate == 0) { Chris@43: Chris@566: SVDEBUG << "AudioCallbackPlaySource::addModel: Source rate changing from 0 to " Chris@566: << model->getSampleRate() << endl; Chris@566: Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: srChanged = true; Chris@43: Chris@43: } else if (model->getSampleRate() != m_sourceSampleRate) { Chris@43: Chris@506: // If this is a read-only wave file model and we have no Chris@506: // other, we can just switch to this model's sample rate Chris@43: Chris@506: if (rowfm) { 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@506: // Only read-only wave file models should be Chris@506: // considered conflicting -- writable wave file models Chris@506: // are derived and we shouldn't take their rates into Chris@506: // account. Also, don't give any particular weight to Chris@506: // a file that's already playing at the wrong rate Chris@506: // anyway Chris@506: ReadOnlyWaveFileModel *other = Chris@506: qobject_cast(*i); Chris@506: if (other && other != rowfm && Chris@506: other->getSampleRate() != model->getSampleRate() && Chris@506: other->getSampleRate() == m_sourceSampleRate) { Chris@233: SVDEBUG << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << *i << " found" << endl; Chris@43: conflicting = true; Chris@43: break; Chris@43: } Chris@43: } Chris@43: Chris@43: if (conflicting) { Chris@43: Chris@233: SVDEBUG << "AudioCallbackPlaySource::addModel: ERROR: " Chris@229: << "New model sample rate does not match" << endl Chris@43: << "existing model(s) (new " << model->getSampleRate() Chris@43: << " vs " << m_sourceSampleRate Chris@43: << "), playback will be wrong" Chris@229: << endl; Chris@43: Chris@43: emit sampleRateMismatch(model->getSampleRate(), Chris@43: m_sourceSampleRate, Chris@43: false); Chris@43: } else { Chris@566: SVDEBUG << "AudioCallbackPlaySource::addModel: Source rate changing from " Chris@566: << m_sourceSampleRate << " to " << model->getSampleRate() << endl; Chris@566: Chris@43: m_sourceSampleRate = model->getSampleRate(); Chris@43: srChanged = true; Chris@43: } Chris@43: } Chris@43: } Chris@43: Chris@366: if (!m_writeBuffers || (int)m_writeBuffers->size() < getTargetChannelCount()) { Chris@570: cerr << "m_writeBuffers size = " << (m_writeBuffers ? m_writeBuffers->size() : 0) << endl; Chris@570: cerr << "target channel count = " << (getTargetChannelCount()) << endl; Chris@43: clearRingBuffers(true, getTargetChannelCount()); Chris@559: buffersIncreased = true; Chris@43: } else { Chris@418: if (willPlay) clearRingBuffers(true); Chris@43: } Chris@43: Chris@552: if (srChanged) { Chris@553: Chris@552: SVCERR << "AudioCallbackPlaySource: Source rate changed" << endl; Chris@553: Chris@552: if (m_resamplerWrapper) { Chris@552: SVCERR << "AudioCallbackPlaySource: Source sample rate changed to " Chris@552: << m_sourceSampleRate << ", updating resampler wrapper" << endl; Chris@552: m_resamplerWrapper->changeApplicationSampleRate Chris@552: (int(round(m_sourceSampleRate))); Chris@552: m_resamplerWrapper->reset(); Chris@552: } Chris@553: Chris@553: delete m_timeStretcher; Chris@553: delete m_monoStretcher; Chris@553: m_timeStretcher = 0; Chris@553: m_monoStretcher = 0; Chris@553: Chris@553: if (m_stretchRatio != 1.f) { Chris@553: setTimeStretch(m_stretchRatio); Chris@553: } Chris@43: } Chris@43: Chris@164: rebuildRangeLists(); Chris@164: Chris@43: m_mutex.unlock(); Chris@43: Chris@43: m_audioGenerator->setTargetChannelCount(getTargetChannelCount()); Chris@43: Chris@559: if (buffersIncreased) { Chris@570: SVDEBUG << "AudioCallbackPlaySource::addModel: Number of buffers increased to " << getTargetChannelCount() << endl; Chris@570: if (getTargetChannelCount() > getDeviceChannelCount()) { Chris@570: SVDEBUG << "AudioCallbackPlaySource::addModel: This is more than the device channel count, signalling channelCountIncreased" << endl; Chris@570: emit channelCountIncreased(getTargetChannelCount()); Chris@570: } else { Chris@570: SVDEBUG << "AudioCallbackPlaySource::addModel: This is no more than the device channel count (" << getDeviceChannelCount() << "), so taking no action" << endl; Chris@570: } Chris@559: } Chris@559: 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@559: SVDEBUG << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s)" << endl; Chris@43: #endif Chris@43: Chris@435: connect(model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), Chris@435: this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); Chris@43: Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::addModel: awakening thread" << endl; Chris@212: #endif Chris@559: Chris@43: m_condition.wakeAll(); Chris@43: } Chris@43: Chris@43: void Chris@435: AudioCallbackPlaySource::modelChangedWithin(sv_frame_t Chris@367: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@367: startFrame Chris@367: #endif Chris@435: , sv_frame_t endFrame) Chris@43: { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@367: SVDEBUG << "AudioCallbackPlaySource::modelChangedWithin(" << startFrame << "," << endFrame << ")" << endl; Chris@43: #endif Chris@93: if (endFrame > m_lastModelEndFrame) { Chris@93: m_lastModelEndFrame = endFrame; Chris@99: rebuildRangeLists(); 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@293: cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << endl; Chris@43: #endif Chris@43: Chris@435: disconnect(model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), Chris@435: this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t))); Chris@43: Chris@43: m_models.erase(model); Chris@43: Chris@566: // I don't think we have to do this any more: if a new model is Chris@566: // loaded at a different rate, we'll hit the non-conflicting path Chris@566: // in addModel and the rate will be updated without problems; but Chris@566: // if a new model is loaded at the rate that we were using for the Chris@566: // last one, then we save work by not having reset this here Chris@566: // Chris@566: // if (m_models.empty()) { Chris@566: // m_sourceSampleRate = 0; Chris@566: // } Chris@43: Chris@436: sv_frame_t lastEnd = 0; Chris@43: for (std::set::const_iterator i = m_models.begin(); Chris@43: i != m_models.end(); ++i) { Chris@164: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << endl; Chris@164: #endif Chris@367: if ((*i)->getEndFrame() > lastEnd) { Chris@367: lastEnd = (*i)->getEndFrame(); Chris@367: } Chris@164: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "(done, lastEnd now " << lastEnd << ")" << endl; Chris@164: #endif Chris@43: } Chris@43: m_lastModelEndFrame = lastEnd; Chris@43: Chris@212: m_audioGenerator->removeModel(model); Chris@212: Chris@43: m_mutex.unlock(); 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@293: cout << "AudioCallbackPlaySource::clearModels()" << endl; Chris@43: #endif Chris@43: Chris@43: m_models.clear(); 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@366: AudioCallbackPlaySource::clearRingBuffers(bool haveLock, int count) Chris@43: { Chris@43: if (!haveLock) m_mutex.lock(); Chris@43: Chris@445: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "clearRingBuffers" << endl; Chris@445: #endif Chris@397: Chris@93: rebuildRangeLists(); Chris@93: Chris@43: if (count == 0) { Chris@436: if (m_writeBuffers) count = int(m_writeBuffers->size()); Chris@43: } Chris@43: Chris@445: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "current playing frame = " << getCurrentPlayingFrame() << endl; Chris@397: Chris@563: cout << "write buffer fill (before) = " << m_writeBufferFill << endl; Chris@445: #endif Chris@445: Chris@93: m_writeBufferFill = getCurrentBufferedFrame(); Chris@43: Chris@445: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "current buffered frame = " << m_writeBufferFill << endl; Chris@445: #endif Chris@397: 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@366: for (int i = 0; i < count; ++i) { Chris@43: m_writeBuffers->push_back(new RingBuffer(m_ringBufferSize)); Chris@43: } Chris@43: Chris@442: m_audioGenerator->reset(); Chris@442: Chris@293: // cout << "AudioCallbackPlaySource::clearRingBuffers: Created " Chris@293: // << count << " write buffers" << endl; Chris@43: Chris@43: if (!haveLock) { Chris@43: m_mutex.unlock(); Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@434: AudioCallbackPlaySource::play(sv_frame_t startFrame) Chris@43: { Chris@540: if (!m_target) return; Chris@540: Chris@414: if (!m_sourceSampleRate) { Chris@563: SVCERR << "AudioCallbackPlaySource::play: No source sample rate available, not playing" << endl; Chris@414: return; Chris@414: } Chris@414: Chris@43: if (m_viewManager->getPlaySelectionMode() && Chris@43: !m_viewManager->getSelections().empty()) { Chris@60: Chris@563: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "AudioCallbackPlaySource::play: constraining frame " << startFrame << " to selection = "; Chris@563: #endif Chris@94: Chris@60: startFrame = m_viewManager->constrainFrameToSelection(startFrame); Chris@60: Chris@563: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << startFrame << endl; Chris@563: #endif Chris@94: Chris@43: } else { Chris@454: if (startFrame < 0) { Chris@454: startFrame = 0; Chris@454: } Chris@43: if (startFrame >= m_lastModelEndFrame) { Chris@43: startFrame = 0; Chris@43: } Chris@43: } Chris@43: Chris@132: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "play(" << startFrame << ") -> aligned playback model "; Chris@132: #endif Chris@60: Chris@60: startFrame = m_viewManager->alignReferenceToPlaybackFrame(startFrame); Chris@60: Chris@189: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << startFrame << endl; Chris@189: #endif 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@102: // NO -- we can end up playing some first -- always reset here Chris@43: Chris@43: m_mutex.lock(); Chris@102: Chris@91: if (m_timeStretcher) { Chris@91: m_timeStretcher->reset(); Chris@91: } Chris@130: if (m_monoStretcher) { Chris@130: m_monoStretcher->reset(); Chris@130: } Chris@102: Chris@102: m_readBufferFill = m_writeBufferFill = startFrame; Chris@102: if (m_readBuffers) { Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@102: RingBuffer *rb = getReadRingBuffer(c); Chris@132: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "reset ring buffer for channel " << c << endl; Chris@132: #endif Chris@102: if (rb) rb->reset(); Chris@102: } Chris@43: } Chris@102: 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@102: m_lastCurrentFrame = 0; Chris@43: m_playing = true; Chris@212: Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::play: awakening thread" << endl; Chris@212: #endif Chris@212: Chris@43: m_condition.wakeAll(); Chris@158: if (changed) { Chris@158: emit playStatusChanged(m_playing); Chris@158: emit activity(tr("Play from %1").arg Chris@158: (RealTime::frame2RealTime Chris@158: (m_playStartFrame, m_sourceSampleRate).toText().c_str())); Chris@158: } Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::stop() Chris@43: { Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@233: SVDEBUG << "AudioCallbackPlaySource::stop()" << endl; Chris@212: #endif Chris@43: bool changed = m_playing; Chris@43: m_playing = false; Chris@212: Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::stop: awakening thread" << endl; Chris@212: #endif Chris@212: Chris@43: m_condition.wakeAll(); Chris@91: m_lastRetrievalTimestamp = 0; Chris@158: if (changed) { Chris@158: emit playStatusChanged(m_playing); Chris@158: emit activity(tr("Stop at %1").arg Chris@158: (RealTime::frame2RealTime Chris@158: (m_lastCurrentFrame, m_sourceSampleRate).toText().c_str())); Chris@158: } Chris@102: m_lastCurrentFrame = 0; 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@552: AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName ) Chris@43: { Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::audioProcessingOverload() Chris@43: { Chris@563: SVCERR << "Audio processing overload!" << endl; Chris@130: Chris@130: if (!m_playing) return; Chris@130: Chris@43: RealTimePluginInstance *ap = m_auditioningPlugin; Chris@130: if (ap && !m_auditioningPluginBypassed) { Chris@43: m_auditioningPluginBypassed = true; Chris@43: emit audioOverloadPluginDisabled(); Chris@130: return; Chris@130: } Chris@130: Chris@130: if (m_timeStretcher && Chris@130: m_timeStretcher->getTimeRatio() < 1.0 && Chris@130: m_stretcherInputCount > 1 && Chris@130: m_monoStretcher && !m_stretchMono) { Chris@130: m_stretchMono = true; Chris@130: emit audioTimeStretchMultiChannelDisabled(); Chris@130: return; Chris@43: } Chris@43: } Chris@43: Chris@43: void Chris@468: AudioCallbackPlaySource::setSystemPlaybackTarget(breakfastquay::SystemPlaybackTarget *target) Chris@43: { Chris@559: if (target == 0) { Chris@559: // reset target-related facts and figures Chris@559: m_deviceSampleRate = 0; Chris@559: m_deviceChannelCount = 0; Chris@559: } Chris@91: m_target = target; Chris@468: } Chris@468: Chris@468: void Chris@551: AudioCallbackPlaySource::setResamplerWrapper(breakfastquay::ResamplerWrapper *w) Chris@551: { Chris@551: m_resamplerWrapper = w; Chris@552: if (m_resamplerWrapper && m_sourceSampleRate != 0) { Chris@552: m_resamplerWrapper->changeApplicationSampleRate Chris@552: (int(round(m_sourceSampleRate))); Chris@552: } Chris@551: } Chris@551: Chris@551: void Chris@468: AudioCallbackPlaySource::setSystemPlaybackBlockSize(int size) Chris@468: { Chris@293: cout << "AudioCallbackPlaySource::setTarget: Block size -> " << size << endl; Chris@193: if (size != 0) { Chris@193: m_blockSize = size; Chris@193: } Chris@193: if (size * 4 > m_ringBufferSize) { Chris@472: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "AudioCallbackPlaySource::setTarget: Buffer size " Chris@472: << size << " > a quarter of ring buffer size " Chris@472: << m_ringBufferSize << ", calling for more ring buffer" Chris@472: << endl; Chris@472: #endif Chris@193: m_ringBufferSize = size * 4; Chris@193: if (m_writeBuffers && !m_writeBuffers->empty()) { Chris@193: clearRingBuffers(); Chris@193: } Chris@193: } Chris@43: } Chris@43: Chris@366: int Chris@43: AudioCallbackPlaySource::getTargetBlockSize() const Chris@43: { Chris@293: // cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << endl; Chris@436: return int(m_blockSize); Chris@43: } Chris@43: Chris@43: void Chris@468: AudioCallbackPlaySource::setSystemPlaybackLatency(int latency) Chris@43: { Chris@43: m_playLatency = latency; Chris@43: } Chris@43: Chris@434: sv_frame_t Chris@43: AudioCallbackPlaySource::getTargetPlayLatency() const Chris@43: { Chris@43: return m_playLatency; Chris@43: } Chris@43: Chris@434: sv_frame_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@553: sv_samplerate_t deviceRate = getDeviceSampleRate(); Chris@436: sv_frame_t latency = m_playLatency; // at target rate Chris@402: RealTime latency_t = RealTime::zeroTime; Chris@402: Chris@553: if (deviceRate != 0) { Chris@553: latency_t = RealTime::frame2RealTime(latency, deviceRate); Chris@402: } Chris@93: Chris@93: return getCurrentFrame(latency_t); Chris@93: } Chris@93: Chris@434: sv_frame_t Chris@93: AudioCallbackPlaySource::getCurrentBufferedFrame() Chris@93: { Chris@93: return getCurrentFrame(RealTime::zeroTime); Chris@93: } Chris@93: Chris@434: sv_frame_t Chris@93: AudioCallbackPlaySource::getCurrentFrame(RealTime latency_t) Chris@93: { Chris@553: // The ring buffers contain data at the source sample rate and all Chris@553: // processing (including time stretching) happens at this Chris@553: // rate. Resampling only happens after the audio data leaves this Chris@553: // class. Chris@553: Chris@553: // (But because historically more than one sample rate could have Chris@553: // been involved here, we do latency calculations using RealTime Chris@553: // values instead of samples.) Chris@43: Chris@553: sv_samplerate_t rate = getSourceSampleRate(); Chris@91: Chris@553: if (rate == 0) return 0; Chris@91: Chris@366: int inbuffer = 0; // at target rate Chris@91: Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *rb = getReadRingBuffer(c); Chris@43: if (rb) { Chris@366: int here = rb->getReadSpace(); Chris@91: if (c == 0 || here < inbuffer) inbuffer = here; Chris@43: } Chris@43: } Chris@43: Chris@436: sv_frame_t readBufferFill = m_readBufferFill; Chris@436: sv_frame_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@102: bool looping = m_viewManager->getPlayLoopMode(); Chris@102: Chris@553: RealTime inbuffer_t = RealTime::frame2RealTime(inbuffer, rate); Chris@91: Chris@436: sv_frame_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@553: RealTime stretchlat_t = RealTime::frame2RealTime(stretchlat, rate); 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@102: if (m_target && Chris@102: m_trustworthyTimestamps && Chris@102: lastRetrievalTimestamp != 0.0) { Chris@91: Chris@553: lastretrieved_t = RealTime::frame2RealTime(lastRetrievedBlockSize, rate); 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@102: if (m_trustworthyTimestamps && !looping) { Chris@91: Chris@102: // this adjustment seems to cause more problems when looping Chris@102: double elapsed = currentTime - lastRetrievalTimestamp; Chris@102: Chris@102: if (elapsed > 0.0) { Chris@102: sincerequest_t = RealTime::fromSeconds(elapsed); Chris@102: } Chris@91: } Chris@91: Chris@91: } else { Chris@91: Chris@553: lastretrieved_t = RealTime::frame2RealTime(getTargetBlockSize(), rate); Chris@62: } Chris@91: Chris@553: RealTime bufferedto_t = RealTime::frame2RealTime(readBufferFill, rate); Chris@91: Chris@91: if (timeRatio != 1.0) { Chris@91: lastretrieved_t = lastretrieved_t / timeRatio; Chris@91: sincerequest_t = sincerequest_t / timeRatio; Chris@163: latency_t = latency_t / timeRatio; Chris@43: } Chris@43: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "\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 quantity: " << lastretrieved_t << endl; Chris@91: #endif Chris@43: 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@193: if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime; Chris@553: sv_frame_t frame = RealTime::realTime2Frame(playing_t, rate); Chris@93: return m_viewManager->alignPlaybackFrameToReference(frame); Chris@93: } Chris@43: Chris@91: int inRange = 0; Chris@91: int index = 0; Chris@91: Chris@366: for (int i = 0; i < (int)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@436: if (inRange >= int(m_rangeStarts.size())) { Chris@436: inRange = int(m_rangeStarts.size())-1; Chris@436: } 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@553: RealTime playstart_t = RealTime::frame2RealTime(m_playStartFrame, rate); Chris@94: if (playing_t < playstart_t) { Chris@563: // cout << "playing_t " << playing_t << " < playstart_t " Chris@293: // << playstart_t << endl; Chris@122: if (/*!!! sincerequest_t > RealTime::zeroTime && */ Chris@94: m_playStartedAt + latency_t + stretchlat_t < Chris@94: RealTime::fromSeconds(currentTime)) { Chris@563: // cout << "but we've been playing for long enough that I think we should disregard it (it probably results from loop wrapping)" << 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@163: Chris@163: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "playing_t " << playing_t; Chris@163: #endif Chris@94: Chris@94: playing_t = playing_t - m_rangeStarts[inRange]; Chris@93: Chris@93: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << " as offset into range " << inRange << " (start =" << m_rangeStarts[inRange] << " duration =" << m_rangeDurations[inRange] << ") = " << playing_t << 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@436: inRange = int(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@563: cout << " playing time: " << playing_t << endl; Chris@93: #endif Chris@93: Chris@93: if (!looping) { Chris@366: if (inRange == (int)m_rangeStarts.size()-1 && Chris@93: playing_t >= m_rangeStarts[inRange] + m_rangeDurations[inRange]) { Chris@563: cout << "Not looping, inRange " << inRange << " == rangeStarts.size()-1, playing_t " << playing_t << " >= m_rangeStarts[inRange] " << m_rangeStarts[inRange] << " + m_rangeDurations[inRange] " << m_rangeDurations[inRange] << " -- stopping" << endl; Chris@93: stop(); Chris@93: } Chris@93: } Chris@93: Chris@93: if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime; Chris@93: Chris@553: sv_frame_t frame = RealTime::realTime2Frame(playing_t, rate); Chris@102: Chris@102: if (m_lastCurrentFrame > 0 && !looping) { Chris@102: if (frame < m_lastCurrentFrame) { Chris@102: frame = m_lastCurrentFrame; Chris@102: } Chris@102: } Chris@102: Chris@102: m_lastCurrentFrame = frame; Chris@102: 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@436: sv_samplerate_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@233: SVDEBUG << "AudioCallbackPlaySource::rebuildRangeLists" << 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@563: cout << "Now have " << m_rangeStarts.size() << " play ranges" << endl; Chris@91: #endif Chris@43: } Chris@43: Chris@43: void Chris@43: AudioCallbackPlaySource::setOutputLevels(float left, float right) Chris@43: { Chris@574: if (left > m_outputLeft) m_outputLeft = left; Chris@574: if (right > m_outputRight) 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@574: m_outputLeft = 0.f; Chris@574: m_outputRight = 0.f; Chris@43: return true; Chris@43: } Chris@43: Chris@43: void Chris@468: AudioCallbackPlaySource::setSystemPlaybackSampleRate(int sr) Chris@43: { Chris@553: m_deviceSampleRate = sr; Chris@43: } Chris@43: Chris@43: void Chris@559: AudioCallbackPlaySource::setSystemPlaybackChannelCount(int count) Chris@43: { Chris@559: m_deviceChannelCount = count; Chris@43: } Chris@43: Chris@43: void Chris@107: AudioCallbackPlaySource::setAuditioningEffect(Auditionable *a) Chris@43: { Chris@107: RealTimePluginInstance *plugin = dynamic_cast(a); Chris@107: if (a && !plugin) { Chris@563: SVCERR << "WARNING: AudioCallbackPlaySource::setAuditioningEffect: auditionable object " << a << " is not a real-time plugin instance" << endl; Chris@107: } Chris@204: Chris@204: m_mutex.lock(); Chris@43: m_auditioningPlugin = plugin; Chris@43: m_auditioningPluginBypassed = false; Chris@204: m_mutex.unlock(); 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@434: sv_samplerate_t Chris@553: AudioCallbackPlaySource::getDeviceSampleRate() const Chris@43: { Chris@553: return m_deviceSampleRate; Chris@43: } Chris@43: Chris@366: int Chris@43: AudioCallbackPlaySource::getSourceChannelCount() const Chris@43: { Chris@43: return m_sourceChannelCount; Chris@43: } Chris@43: Chris@366: int 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@559: int Chris@559: AudioCallbackPlaySource::getDeviceChannelCount() const Chris@559: { Chris@559: return m_deviceChannelCount; Chris@559: } Chris@559: Chris@434: sv_samplerate_t Chris@43: AudioCallbackPlaySource::getSourceSampleRate() const Chris@43: { Chris@43: return m_sourceSampleRate; Chris@43: } Chris@43: Chris@43: void Chris@436: AudioCallbackPlaySource::setTimeStretch(double factor) Chris@43: { Chris@91: m_stretchRatio = factor; Chris@91: Chris@553: int rate = int(getSourceSampleRate()); Chris@553: if (!rate) return; // have to make our stretcher later Chris@244: Chris@436: if (m_timeStretcher || (factor == 1.0)) { Chris@91: // stretch ratio will be set in next process call if appropriate Chris@62: } else { Chris@91: m_stretcherInputCount = getTargetChannelCount(); Chris@62: RubberBandStretcher *stretcher = new RubberBandStretcher Chris@553: (rate, Chris@91: m_stretcherInputCount, Chris@62: RubberBandStretcher::OptionProcessRealTime, Chris@62: factor); Chris@130: RubberBandStretcher *monoStretcher = new RubberBandStretcher Chris@553: (rate, Chris@130: 1, Chris@130: RubberBandStretcher::OptionProcessRealTime, Chris@130: factor); Chris@91: m_stretcherInputs = new float *[m_stretcherInputCount]; Chris@436: m_stretcherInputSizes = new sv_frame_t[m_stretcherInputCount]; Chris@366: for (int 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@130: m_monoStretcher = monoStretcher; Chris@62: m_timeStretcher = stretcher; Chris@62: } Chris@158: Chris@158: emit activity(tr("Change time-stretch factor to %1").arg(factor)); Chris@43: } Chris@43: Chris@471: int Chris@559: AudioCallbackPlaySource::getSourceSamples(float *const *buffer, Chris@559: int requestedChannels, Chris@559: int count) Chris@43: { Chris@559: // In principle, the target will handle channel mapping in cases Chris@559: // where our channel count differs from the device's. But that Chris@559: // only holds if our channel count doesn't change -- i.e. if Chris@559: // getApplicationChannelCount() always returns the same value as Chris@559: // it did when the target was created, and if this function always Chris@559: // returns that number of channels. Chris@559: // Chris@559: // Unfortunately that can't hold for us -- we always have at least Chris@559: // 2 channels but if the user opens a new main model with more Chris@559: // channels than that (and more than the last main model) then our Chris@559: // target channel count necessarily gets increased. Chris@559: // Chris@559: // We have: Chris@559: // Chris@559: // getSourceChannelCount() -> number of channels available to Chris@559: // provide from real model data Chris@559: // Chris@559: // getTargetChannelCount() -> number we will actually provide; Chris@559: // same as getSourceChannelCount() except that it is always at Chris@559: // least 2 Chris@559: // Chris@559: // getDeviceChannelCount() -> number the device will emit, usually Chris@559: // equal to the value of getTargetChannelCount() at the time the Chris@559: // device was initialised, unless the device could not provide Chris@559: // that number Chris@559: // Chris@559: // requestedChannels -> number the device is expecting from us, Chris@559: // always equal to the value of getTargetChannelCount() at the Chris@559: // time the device was initialised Chris@559: // Chris@559: // If the requested channel count is at least the target channel Chris@559: // count, then we go ahead and provide the target channels as Chris@559: // expected. We just zero any spare channels. Chris@559: // Chris@559: // If the requested channel count is smaller than the target Chris@559: // channel count, then we don't know what to do and we provide Chris@559: // nothing. This shouldn't happen as long as management is on the Chris@559: // ball -- we emit channelCountIncreased() when the target channel Chris@559: // count increases, and whatever code "owns" the driver should Chris@559: // have reopened the audio device when it got that signal. But Chris@559: // there's a race condition there, which we accommodate with this Chris@559: // check. Chris@559: Chris@559: int channels = getTargetChannelCount(); Chris@559: Chris@43: if (!m_playing) { Chris@193: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "AudioCallbackPlaySource::getSourceSamples: Not playing" << endl; Chris@193: #endif Chris@559: v_zero_channels(buffer, requestedChannels, count); Chris@471: return 0; Chris@43: } Chris@559: if (requestedChannels < channels) { Chris@559: SVDEBUG << "AudioCallbackPlaySource::getSourceSamples: Not enough device channels (" << requestedChannels << ", need " << channels << "); hoping device is about to be reopened" << endl; Chris@559: v_zero_channels(buffer, requestedChannels, count); Chris@559: return 0; Chris@559: } Chris@559: if (requestedChannels > channels) { Chris@559: v_zero_channels(buffer + channels, requestedChannels - channels, count); Chris@559: } Chris@43: Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "AudioCallbackPlaySource::getSourceSamples: Playing" << endl; Chris@212: #endif Chris@212: 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@559: for (int ch = 0; ch < channels; ++ch) { Chris@43: Chris@43: RingBuffer *rb = getReadRingBuffer(ch); Chris@43: Chris@43: if (!rb) { Chris@563: SVCERR << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@43: << "No ring buffer available for channel " << ch Chris@293: << ", returning no data here" << endl; Chris@43: count = 0; Chris@43: break; Chris@43: } Chris@43: Chris@366: int rs = rb->getReadSpace(); Chris@43: if (rs < count) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: " Chris@43: << "Ring buffer for channel " << ch << " has only " Chris@193: << rs << " (of " << count << ") samples available (" Chris@193: << "ring buffer size is " << rb->getSize() << ", write " Chris@193: << "space " << rb->getWriteSpace() << "), " Chris@293: << "reducing request size" << endl; Chris@43: #endif Chris@43: count = rs; Chris@43: } Chris@43: } Chris@43: Chris@471: if (count == 0) return 0; Chris@43: Chris@62: RubberBandStretcher *ts = m_timeStretcher; Chris@130: RubberBandStretcher *ms = m_monoStretcher; Chris@130: Chris@436: double ratio = ts ? ts->getTimeRatio() : 1.0; Chris@91: Chris@91: if (ratio != m_stretchRatio) { Chris@91: if (!ts) { Chris@563: SVCERR << "WARNING: AudioCallbackPlaySource::getSourceSamples: Time ratio change to " << m_stretchRatio << " is pending, but no stretcher is set" << endl; Chris@436: m_stretchRatio = 1.0; Chris@91: } else { Chris@91: ts->setTimeRatio(m_stretchRatio); Chris@130: if (ms) ms->setTimeRatio(m_stretchRatio); Chris@130: if (m_stretchRatio >= 1.0) m_stretchMono = false; Chris@130: } Chris@130: } Chris@130: Chris@130: int stretchChannels = m_stretcherInputCount; Chris@130: if (m_stretchMono) { Chris@130: if (ms) { Chris@130: ts = ms; Chris@130: stretchChannels = 1; Chris@130: } else { Chris@130: m_stretchMono = false; 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@130: int got = 0; Chris@43: Chris@563: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "channels == " << channels << endl; Chris@563: #endif Chris@555: Chris@559: for (int ch = 0; ch < channels; ++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@436: sv_frame_t request = count; Chris@43: if (ch > 0) request = got; Chris@43: Chris@436: got = rb->read(buffer[ch], int(request)); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@293: cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << endl; Chris@43: #endif Chris@43: } Chris@43: Chris@559: for (int ch = 0; ch < channels; ++ch) { Chris@130: for (int 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@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::getSamples: awakening thread" << endl; Chris@212: #endif Chris@212: Chris@43: m_condition.wakeAll(); Chris@91: Chris@471: return got; Chris@43: } Chris@43: Chris@436: sv_frame_t available; Chris@436: sv_frame_t fedToStretcher = 0; Chris@91: int warned = 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@436: sv_frame_t reqd = lrint(double(count - available) / ratio); Chris@436: reqd = std::max(reqd, sv_frame_t(ts->getSamplesRequired())); Chris@91: if (reqd == 0) reqd = 1; Chris@91: Chris@436: sv_frame_t got = reqd; Chris@91: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "reqd = " <= m_stretcherInputCount) continue; Chris@91: RingBuffer *rb = getReadRingBuffer(c); Chris@91: if (rb) { Chris@436: sv_frame_t gotHere; Chris@130: if (stretchChannels == 1 && c > 0) { Chris@436: gotHere = rb->readAdding(m_stretcherInputs[0], int(got)); Chris@130: } else { Chris@436: gotHere = rb->read(m_stretcherInputs[c], int(got)); Chris@130: } Chris@91: if (gotHere < got) got = gotHere; Chris@91: Chris@91: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@91: if (c == 0) { Chris@563: cout << "feeding stretcher: got " << gotHere Chris@229: << ", " << rb->getReadSpace() << " remain" << endl; Chris@91: } Chris@62: #endif Chris@43: Chris@91: } else { Chris@563: SVCERR << "WARNING: No ring buffer available for channel " << c << " in stretcher input block" << endl; Chris@43: } Chris@43: } Chris@43: Chris@43: if (got < reqd) { Chris@563: SVCERR << "WARNING: Read underrun in playback (" Chris@293: << got << " < " << reqd << ")" << endl; Chris@43: } Chris@43: Chris@463: ts->process(m_stretcherInputs, size_t(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@563: SVCERR << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << endl; Chris@43: if (++warned == 5) break; Chris@43: } Chris@43: } Chris@43: Chris@463: ts->retrieve(buffer, size_t(count)); Chris@43: Chris@559: v_zero_channels(buffer + stretchChannels, channels - stretchChannels, count); Chris@130: Chris@43: applyAuditioningEffect(count, buffer); Chris@43: Chris@212: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySource::getSamples [stretched]: awakening thread" << endl; Chris@212: #endif Chris@212: Chris@43: m_condition.wakeAll(); Chris@43: Chris@471: return count; Chris@43: } Chris@43: Chris@43: void Chris@559: AudioCallbackPlaySource::applyAuditioningEffect(sv_frame_t count, float *const *buffers) Chris@43: { Chris@43: if (m_auditioningPluginBypassed) return; Chris@43: RealTimePluginInstance *plugin = m_auditioningPlugin; Chris@43: if (!plugin) return; Chris@204: Chris@366: if ((int)plugin->getAudioInputCount() != getTargetChannelCount()) { Chris@563: // cout << "plugin input count " << plugin->getAudioInputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@293: // << endl; Chris@43: return; Chris@43: } Chris@366: if ((int)plugin->getAudioOutputCount() != getTargetChannelCount()) { Chris@563: // cout << "plugin output count " << plugin->getAudioOutputCount() Chris@43: // << " != our channel count " << getTargetChannelCount() Chris@293: // << endl; Chris@43: return; Chris@43: } Chris@366: if ((int)plugin->getBufferSize() < count) { Chris@563: // cout << "plugin buffer size " << plugin->getBufferSize() Chris@102: // << " < our block size " << count Chris@293: // << endl; Chris@43: return; Chris@43: } Chris@43: Chris@43: float **ib = plugin->getAudioInputBuffers(); Chris@43: float **ob = plugin->getAudioOutputBuffers(); Chris@43: Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@366: for (int i = 0; i < count; ++i) { Chris@43: ib[c][i] = buffers[c][i]; Chris@43: } Chris@43: } Chris@43: Chris@436: plugin->run(Vamp::RealTime::zeroTime, int(count)); Chris@43: Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@366: for (int 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@436: static sv_frame_t tmpSize = 0; Chris@43: Chris@434: sv_frame_t space = 0; Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@434: sv_frame_t spaceHere = wb->getWriteSpace(); Chris@43: if (c == 0 || spaceHere < space) space = spaceHere; Chris@43: } Chris@43: } Chris@43: Chris@103: if (space == 0) { Chris@103: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySourceFillThread: no space to fill" << endl; Chris@103: #endif Chris@103: return false; Chris@103: } Chris@43: Chris@544: // space is now the number of samples that can be written on each Chris@544: // channel's write ringbuffer Chris@544: Chris@434: sv_frame_t f = m_writeBufferFill; Chris@43: Chris@43: bool readWriteEqual = (m_readBuffers == m_writeBuffers); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@193: if (!readWriteEqual) { Chris@293: cout << "AudioCallbackPlaySourceFillThread: note read buffers != write buffers" << endl; Chris@193: } Chris@293: cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << endl; Chris@43: #endif Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "buffered to " << f << " already" << endl; Chris@43: #endif Chris@43: Chris@366: int channels = getTargetChannelCount(); Chris@43: Chris@43: static float **bufferPtrs = 0; Chris@366: static int 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@436: sv_frame_t generatorBlockSize = m_audioGenerator->getBlockSize(); Chris@43: Chris@546: // space must be a multiple of generatorBlockSize Chris@546: sv_frame_t reqSpace = space; Chris@546: space = (reqSpace / generatorBlockSize) * generatorBlockSize; Chris@546: if (space == 0) { Chris@546: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@546: cout << "requested fill of " << reqSpace Chris@546: << " is less than generator block size of " Chris@546: << generatorBlockSize << ", leaving it" << endl; Chris@546: #endif Chris@546: return false; Chris@43: } Chris@43: Chris@546: if (tmpSize < channels * space) { Chris@546: delete[] tmp; Chris@546: tmp = new float[channels * space]; Chris@546: tmpSize = channels * space; Chris@546: } Chris@43: Chris@546: for (int c = 0; c < channels; ++c) { Chris@43: Chris@546: bufferPtrs[c] = tmp + c * space; Chris@546: Chris@546: for (int i = 0; i < space; ++i) { Chris@546: tmp[c * space + i] = 0.0f; Chris@546: } Chris@546: } Chris@43: Chris@546: sv_frame_t got = mixModels(f, space, bufferPtrs); // also modifies f Chris@43: Chris@546: for (int c = 0; c < channels; ++c) { Chris@43: Chris@546: RingBuffer *wb = getWriteRingBuffer(c); Chris@546: if (wb) { Chris@546: int actual = wb->write(bufferPtrs[c], int(got)); Chris@546: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@546: cout << "Wrote " << actual << " samples for ch " << c << ", now " Chris@546: << wb->getReadSpace() << " to read" Chris@546: << endl; Chris@546: #endif Chris@546: if (actual < got) { Chris@563: SVCERR << "WARNING: Buffer overrun in channel " << c Chris@563: << ": wrote " << actual << " of " << got Chris@563: << " samples" << endl; Chris@546: } Chris@546: } Chris@546: } Chris@43: Chris@546: m_writeBufferFill = f; Chris@546: if (readWriteEqual) m_readBufferFill = f; Chris@43: Chris@163: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "Read buffer fill is now " << m_readBufferFill << ", write buffer fill " Chris@563: << m_writeBufferFill << endl; Chris@163: #endif Chris@163: Chris@546: //!!! 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: return true; Chris@43: } Chris@43: Chris@434: sv_frame_t Chris@434: AudioCallbackPlaySource::mixModels(sv_frame_t &frame, sv_frame_t count, float **buffers) Chris@43: { Chris@434: sv_frame_t processed = 0; Chris@434: sv_frame_t chunkStart = frame; Chris@434: sv_frame_t chunkSize = count; Chris@434: sv_frame_t selectionSize = 0; Chris@434: sv_frame_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@366: int channels = getTargetChannelCount(); Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "mixModels: start " << frame << ", size " << count << ", channels " << channels << endl; Chris@43: #endif Chris@563: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: if (constrained) { Chris@563: cout << "Manager has " << m_viewManager->getSelections().size() << " selection(s):" << endl; Chris@563: for (auto sel: m_viewManager->getSelections()) { Chris@563: cout << sel.getStartFrame() << " -> " << sel.getEndFrame() Chris@563: << " (" << (sel.getEndFrame() - sel.getStartFrame()) << " frames)" Chris@563: << endl; Chris@563: } Chris@563: } Chris@563: #endif Chris@563: Chris@563: static float **chunkBufferPtrs = 0; Chris@563: static int chunkBufferPtrCount = 0; 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@366: for (int 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@434: sv_frame_t fadeIn = 0, fadeOut = 0; Chris@43: Chris@43: if (constrained) { Chris@60: Chris@434: sv_frame_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@434: sv_frame_t sf = m_viewManager->alignReferenceToPlaybackFrame Chris@60: (selection.getStartFrame()); Chris@434: sv_frame_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@563: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << endl; Chris@563: #endif Chris@563: Chris@43: if (!chunkSize) { 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@562: if (frame < nextChunkStart) { Chris@562: frame = nextChunkStart; Chris@562: } Chris@562: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@562: cout << "mixModels: ending at " << nextChunkStart << ", returning frame as " Chris@562: << frame << endl; Chris@562: #endif Chris@43: return count; Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@563: cout << "mixModels: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << endl; Chris@43: #endif 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@366: (void) m_audioGenerator->mixModel(*mi, chunkStart, Chris@366: chunkSize, chunkBufferPtrs, Chris@366: fadeIn, fadeOut); Chris@43: } Chris@43: Chris@366: for (int 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@563: cout << "mixModels returning " << processed << " frames to " << nextChunkStart << 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@366: for (int 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@193: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "AudioCallbackPlaySource::unifyRingBuffers: Not unifying: write buffer has less (" << wb->getReadSpace() << ") than " << m_blockSize*2 << " to read and write buffer fill (" << m_writeBufferFill << ") is not close to end frame (" << m_lastModelEndFrame << ")" << endl; Chris@193: #endif Chris@43: return; Chris@43: } Chris@43: } Chris@43: break; Chris@43: } Chris@43: } Chris@43: Chris@436: sv_frame_t rf = m_readBufferFill; Chris@43: RingBuffer *rb = getReadRingBuffer(0); Chris@43: if (rb) { Chris@366: int rs = rb->getReadSpace(); Chris@43: //!!! incorrect when in non-contiguous selection, see comments elsewhere Chris@293: // cout << "rs = " << rs << endl; Chris@43: if (rs < rf) rf -= rs; Chris@43: else rf = 0; Chris@43: } Chris@43: Chris@193: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "AudioCallbackPlaySource::unifyRingBuffers: m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << endl; Chris@193: #endif Chris@43: Chris@436: sv_frame_t wf = m_writeBufferFill; Chris@436: sv_frame_t skip = 0; Chris@366: for (int c = 0; c < getTargetChannelCount(); ++c) { Chris@43: RingBuffer *wb = getWriteRingBuffer(c); Chris@43: if (wb) { Chris@43: if (c == 0) { Chris@43: Chris@366: int wrs = wb->getReadSpace(); Chris@293: // cout << "wrs = " << wrs << endl; Chris@43: Chris@43: if (wrs < wf) wf -= wrs; Chris@43: else wf = 0; Chris@293: // cout << "wf = " << wf << endl; Chris@43: Chris@43: if (wf < rf) skip = rf - wf; Chris@43: if (skip == 0) break; Chris@43: } Chris@43: Chris@293: // cout << "skipping " << skip << endl; Chris@436: wb->skip(int(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@193: #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING Chris@563: cout << "unified" << endl; Chris@193: #endif 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@293: cout << "AudioCallbackPlaySourceFillThread starting" << 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@293: cout << "AudioCallbackPlaySourceFillThread: not waiting" << 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@436: double ms = 100; Chris@43: if (s.getSourceSampleRate() > 0) { Chris@436: ms = double(s.m_ringBufferSize) / 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@293: if (!s.m_playing) cout << endl; Chris@293: cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << endl; Chris@43: #endif Chris@43: Chris@366: s.m_condition.wait(&s.m_mutex, int(ms)); Chris@43: } Chris@43: Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySourceFillThread: awoken" << endl; Chris@43: #endif Chris@43: Chris@43: work = false; Chris@43: Chris@103: if (!s.getSourceSampleRate()) { Chris@103: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySourceFillThread: source sample rate is zero" << endl; Chris@103: #endif Chris@103: continue; Chris@103: } Chris@43: Chris@43: bool playing = s.m_playing; Chris@43: Chris@43: if (playing && !previouslyPlaying) { Chris@43: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@293: cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << endl; Chris@43: #endif Chris@366: for (int 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: