Mercurial > hg > svapp
changeset 0:db6fcbd4405c
initial import
author | Chris Cannam |
---|---|
date | Tue, 10 Jan 2006 16:33:16 +0000 |
parents | |
children | 97c69acdcb82 |
files | audioio/AudioCallbackPlaySource.cpp audioio/AudioCallbackPlaySource.h audioio/AudioCallbackPlayTarget.cpp audioio/AudioCallbackPlayTarget.h audioio/AudioCoreAudioTarget.cpp audioio/AudioCoreAudioTarget.h audioio/AudioGenerator.cpp audioio/AudioGenerator.h audioio/AudioJACKTarget.cpp audioio/AudioJACKTarget.h audioio/AudioPortAudioTarget.cpp audioio/AudioPortAudioTarget.h audioio/AudioTargetFactory.cpp audioio/AudioTargetFactory.h |
diffstat | 14 files changed, 2178 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCallbackPlaySource.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,759 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#include "AudioCallbackPlaySource.h" + +#include "AudioGenerator.h" + +#include "base/Model.h" +#include "base/ViewManager.h" +#include "model/DenseTimeValueModel.h" +#include "model/SparseOneDimensionalModel.h" +#include "dsp/timestretching/IntegerTimeStretcher.h" + +#include <iostream> + +//#define DEBUG_AUDIO_PLAY_SOURCE 1 + +//const size_t AudioCallbackPlaySource::m_ringBufferSize = 102400; +const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071; + +AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) : + m_viewManager(manager), + m_audioGenerator(new AudioGenerator(manager)), + m_bufferCount(0), + m_blockSize(1024), + m_sourceSampleRate(0), + m_targetSampleRate(0), + m_playLatency(0), + m_playing(false), + m_exiting(false), + m_bufferedToFrame(0), + m_outputLeft(0.0), + m_outputRight(0.0), + m_slowdownCounter(0), + m_timeStretcher(0), + m_fillThread(0), + m_converter(0) +{ + // preallocate some slots, to avoid reallocation in an + // un-thread-safe manner later + while (m_buffers.size() < 20) m_buffers.push_back(0); + + m_viewManager->setAudioPlaySource(this); +} + +AudioCallbackPlaySource::~AudioCallbackPlaySource() +{ + m_exiting = true; + + if (m_fillThread) { + m_condition.wakeAll(); + m_fillThread->wait(); + delete m_fillThread; + } + + clearModels(); +} + +void +AudioCallbackPlaySource::addModel(Model *model) +{ + m_mutex.lock(); + + m_models.insert(model); + + bool buffersChanged = false, srChanged = false; + + if (m_sourceSampleRate == 0) { + + m_sourceSampleRate = model->getSampleRate(); + srChanged = true; + + } else if (model->getSampleRate() != m_sourceSampleRate) { + std::cerr << "AudioCallbackPlaySource::addModel: ERROR: " + << "New model sample rate does not match" << std::endl + << "existing model(s) (new " << model->getSampleRate() + << " vs " << m_sourceSampleRate + << "), playback will be wrong" + << std::endl; + } + + size_t sz = m_ringBufferSize; + if (m_bufferCount > 0) { + sz = m_buffers[0]->getSize(); + } + + size_t modelChannels = 1; + DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model); + if (dtvm) modelChannels = dtvm->getChannelCount(); + + while (m_bufferCount < modelChannels) { + + if (m_buffers.size() < modelChannels) { + // This is a hideously chancy operation -- the RT thread + // could be using this vector. We allocated several slots + // in the ctor to avoid exactly this, but if we ever end + // up with more channels than that (!) then we're just + // going to have to risk it + m_buffers.push_back(new RingBuffer<float>(sz)); + + } else { + // The usual case + m_buffers[m_bufferCount] = new RingBuffer<float>(sz); + } + + ++m_bufferCount; + buffersChanged = true; + } + + if (buffersChanged) { + m_audioGenerator->setTargetChannelCount(m_bufferCount); + } + + if (buffersChanged || srChanged) { + + if (m_converter) { + src_delete(m_converter); + m_converter = 0; + } + + if (getSourceSampleRate() != getTargetSampleRate()) { + + int err = 0; + m_converter = src_new(SRC_SINC_FASTEST, m_bufferCount, &err); + if (!m_converter) { + std::cerr + << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: " + << src_strerror(err) << std::endl; + } + } + } + + m_audioGenerator->addModel(model); + + m_mutex.unlock(); + + if (!m_fillThread) { + m_fillThread = new AudioCallbackPlaySourceFillThread(*this); + m_fillThread->start(); + } + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl; +#endif + emit modelReplaced(); + + if (srChanged && (getSourceSampleRate() != getTargetSampleRate())) { + emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate()); + } +} + +void +AudioCallbackPlaySource::removeModel(Model *model) +{ + m_mutex.lock(); + + m_models.erase(model); + + if (m_models.empty()) { + if (m_converter) { + src_delete(m_converter); + m_converter = 0; + } + m_sourceSampleRate = 0; + } + + m_audioGenerator->removeModel(model); + + m_mutex.unlock(); +} + +void +AudioCallbackPlaySource::clearModels() +{ + m_mutex.lock(); + + m_models.clear(); + + if (m_converter) { + src_delete(m_converter); + m_converter = 0; + } + + m_audioGenerator->clearModels(); + + m_sourceSampleRate = 0; + + m_mutex.unlock(); +} + +void +AudioCallbackPlaySource::play(size_t startFrame) +{ + // The fill thread will automatically empty its buffers before + // starting again if we have not so far been playing, but not if + // we're just re-seeking. + + if (m_playing) { + m_mutex.lock(); + m_bufferedToFrame = startFrame; + for (size_t c = 0; c < m_bufferCount; ++c) { + getRingBuffer(c).reset(); + if (m_converter) src_reset(m_converter); + } + m_mutex.unlock(); + } else { + m_bufferedToFrame = startFrame; + } + + m_audioGenerator->reset(); + + m_playing = true; + m_condition.wakeAll(); +} + +void +AudioCallbackPlaySource::stop() +{ + m_playing = false; + m_condition.wakeAll(); +} + +void +AudioCallbackPlaySource::setTargetBlockSize(size_t size) +{ + std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl; + m_blockSize = size; + for (size_t i = 0; i < m_bufferCount; ++i) { + getRingBuffer(i).resize(m_ringBufferSize); + } +} + +size_t +AudioCallbackPlaySource::getTargetBlockSize() const +{ + std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl; + return m_blockSize; +} + +void +AudioCallbackPlaySource::setTargetPlayLatency(size_t latency) +{ + m_playLatency = latency; +} + +size_t +AudioCallbackPlaySource::getTargetPlayLatency() const +{ + return m_playLatency; +} + +size_t +AudioCallbackPlaySource::getCurrentPlayingFrame() +{ + bool resample = false; + double ratio = 1.0; + + if (getSourceSampleRate() != getTargetSampleRate()) { + resample = true; + ratio = double(getSourceSampleRate()) / double(getTargetSampleRate()); + } + + size_t readSpace = 0; + for (size_t c = 0; c < getSourceChannelCount(); ++c) { + size_t spaceHere = getRingBuffer(c).getReadSpace(); + if (c == 0 || spaceHere < readSpace) readSpace = spaceHere; + } + + if (resample) { + readSpace = size_t(readSpace * ratio + 0.1); + } + + size_t lastRequestedFrame = 0; + if (m_bufferedToFrame > readSpace) { + lastRequestedFrame = m_bufferedToFrame - readSpace; + } + + size_t framePlaying = lastRequestedFrame; + + size_t latency = m_playLatency; + if (resample) latency = size_t(m_playLatency * ratio + 0.1); + + TimeStretcherData *timeStretcher = m_timeStretcher; + if (timeStretcher) { + latency += timeStretcher->getStretcher(0)->getProcessingLatency(); + } + + if (framePlaying > latency) { + framePlaying = framePlaying - latency; + } else { + framePlaying = 0; + } + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "getCurrentPlayingFrame: readSpace " << readSpace << ", lastRequestedFrame " << lastRequestedFrame << ", framePlaying " << framePlaying << ", latency " << latency << std::endl; +#endif + + return framePlaying; +} + +void +AudioCallbackPlaySource::setOutputLevels(float left, float right) +{ + m_outputLeft = left; + m_outputRight = right; +} + +bool +AudioCallbackPlaySource::getOutputLevels(float &left, float &right) +{ + left = m_outputLeft; + right = m_outputRight; + return true; +} + +void +AudioCallbackPlaySource::setTargetSampleRate(size_t sr) +{ + m_targetSampleRate = sr; +} + +size_t +AudioCallbackPlaySource::getTargetSampleRate() const +{ + if (m_targetSampleRate) return m_targetSampleRate; + else return getSourceSampleRate(); +} + +size_t +AudioCallbackPlaySource::getSourceChannelCount() const +{ + return m_bufferCount; +} + +size_t +AudioCallbackPlaySource::getSourceSampleRate() const +{ + return m_sourceSampleRate; +} + +AudioCallbackPlaySource::TimeStretcherData::TimeStretcherData(size_t channels, + size_t factor, + size_t blockSize) : + m_factor(factor), + m_blockSize(blockSize) +{ + std::cerr << "TimeStretcherData::TimeStretcherData(" << channels << ", " << factor << ", " << blockSize << ")" << std::endl; + + for (size_t ch = 0; ch < channels; ++ch) { + m_stretcher[ch] = StretcherBuffer + //!!! We really need to measure performance and work out + //what sort of quality level to use -- or at least to + //allow the user to configure it + (new IntegerTimeStretcher(factor, blockSize, 128), + new double[blockSize * factor]); + } + m_stretchInputBuffer = new double[blockSize]; +} + +AudioCallbackPlaySource::TimeStretcherData::~TimeStretcherData() +{ + std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl; + + while (!m_stretcher.empty()) { + delete m_stretcher.begin()->second.first; + delete[] m_stretcher.begin()->second.second; + m_stretcher.erase(m_stretcher.begin()); + } + delete m_stretchInputBuffer; +} + +IntegerTimeStretcher * +AudioCallbackPlaySource::TimeStretcherData::getStretcher(size_t channel) +{ + return m_stretcher[channel].first; +} + +double * +AudioCallbackPlaySource::TimeStretcherData::getOutputBuffer(size_t channel) +{ + return m_stretcher[channel].second; +} + +double * +AudioCallbackPlaySource::TimeStretcherData::getInputBuffer() +{ + return m_stretchInputBuffer; +} + +void +AudioCallbackPlaySource::TimeStretcherData::run(size_t channel) +{ + getStretcher(channel)->process(getInputBuffer(), + getOutputBuffer(channel), + m_blockSize); +} + +void +AudioCallbackPlaySource::setSlowdownFactor(size_t factor) +{ + // Avoid locks -- create, assign, mark old one for scavenging + // later (as a call to getSourceSamples may still be using it) + + TimeStretcherData *existingStretcher = m_timeStretcher; + + if (existingStretcher && existingStretcher->getFactor() == factor) { + return; + } + + if (factor > 1) { + TimeStretcherData *newStretcher = new TimeStretcherData + (getSourceChannelCount(), factor, getTargetBlockSize()); + m_slowdownCounter = 0; + m_timeStretcher = newStretcher; + } else { + m_timeStretcher = 0; + } + + if (existingStretcher) { + m_timeStretcherScavenger.claim(existingStretcher); + } +} + +size_t +AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer) +{ + if (!m_playing) { + for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { + for (size_t i = 0; i < count; ++i) { + buffer[ch][i] = 0.0; + } + } + return 0; + } + + TimeStretcherData *timeStretcher = m_timeStretcher; + + if (!timeStretcher || timeStretcher->getFactor() == 1) { + + size_t got = 0; + + for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { + + RingBuffer<float> &rb = *m_buffers[ch]; + + // this is marginally more likely to leave our channels in + // sync after a processing failure than just passing "count": + size_t request = count; + if (ch > 0) request = got; + + got = rb.read(buffer[ch], request); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", signalling for more (possibly)" << std::endl; +#endif + } + + for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { + for (size_t i = got; i < count; ++i) { + buffer[ch][i] = 0.0; + } + } + + m_condition.wakeAll(); + return got; + } + + if (m_slowdownCounter == 0) { + + size_t got = 0; + double *ib = timeStretcher->getInputBuffer(); + + for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { + + RingBuffer<float> &rb = *m_buffers[ch]; + size_t request = count; + if (ch > 0) request = got; // see above + got = rb.read(buffer[ch], request); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", running time stretcher" << std::endl; +#endif + + for (size_t i = 0; i < count; ++i) { + ib[i] = buffer[ch][i]; + } + + timeStretcher->run(ch); + } + + } else if (m_slowdownCounter >= timeStretcher->getFactor()) { + // reset this in case the factor has changed leaving the + // counter out of range + m_slowdownCounter = 0; + } + + for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) { + + double *ob = timeStretcher->getOutputBuffer(ch); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cerr << "AudioCallbackPlaySource::getSamples: Copying from (" << (m_slowdownCounter * count) << "," << count << ") to buffer" << std::endl; +#endif + + for (size_t i = 0; i < count; ++i) { + buffer[ch][i] = ob[m_slowdownCounter * count + i]; + } + } + + if (m_slowdownCounter == 0) m_condition.wakeAll(); + m_slowdownCounter = (m_slowdownCounter + 1) % timeStretcher->getFactor(); + return count; +} + +void +AudioCallbackPlaySource::fillBuffers() +{ + static float *tmp = 0; + static size_t tmpSize = 0; + + size_t space = 0; + for (size_t c = 0; c < m_bufferCount; ++c) { + size_t spaceHere = getRingBuffer(c).getWriteSpace(); + if (c == 0 || spaceHere < space) space = spaceHere; + } + + if (space == 0) return; + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl; +#endif + + size_t f = m_bufferedToFrame; + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "buffered to " << f << " already" << std::endl; +#endif + + bool resample = (getSourceSampleRate() != getTargetSampleRate()); + size_t channels = getSourceChannelCount(); + size_t orig = space; + size_t got = 0; + + static float **bufferPtrs = 0; + static size_t bufferPtrCount = 0; + + if (bufferPtrCount < channels) { + if (bufferPtrs) delete[] bufferPtrs; + bufferPtrs = new float *[channels]; + bufferPtrCount = channels; + } + + size_t generatorBlockSize = m_audioGenerator->getBlockSize(); + + if (resample && m_converter) { + + double ratio = + double(getTargetSampleRate()) / double(getSourceSampleRate()); + orig = size_t(orig / ratio + 0.1); + + // orig must be a multiple of generatorBlockSize + orig = (orig / generatorBlockSize) * generatorBlockSize; + if (orig == 0) return; + + size_t work = std::max(orig, space); + + // We only allocate one buffer, but we use it in two halves. + // We place the non-interleaved values in the second half of + // the buffer (orig samples for channel 0, orig samples for + // channel 1 etc), and then interleave them into the first + // half of the buffer. Then we resample back into the second + // half (interleaved) and de-interleave the results back to + // the start of the buffer for insertion into the ringbuffers. + // What a faff -- especially as we've already de-interleaved + // the audio data from the source file elsewhere before we + // even reach this point. + + if (tmpSize < channels * work * 2) { + delete[] tmp; + tmp = new float[channels * work * 2]; + tmpSize = channels * work * 2; + } + + float *nonintlv = tmp + channels * work; + float *intlv = tmp; + float *srcout = tmp + channels * work; + + for (size_t c = 0; c < channels; ++c) { + for (size_t i = 0; i < orig; ++i) { + nonintlv[channels * i + c] = 0.0f; + } + } + + for (std::set<Model *>::iterator mi = m_models.begin(); + mi != m_models.end(); ++mi) { + + for (size_t c = 0; c < channels; ++c) { + bufferPtrs[c] = nonintlv + c * orig; + } + + size_t gotHere = m_audioGenerator->mixModel + (*mi, f, orig, bufferPtrs); + + got = std::max(got, gotHere); + } + + // and interleave into first half + for (size_t c = 0; c < channels; ++c) { + for (size_t i = 0; i < orig; ++i) { + float sample = 0; + if (i < got) { + sample = nonintlv[c * orig + i]; + } + intlv[channels * i + c] = sample; + } + } + + SRC_DATA data; + data.data_in = intlv; + data.data_out = srcout; + data.input_frames = orig; + data.output_frames = work; + data.src_ratio = ratio; + data.end_of_input = 0; + + int err = src_process(m_converter, &data); + size_t toCopy = size_t(work * ratio + 0.1); + + if (err) { + std::cerr + << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: " + << src_strerror(err) << std::endl; + //!!! Then what? + } else { + got = data.input_frames_used; + toCopy = data.output_frames_gen; +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl; +#endif + } + + for (size_t c = 0; c < channels; ++c) { + for (size_t i = 0; i < toCopy; ++i) { + tmp[i] = srcout[channels * i + c]; + } + getRingBuffer(c).write(tmp, toCopy); + } + + } else { + + // space must be a multiple of generatorBlockSize + space = (space / generatorBlockSize) * generatorBlockSize; + if (space == 0) return; + + if (tmpSize < channels * space) { + delete[] tmp; + tmp = new float[channels * space]; + tmpSize = channels * space; + } + + for (size_t c = 0; c < channels; ++c) { + + bufferPtrs[c] = tmp + c * space; + + for (size_t i = 0; i < space; ++i) { + tmp[c * space + i] = 0.0f; + } + } + + for (std::set<Model *>::iterator mi = m_models.begin(); + mi != m_models.end(); ++mi) { + + got = m_audioGenerator->mixModel + (*mi, f, space, bufferPtrs); + } + + for (size_t c = 0; c < channels; ++c) { + + got = getRingBuffer(c).write(bufferPtrs[c], space); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cerr << "Wrote " << got << " frames for ch " << c << ", now " + << getRingBuffer(c).getReadSpace() << " to read" + << std::endl; +#endif + } + } + + m_bufferedToFrame = f + got; +} + +void +AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run() +{ + AudioCallbackPlaySource &s(m_source); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl; +#endif + + s.m_mutex.lock(); + + bool previouslyPlaying = s.m_playing; + + while (!s.m_exiting) { + + s.m_timeStretcherScavenger.scavenge(); + + float ms = 100; + if (s.getSourceSampleRate() > 0) { + ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0; + } + + if (!s.m_playing) ms *= 10; + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms/4 << "ms..." << std::endl; +#endif + + s.m_condition.wait(&s.m_mutex, size_t(ms / 4)); + +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl; +#endif + + if (!s.getSourceSampleRate()) continue; + + bool playing = s.m_playing; + + if (playing && !previouslyPlaying) { +#ifdef DEBUG_AUDIO_PLAY_SOURCE + std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl; +#endif + for (size_t c = 0; c < s.getSourceChannelCount(); ++c) { + s.getRingBuffer(c).reset(); + } + } + previouslyPlaying = playing; + + if (!playing) continue; + + s.fillBuffers(); + } + + s.m_mutex.unlock(); +} + + + +#ifdef INCLUDE_MOCFILES +#include "AudioCallbackPlaySource.moc.cpp" +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCallbackPlaySource.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,242 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_CALLBACK_PLAY_SOURCE_H_ +#define _AUDIO_CALLBACK_PLAY_SOURCE_H_ + +#include "base/RingBuffer.h" +#include "base/AudioPlaySource.h" +#include "base/Scavenger.h" + +#include <QObject> +#include <QMutex> +#include <QWaitCondition> +#include <QThread> + +#include <samplerate.h> + +#include <set> +#include <map> + +class Model; +class ViewManager; +class AudioGenerator; +class IntegerTimeStretcher; + +/** + * AudioCallbackPlaySource manages audio data supply to callback-based + * audio APIs such as JACK or CoreAudio. It maintains one ring buffer + * per channel, filled during playback by a non-realtime thread, and + * provides a method for a realtime thread to pick up the latest + * available sample data from these buffers. + */ +class AudioCallbackPlaySource : public virtual QObject, + public AudioPlaySource +{ + Q_OBJECT + +public: + AudioCallbackPlaySource(ViewManager *); + virtual ~AudioCallbackPlaySource(); + + /** + * Add a data model to be played from. The source can mix + * playback from a number of sources including dense and sparse + * models. The models must match in sample rate, but they don't + * have to have identical numbers of channels. + */ + virtual void addModel(Model *model); + + /** + * Remove a model. + */ + virtual void removeModel(Model *model); + + /** + * Remove all models. (Silence will ensue.) + */ + virtual void clearModels(); + + /** + * Start making data available in the ring buffers for playback, + * from the given frame. If playback is already under way, reseek + * to the given frame and continue. + */ + virtual void play(size_t startFrame); + + /** + * Stop playback and ensure that no more data is returned. + */ + virtual void stop(); + + /** + * Return whether playback is currently supposed to be happening. + */ + virtual bool isPlaying() const { return m_playing; } + + /** + * Return the frame number that is currently expected to be coming + * out of the speakers. (i.e. compensating for playback latency.) + */ + virtual size_t getCurrentPlayingFrame(); + + /** + * Set the block size of the target audio device. This should + * be called by the target class. + */ + void setTargetBlockSize(size_t); + + /** + * Get the block size of the target audio device. + */ + size_t getTargetBlockSize() const; + + /** + * Set the playback latency of the target audio device, in frames + * at the target sample rate. This is the difference between the + * frame currently "leaving the speakers" and the last frame (or + * highest last frame across all channels) requested via + * getSamples(). The default is zero. + */ + void setTargetPlayLatency(size_t); + + /** + * Get the playback latency of the target audio device. + */ + size_t getTargetPlayLatency() const; + + /** + * Specify that the target audio device has a fixed sample rate + * (i.e. cannot accommodate arbitrary sample rates based on the + * source). If the target sets this to something other than the + * source sample rate, this class will resample automatically to + * fit. + */ + void setTargetSampleRate(size_t); + + /** + * Return the sample rate set by the target audio device (or the + * source sample rate if the target hasn't set one). + */ + size_t getTargetSampleRate() const; + + /** + * Set the current output levels for metering (for call from the + * target) + */ + void setOutputLevels(float left, float right); + + /** + * Return the current (or thereabouts) output levels in the range + * 0.0 -> 1.0, for metering purposes. + */ + virtual bool getOutputLevels(float &left, float &right); + + /** + * Get the number of channels of audio that will be available. + * This may safely be called from a realtime thread. Returns 0 if + * there is no source yet available. + */ + size_t getSourceChannelCount() const; + + /** + * Get the actual sample rate of the source material. This may + * safely be called from a realtime thread. Returns 0 if there is + * no source yet available. + */ + size_t getSourceSampleRate() const; + + /** + * Get "count" samples (at the target sample rate) of the mixed + * audio data, in all channels. This may safely be called from a + * realtime thread. + */ + size_t getSourceSamples(size_t count, float **buffer); + + void setSlowdownFactor(size_t factor); + +signals: + void modelReplaced(); + + /// Just a warning + void sampleRateMismatch(size_t requested, size_t available); + +protected: + ViewManager *m_viewManager; + AudioGenerator *m_audioGenerator; + + std::set<Model *> m_models; + std::vector<RingBuffer<float> *> m_buffers; + size_t m_bufferCount; + size_t m_blockSize; + size_t m_sourceSampleRate; + size_t m_targetSampleRate; + size_t m_playLatency; + bool m_playing; + bool m_exiting; + size_t m_bufferedToFrame; + static const size_t m_ringBufferSize; + float m_outputLeft; + float m_outputRight; + + RingBuffer<float> &getRingBuffer(size_t c) { + return *m_buffers[c]; + } + + class TimeStretcherData + { + public: + TimeStretcherData(size_t channels, size_t factor, size_t blockSize); + ~TimeStretcherData(); + + size_t getFactor() const { return m_factor; } + IntegerTimeStretcher *getStretcher(size_t channel); + double *getOutputBuffer(size_t channel); + double *getInputBuffer(); + + void run(size_t channel); + + protected: + TimeStretcherData(const TimeStretcherData &); // not provided + TimeStretcherData &operator=(const TimeStretcherData &); // not provided + + typedef std::pair<IntegerTimeStretcher *, double *> StretcherBuffer; + std::map<size_t, StretcherBuffer> m_stretcher; + double *m_stretchInputBuffer; + size_t m_factor; + size_t m_blockSize; + }; + + size_t m_slowdownCounter; + TimeStretcherData *m_timeStretcher; + Scavenger<TimeStretcherData> m_timeStretcherScavenger; + + void fillBuffers(); // Called from fill thread, m_playing true, mutex held + + class AudioCallbackPlaySourceFillThread : public QThread + { + public: + AudioCallbackPlaySourceFillThread(AudioCallbackPlaySource &source) : + m_source(source) { } + + virtual void run(); + + protected: + AudioCallbackPlaySource &m_source; + }; + + QMutex m_mutex; + QWaitCondition m_condition; + AudioCallbackPlaySourceFillThread *m_fillThread; + SRC_STATE *m_converter; +}; + +#endif + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCallbackPlayTarget.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,40 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#include "AudioCallbackPlayTarget.h" +#include "AudioCallbackPlaySource.h" + +#include <iostream> + +AudioCallbackPlayTarget::AudioCallbackPlayTarget(AudioCallbackPlaySource *source) : + m_source(source), + m_outputGain(1.0) +{ + if (m_source) { + connect(m_source, SIGNAL(modelReplaced()), + this, SLOT(sourceModelReplaced())); + } +} + +AudioCallbackPlayTarget::~AudioCallbackPlayTarget() +{ +} + +void +AudioCallbackPlayTarget::setOutputGain(float gain) +{ + m_outputGain = gain; +} + +#ifdef INCLUDE_MOCFILES +#ifdef INCLUDE_MOCFILES +#include "AudioCallbackPlayTarget.moc.cpp" +#endif +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCallbackPlayTarget.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,45 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_CALLBACK_PLAY_TARGET_H_ +#define _AUDIO_CALLBACK_PLAY_TARGET_H_ + +#include <QObject> + +class AudioCallbackPlaySource; + +class AudioCallbackPlayTarget : public QObject +{ + Q_OBJECT + +public: + AudioCallbackPlayTarget(AudioCallbackPlaySource *source); + virtual ~AudioCallbackPlayTarget(); + + virtual bool isOK() const = 0; + + float getOutputGain() const { + return m_outputGain; + } + +public slots: + /** + * Set the playback gain (0.0 = silence, 1.0 = levels unmodified) + */ + virtual void setOutputGain(float gain); + + virtual void sourceModelReplaced() = 0; + +protected: + AudioCallbackPlaySource *m_source; + float m_outputGain; +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCoreAudioTarget.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,16 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifdef HAVE_COREAUDIO + +#include "AudioCoreAudioTarget.h" + + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioCoreAudioTarget.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,58 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_CORE_AUDIO_TARGET_H_ +#define _AUDIO_CORE_AUDIO_TARGET_H_ + +#ifdef HAVE_COREAUDIO + +#include <jack/jack.h> +#include <vector> + +#include <CoreAudio/CoreAudio.h> +#include <CoreAudio/CoreAudioTypes.h> +#include <AudioUnit/AUComponent.h> +#include <AudioUnit/AudioUnitProperties.h> +#include <AudioUnit/AudioUnitParameters.h> +#include <AudioUnit/AudioOutputUnit.h> + +#include "AudioCallbackPlayTarget.h" + +class AudioCallbackPlaySource; + +class AudioCoreAudioTarget : public AudioCallbackPlayTarget +{ + Q_OBJECT + +public: + AudioCoreAudioTarget(AudioCallbackPlaySource *source); + ~AudioCoreAudioTarget(); + + virtual bool isOK() const; + +public slots: + virtual void sourceModelReplaced(); + +protected: + OSStatus process(void *data, + AudioUnitRenderActionFlags *flags, + const AudioTimeStamp *timestamp, + unsigned int inbus, + unsigned int inframes, + AudioBufferList *ioData); + + int m_bufferSize; + int m_sampleRate; + int m_latency; +}; + +#endif /* HAVE_COREAUDIO */ + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioGenerator.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,324 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#include "AudioGenerator.h" + +#include "base/ViewManager.h" +#include "base/PlayParameters.h" + +#include "model/DenseTimeValueModel.h" +#include "model/SparseOneDimensionalModel.h" + +#include "plugin/RealTimePluginFactory.h" +#include "plugin/RealTimePluginInstance.h" +#include "plugin/PluginIdentifier.h" +#include "plugin/api/alsa/seq_event.h" + +#include <iostream> + +const size_t +AudioGenerator::m_pluginBlockSize = 2048; + +// #define DEBUG_AUDIO_GENERATOR 1 + +AudioGenerator::AudioGenerator(ViewManager *manager) : + m_viewManager(manager), + m_sourceSampleRate(0), + m_targetChannelCount(1) +{ +} + +AudioGenerator::~AudioGenerator() +{ +} + +void +AudioGenerator::addModel(Model *model) +{ + if (m_sourceSampleRate == 0) { + + m_sourceSampleRate = model->getSampleRate(); + + } else { + + DenseTimeValueModel *dtvm = + dynamic_cast<DenseTimeValueModel *>(model); + + if (dtvm) { + m_sourceSampleRate = model->getSampleRate(); + } + } + + SparseOneDimensionalModel *sodm = + dynamic_cast<SparseOneDimensionalModel *>(model); + if (!sodm) return; // nothing else to initialise + +// QString pluginId = "dssi:/usr/lib/dssi/dssi-vst.so:FEARkILLERrev1.dll"; +// QString pluginId = "dssi:/usr/lib/dssi/hexter.so:hexter"; +// QString pluginId = "dssi:/usr/lib/dssi/sineshaper.so:sineshaper"; +// QString pluginId = "dssi:/usr/local/lib/dssi/xsynth-dssi.so:Xsynth"; +// QString pluginId = "dssi:/usr/local/lib/dssi/trivial_synth.so:TS"; + QString pluginId = QString("dssi:%1:sample_player"). + arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME); + RealTimePluginFactory *factory = + RealTimePluginFactory::instanceFor(pluginId); + + if (!factory) { + std::cerr << "Failed to get plugin factory" << std::endl; + return; + } + + RealTimePluginInstance *instance = + factory->instantiatePlugin + (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount); + + if (instance) { + m_synthMap[sodm] = instance; + for (unsigned int i = 0; i < instance->getParameterCount(); ++i) { + instance->setParameterValue(i, instance->getParameterDefault(i)); + } + QString program = instance->getProgram(0, 0); + if (program != "") { + std::cerr << "selecting program " << program.toLocal8Bit().data() << std::endl; + instance->selectProgram(program); + } + instance->selectProgram("cowbell"); //!!! + instance->setIdealChannelCount(m_targetChannelCount); // reset! + } else { + std::cerr << "Failed to instantiate plugin" << std::endl; + } +} + +void +AudioGenerator::removeModel(Model *model) +{ + SparseOneDimensionalModel *sodm = + dynamic_cast<SparseOneDimensionalModel *>(model); + if (!sodm) return; // nothing to do + + if (m_synthMap.find(sodm) == m_synthMap.end()) return; + + RealTimePluginInstance *instance = m_synthMap[sodm]; + m_synthMap.erase(sodm); + delete instance; +} + +void +AudioGenerator::clearModels() +{ + while (!m_synthMap.empty()) { + RealTimePluginInstance *instance = m_synthMap.begin()->second; + m_synthMap.erase(m_synthMap.begin()); + delete instance; + } +} + +void +AudioGenerator::reset() +{ + for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { + if (i->second) { + i->second->silence(); + i->second->discardEvents(); + } + } + + m_noteOffs.clear(); +} + +void +AudioGenerator::setTargetChannelCount(size_t targetChannelCount) +{ + m_targetChannelCount = targetChannelCount; + + for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { + if (i->second) i->second->setIdealChannelCount(targetChannelCount); + } +} + +size_t +AudioGenerator::getBlockSize() const +{ + return m_pluginBlockSize; +} + +size_t +AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount, + float **buffer) +{ + if (m_sourceSampleRate == 0) { + std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl; + return frameCount; + } + + PlayParameters *parameters = m_viewManager->getPlayParameters(model); + if (!parameters) return frameCount; + + bool playing = !parameters->isPlayMuted(); + if (!playing) return frameCount; + + float gain = parameters->getPlayGain(); + float pan = parameters->getPlayPan(); + + DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model); + if (dtvm) { + return mixDenseTimeValueModel(dtvm, startFrame, frameCount, + buffer, gain, pan); + } + + SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *> + (model); + if (sodm) { + return mixSparseOneDimensionalModel(sodm, startFrame, frameCount, + buffer, gain, pan); + } + + return frameCount; +} + +size_t +AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm, + size_t startFrame, size_t frames, + float **buffer, float gain, float pan) +{ + static float *channelBuffer = 0; + static size_t channelBufSiz = 0; + + if (channelBufSiz < frames) { + delete[] channelBuffer; + channelBuffer = new float[frames]; + channelBufSiz = frames; + } + + size_t got = 0; + + for (size_t c = 0; c < m_targetChannelCount && c < dtvm->getChannelCount(); ++c) { + got = dtvm->getValues(c, startFrame, startFrame + frames, channelBuffer); + for (size_t i = 0; i < frames; ++i) { + buffer[c][i] += gain * channelBuffer[i]; + } + } + + return got; +} + +size_t +AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm, + size_t startFrame, size_t frames, + float **buffer, float gain, float pan) +{ + RealTimePluginInstance *plugin = m_synthMap[sodm]; + if (!plugin) return 0; + + size_t latency = plugin->getLatency(); + size_t blocks = frames / m_pluginBlockSize; + + //!!! hang on -- the fact that the audio callback play source's + //buffer is a multiple of the plugin's buffer size doesn't mean + //that we always get called for a multiple of it here (because it + //also depends on the JACK block size). how should we ensure that + //all models write the same amount in to the mix, and that we + //always have a multiple of the plugin buffer size? I guess this + //class has to be queryable for the plugin buffer size & the + //callback play source has to use that as a multiple for all the + //calls to mixModel + + size_t got = blocks * m_pluginBlockSize; + +#ifdef DEBUG_AUDIO_GENERATOR + std::cout << "mixModel [sparse]: frames " << frames + << ", blocks " << blocks << std::endl; +#endif + + snd_seq_event_t onEv; + onEv.type = SND_SEQ_EVENT_NOTEON; + onEv.data.note.channel = 0; + onEv.data.note.note = 64; + onEv.data.note.velocity = 127; + + snd_seq_event_t offEv; + offEv.type = SND_SEQ_EVENT_NOTEOFF; + offEv.data.note.channel = 0; + offEv.data.note.velocity = 0; + + NoteOffSet ¬eOffs = m_noteOffs[sodm]; + + for (size_t i = 0; i < blocks; ++i) { + + size_t reqStart = startFrame + i * m_pluginBlockSize; + + SparseOneDimensionalModel::PointList points = + sodm->getPoints(reqStart > 0 ? reqStart + latency : reqStart, + reqStart + latency + m_pluginBlockSize); + + RealTime blockTime = RealTime::frame2RealTime + (startFrame + i * m_pluginBlockSize, m_sourceSampleRate); + + for (SparseOneDimensionalModel::PointList::iterator pli = + points.begin(); pli != points.end(); ++pli) { + + size_t pliFrame = pli->frame; + if (pliFrame >= latency) pliFrame -= latency; + + while (noteOffs.begin() != noteOffs.end() && + noteOffs.begin()->frame <= pliFrame) { + + RealTime eventTime = RealTime::frame2RealTime + (noteOffs.begin()->frame, m_sourceSampleRate); + + offEv.data.note.note = noteOffs.begin()->pitch; + plugin->sendEvent(eventTime, &offEv); + noteOffs.erase(noteOffs.begin()); + } + + RealTime eventTime = RealTime::frame2RealTime + (pliFrame, m_sourceSampleRate); + + plugin->sendEvent(eventTime, &onEv); + +#ifdef DEBUG_AUDIO_GENERATOR + std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; +#endif + + size_t duration = 7000; // frames [for now] + NoteOff noff; + noff.pitch = onEv.data.note.note; + noff.frame = pliFrame + duration; + noteOffs.insert(noff); + } + + while (noteOffs.begin() != noteOffs.end() && + noteOffs.begin()->frame <= + startFrame + i * m_pluginBlockSize + m_pluginBlockSize) { + + RealTime eventTime = RealTime::frame2RealTime + (noteOffs.begin()->frame, m_sourceSampleRate); + + offEv.data.note.note = noteOffs.begin()->pitch; + plugin->sendEvent(eventTime, &offEv); + noteOffs.erase(noteOffs.begin()); + } + + plugin->run(blockTime); + float **outs = plugin->getAudioOutputBuffers(); + + for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) { +#ifdef DEBUG_AUDIO_GENERATOR + std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; +#endif + + for (size_t j = 0; j < m_pluginBlockSize; ++j) { + buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j]; + } + } + } + + return got; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioGenerator.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,106 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_GENERATOR_H_ +#define _AUDIO_GENERATOR_H_ + +class Model; +class ViewManager; +class DenseTimeValueModel; +class SparseOneDimensionalModel; +class RealTimePluginInstance; + +#include <set> +#include <map> + +class AudioGenerator +{ +public: + AudioGenerator(ViewManager *); + virtual ~AudioGenerator(); + + /** + * Add a data model to be played from and initialise any + * necessary audio generation code. + */ + virtual void addModel(Model *model); + + /** + * Remove a model. + */ + virtual void removeModel(Model *model); + + /** + * Remove all models. + */ + virtual void clearModels(); + + /** + * Reset playback, clearing plugins and the like. + */ + virtual void reset(); + + /** + * Set the target channel count. The buffer parameter to mixModel + * must always point to at least this number of arrays. + */ + virtual void setTargetChannelCount(size_t channelCount); + + /** + * Return the internal processing block size. The frameCount + * argument to all mixModel calls must be a multiple of this + * value. + */ + virtual size_t getBlockSize() const; + + /** + * Mix a single model into an output buffer. + */ + virtual size_t mixModel(Model *model, size_t startFrame, size_t frameCount, + float **buffer); + +protected: + ViewManager *m_viewManager; + size_t m_sourceSampleRate; + size_t m_targetChannelCount; + + struct NoteOff { + + int pitch; + size_t frame; + + struct Comparator { + bool operator()(const NoteOff &n1, const NoteOff &n2) const { + return n1.frame < n2.frame; + } + }; + }; + + typedef std::map<SparseOneDimensionalModel *, + RealTimePluginInstance *> PluginMap; + + typedef std::set<NoteOff, NoteOff::Comparator> NoteOffSet; + typedef std::map<SparseOneDimensionalModel *, NoteOffSet> NoteOffMap; + + PluginMap m_synthMap; + NoteOffMap m_noteOffs; + + virtual size_t mixDenseTimeValueModel + (DenseTimeValueModel *model, size_t startFrame, size_t frameCount, + float **buffer, float gain, float pan); + + virtual size_t mixSparseOneDimensionalModel + (SparseOneDimensionalModel *model, size_t startFrame, size_t frameCount, + float **buffer, float gain, float pan); + + static const size_t m_pluginBlockSize; +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioJACKTarget.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,208 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifdef HAVE_JACK + +#include "AudioJACKTarget.h" +#include "AudioCallbackPlaySource.h" + +#include <iostream> + +//#define DEBUG_AUDIO_JACK_TARGET 1 + +AudioJACKTarget::AudioJACKTarget(AudioCallbackPlaySource *source) : + AudioCallbackPlayTarget(source), + m_client(0), + m_bufferSize(0), + m_sampleRate(0) +{ + char name[20]; + strcpy(name, "Sonic Visualiser"); + m_client = jack_client_new(name); + + if (!m_client) { + sprintf(name, "Sonic Visualiser (%d)", (int)getpid()); + m_client = jack_client_new(name); + if (!m_client) { + std::cerr + << "ERROR: AudioJACKTarget: Failed to connect to JACK server" + << std::endl; + } + } + + if (!m_client) return; + + m_bufferSize = jack_get_buffer_size(m_client); + m_sampleRate = jack_get_sample_rate(m_client); + + jack_set_process_callback(m_client, processStatic, this); + + if (jack_activate(m_client)) { + std::cerr << "ERROR: AudioJACKTarget: Failed to activate JACK client" + << std::endl; + } + + if (m_source) { + sourceModelReplaced(); + } +} + +AudioJACKTarget::~AudioJACKTarget() +{ + if (m_client) { + jack_deactivate(m_client); + jack_client_close(m_client); + } +} + +bool +AudioJACKTarget::isOK() const +{ + return (m_client != 0); +} + +int +AudioJACKTarget::processStatic(jack_nframes_t nframes, void *arg) +{ + return ((AudioJACKTarget *)arg)->process(nframes); +} + +void +AudioJACKTarget::sourceModelReplaced() +{ + m_mutex.lock(); + + m_source->setTargetBlockSize(m_bufferSize); + m_source->setTargetSampleRate(m_sampleRate); + + size_t channels = m_source->getSourceChannelCount(); + + if (channels == m_outputs.size() || !m_client) { + m_mutex.unlock(); + return; + } + + const char **ports = + jack_get_ports(m_client, NULL, NULL, + JackPortIsPhysical | JackPortIsInput); + size_t physicalPortCount = 0; + while (ports[physicalPortCount]) ++physicalPortCount; + +#ifdef DEBUG_AUDIO_JACK_TARGET + std::cerr << "AudioJACKTarget::sourceModelReplaced: have " << channels << " channels and " << physicalPortCount << " physical ports" << std::endl; +#endif + + while (m_outputs.size() < channels) { + + char name[20]; + jack_port_t *port; + + sprintf(name, "out %d", m_outputs.size() + 1); + + port = jack_port_register(m_client, + name, + JACK_DEFAULT_AUDIO_TYPE, + JackPortIsOutput, + 0); + + if (!port) { + std::cerr + << "ERROR: AudioJACKTarget: Failed to create JACK output port " + << m_outputs.size() << std::endl; + } else { + m_source->setTargetPlayLatency(jack_port_get_latency(port)); + } + + if (m_outputs.size() < physicalPortCount) { + jack_connect(m_client, jack_port_name(port), ports[m_outputs.size()]); + } + + m_outputs.push_back(port); + } + + while (m_outputs.size() > channels) { + std::vector<jack_port_t *>::iterator itr = m_outputs.end(); + --itr; + jack_port_t *port = *itr; + if (port) jack_port_unregister(m_client, port); + m_outputs.erase(itr); + } + + m_mutex.unlock(); +} + +int +AudioJACKTarget::process(jack_nframes_t nframes) +{ + if (!m_mutex.tryLock()) { + return 0; + } + + if (m_outputs.empty()) { + m_mutex.unlock(); + return 0; + } + +#ifdef DEBUG_AUDIO_JACK_TARGET + std::cout << "AudioJACKTarget::process(" << nframes << "): have a source" << std::endl; +#endif + +#ifdef DEBUG_AUDIO_JACK_TARGET + if (m_bufferSize != nframes) { + std::cerr << "WARNING: m_bufferSize != nframes (" << m_bufferSize << " != " << nframes << ")" << std::endl; + } +#endif + + float **buffers = (float **)alloca(m_outputs.size() * sizeof(float *)); + + for (size_t ch = 0; ch < m_outputs.size(); ++ch) { + buffers[ch] = (float *)jack_port_get_buffer(m_outputs[ch], nframes); + } + + if (m_source) { + m_source->getSourceSamples(nframes, buffers); + } else { + for (size_t ch = 0; ch < m_outputs.size(); ++ch) { + for (size_t i = 0; i < nframes; ++i) { + buffers[ch][i] = 0.0; + } + } + } + + float peakLeft = 0.0, peakRight = 0.0; + + for (size_t ch = 0; ch < m_outputs.size(); ++ch) { + + float peak = 0.0; + + for (size_t i = 0; i < nframes; ++i) { + buffers[ch][i] *= m_outputGain; + float sample = fabsf(buffers[ch][i]); + if (sample > peak) peak = sample; + } + + if (ch == 0) peakLeft = peak; + if (ch > 0 || m_outputs.size() == 1) peakRight = peak; + } + + if (m_source) { + m_source->setOutputLevels(peakLeft, peakRight); + } + + m_mutex.unlock(); + return 0; +} + + +#ifdef INCLUDE_MOCFILES +#include "AudioJACKTarget.moc.cpp" +#endif + +#endif /* HAVE_JACK */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioJACKTarget.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,52 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_JACK_TARGET_H_ +#define _AUDIO_JACK_TARGET_H_ + +#ifdef HAVE_JACK + +#include <jack/jack.h> +#include <vector> + +#include "AudioCallbackPlayTarget.h" + +#include <QMutex> + +class AudioCallbackPlaySource; + +class AudioJACKTarget : public AudioCallbackPlayTarget +{ + Q_OBJECT + +public: + AudioJACKTarget(AudioCallbackPlaySource *source); + virtual ~AudioJACKTarget(); + + virtual bool isOK() const; + +public slots: + virtual void sourceModelReplaced(); + +protected: + int process(jack_nframes_t nframes); + + static int processStatic(jack_nframes_t, void *); + + jack_client_t *m_client; + std::vector<jack_port_t *> m_outputs; + jack_nframes_t m_bufferSize; + jack_nframes_t m_sampleRate; + QMutex m_mutex; +}; + +#endif /* HAVE_JACK */ + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioPortAudioTarget.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,190 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifdef HAVE_PORTAUDIO + +#include "AudioPortAudioTarget.h" +#include "AudioCallbackPlaySource.h" + +#include <iostream> +#include <cassert> +#include <cmath> + +//#define DEBUG_AUDIO_PORT_AUDIO_TARGET 1 + +AudioPortAudioTarget::AudioPortAudioTarget(AudioCallbackPlaySource *source) : + AudioCallbackPlayTarget(source), + m_stream(0), + m_bufferSize(0), + m_sampleRate(0), + m_latency(0) +{ + PaError err; + + err = Pa_Initialize(); + if (err != paNoError) { + std::cerr << "ERROR: AudioPortAudioTarget: Failed to initialize PortAudio" << std::endl; + return; + } + + m_bufferSize = 1024; + m_sampleRate = 44100; + if (m_source && (m_source->getSourceSampleRate() != 0)) { + m_sampleRate = m_source->getSourceSampleRate(); + } + + m_latency = Pa_GetMinNumBuffers(m_bufferSize, m_sampleRate) * m_bufferSize; + + err = Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32, + m_sampleRate, m_bufferSize, 0, + processStatic, this); + + if (err != paNoError) { + std::cerr << "ERROR: AudioPortAudioTarget: Failed to open PortAudio stream" << std::endl; + m_stream = 0; + Pa_Terminate(); + return; + } + + err = Pa_StartStream(m_stream); + + if (err != paNoError) { + std::cerr << "ERROR: AudioPortAudioTarget: Failed to start PortAudio stream" << std::endl; + Pa_CloseStream(m_stream); + m_stream = 0; + Pa_Terminate(); + return; + } + + if (m_source) { + std::cerr << "AudioPortAudioTarget: block size " << m_bufferSize << std::endl; + m_source->setTargetBlockSize(m_bufferSize); + m_source->setTargetSampleRate(m_sampleRate); + m_source->setTargetPlayLatency(m_latency); + } +} + +AudioPortAudioTarget::~AudioPortAudioTarget() +{ + if (m_stream) { + PaError err; + err = Pa_CloseStream(m_stream); + if (err != paNoError) { + std::cerr << "ERROR: AudioPortAudioTarget: Failed to close PortAudio stream" << std::endl; + } + Pa_Terminate(); + } +} + +bool +AudioPortAudioTarget::isOK() const +{ + return (m_stream != 0); +} + +int +AudioPortAudioTarget::processStatic(void *input, void *output, + unsigned long nframes, + PaTimestamp outTime, void *data) +{ + return ((AudioPortAudioTarget *)data)->process(input, output, + nframes, outTime); +} + +void +AudioPortAudioTarget::sourceModelReplaced() +{ + m_source->setTargetSampleRate(m_sampleRate); +} + +int +AudioPortAudioTarget::process(void *inputBuffer, void *outputBuffer, + unsigned long nframes, + PaTimestamp) +{ +#ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET + std::cout << "AudioPortAudioTarget::process(" << nframes << ")" << std::endl; +#endif + + if (!m_source) return 0; + + float *output = (float *)outputBuffer; + + assert(nframes <= m_bufferSize); + + static float **tmpbuf = 0; + static size_t tmpbufch = 0; + static size_t tmpbufsz = 0; + + size_t sourceChannels = m_source->getSourceChannelCount(); + + if (!tmpbuf || tmpbufch != sourceChannels || tmpbufsz < m_bufferSize) { + + if (tmpbuf) { + for (size_t i = 0; i < tmpbufch; ++i) { + delete[] tmpbuf[i]; + } + delete[] tmpbuf; + } + + tmpbufch = sourceChannels; + tmpbufsz = m_bufferSize; + tmpbuf = new float *[tmpbufch]; + + for (size_t i = 0; i < tmpbufch; ++i) { + tmpbuf[i] = new float[tmpbufsz]; + } + } + + m_source->getSourceSamples(nframes, tmpbuf); + + float peakLeft = 0.0, peakRight = 0.0; + + for (size_t ch = 0; ch < 2; ++ch) { + + float peak = 0.0; + + if (ch < sourceChannels) { + + // PortAudio samples are interleaved + for (size_t i = 0; i < nframes; ++i) { + output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain; + float sample = fabsf(output[i * 2 + ch]); + if (sample > peak) peak = sample; + } + + } else if (ch == 1 && sourceChannels == 1) { + + for (size_t i = 0; i < nframes; ++i) { + output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain; + float sample = fabsf(output[i * 2 + ch]); + if (sample > peak) peak = sample; + } + + } else { + for (size_t i = 0; i < nframes; ++i) { + output[i * 2 + ch] = 0; + } + } + + if (ch == 0) peakLeft = peak; + if (ch > 0 || sourceChannels == 1) peakRight = peak; + } + + m_source->setOutputLevels(peakLeft, peakRight); + + return 0; +} + +#ifdef INCLUDE_MOCFILES +#include "AudioPortAudioTarget.moc.cpp" +#endif + +#endif /* HAVE_PORTAUDIO */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioPortAudioTarget.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,52 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_PORT_AUDIO_TARGET_H_ +#define _AUDIO_PORT_AUDIO_TARGET_H_ + +#ifdef HAVE_PORTAUDIO + +#include <portaudio.h> +#include <vector> + +#include "AudioCallbackPlayTarget.h" + +class AudioCallbackPlaySource; + +class AudioPortAudioTarget : public AudioCallbackPlayTarget +{ + Q_OBJECT + +public: + AudioPortAudioTarget(AudioCallbackPlaySource *source); + virtual ~AudioPortAudioTarget(); + + virtual bool isOK() const; + +public slots: + virtual void sourceModelReplaced(); + +protected: + int process(void *input, void *output, unsigned long frames, + PaTimestamp outTime); + + static int processStatic(void *, void *, unsigned long, + PaTimestamp, void *); + + PortAudioStream *m_stream; + + int m_bufferSize; + int m_sampleRate; + int m_latency; +}; + +#endif /* HAVE_PORTAUDIO */ + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioTargetFactory.cpp Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,63 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#include "AudioTargetFactory.h" + +#include "AudioJACKTarget.h" +#include "AudioCoreAudioTarget.h" +#include "AudioPortAudioTarget.h" + +#include <iostream> + +AudioCallbackPlayTarget * +AudioTargetFactory::createCallbackTarget(AudioCallbackPlaySource *source) +{ + AudioCallbackPlayTarget *target = 0; + +#ifdef HAVE_JACK + target = new AudioJACKTarget(source); + if (target->isOK()) return target; + else { + std::cerr << "WARNING: AudioTargetFactory::createCallbackTarget: Failed to open JACK target" << std::endl; + delete target; + } +#endif + +#ifdef HAVE_COREAUDIO + target = new AudioCoreAudioTarget(source); + if (target->isOK()) return target; + else { + std::cerr << "WARNING: AudioTargetFactory::createCallbackTarget: Failed to open CoreAudio target" << std::endl; + delete target; + } +#endif + +#ifdef HAVE_DIRECTSOUND + target = new AudioDirectSoundTarget(source); + if (target->isOK()) return target; + else { + std::cerr << "WARNING: AudioTargetFactory::createCallbackTarget: Failed to open DirectSound target" << std::endl; + delete target; + } +#endif + +#ifdef HAVE_PORTAUDIO + target = new AudioPortAudioTarget(source); + if (target->isOK()) return target; + else { + std::cerr << "WARNING: AudioTargetFactory::createCallbackTarget: Failed to open PortAudio target" << std::endl; + delete target; + } +#endif + + std::cerr << "WARNING: AudioTargetFactory::createCallbackTarget: No suitable targets available" << std::endl; + return 0; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audioio/AudioTargetFactory.h Tue Jan 10 16:33:16 2006 +0000 @@ -0,0 +1,23 @@ +/* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ + +/* + A waveform viewer and audio annotation editor. + Chris Cannam, Queen Mary University of London, 2005 + + This is experimental software. Not for distribution. +*/ + +#ifndef _AUDIO_TARGET_FACTORY_H_ +#define _AUDIO_TARGET_FACTORY_H_ + +class AudioCallbackPlaySource; +class AudioCallbackPlayTarget; + +class AudioTargetFactory +{ +public: + static AudioCallbackPlayTarget *createCallbackTarget(AudioCallbackPlaySource *); +}; + +#endif +