Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@1: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "AudioCallbackPlaySource.h" Chris@0: Chris@0: #include "AudioGenerator.h" Chris@0: Chris@0: #include "base/Model.h" Chris@0: #include "base/ViewManager.h" Chris@0: #include "model/DenseTimeValueModel.h" Chris@0: #include "model/SparseOneDimensionalModel.h" Chris@0: #include "dsp/timestretching/IntegerTimeStretcher.h" Chris@0: Chris@0: #include Chris@0: Chris@0: //#define DEBUG_AUDIO_PLAY_SOURCE 1 Chris@0: Chris@0: //const size_t AudioCallbackPlaySource::m_ringBufferSize = 102400; Chris@0: const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071; Chris@0: Chris@0: AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) : Chris@0: m_viewManager(manager), Chris@0: m_audioGenerator(new AudioGenerator(manager)), Chris@0: m_bufferCount(0), Chris@0: m_blockSize(1024), Chris@0: m_sourceSampleRate(0), Chris@0: m_targetSampleRate(0), Chris@0: m_playLatency(0), Chris@0: m_playing(false), Chris@0: m_exiting(false), Chris@0: m_bufferedToFrame(0), Chris@0: m_outputLeft(0.0), Chris@0: m_outputRight(0.0), Chris@0: m_slowdownCounter(0), Chris@0: m_timeStretcher(0), Chris@0: m_fillThread(0), Chris@0: m_converter(0) Chris@0: { Chris@0: // preallocate some slots, to avoid reallocation in an Chris@0: // un-thread-safe manner later Chris@0: while (m_buffers.size() < 20) m_buffers.push_back(0); Chris@0: Chris@0: m_viewManager->setAudioPlaySource(this); Chris@3: Chris@3: connect(m_viewManager, SIGNAL(selectionChanged()), Chris@3: this, SLOT(selectionChanged())); Chris@3: connect(m_viewManager, SIGNAL(playLoopModeChanged()), Chris@3: this, SLOT(playLoopModeChanged())); Chris@3: connect(m_viewManager, SIGNAL(playSelectionModeChanged()), Chris@3: this, SLOT(playSelectionModeChanged())); Chris@0: } Chris@0: Chris@0: AudioCallbackPlaySource::~AudioCallbackPlaySource() Chris@0: { Chris@0: m_exiting = true; Chris@0: Chris@0: if (m_fillThread) { Chris@0: m_condition.wakeAll(); Chris@0: m_fillThread->wait(); Chris@0: delete m_fillThread; Chris@0: } Chris@0: Chris@0: clearModels(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::addModel(Model *model) Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@0: m_models.insert(model); Chris@0: Chris@0: bool buffersChanged = false, srChanged = false; Chris@0: Chris@0: if (m_sourceSampleRate == 0) { Chris@0: Chris@0: m_sourceSampleRate = model->getSampleRate(); Chris@0: srChanged = true; Chris@0: Chris@0: } else if (model->getSampleRate() != m_sourceSampleRate) { Chris@0: std::cerr << "AudioCallbackPlaySource::addModel: ERROR: " Chris@0: << "New model sample rate does not match" << std::endl Chris@0: << "existing model(s) (new " << model->getSampleRate() Chris@0: << " vs " << m_sourceSampleRate Chris@0: << "), playback will be wrong" Chris@0: << std::endl; Chris@0: } Chris@0: Chris@0: size_t sz = m_ringBufferSize; Chris@0: if (m_bufferCount > 0) { Chris@0: sz = m_buffers[0]->getSize(); Chris@0: } Chris@0: Chris@0: size_t modelChannels = 1; Chris@0: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@0: if (dtvm) modelChannels = dtvm->getChannelCount(); Chris@0: Chris@0: while (m_bufferCount < modelChannels) { Chris@0: Chris@0: if (m_buffers.size() < modelChannels) { Chris@0: // This is a hideously chancy operation -- the RT thread Chris@0: // could be using this vector. We allocated several slots Chris@0: // in the ctor to avoid exactly this, but if we ever end Chris@0: // up with more channels than that (!) then we're just Chris@0: // going to have to risk it Chris@0: m_buffers.push_back(new RingBuffer(sz)); Chris@0: Chris@0: } else { Chris@0: // The usual case Chris@0: m_buffers[m_bufferCount] = new RingBuffer(sz); Chris@0: } Chris@0: Chris@0: ++m_bufferCount; Chris@0: buffersChanged = true; Chris@0: } Chris@0: Chris@0: if (buffersChanged) { Chris@0: m_audioGenerator->setTargetChannelCount(m_bufferCount); Chris@0: } Chris@0: Chris@0: if (buffersChanged || srChanged) { Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@0: m_converter = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: m_audioGenerator->addModel(model); Chris@0: Chris@0: m_mutex.unlock(); Chris@0: Chris@0: if (!m_fillThread) { Chris@0: m_fillThread = new AudioCallbackPlaySourceFillThread(*this); Chris@0: m_fillThread->start(); Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl; Chris@0: #endif Chris@0: Chris@1: if (buffersChanged || srChanged) { Chris@1: emit modelReplaced(); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::removeModel(Model *model) Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@0: m_models.erase(model); Chris@0: Chris@0: if (m_models.empty()) { Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@0: m_converter = 0; Chris@0: } Chris@0: m_sourceSampleRate = 0; Chris@0: } Chris@0: Chris@0: m_audioGenerator->removeModel(model); Chris@0: Chris@0: m_mutex.unlock(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::clearModels() Chris@0: { Chris@0: m_mutex.lock(); Chris@0: Chris@0: m_models.clear(); Chris@0: Chris@0: if (m_converter) { Chris@0: src_delete(m_converter); Chris@0: m_converter = 0; Chris@0: } Chris@0: Chris@0: m_audioGenerator->clearModels(); Chris@0: Chris@0: m_sourceSampleRate = 0; Chris@0: Chris@0: m_mutex.unlock(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::play(size_t startFrame) Chris@0: { Chris@0: // The fill thread will automatically empty its buffers before Chris@0: // starting again if we have not so far been playing, but not if Chris@0: // we're just re-seeking. Chris@0: Chris@0: if (m_playing) { Chris@0: m_mutex.lock(); Chris@0: m_bufferedToFrame = startFrame; Chris@0: for (size_t c = 0; c < m_bufferCount; ++c) { Chris@0: getRingBuffer(c).reset(); Chris@0: if (m_converter) src_reset(m_converter); Chris@0: } Chris@0: m_mutex.unlock(); Chris@0: } else { Chris@0: m_bufferedToFrame = startFrame; Chris@0: } Chris@0: Chris@0: m_audioGenerator->reset(); Chris@0: Chris@0: m_playing = true; Chris@0: m_condition.wakeAll(); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::stop() Chris@0: { Chris@0: m_playing = false; Chris@0: m_condition.wakeAll(); Chris@0: } Chris@0: Chris@0: void Chris@3: AudioCallbackPlaySource::selectionChanged() Chris@3: { Chris@3: if (m_viewManager->getPlaySelectionMode()) { Chris@3: m_mutex.lock(); Chris@3: for (size_t c = 0; c < m_bufferCount; ++c) { Chris@3: getRingBuffer(c).reset(); Chris@3: } Chris@3: m_mutex.unlock(); Chris@3: } Chris@3: } Chris@3: Chris@3: void Chris@3: AudioCallbackPlaySource::playLoopModeChanged() Chris@3: { Chris@3: m_mutex.lock(); Chris@3: for (size_t c = 0; c < m_bufferCount; ++c) { Chris@3: getRingBuffer(c).reset(); Chris@3: } Chris@3: m_mutex.unlock(); Chris@3: } Chris@3: Chris@3: void Chris@3: AudioCallbackPlaySource::playSelectionModeChanged() Chris@3: { Chris@3: if (!m_viewManager->getSelections().empty()) { Chris@3: m_mutex.lock(); Chris@3: for (size_t c = 0; c < m_bufferCount; ++c) { Chris@3: getRingBuffer(c).reset(); Chris@3: } Chris@3: m_mutex.unlock(); Chris@3: } Chris@3: } Chris@3: Chris@3: void Chris@0: AudioCallbackPlaySource::setTargetBlockSize(size_t size) Chris@0: { Chris@0: std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl; Chris@0: m_blockSize = size; Chris@0: for (size_t i = 0; i < m_bufferCount; ++i) { Chris@0: getRingBuffer(i).resize(m_ringBufferSize); Chris@0: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetBlockSize() const Chris@0: { Chris@0: std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl; Chris@0: return m_blockSize; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setTargetPlayLatency(size_t latency) Chris@0: { Chris@0: m_playLatency = latency; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetPlayLatency() const Chris@0: { Chris@0: return m_playLatency; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getCurrentPlayingFrame() Chris@0: { Chris@0: bool resample = false; Chris@0: double ratio = 1.0; Chris@0: Chris@0: if (getSourceSampleRate() != getTargetSampleRate()) { Chris@0: resample = true; Chris@0: ratio = double(getSourceSampleRate()) / double(getTargetSampleRate()); Chris@0: } Chris@0: Chris@0: size_t readSpace = 0; Chris@0: for (size_t c = 0; c < getSourceChannelCount(); ++c) { Chris@0: size_t spaceHere = getRingBuffer(c).getReadSpace(); Chris@0: if (c == 0 || spaceHere < readSpace) readSpace = spaceHere; Chris@0: } Chris@0: Chris@0: if (resample) { Chris@0: readSpace = size_t(readSpace * ratio + 0.1); Chris@0: } Chris@0: Chris@0: size_t latency = m_playLatency; Chris@0: if (resample) latency = size_t(m_playLatency * ratio + 0.1); Chris@0: Chris@0: TimeStretcherData *timeStretcher = m_timeStretcher; Chris@0: if (timeStretcher) { Chris@0: latency += timeStretcher->getStretcher(0)->getProcessingLatency(); Chris@0: } Chris@0: Chris@3: latency += readSpace; Chris@3: size_t bufferedFrame = m_bufferedToFrame; Chris@3: Chris@3: size_t framePlaying = bufferedFrame; Chris@3: if (framePlaying > latency) framePlaying -= latency; Chris@3: else framePlaying = 0; Chris@3: Chris@3: if (!m_viewManager->getPlaySelectionMode()) { Chris@3: return framePlaying; Chris@0: } Chris@0: Chris@3: ViewManager::SelectionList selections = m_viewManager->getSelections(); Chris@3: if (selections.empty()) { Chris@3: return framePlaying; Chris@3: } Chris@3: Chris@3: ViewManager::SelectionList::const_iterator i; Chris@3: Chris@3: for (i = selections.begin(); i != selections.end(); ++i) { Chris@3: if (i->contains(bufferedFrame)) break; Chris@3: } Chris@3: Chris@3: size_t f = bufferedFrame; Chris@3: Chris@3: std::cerr << "getCurrentPlayingFrame: f=" << f << ", latency=" << latency << std::endl; Chris@3: Chris@3: if (i == selections.end()) { Chris@3: --i; Chris@3: if (i->getEndFrame() + latency < f) { Chris@3: return framePlaying; Chris@3: } else { Chris@3: std::cerr << "latency <- " << latency << "-(" << f << "-" << i->getEndFrame() << ")" << std::endl; Chris@3: latency -= (f - i->getEndFrame()); Chris@3: f = i->getEndFrame(); Chris@3: } Chris@3: } Chris@3: Chris@3: std::cerr << "i=(" << i->getStartFrame() << "," << i->getEndFrame() << ") f=" << f << ", latency=" << latency << std::endl; Chris@3: Chris@3: while (latency > 0) { Chris@3: size_t offset = f - i->getStartFrame(); Chris@3: if (offset >= latency) { Chris@3: if (f > latency) { Chris@3: framePlaying = f - latency; Chris@3: } else { Chris@3: framePlaying = 0; Chris@3: } Chris@3: break; Chris@3: } else { Chris@3: if (i == selections.begin()) { Chris@3: if (m_viewManager->getPlayLoopMode()) { Chris@3: i = selections.end(); Chris@3: } Chris@3: } Chris@3: latency -= offset; Chris@3: --i; Chris@3: f = i->getEndFrame(); Chris@3: } Chris@3: } Chris@0: Chris@0: return framePlaying; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setOutputLevels(float left, float right) Chris@0: { Chris@0: m_outputLeft = left; Chris@0: m_outputRight = right; Chris@0: } Chris@0: Chris@0: bool Chris@0: AudioCallbackPlaySource::getOutputLevels(float &left, float &right) Chris@0: { Chris@0: left = m_outputLeft; Chris@0: right = m_outputRight; Chris@0: return true; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setTargetSampleRate(size_t sr) Chris@0: { Chris@0: m_targetSampleRate = sr; Chris@1: Chris@1: if (getSourceSampleRate() != getTargetSampleRate()) { Chris@1: Chris@1: int err = 0; Chris@1: m_converter = src_new(SRC_SINC_BEST_QUALITY, m_bufferCount, &err); Chris@1: if (!m_converter) { Chris@1: std::cerr Chris@1: << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: " Chris@1: << src_strerror(err) << std::endl; Chris@1: } Chris@1: Chris@1: emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate()); Chris@1: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getTargetSampleRate() const Chris@0: { Chris@0: if (m_targetSampleRate) return m_targetSampleRate; Chris@0: else return getSourceSampleRate(); Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceChannelCount() const Chris@0: { Chris@0: return m_bufferCount; Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceSampleRate() const Chris@0: { Chris@0: return m_sourceSampleRate; Chris@0: } Chris@0: Chris@0: AudioCallbackPlaySource::TimeStretcherData::TimeStretcherData(size_t channels, Chris@0: size_t factor, Chris@0: size_t blockSize) : Chris@0: m_factor(factor), Chris@0: m_blockSize(blockSize) Chris@0: { Chris@0: std::cerr << "TimeStretcherData::TimeStretcherData(" << channels << ", " << factor << ", " << blockSize << ")" << std::endl; Chris@0: Chris@0: for (size_t ch = 0; ch < channels; ++ch) { Chris@0: m_stretcher[ch] = StretcherBuffer Chris@0: //!!! We really need to measure performance and work out Chris@0: //what sort of quality level to use -- or at least to Chris@0: //allow the user to configure it Chris@0: (new IntegerTimeStretcher(factor, blockSize, 128), Chris@0: new double[blockSize * factor]); Chris@0: } Chris@0: m_stretchInputBuffer = new double[blockSize]; Chris@0: } Chris@0: Chris@0: AudioCallbackPlaySource::TimeStretcherData::~TimeStretcherData() Chris@0: { Chris@0: std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl; Chris@0: Chris@0: while (!m_stretcher.empty()) { Chris@0: delete m_stretcher.begin()->second.first; Chris@0: delete[] m_stretcher.begin()->second.second; Chris@0: m_stretcher.erase(m_stretcher.begin()); Chris@0: } Chris@0: delete m_stretchInputBuffer; Chris@0: } Chris@0: Chris@0: IntegerTimeStretcher * Chris@0: AudioCallbackPlaySource::TimeStretcherData::getStretcher(size_t channel) Chris@0: { Chris@0: return m_stretcher[channel].first; Chris@0: } Chris@0: Chris@0: double * Chris@0: AudioCallbackPlaySource::TimeStretcherData::getOutputBuffer(size_t channel) Chris@0: { Chris@0: return m_stretcher[channel].second; Chris@0: } Chris@0: Chris@0: double * Chris@0: AudioCallbackPlaySource::TimeStretcherData::getInputBuffer() Chris@0: { Chris@0: return m_stretchInputBuffer; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::TimeStretcherData::run(size_t channel) Chris@0: { Chris@0: getStretcher(channel)->process(getInputBuffer(), Chris@0: getOutputBuffer(channel), Chris@0: m_blockSize); Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::setSlowdownFactor(size_t factor) Chris@0: { Chris@0: // Avoid locks -- create, assign, mark old one for scavenging Chris@0: // later (as a call to getSourceSamples may still be using it) Chris@0: Chris@0: TimeStretcherData *existingStretcher = m_timeStretcher; Chris@0: Chris@0: if (existingStretcher && existingStretcher->getFactor() == factor) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (factor > 1) { Chris@0: TimeStretcherData *newStretcher = new TimeStretcherData Chris@0: (getSourceChannelCount(), factor, getTargetBlockSize()); Chris@0: m_slowdownCounter = 0; Chris@0: m_timeStretcher = newStretcher; Chris@0: } else { Chris@0: m_timeStretcher = 0; Chris@0: } Chris@0: Chris@0: if (existingStretcher) { Chris@0: m_timeStretcherScavenger.claim(existingStretcher); Chris@0: } Chris@0: } Chris@0: Chris@0: size_t Chris@0: AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer) Chris@0: { Chris@0: if (!m_playing) { Chris@0: for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { Chris@0: for (size_t i = 0; i < count; ++i) { Chris@0: buffer[ch][i] = 0.0; Chris@0: } Chris@0: } Chris@0: return 0; Chris@0: } Chris@0: Chris@0: TimeStretcherData *timeStretcher = m_timeStretcher; Chris@0: Chris@0: if (!timeStretcher || timeStretcher->getFactor() == 1) { Chris@0: Chris@0: size_t got = 0; Chris@0: Chris@0: for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { Chris@0: Chris@0: RingBuffer &rb = *m_buffers[ch]; Chris@0: Chris@0: // this is marginally more likely to leave our channels in Chris@0: // sync after a processing failure than just passing "count": Chris@0: size_t request = count; Chris@0: if (ch > 0) request = got; Chris@0: Chris@0: got = rb.read(buffer[ch], request); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", signalling for more (possibly)" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { Chris@0: for (size_t i = got; i < count; ++i) { Chris@0: buffer[ch][i] = 0.0; Chris@0: } Chris@0: } Chris@0: Chris@0: m_condition.wakeAll(); Chris@0: return got; Chris@0: } Chris@0: Chris@0: if (m_slowdownCounter == 0) { Chris@0: Chris@0: size_t got = 0; Chris@0: double *ib = timeStretcher->getInputBuffer(); Chris@0: Chris@0: for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { Chris@0: Chris@0: RingBuffer &rb = *m_buffers[ch]; Chris@0: size_t request = count; Chris@0: if (ch > 0) request = got; // see above Chris@0: got = rb.read(buffer[ch], request); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", running time stretcher" << std::endl; Chris@0: #endif Chris@0: Chris@0: for (size_t i = 0; i < count; ++i) { Chris@0: ib[i] = buffer[ch][i]; Chris@0: } Chris@0: Chris@0: timeStretcher->run(ch); Chris@0: } Chris@0: Chris@0: } else if (m_slowdownCounter >= timeStretcher->getFactor()) { Chris@0: // reset this in case the factor has changed leaving the Chris@0: // counter out of range Chris@0: m_slowdownCounter = 0; Chris@0: } Chris@0: Chris@0: for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { Chris@0: Chris@0: double *ob = timeStretcher->getOutputBuffer(ch); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cerr << "AudioCallbackPlaySource::getSamples: Copying from (" << (m_slowdownCounter * count) << "," << count << ") to buffer" << std::endl; Chris@0: #endif Chris@0: Chris@0: for (size_t i = 0; i < count; ++i) { Chris@0: buffer[ch][i] = ob[m_slowdownCounter * count + i]; Chris@0: } Chris@0: } Chris@0: Chris@0: if (m_slowdownCounter == 0) m_condition.wakeAll(); Chris@0: m_slowdownCounter = (m_slowdownCounter + 1) % timeStretcher->getFactor(); Chris@0: return count; Chris@0: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::fillBuffers() Chris@0: { Chris@0: static float *tmp = 0; Chris@0: static size_t tmpSize = 0; Chris@0: Chris@0: size_t space = 0; Chris@0: for (size_t c = 0; c < m_bufferCount; ++c) { Chris@0: size_t spaceHere = getRingBuffer(c).getWriteSpace(); Chris@0: if (c == 0 || spaceHere < space) space = spaceHere; Chris@0: } Chris@0: Chris@0: if (space == 0) return; Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl; Chris@0: #endif Chris@0: Chris@0: size_t f = m_bufferedToFrame; Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "buffered to " << f << " already" << std::endl; Chris@0: #endif Chris@0: Chris@0: bool resample = (getSourceSampleRate() != getTargetSampleRate()); Chris@1: Chris@1: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@1: std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl; Chris@1: #endif Chris@1: Chris@0: size_t channels = getSourceChannelCount(); Chris@0: size_t orig = space; Chris@0: size_t got = 0; Chris@0: Chris@0: static float **bufferPtrs = 0; Chris@0: static size_t bufferPtrCount = 0; Chris@0: Chris@0: if (bufferPtrCount < channels) { Chris@0: if (bufferPtrs) delete[] bufferPtrs; Chris@0: bufferPtrs = new float *[channels]; Chris@0: bufferPtrCount = channels; Chris@0: } Chris@0: Chris@0: size_t generatorBlockSize = m_audioGenerator->getBlockSize(); Chris@0: Chris@1: if (resample && !m_converter) { Chris@1: static bool warned = false; Chris@1: if (!warned) { Chris@1: std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl; Chris@1: warned = true; Chris@1: } Chris@1: } Chris@1: Chris@0: if (resample && m_converter) { Chris@0: Chris@0: double ratio = Chris@0: double(getTargetSampleRate()) / double(getSourceSampleRate()); Chris@0: orig = size_t(orig / ratio + 0.1); Chris@0: Chris@0: // orig must be a multiple of generatorBlockSize Chris@0: orig = (orig / generatorBlockSize) * generatorBlockSize; Chris@0: if (orig == 0) return; Chris@0: Chris@0: size_t work = std::max(orig, space); Chris@0: Chris@0: // We only allocate one buffer, but we use it in two halves. Chris@0: // We place the non-interleaved values in the second half of Chris@0: // the buffer (orig samples for channel 0, orig samples for Chris@0: // channel 1 etc), and then interleave them into the first Chris@0: // half of the buffer. Then we resample back into the second Chris@0: // half (interleaved) and de-interleave the results back to Chris@0: // the start of the buffer for insertion into the ringbuffers. Chris@0: // What a faff -- especially as we've already de-interleaved Chris@0: // the audio data from the source file elsewhere before we Chris@0: // even reach this point. Chris@0: Chris@0: if (tmpSize < channels * work * 2) { Chris@0: delete[] tmp; Chris@0: tmp = new float[channels * work * 2]; Chris@0: tmpSize = channels * work * 2; Chris@0: } Chris@0: Chris@0: float *nonintlv = tmp + channels * work; Chris@0: float *intlv = tmp; Chris@0: float *srcout = tmp + channels * work; Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < orig; ++i) { Chris@0: nonintlv[channels * i + c] = 0.0f; Chris@0: } Chris@0: } Chris@0: Chris@3: for (size_t c = 0; c < channels; ++c) { Chris@3: bufferPtrs[c] = nonintlv + c * orig; Chris@3: } Chris@0: Chris@3: bool ended = !mixModels(f, orig, bufferPtrs); Chris@3: got = orig; Chris@0: Chris@0: // and interleave into first half Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < orig; ++i) { Chris@0: float sample = 0; Chris@0: if (i < got) { Chris@0: sample = nonintlv[c * orig + i]; Chris@0: } Chris@0: intlv[channels * i + c] = sample; Chris@0: } Chris@0: } Chris@0: Chris@0: SRC_DATA data; Chris@0: data.data_in = intlv; Chris@0: data.data_out = srcout; Chris@0: data.input_frames = orig; Chris@0: data.output_frames = work; Chris@0: data.src_ratio = ratio; Chris@0: data.end_of_input = 0; Chris@0: Chris@0: int err = src_process(m_converter, &data); Chris@0: size_t toCopy = size_t(work * ratio + 0.1); Chris@0: Chris@0: if (err) { Chris@0: std::cerr Chris@0: << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: " Chris@0: << src_strerror(err) << std::endl; Chris@0: //!!! Then what? Chris@0: } else { Chris@0: got = data.input_frames_used; Chris@0: toCopy = data.output_frames_gen; Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: for (size_t i = 0; i < toCopy; ++i) { Chris@0: tmp[i] = srcout[channels * i + c]; Chris@0: } Chris@0: getRingBuffer(c).write(tmp, toCopy); Chris@0: } Chris@3: Chris@3: m_bufferedToFrame = f; Chris@0: Chris@0: } else { Chris@0: Chris@0: // space must be a multiple of generatorBlockSize Chris@0: space = (space / generatorBlockSize) * generatorBlockSize; Chris@0: if (space == 0) return; Chris@0: Chris@0: if (tmpSize < channels * space) { Chris@0: delete[] tmp; Chris@0: tmp = new float[channels * space]; Chris@0: tmpSize = channels * space; Chris@0: } Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: Chris@0: bufferPtrs[c] = tmp + c * space; Chris@3: Chris@0: for (size_t i = 0; i < space; ++i) { Chris@0: tmp[c * space + i] = 0.0f; Chris@0: } Chris@0: } Chris@0: Chris@3: bool ended = !mixModels(f, space, bufferPtrs); Chris@0: Chris@0: for (size_t c = 0; c < channels; ++c) { Chris@0: Chris@3: getRingBuffer(c).write(bufferPtrs[c], space); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cerr << "Wrote " << got << " frames for ch " << c << ", now " Chris@0: << getRingBuffer(c).getReadSpace() << " to read" Chris@0: << std::endl; Chris@0: #endif Chris@0: } Chris@3: Chris@3: m_bufferedToFrame = f; Chris@3: //!!! how do we know when ended? need to mark up a fully-buffered flag and check this if we find the buffers empty in getSourceSamples Chris@0: } Chris@3: } Chris@3: Chris@3: bool Chris@3: AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers) Chris@3: { Chris@3: size_t processed = 0; Chris@3: size_t chunkStart = frame; Chris@3: size_t chunkSize = count; Chris@3: size_t nextChunkStart = chunkStart + chunkSize; Chris@0: Chris@3: bool useSelection = (m_viewManager->getPlaySelectionMode() && Chris@3: !m_viewManager->getSelections().empty()); Chris@3: Chris@3: static float **chunkBufferPtrs = 0; Chris@3: static size_t chunkBufferPtrCount = 0; Chris@3: size_t channels = getSourceChannelCount(); Chris@3: Chris@3: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@3: std::cerr << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl; Chris@3: #endif Chris@3: Chris@3: if (chunkBufferPtrCount < channels) { Chris@3: if (chunkBufferPtrs) delete[] chunkBufferPtrs; Chris@3: chunkBufferPtrs = new float *[channels]; Chris@3: chunkBufferPtrCount = channels; Chris@3: } Chris@3: Chris@3: for (size_t c = 0; c < channels; ++c) { Chris@3: chunkBufferPtrs[c] = buffers[c]; Chris@3: } Chris@3: Chris@3: while (processed < count) { Chris@3: Chris@3: chunkSize = count - processed; Chris@3: nextChunkStart = chunkStart + chunkSize; Chris@3: Chris@3: if (useSelection) { Chris@3: Chris@3: Selection selection = Chris@3: m_viewManager->getContainingSelection(chunkStart, true); Chris@3: Chris@3: if (selection.isEmpty()) { Chris@3: if (m_viewManager->getPlayLoopMode()) { Chris@3: selection = *m_viewManager->getSelections().begin(); Chris@3: chunkStart = selection.getStartFrame(); Chris@3: } Chris@3: } Chris@3: Chris@3: if (selection.isEmpty()) { Chris@3: Chris@3: chunkSize = 0; Chris@3: nextChunkStart = chunkStart; Chris@3: Chris@3: } else { Chris@3: Chris@3: if (chunkStart < selection.getStartFrame()) { Chris@3: chunkStart = selection.getStartFrame(); Chris@3: } Chris@3: Chris@3: nextChunkStart = std::min(chunkStart + chunkSize, Chris@3: selection.getEndFrame()); Chris@3: Chris@3: chunkSize = nextChunkStart - chunkStart; Chris@3: } Chris@3: } Chris@3: Chris@3: if (!chunkSize) { Chris@3: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@3: std::cerr << "Ending selection playback at " << nextChunkStart << std::endl; Chris@3: #endif Chris@3: // We need to maintain full buffers so that the other Chris@3: // thread can tell where it's got to in the playback -- so Chris@3: // return the full amount here Chris@3: frame = frame + count; Chris@3: return false; Chris@3: } Chris@3: Chris@3: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@3: std::cerr << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl; Chris@3: #endif Chris@3: Chris@3: size_t got = 0; Chris@3: Chris@3: for (std::set::iterator mi = m_models.begin(); Chris@3: mi != m_models.end(); ++mi) { Chris@3: Chris@3: got = m_audioGenerator->mixModel(*mi, chunkStart, Chris@3: chunkSize, chunkBufferPtrs); Chris@3: } Chris@3: Chris@3: for (size_t c = 0; c < channels; ++c) { Chris@3: chunkBufferPtrs[c] += chunkSize; Chris@3: } Chris@3: Chris@3: processed += chunkSize; Chris@3: chunkStart = nextChunkStart; Chris@3: } Chris@3: Chris@3: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@3: std::cerr << "Returning selection playback at " << nextChunkStart << std::endl; Chris@3: #endif Chris@3: Chris@3: frame = nextChunkStart; Chris@3: return true; Chris@3: } Chris@0: Chris@0: void Chris@0: AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run() Chris@0: { Chris@0: AudioCallbackPlaySource &s(m_source); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl; Chris@0: #endif Chris@0: Chris@0: s.m_mutex.lock(); Chris@0: Chris@0: bool previouslyPlaying = s.m_playing; Chris@0: Chris@0: while (!s.m_exiting) { Chris@0: Chris@0: s.m_timeStretcherScavenger.scavenge(); Chris@0: Chris@0: float ms = 100; Chris@0: if (s.getSourceSampleRate() > 0) { Chris@0: ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0; Chris@0: } Chris@0: Chris@0: if (!s.m_playing) ms *= 10; Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms/4 << "ms..." << std::endl; Chris@0: #endif Chris@0: Chris@0: s.m_condition.wait(&s.m_mutex, size_t(ms / 4)); Chris@0: Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl; Chris@0: #endif Chris@0: Chris@0: if (!s.getSourceSampleRate()) continue; Chris@0: Chris@0: bool playing = s.m_playing; Chris@0: Chris@0: if (playing && !previouslyPlaying) { Chris@0: #ifdef DEBUG_AUDIO_PLAY_SOURCE Chris@0: std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl; Chris@0: #endif Chris@0: for (size_t c = 0; c < s.getSourceChannelCount(); ++c) { Chris@0: s.getRingBuffer(c).reset(); Chris@0: } Chris@0: } Chris@0: previouslyPlaying = playing; Chris@0: Chris@0: if (!playing) continue; Chris@0: Chris@0: s.fillBuffers(); Chris@0: } Chris@0: Chris@0: s.m_mutex.unlock(); Chris@0: } Chris@0: Chris@0: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "AudioCallbackPlaySource.moc.cpp" Chris@0: #endif Chris@0: