annotate audioio/AudioCallbackPlaySource.h @ 91:9fc4b256c283

* PortAudio driver: do not specify frames per buffer, let PA decide * Remove old non-RubberBand time stretcher -- it doesn't work with varying buffer sizes such as the PA driver may now be using * Rewrite getCurrentPlayingFrame for greater correctness when using long buffer sizes (interpolating according to audio stream timestamp) * Several changes to make the timestretch management RT safe(r)
author Chris Cannam
date Fri, 08 Feb 2008 17:51:15 +0000
parents ae2627ac7db2
children 737b373246b5
rev   line source
Chris@43 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@43 2
Chris@43 3 /*
Chris@43 4 Sonic Visualiser
Chris@43 5 An audio file viewer and annotation editor.
Chris@43 6 Centre for Digital Music, Queen Mary, University of London.
Chris@43 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@43 8
Chris@43 9 This program is free software; you can redistribute it and/or
Chris@43 10 modify it under the terms of the GNU General Public License as
Chris@43 11 published by the Free Software Foundation; either version 2 of the
Chris@43 12 License, or (at your option) any later version. See the file
Chris@43 13 COPYING included with this distribution for more information.
Chris@43 14 */
Chris@43 15
Chris@43 16 #ifndef _AUDIO_CALLBACK_PLAY_SOURCE_H_
Chris@43 17 #define _AUDIO_CALLBACK_PLAY_SOURCE_H_
Chris@43 18
Chris@43 19 #include "base/RingBuffer.h"
Chris@43 20 #include "base/AudioPlaySource.h"
Chris@43 21 #include "base/PropertyContainer.h"
Chris@43 22 #include "base/Scavenger.h"
Chris@43 23
Chris@43 24 #include <QObject>
Chris@43 25 #include <QMutex>
Chris@43 26 #include <QWaitCondition>
Chris@43 27
Chris@43 28 #include "base/Thread.h"
Chris@43 29
Chris@43 30 #include <samplerate.h>
Chris@43 31
Chris@43 32 #include <set>
Chris@43 33 #include <map>
Chris@43 34
Chris@91 35 namespace RubberBand {
Chris@91 36 class RubberBandStretcher;
Chris@91 37 }
Chris@62 38
Chris@43 39 class Model;
Chris@43 40 class ViewManager;
Chris@43 41 class AudioGenerator;
Chris@43 42 class PlayParameters;
Chris@43 43 class RealTimePluginInstance;
Chris@91 44 class AudioCallbackPlayTarget;
Chris@43 45
Chris@43 46 /**
Chris@43 47 * AudioCallbackPlaySource manages audio data supply to callback-based
Chris@43 48 * audio APIs such as JACK or CoreAudio. It maintains one ring buffer
Chris@43 49 * per channel, filled during playback by a non-realtime thread, and
Chris@43 50 * provides a method for a realtime thread to pick up the latest
Chris@43 51 * available sample data from these buffers.
Chris@43 52 */
Chris@43 53 class AudioCallbackPlaySource : public virtual QObject,
Chris@43 54 public AudioPlaySource
Chris@43 55 {
Chris@43 56 Q_OBJECT
Chris@43 57
Chris@43 58 public:
Chris@57 59 AudioCallbackPlaySource(ViewManager *, QString clientName);
Chris@43 60 virtual ~AudioCallbackPlaySource();
Chris@43 61
Chris@43 62 /**
Chris@43 63 * Add a data model to be played from. The source can mix
Chris@43 64 * playback from a number of sources including dense and sparse
Chris@43 65 * models. The models must match in sample rate, but they don't
Chris@43 66 * have to have identical numbers of channels.
Chris@43 67 */
Chris@43 68 virtual void addModel(Model *model);
Chris@43 69
Chris@43 70 /**
Chris@43 71 * Remove a model.
Chris@43 72 */
Chris@43 73 virtual void removeModel(Model *model);
Chris@43 74
Chris@43 75 /**
Chris@43 76 * Remove all models. (Silence will ensue.)
Chris@43 77 */
Chris@43 78 virtual void clearModels();
Chris@43 79
Chris@43 80 /**
Chris@43 81 * Start making data available in the ring buffers for playback,
Chris@43 82 * from the given frame. If playback is already under way, reseek
Chris@43 83 * to the given frame and continue.
Chris@43 84 */
Chris@43 85 virtual void play(size_t startFrame);
Chris@43 86
Chris@43 87 /**
Chris@43 88 * Stop playback and ensure that no more data is returned.
Chris@43 89 */
Chris@43 90 virtual void stop();
Chris@43 91
Chris@43 92 /**
Chris@43 93 * Return whether playback is currently supposed to be happening.
Chris@43 94 */
Chris@43 95 virtual bool isPlaying() const { return m_playing; }
Chris@43 96
Chris@43 97 /**
Chris@43 98 * Return the frame number that is currently expected to be coming
Chris@43 99 * out of the speakers. (i.e. compensating for playback latency.)
Chris@43 100 */
Chris@43 101 virtual size_t getCurrentPlayingFrame();
Chris@43 102
Chris@43 103 /**
Chris@43 104 * Return the frame at which playback is expected to end (if not looping).
Chris@43 105 */
Chris@43 106 virtual size_t getPlayEndFrame() { return m_lastModelEndFrame; }
Chris@43 107
Chris@43 108 /**
Chris@91 109 * Set the target and the block size of the target audio device.
Chris@91 110 * This should be called by the target class.
Chris@43 111 */
Chris@91 112 void setTarget(AudioCallbackPlayTarget *, size_t blockSize);
Chris@43 113
Chris@43 114 /**
Chris@91 115 * Get the block size of the target audio device. This may be an
Chris@91 116 * estimate or upper bound, if the target has a variable block
Chris@91 117 * size; the source should behave itself even if this value turns
Chris@91 118 * out to be inaccurate.
Chris@43 119 */
Chris@43 120 size_t getTargetBlockSize() const;
Chris@43 121
Chris@43 122 /**
Chris@43 123 * Set the playback latency of the target audio device, in frames
Chris@43 124 * at the target sample rate. This is the difference between the
Chris@43 125 * frame currently "leaving the speakers" and the last frame (or
Chris@43 126 * highest last frame across all channels) requested via
Chris@43 127 * getSamples(). The default is zero.
Chris@43 128 */
Chris@43 129 void setTargetPlayLatency(size_t);
Chris@43 130
Chris@43 131 /**
Chris@43 132 * Get the playback latency of the target audio device.
Chris@43 133 */
Chris@43 134 size_t getTargetPlayLatency() const;
Chris@43 135
Chris@43 136 /**
Chris@43 137 * Specify that the target audio device has a fixed sample rate
Chris@43 138 * (i.e. cannot accommodate arbitrary sample rates based on the
Chris@43 139 * source). If the target sets this to something other than the
Chris@43 140 * source sample rate, this class will resample automatically to
Chris@43 141 * fit.
Chris@43 142 */
Chris@43 143 void setTargetSampleRate(size_t);
Chris@43 144
Chris@43 145 /**
Chris@43 146 * Return the sample rate set by the target audio device (or the
Chris@43 147 * source sample rate if the target hasn't set one).
Chris@43 148 */
Chris@43 149 virtual size_t getTargetSampleRate() const;
Chris@43 150
Chris@43 151 /**
Chris@43 152 * Set the current output levels for metering (for call from the
Chris@43 153 * target)
Chris@43 154 */
Chris@43 155 void setOutputLevels(float left, float right);
Chris@43 156
Chris@43 157 /**
Chris@43 158 * Return the current (or thereabouts) output levels in the range
Chris@43 159 * 0.0 -> 1.0, for metering purposes.
Chris@43 160 */
Chris@43 161 virtual bool getOutputLevels(float &left, float &right);
Chris@43 162
Chris@43 163 /**
Chris@43 164 * Get the number of channels of audio that in the source models.
Chris@43 165 * This may safely be called from a realtime thread. Returns 0 if
Chris@43 166 * there is no source yet available.
Chris@43 167 */
Chris@43 168 size_t getSourceChannelCount() const;
Chris@43 169
Chris@43 170 /**
Chris@43 171 * Get the number of channels of audio that will be provided
Chris@43 172 * to the play target. This may be more than the source channel
Chris@43 173 * count: for example, a mono source will provide 2 channels
Chris@43 174 * after pan.
Chris@43 175 * This may safely be called from a realtime thread. Returns 0 if
Chris@43 176 * there is no source yet available.
Chris@43 177 */
Chris@43 178 size_t getTargetChannelCount() const;
Chris@43 179
Chris@43 180 /**
Chris@43 181 * Get the actual sample rate of the source material. This may
Chris@43 182 * safely be called from a realtime thread. Returns 0 if there is
Chris@43 183 * no source yet available.
Chris@43 184 */
Chris@43 185 virtual size_t getSourceSampleRate() const;
Chris@43 186
Chris@43 187 /**
Chris@43 188 * Get "count" samples (at the target sample rate) of the mixed
Chris@43 189 * audio data, in all channels. This may safely be called from a
Chris@43 190 * realtime thread.
Chris@43 191 */
Chris@43 192 size_t getSourceSamples(size_t count, float **buffer);
Chris@43 193
Chris@43 194 /**
Chris@91 195 * Set the time stretcher factor (i.e. playback speed).
Chris@43 196 */
Chris@91 197 void setTimeStretch(float factor);
Chris@43 198
Chris@43 199 /**
Chris@43 200 * Set the resampler quality, 0 - 2 where 0 is fastest and 2 is
Chris@43 201 * highest quality.
Chris@43 202 */
Chris@43 203 void setResampleQuality(int q);
Chris@43 204
Chris@43 205 /**
Chris@43 206 * Set a single real-time plugin as a processing effect for
Chris@43 207 * auditioning during playback.
Chris@43 208 *
Chris@43 209 * The plugin must have been initialised with
Chris@43 210 * getTargetChannelCount() channels and a getTargetBlockSize()
Chris@43 211 * sample frame processing block size.
Chris@43 212 *
Chris@43 213 * This playback source takes ownership of the plugin, which will
Chris@43 214 * be deleted at some point after the following call to
Chris@43 215 * setAuditioningPlugin (depending on real-time constraints).
Chris@43 216 *
Chris@43 217 * Pass a null pointer to remove the current auditioning plugin,
Chris@43 218 * if any.
Chris@43 219 */
Chris@43 220 void setAuditioningPlugin(RealTimePluginInstance *plugin);
Chris@43 221
Chris@43 222 /**
Chris@43 223 * Specify that only the given set of models should be played.
Chris@43 224 */
Chris@43 225 void setSoloModelSet(std::set<Model *>s);
Chris@43 226
Chris@43 227 /**
Chris@43 228 * Specify that all models should be played as normal (if not
Chris@43 229 * muted).
Chris@43 230 */
Chris@43 231 void clearSoloModelSet();
Chris@43 232
Chris@57 233 QString getClientName() const { return m_clientName; }
Chris@57 234
Chris@43 235 signals:
Chris@43 236 void modelReplaced();
Chris@43 237
Chris@43 238 void playStatusChanged(bool isPlaying);
Chris@43 239
Chris@43 240 void sampleRateMismatch(size_t requested, size_t available, bool willResample);
Chris@43 241
Chris@43 242 void audioOverloadPluginDisabled();
Chris@43 243
Chris@43 244 public slots:
Chris@43 245 void audioProcessingOverload();
Chris@43 246
Chris@43 247 protected slots:
Chris@43 248 void selectionChanged();
Chris@43 249 void playLoopModeChanged();
Chris@43 250 void playSelectionModeChanged();
Chris@43 251 void playParametersChanged(PlayParameters *);
Chris@43 252 void preferenceChanged(PropertyContainer::PropertyName);
Chris@43 253 void modelChanged(size_t startFrame, size_t endFrame);
Chris@43 254
Chris@43 255 protected:
Chris@57 256 ViewManager *m_viewManager;
Chris@57 257 AudioGenerator *m_audioGenerator;
Chris@57 258 QString m_clientName;
Chris@43 259
Chris@43 260 class RingBufferVector : public std::vector<RingBuffer<float> *> {
Chris@43 261 public:
Chris@43 262 virtual ~RingBufferVector() {
Chris@43 263 while (!empty()) {
Chris@43 264 delete *begin();
Chris@43 265 erase(begin());
Chris@43 266 }
Chris@43 267 }
Chris@43 268 };
Chris@43 269
Chris@43 270 std::set<Model *> m_models;
Chris@43 271 RingBufferVector *m_readBuffers;
Chris@43 272 RingBufferVector *m_writeBuffers;
Chris@43 273 size_t m_readBufferFill;
Chris@43 274 size_t m_writeBufferFill;
Chris@43 275 Scavenger<RingBufferVector> m_bufferScavenger;
Chris@43 276 size_t m_sourceChannelCount;
Chris@43 277 size_t m_blockSize;
Chris@43 278 size_t m_sourceSampleRate;
Chris@43 279 size_t m_targetSampleRate;
Chris@43 280 size_t m_playLatency;
Chris@91 281 AudioCallbackPlayTarget *m_target;
Chris@91 282 double m_lastRetrievalTimestamp;
Chris@91 283 size_t m_lastRetrievedBlockSize;
Chris@43 284 bool m_playing;
Chris@43 285 bool m_exiting;
Chris@43 286 size_t m_lastModelEndFrame;
Chris@43 287 static const size_t m_ringBufferSize;
Chris@43 288 float m_outputLeft;
Chris@43 289 float m_outputRight;
Chris@43 290 RealTimePluginInstance *m_auditioningPlugin;
Chris@43 291 bool m_auditioningPluginBypassed;
Chris@43 292 Scavenger<RealTimePluginInstance> m_pluginScavenger;
Chris@43 293
Chris@43 294 RingBuffer<float> *getWriteRingBuffer(size_t c) {
Chris@43 295 if (m_writeBuffers && c < m_writeBuffers->size()) {
Chris@43 296 return (*m_writeBuffers)[c];
Chris@43 297 } else {
Chris@43 298 return 0;
Chris@43 299 }
Chris@43 300 }
Chris@43 301
Chris@43 302 RingBuffer<float> *getReadRingBuffer(size_t c) {
Chris@43 303 RingBufferVector *rb = m_readBuffers;
Chris@43 304 if (rb && c < rb->size()) {
Chris@43 305 return (*rb)[c];
Chris@43 306 } else {
Chris@43 307 return 0;
Chris@43 308 }
Chris@43 309 }
Chris@43 310
Chris@43 311 void clearRingBuffers(bool haveLock = false, size_t count = 0);
Chris@43 312 void unifyRingBuffers();
Chris@43 313
Chris@62 314 RubberBand::RubberBandStretcher *m_timeStretcher;
Chris@91 315 float m_stretchRatio;
Chris@91 316
Chris@91 317 size_t m_stretcherInputCount;
Chris@91 318 float **m_stretcherInputs;
Chris@91 319 size_t *m_stretcherInputSizes;
Chris@43 320
Chris@43 321 // Called from fill thread, m_playing true, mutex held
Chris@43 322 // Return true if work done
Chris@43 323 bool fillBuffers();
Chris@43 324
Chris@43 325 // Called from fillBuffers. Return the number of frames written,
Chris@43 326 // which will be count or fewer. Return in the frame argument the
Chris@43 327 // new buffered frame position (which may be earlier than the
Chris@43 328 // frame argument passed in, in the case of looping).
Chris@43 329 size_t mixModels(size_t &frame, size_t count, float **buffers);
Chris@43 330
Chris@43 331 // Called from getSourceSamples.
Chris@43 332 void applyAuditioningEffect(size_t count, float **buffers);
Chris@43 333
Chris@43 334 class FillThread : public Thread
Chris@43 335 {
Chris@43 336 public:
Chris@43 337 FillThread(AudioCallbackPlaySource &source) :
Chris@43 338 Thread(Thread::NonRTThread),
Chris@43 339 m_source(source) { }
Chris@43 340
Chris@43 341 virtual void run();
Chris@43 342
Chris@43 343 protected:
Chris@43 344 AudioCallbackPlaySource &m_source;
Chris@43 345 };
Chris@43 346
Chris@43 347 QMutex m_mutex;
Chris@43 348 QWaitCondition m_condition;
Chris@43 349 FillThread *m_fillThread;
Chris@43 350 SRC_STATE *m_converter;
Chris@43 351 SRC_STATE *m_crapConverter; // for use when playing very fast
Chris@43 352 int m_resampleQuality;
Chris@43 353 void initialiseConverter();
Chris@43 354 };
Chris@43 355
Chris@43 356 #endif
Chris@43 357
Chris@43 358