1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Sonic Visualiser
5     An audio file viewer and annotation editor.
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam and QMUL.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_AUDIO_CALLBACK_PLAY_SOURCE_H
17 #define SV_AUDIO_CALLBACK_PLAY_SOURCE_H
18 
19 #include "base/RingBuffer.h"
20 #include "base/AudioPlaySource.h"
21 #include "base/PropertyContainer.h"
22 #include "base/Scavenger.h"
23 
24 #include <bqaudioio/ApplicationPlaybackSource.h>
25 
26 #include <QObject>
27 #include <QMutex>
28 #include <QWaitCondition>
29 
30 #include "base/Thread.h"
31 #include "base/RealTime.h"
32 #include "data/model/Model.h"
33 
34 #include <samplerate.h>
35 
36 #include <set>
37 #include <map>
38 
39 namespace RubberBand {
40     class RubberBandStretcher;
41 }
42 
43 namespace breakfastquay {
44     class ResamplerWrapper;
45 }
46 
47 class Model;
48 class ViewManagerBase;
49 class AudioGenerator;
50 class PlayParameters;
51 class RealTimePluginInstance;
52 class AudioCallbackPlayTarget;
53 
54 /**
55  * AudioCallbackPlaySource manages audio data supply to callback-based
56  * audio APIs such as JACK or CoreAudio.  It maintains one ring buffer
57  * per channel, filled during playback by a non-realtime thread, and
58  * provides a method for a realtime thread to pick up the latest
59  * available sample data from these buffers.
60  */
61 class AudioCallbackPlaySource : public QObject,
62                                 public AudioPlaySource,
63                                 public breakfastquay::ApplicationPlaybackSource
64 {
65     Q_OBJECT
66 
67 public:
68     AudioCallbackPlaySource(ViewManagerBase *, QString clientName);
69     virtual ~AudioCallbackPlaySource();
70 
71     /**
72      * Add a data model to be played from.  The source can mix
73      * playback from a number of sources including dense and sparse
74      * models.  The models must match in sample rate, but they don't
75      * have to have identical numbers of channels.
76      */
77     virtual void addModel(ModelId model);
78 
79     /**
80      * Remove a model.
81      */
82     virtual void removeModel(ModelId model);
83 
84     /**
85      * Remove all models.  (Silence will ensue.)
86      */
87     virtual void clearModels();
88 
89     /**
90      * Start making data available in the ring buffers for playback,
91      * from the given frame.  If playback is already under way, reseek
92      * to the given frame and continue.
93      */
94     virtual void play(sv_frame_t startFrame) override;
95 
96     /**
97      * Stop playback and ensure that no more data is returned.
98      */
99     virtual void stop() override;
100 
101     /**
102      * Return whether playback is currently supposed to be happening.
103      */
isPlaying()104     virtual bool isPlaying() const override { return m_playing; }
105 
106     /**
107      * Return the frame number that is currently expected to be coming
108      * out of the speakers.  (i.e. compensating for playback latency.)
109      */
110     virtual sv_frame_t getCurrentPlayingFrame() override;
111 
112     /**
113      * Return the last frame that would come out of the speakers if we
114      * stopped playback right now.
115      */
116     virtual sv_frame_t getCurrentBufferedFrame();
117 
118     /**
119      * Return the frame at which playback is expected to end (if not looping).
120      */
getPlayEndFrame()121     virtual sv_frame_t getPlayEndFrame() { return m_lastModelEndFrame; }
122 
123     /**
124      * Set the playback target.
125      */
126     virtual void setSystemPlaybackTarget(breakfastquay::SystemPlaybackTarget *);
127 
128     /**
129      * Set the resampler wrapper, if one is in use.
130      */
131     virtual void setResamplerWrapper(breakfastquay::ResamplerWrapper *);
132 
133     /**
134      * Set the block size of the target audio device.  This should be
135      * called by the target class.
136      */
137     virtual void setSystemPlaybackBlockSize(int blockSize) override;
138 
139     /**
140      * Get the block size of the target audio device.  This may be an
141      * estimate or upper bound, if the target has a variable block
142      * size; the source should behave itself even if this value turns
143      * out to be inaccurate.
144      */
145     virtual int getTargetBlockSize() const override;
146 
147     /**
148      * Set the playback latency of the target audio device, in frames
149      * at the device sample rate.  This is the difference between the
150      * frame currently "leaving the speakers" and the last frame (or
151      * highest last frame across all channels) requested via
152      * getSamples().  The default is zero.
153      */
154     virtual void setSystemPlaybackLatency(int) override;
155 
156     /**
157      * Get the playback latency of the target audio device.
158      */
159     sv_frame_t getTargetPlayLatency() const;
160 
161     /**
162      * Specify that the target audio device has a fixed sample rate
163      * (i.e. cannot accommodate arbitrary sample rates based on the
164      * source).  If the target sets this to something other than the
165      * source sample rate, this class will resample automatically to
166      * fit.
167      */
168     virtual void setSystemPlaybackSampleRate(int) override;
169 
170     /**
171      * Return the sample rate set by the target audio device (or the
172      * source sample rate if the target hasn't set one).
173      */
174     virtual sv_samplerate_t getDeviceSampleRate() const override;
175 
176     /**
177      * Indicate how many channels the target audio device was opened
178      * with. Note that the target device does channel mixing in the
179      * case where our requested channel count does not match its, so
180      * long as we provide the number of channels we specified when the
181      * target was started in getApplicationChannelCount().
182      */
183     virtual void setSystemPlaybackChannelCount(int) override;
184 
185     /**
186      * Set the current output levels for metering (for call from the
187      * target)
188      */
189     virtual void setOutputLevels(float left, float right) override;
190 
191     /**
192      * Return the current output levels in the range 0.0 -> 1.0, for
193      * metering purposes. The values returned are the peak values
194      * since the last time this function was called (after which they
195      * are reset to zero until setOutputLevels is called again by the
196      * driver).
197      *
198      * Return true if the values have been set since this function was
199      * last called (i.e. if they are meaningful). Return false if they
200      * have not been set (in which case both will be zero).
201      */
202     virtual bool getOutputLevels(float &left, float &right) override;
203 
204     /**
205      * Get the number of channels of audio that in the source models.
206      * This may safely be called from a realtime thread.  Returns 0 if
207      * there is no source yet available.
208      */
209     int getSourceChannelCount() const;
210 
211     /**
212      * Get the number of channels of audio that will be provided
213      * to the play target.  This may be more than the source channel
214      * count: for example, a mono source will provide 2 channels
215      * after pan.
216      *
217      * This may safely be called from a realtime thread.  Returns 0 if
218      * there is no source yet available.
219      *
220      * override from AudioPlaySource
221      */
222     virtual int getTargetChannelCount() const override;
223 
224     /**
225      * Get the number of channels of audio the device is
226      * expecting. Equal to whatever getTargetChannelCount() was
227      * returning at the time the device was initialised.
228      */
229     int getDeviceChannelCount() const;
230 
231     /**
232      * ApplicationPlaybackSource equivalent of the above.
233      *
234      * override from breakfastquay::ApplicationPlaybackSource
235      */
getApplicationChannelCount()236     virtual int getApplicationChannelCount() const override {
237         return getTargetChannelCount();
238     }
239 
240     /**
241      * Get the actual sample rate of the source material (the main
242      * model).  This may safely be called from a realtime thread.
243      * Returns 0 if there is no source yet available.
244      *
245      * When this changes, the AudioCallbackPlaySource notifies its
246      * ResamplerWrapper of the new sample rate so that it can resample
247      * correctly on the way to the device (which is opened at a fixed
248      * rate, see getApplicationSampleRate).
249      */
250     virtual sv_samplerate_t getSourceSampleRate() const override;
251 
252     /**
253      * ApplicationPlaybackSource interface method: get the sample rate
254      * at which the application wants the device to be opened. We
255      * always allow the device to open at its default rate, and then
256      * we resample if the audio is at a different rate. This avoids
257      * having to close and re-open the device to obtain consistent
258      * behaviour for consecutive sessions with different source rates.
259      */
getApplicationSampleRate()260     virtual int getApplicationSampleRate() const override {
261         return 0;
262     }
263 
264     /**
265      * Get "count" samples (at the target sample rate) of the mixed
266      * audio data, in all channels.  This may safely be called from a
267      * realtime thread.
268      */
269     virtual int getSourceSamples(float *const *buffer, int nchannels, int count) override;
270 
271     /**
272      * Set the time stretcher factor (i.e. playback speed).
273      */
274     void setTimeStretch(double factor);
275 
276     /**
277      * Set a single real-time plugin as a processing effect for
278      * auditioning during playback.
279      *
280      * The plugin must have been initialised with
281      * getTargetChannelCount() channels and a getTargetBlockSize()
282      * sample frame processing block size.
283      *
284      * This playback source takes ownership of the plugin, which will
285      * be deleted at some point after the following call to
286      * setAuditioningEffect (depending on real-time constraints).
287      *
288      * Pass a null pointer to remove the current auditioning plugin,
289      * if any.
290      */
291     virtual void setAuditioningEffect(Auditionable *plugin) override;
292 
293     /**
294      * Specify that only the given set of models should be played.
295      */
296     void setSoloModelSet(std::set<ModelId>s);
297 
298     /**
299      * Specify that all models should be played as normal (if not
300      * muted).
301      */
302     void clearSoloModelSet();
303 
getClientName()304     virtual std::string getClientName() const override {
305         return m_clientName;
306     }
307 
308 signals:
309     void playStatusChanged(bool isPlaying);
310 
311     void sampleRateMismatch(sv_samplerate_t requested,
312                             sv_samplerate_t available,
313                             bool willResample);
314 
315     void channelCountIncreased(int count); // target channel count (see getTargetChannelCount())
316 
317     void audioOverloadPluginDisabled();
318     void audioTimeStretchMultiChannelDisabled();
319 
320     void activity(QString);
321 
322 public slots:
323     void audioProcessingOverload() override;
324 
325 protected slots:
326     void selectionChanged();
327     void playLoopModeChanged();
328     void playSelectionModeChanged();
329     void playParametersChanged(int);
330     void preferenceChanged(PropertyContainer::PropertyName);
331     void modelChangedWithin(ModelId, sv_frame_t startFrame, sv_frame_t endFrame);
332 
333 protected:
334     ViewManagerBase                  *m_viewManager;
335     AudioGenerator                   *m_audioGenerator;
336     std::string                       m_clientName;
337 
338     class RingBufferVector : public std::vector<RingBuffer<float> *> {
339     public:
~RingBufferVector()340         virtual ~RingBufferVector() {
341             while (!empty()) {
342                 delete *begin();
343                 erase(begin());
344             }
345         }
346     };
347 
348     std::set<ModelId>                 m_models;
349     RingBufferVector                 *m_readBuffers;
350     RingBufferVector                 *m_writeBuffers;
351     sv_frame_t                        m_readBufferFill;
352     sv_frame_t                        m_writeBufferFill;
353     Scavenger<RingBufferVector>       m_bufferScavenger;
354     int                               m_sourceChannelCount;
355     sv_frame_t                        m_blockSize;
356     sv_samplerate_t                   m_sourceSampleRate;
357     sv_samplerate_t                   m_deviceSampleRate;
358     int                               m_deviceChannelCount;
359     sv_frame_t                        m_playLatency;
360     breakfastquay::SystemPlaybackTarget *m_target;
361     double                            m_lastRetrievalTimestamp;
362     sv_frame_t                        m_lastRetrievedBlockSize;
363     bool                              m_trustworthyTimestamps;
364     sv_frame_t                        m_lastCurrentFrame;
365     bool                              m_playing;
366     bool                              m_exiting;
367     sv_frame_t                        m_lastModelEndFrame;
368     int                               m_ringBufferSize;
369     float                             m_outputLeft;
370     float                             m_outputRight;
371     bool                              m_levelsSet;
372     RealTimePluginInstance           *m_auditioningPlugin;
373     bool                              m_auditioningPluginBypassed;
374     Scavenger<RealTimePluginInstance> m_pluginScavenger;
375     sv_frame_t                        m_playStartFrame;
376     bool                              m_playStartFramePassed;
377     RealTime                          m_playStartedAt;
378 
getWriteRingBuffer(int c)379     RingBuffer<float> *getWriteRingBuffer(int c) {
380         if (m_writeBuffers && c < (int)m_writeBuffers->size()) {
381             return (*m_writeBuffers)[c];
382         } else {
383             return 0;
384         }
385     }
386 
getReadRingBuffer(int c)387     RingBuffer<float> *getReadRingBuffer(int c) {
388         RingBufferVector *rb = m_readBuffers;
389         if (rb && c < (int)rb->size()) {
390             return (*rb)[c];
391         } else {
392             return 0;
393         }
394     }
395 
396     void clearRingBuffers(bool haveLock = false, int count = 0);
397     void unifyRingBuffers();
398 
399     RubberBand::RubberBandStretcher *m_timeStretcher;
400     RubberBand::RubberBandStretcher *m_monoStretcher;
401     double m_stretchRatio;
402     bool m_stretchMono;
403 
404     int m_stretcherInputCount;
405     float **m_stretcherInputs;
406     sv_frame_t *m_stretcherInputSizes;
407 
408     // Called from fill thread, m_playing true, mutex held
409     // Return true if work done
410     bool fillBuffers();
411 
412     // Called from fillBuffers.  Return the number of frames written,
413     // which will be count or fewer.  Return in the frame argument the
414     // new buffered frame position (which may be earlier than the
415     // frame argument passed in, in the case of looping).
416     sv_frame_t mixModels(sv_frame_t &frame, sv_frame_t count, float **buffers);
417 
418     // Called from getSourceSamples.
419     void applyAuditioningEffect(sv_frame_t count, float *const *buffers);
420 
421     // Ranges of current selections, if play selection is active
422     std::vector<RealTime> m_rangeStarts;
423     std::vector<RealTime> m_rangeDurations;
424     void rebuildRangeLists();
425 
426     sv_frame_t getCurrentFrame(RealTime outputLatency);
427 
428     class FillThread : public Thread
429     {
430     public:
FillThread(AudioCallbackPlaySource & source)431         FillThread(AudioCallbackPlaySource &source) :
432             Thread(Thread::NonRTThread),
433             m_source(source) { }
434 
435         void run() override;
436 
437     protected:
438         AudioCallbackPlaySource &m_source;
439     };
440 
441     QMutex m_mutex;
442     QWaitCondition m_condition;
443     FillThread *m_fillThread;
444     breakfastquay::ResamplerWrapper *m_resamplerWrapper; // I don't own this
445 };
446 
447 #endif
448 
449 
450