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 #include "AudioCallbackPlaySource.h"
|
Chris@43
|
17
|
Chris@43
|
18 #include "AudioGenerator.h"
|
Chris@738
|
19 #include "TimeStretchWrapper.h"
|
Chris@739
|
20 #include "EffectWrapper.h"
|
Chris@43
|
21
|
Chris@43
|
22 #include "data/model/Model.h"
|
Chris@105
|
23 #include "base/ViewManagerBase.h"
|
Chris@43
|
24 #include "base/PlayParameterRepository.h"
|
Chris@43
|
25 #include "base/Preferences.h"
|
Chris@43
|
26 #include "data/model/DenseTimeValueModel.h"
|
Chris@43
|
27 #include "data/model/WaveFileModel.h"
|
Chris@506
|
28 #include "data/model/ReadOnlyWaveFileModel.h"
|
Chris@43
|
29 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@43
|
30 #include "plugin/RealTimePluginInstance.h"
|
Chris@62
|
31
|
Chris@468
|
32 #include "bqaudioio/SystemPlaybackTarget.h"
|
Chris@551
|
33 #include "bqaudioio/ResamplerWrapper.h"
|
Chris@91
|
34
|
Chris@559
|
35 #include "bqvec/VectorOps.h"
|
Chris@559
|
36
|
Chris@559
|
37 using breakfastquay::v_zero_channels;
|
Chris@559
|
38
|
Chris@43
|
39 #include <iostream>
|
Chris@43
|
40 #include <cassert>
|
Chris@43
|
41
|
Chris@510
|
42 //#define DEBUG_AUDIO_PLAY_SOURCE 1
|
Chris@43
|
43 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
|
Chris@43
|
44
|
Chris@366
|
45 static const int DEFAULT_RING_BUFFER_SIZE = 131071;
|
Chris@43
|
46
|
Chris@105
|
47 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManagerBase *manager,
|
Chris@57
|
48 QString clientName) :
|
Chris@43
|
49 m_viewManager(manager),
|
Chris@43
|
50 m_audioGenerator(new AudioGenerator()),
|
Chris@468
|
51 m_clientName(clientName.toUtf8().data()),
|
Chris@636
|
52 m_readBuffers(nullptr),
|
Chris@636
|
53 m_writeBuffers(nullptr),
|
Chris@43
|
54 m_readBufferFill(0),
|
Chris@43
|
55 m_writeBufferFill(0),
|
Chris@43
|
56 m_bufferScavenger(1),
|
Chris@43
|
57 m_sourceChannelCount(0),
|
Chris@43
|
58 m_blockSize(1024),
|
Chris@43
|
59 m_sourceSampleRate(0),
|
Chris@553
|
60 m_deviceSampleRate(0),
|
Chris@559
|
61 m_deviceChannelCount(0),
|
Chris@43
|
62 m_playLatency(0),
|
Chris@636
|
63 m_target(nullptr),
|
Chris@91
|
64 m_lastRetrievalTimestamp(0.0),
|
Chris@91
|
65 m_lastRetrievedBlockSize(0),
|
Chris@102
|
66 m_trustworthyTimestamps(true),
|
Chris@102
|
67 m_lastCurrentFrame(0),
|
Chris@43
|
68 m_playing(false),
|
Chris@43
|
69 m_exiting(false),
|
Chris@43
|
70 m_lastModelEndFrame(0),
|
Chris@193
|
71 m_ringBufferSize(DEFAULT_RING_BUFFER_SIZE),
|
Chris@43
|
72 m_outputLeft(0.0),
|
Chris@43
|
73 m_outputRight(0.0),
|
Chris@580
|
74 m_levelsSet(false),
|
Chris@94
|
75 m_playStartFrame(0),
|
Chris@94
|
76 m_playStartFramePassed(false),
|
Chris@636
|
77 m_fillThread(nullptr),
|
Chris@738
|
78 m_resamplerWrapper(nullptr),
|
Chris@739
|
79 m_timeStretchWrapper(nullptr),
|
Chris@739
|
80 m_auditioningEffectWrapper(nullptr)
|
Chris@43
|
81 {
|
Chris@43
|
82 m_viewManager->setAudioPlaySource(this);
|
Chris@43
|
83
|
Chris@43
|
84 connect(m_viewManager, SIGNAL(selectionChanged()),
|
Chris@595
|
85 this, SLOT(selectionChanged()));
|
Chris@43
|
86 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
|
Chris@595
|
87 this, SLOT(playLoopModeChanged()));
|
Chris@43
|
88 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
|
Chris@595
|
89 this, SLOT(playSelectionModeChanged()));
|
Chris@43
|
90
|
Chris@300
|
91 connect(this, SIGNAL(playStatusChanged(bool)),
|
Chris@300
|
92 m_viewManager, SLOT(playStatusChanged(bool)));
|
Chris@300
|
93
|
Chris@43
|
94 connect(PlayParameterRepository::getInstance(),
|
Chris@687
|
95 SIGNAL(playParametersChanged(int)),
|
Chris@687
|
96 this, SLOT(playParametersChanged(int)));
|
Chris@43
|
97
|
Chris@43
|
98 connect(Preferences::getInstance(),
|
Chris@43
|
99 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
|
Chris@43
|
100 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
|
Chris@43
|
101 }
|
Chris@43
|
102
|
Chris@43
|
103 AudioCallbackPlaySource::~AudioCallbackPlaySource()
|
Chris@43
|
104 {
|
Chris@177
|
105 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@233
|
106 SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource entering" << endl;
|
Chris@177
|
107 #endif
|
Chris@43
|
108 m_exiting = true;
|
Chris@43
|
109
|
Chris@43
|
110 if (m_fillThread) {
|
Chris@212
|
111 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
112 cout << "AudioCallbackPlaySource dtor: awakening thread" << endl;
|
Chris@212
|
113 #endif
|
Chris@212
|
114 m_condition.wakeAll();
|
Chris@595
|
115 m_fillThread->wait();
|
Chris@595
|
116 delete m_fillThread;
|
Chris@43
|
117 }
|
Chris@43
|
118
|
Chris@43
|
119 clearModels();
|
Chris@43
|
120
|
Chris@43
|
121 if (m_readBuffers != m_writeBuffers) {
|
Chris@595
|
122 delete m_readBuffers;
|
Chris@43
|
123 }
|
Chris@43
|
124
|
Chris@43
|
125 delete m_writeBuffers;
|
Chris@43
|
126
|
Chris@43
|
127 delete m_audioGenerator;
|
Chris@43
|
128
|
Chris@739
|
129 delete m_timeStretchWrapper;
|
Chris@739
|
130 delete m_auditioningEffectWrapper;
|
Chris@739
|
131 delete m_resamplerWrapper;
|
Chris@739
|
132
|
Chris@43
|
133 m_bufferScavenger.scavenge(true);
|
Chris@43
|
134 m_pluginScavenger.scavenge(true);
|
Chris@177
|
135 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@233
|
136 SVDEBUG << "AudioCallbackPlaySource::~AudioCallbackPlaySource finishing" << endl;
|
Chris@177
|
137 #endif
|
Chris@43
|
138 }
|
Chris@43
|
139
|
Chris@738
|
140 breakfastquay::ApplicationPlaybackSource *
|
Chris@738
|
141 AudioCallbackPlaySource::getApplicationPlaybackSource()
|
Chris@738
|
142 {
|
Chris@738
|
143 QMutexLocker locker(&m_mutex);
|
Chris@738
|
144
|
Chris@738
|
145 if (m_timeStretchWrapper) {
|
Chris@738
|
146 return m_timeStretchWrapper;
|
Chris@738
|
147 }
|
Chris@738
|
148
|
Chris@738
|
149 checkWrappers();
|
Chris@738
|
150 return m_timeStretchWrapper;
|
Chris@738
|
151 }
|
Chris@738
|
152
|
Chris@738
|
153 void
|
Chris@738
|
154 AudioCallbackPlaySource::checkWrappers()
|
Chris@738
|
155 {
|
Chris@738
|
156 // to be called only with m_mutex held
|
Chris@738
|
157
|
Chris@738
|
158 if (!m_resamplerWrapper) {
|
Chris@738
|
159 m_resamplerWrapper = new breakfastquay::ResamplerWrapper(this);
|
Chris@738
|
160 }
|
Chris@739
|
161 if (!m_auditioningEffectWrapper) {
|
Chris@739
|
162 m_auditioningEffectWrapper = new EffectWrapper(m_resamplerWrapper);
|
Chris@739
|
163 }
|
Chris@738
|
164 if (!m_timeStretchWrapper) {
|
Chris@739
|
165 m_timeStretchWrapper = new TimeStretchWrapper(m_auditioningEffectWrapper);
|
Chris@738
|
166 }
|
Chris@738
|
167 }
|
Chris@738
|
168
|
Chris@43
|
169 void
|
Chris@682
|
170 AudioCallbackPlaySource::addModel(ModelId modelId)
|
Chris@43
|
171 {
|
Chris@682
|
172 if (m_models.find(modelId) != m_models.end()) return;
|
Chris@43
|
173
|
Chris@682
|
174 bool willPlay = m_audioGenerator->addModel(modelId);
|
Chris@682
|
175
|
Chris@682
|
176 auto model = ModelById::get(modelId);
|
Chris@682
|
177 if (!model) return;
|
Chris@43
|
178
|
Chris@43
|
179 m_mutex.lock();
|
Chris@43
|
180
|
Chris@682
|
181 m_models.insert(modelId);
|
Chris@682
|
182
|
Chris@43
|
183 if (model->getEndFrame() > m_lastModelEndFrame) {
|
Chris@595
|
184 m_lastModelEndFrame = model->getEndFrame();
|
Chris@43
|
185 }
|
Chris@43
|
186
|
Chris@559
|
187 bool buffersIncreased = false, srChanged = false;
|
Chris@43
|
188
|
Chris@366
|
189 int modelChannels = 1;
|
Chris@682
|
190 auto rowfm = std::dynamic_pointer_cast<ReadOnlyWaveFileModel>(model);
|
Chris@506
|
191 if (rowfm) modelChannels = rowfm->getChannelCount();
|
Chris@43
|
192 if (modelChannels > m_sourceChannelCount) {
|
Chris@595
|
193 m_sourceChannelCount = modelChannels;
|
Chris@43
|
194 }
|
Chris@43
|
195
|
Chris@43
|
196 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@295
|
197 cout << "AudioCallbackPlaySource: Adding model with " << modelChannels << " channels at rate " << model->getSampleRate() << endl;
|
Chris@43
|
198 #endif
|
Chris@43
|
199
|
Chris@43
|
200 if (m_sourceSampleRate == 0) {
|
Chris@43
|
201
|
Chris@566
|
202 SVDEBUG << "AudioCallbackPlaySource::addModel: Source rate changing from 0 to "
|
Chris@566
|
203 << model->getSampleRate() << endl;
|
Chris@566
|
204
|
Chris@595
|
205 m_sourceSampleRate = model->getSampleRate();
|
Chris@595
|
206 srChanged = true;
|
Chris@43
|
207
|
Chris@43
|
208 } else if (model->getSampleRate() != m_sourceSampleRate) {
|
Chris@43
|
209
|
Chris@506
|
210 // If this is a read-only wave file model and we have no
|
Chris@506
|
211 // other, we can just switch to this model's sample rate
|
Chris@43
|
212
|
Chris@506
|
213 if (rowfm) {
|
Chris@43
|
214
|
Chris@43
|
215 bool conflicting = false;
|
Chris@43
|
216
|
Chris@682
|
217 for (ModelId otherId: m_models) {
|
Chris@506
|
218 // Only read-only wave file models should be
|
Chris@506
|
219 // considered conflicting -- writable wave file models
|
Chris@506
|
220 // are derived and we shouldn't take their rates into
|
Chris@506
|
221 // account. Also, don't give any particular weight to
|
Chris@506
|
222 // a file that's already playing at the wrong rate
|
Chris@506
|
223 // anyway
|
Chris@682
|
224 if (otherId == modelId) continue;
|
Chris@682
|
225 auto other = ModelById::getAs<ReadOnlyWaveFileModel>(otherId);
|
Chris@682
|
226 if (other &&
|
Chris@506
|
227 other->getSampleRate() != model->getSampleRate() &&
|
Chris@506
|
228 other->getSampleRate() == m_sourceSampleRate) {
|
Chris@682
|
229 SVDEBUG << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << otherId << " found" << endl;
|
Chris@43
|
230 conflicting = true;
|
Chris@43
|
231 break;
|
Chris@43
|
232 }
|
Chris@43
|
233 }
|
Chris@43
|
234
|
Chris@43
|
235 if (conflicting) {
|
Chris@43
|
236
|
Chris@625
|
237 SVCERR << "AudioCallbackPlaySource::addModel: ERROR: "
|
Chris@229
|
238 << "New model sample rate does not match" << endl
|
Chris@43
|
239 << "existing model(s) (new " << model->getSampleRate()
|
Chris@43
|
240 << " vs " << m_sourceSampleRate
|
Chris@43
|
241 << "), playback will be wrong"
|
Chris@229
|
242 << endl;
|
Chris@43
|
243
|
Chris@43
|
244 emit sampleRateMismatch(model->getSampleRate(),
|
Chris@43
|
245 m_sourceSampleRate,
|
Chris@43
|
246 false);
|
Chris@43
|
247 } else {
|
Chris@566
|
248 SVDEBUG << "AudioCallbackPlaySource::addModel: Source rate changing from "
|
Chris@566
|
249 << m_sourceSampleRate << " to " << model->getSampleRate() << endl;
|
Chris@566
|
250
|
Chris@43
|
251 m_sourceSampleRate = model->getSampleRate();
|
Chris@43
|
252 srChanged = true;
|
Chris@43
|
253 }
|
Chris@43
|
254 }
|
Chris@43
|
255 }
|
Chris@43
|
256
|
Chris@366
|
257 if (!m_writeBuffers || (int)m_writeBuffers->size() < getTargetChannelCount()) {
|
Chris@570
|
258 cerr << "m_writeBuffers size = " << (m_writeBuffers ? m_writeBuffers->size() : 0) << endl;
|
Chris@570
|
259 cerr << "target channel count = " << (getTargetChannelCount()) << endl;
|
Chris@595
|
260 clearRingBuffers(true, getTargetChannelCount());
|
Chris@595
|
261 buffersIncreased = true;
|
Chris@43
|
262 } else {
|
Chris@595
|
263 if (willPlay) clearRingBuffers(true);
|
Chris@43
|
264 }
|
Chris@43
|
265
|
Chris@552
|
266 if (srChanged) {
|
Chris@553
|
267
|
Chris@738
|
268 checkWrappers();
|
Chris@553
|
269
|
Chris@738
|
270 SVCERR << "AudioCallbackPlaySource: Source sample rate changed to "
|
Chris@738
|
271 << m_sourceSampleRate << ", updating resampler wrapper"
|
Chris@738
|
272 << endl;
|
Chris@738
|
273 m_resamplerWrapper->changeApplicationSampleRate
|
Chris@738
|
274 (int(round(m_sourceSampleRate)));
|
Chris@738
|
275 m_resamplerWrapper->reset();
|
Chris@43
|
276 }
|
Chris@43
|
277
|
Chris@164
|
278 rebuildRangeLists();
|
Chris@164
|
279
|
Chris@43
|
280 m_mutex.unlock();
|
Chris@43
|
281
|
Chris@43
|
282 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
|
Chris@43
|
283
|
Chris@559
|
284 if (buffersIncreased) {
|
Chris@570
|
285 SVDEBUG << "AudioCallbackPlaySource::addModel: Number of buffers increased to " << getTargetChannelCount() << endl;
|
Chris@570
|
286 if (getTargetChannelCount() > getDeviceChannelCount()) {
|
Chris@570
|
287 SVDEBUG << "AudioCallbackPlaySource::addModel: This is more than the device channel count, signalling channelCountIncreased" << endl;
|
Chris@570
|
288 emit channelCountIncreased(getTargetChannelCount());
|
Chris@570
|
289 } else {
|
Chris@570
|
290 SVDEBUG << "AudioCallbackPlaySource::addModel: This is no more than the device channel count (" << getDeviceChannelCount() << "), so taking no action" << endl;
|
Chris@570
|
291 }
|
Chris@559
|
292 }
|
Chris@559
|
293
|
Chris@43
|
294 if (!m_fillThread) {
|
Chris@595
|
295 m_fillThread = new FillThread(*this);
|
Chris@595
|
296 m_fillThread->start();
|
Chris@43
|
297 }
|
Chris@43
|
298
|
Chris@43
|
299 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@559
|
300 SVDEBUG << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s)" << endl;
|
Chris@43
|
301 #endif
|
Chris@43
|
302
|
Chris@687
|
303 connect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
|
Chris@687
|
304 this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
|
Chris@43
|
305
|
Chris@212
|
306 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
307 cout << "AudioCallbackPlaySource::addModel: awakening thread" << endl;
|
Chris@212
|
308 #endif
|
Chris@559
|
309
|
Chris@43
|
310 m_condition.wakeAll();
|
Chris@43
|
311 }
|
Chris@43
|
312
|
Chris@43
|
313 void
|
Chris@687
|
314 AudioCallbackPlaySource::modelChangedWithin(ModelId, sv_frame_t
|
Chris@367
|
315 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@367
|
316 startFrame
|
Chris@367
|
317 #endif
|
Chris@435
|
318 , sv_frame_t endFrame)
|
Chris@43
|
319 {
|
Chris@43
|
320 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@367
|
321 SVDEBUG << "AudioCallbackPlaySource::modelChangedWithin(" << startFrame << "," << endFrame << ")" << endl;
|
Chris@43
|
322 #endif
|
Chris@93
|
323 if (endFrame > m_lastModelEndFrame) {
|
Chris@93
|
324 m_lastModelEndFrame = endFrame;
|
Chris@99
|
325 rebuildRangeLists();
|
Chris@93
|
326 }
|
Chris@43
|
327 }
|
Chris@43
|
328
|
Chris@43
|
329 void
|
Chris@682
|
330 AudioCallbackPlaySource::removeModel(ModelId modelId)
|
Chris@43
|
331 {
|
Chris@682
|
332 auto model = ModelById::get(modelId);
|
Chris@682
|
333 if (!model) return;
|
Chris@682
|
334
|
Chris@43
|
335 m_mutex.lock();
|
Chris@43
|
336
|
Chris@43
|
337 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@682
|
338 cout << "AudioCallbackPlaySource::removeModel(" << modelId << ")" << endl;
|
Chris@43
|
339 #endif
|
Chris@43
|
340
|
Chris@687
|
341 disconnect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
|
Chris@687
|
342 this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
|
Chris@43
|
343
|
Chris@682
|
344 m_models.erase(modelId);
|
Chris@43
|
345
|
Chris@436
|
346 sv_frame_t lastEnd = 0;
|
Chris@682
|
347 for (ModelId otherId: m_models) {
|
Chris@164
|
348 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@682
|
349 cout << "AudioCallbackPlaySource::removeModel(" << modelId << "): checking end frame on model " << otherId << endl;
|
Chris@164
|
350 #endif
|
Chris@682
|
351 if (auto other = ModelById::get(otherId)) {
|
Chris@682
|
352 if (other->getEndFrame() > lastEnd) {
|
Chris@682
|
353 lastEnd = other->getEndFrame();
|
Chris@682
|
354 }
|
Chris@367
|
355 }
|
Chris@164
|
356 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
357 cout << "(done, lastEnd now " << lastEnd << ")" << endl;
|
Chris@164
|
358 #endif
|
Chris@43
|
359 }
|
Chris@43
|
360 m_lastModelEndFrame = lastEnd;
|
Chris@43
|
361
|
Chris@682
|
362 m_audioGenerator->removeModel(modelId);
|
Chris@212
|
363
|
Chris@680
|
364 if (m_models.empty()) {
|
Chris@680
|
365 m_sourceSampleRate = 0;
|
Chris@680
|
366 }
|
Chris@680
|
367
|
Chris@43
|
368 m_mutex.unlock();
|
Chris@43
|
369
|
Chris@43
|
370 clearRingBuffers();
|
Chris@43
|
371 }
|
Chris@43
|
372
|
Chris@43
|
373 void
|
Chris@43
|
374 AudioCallbackPlaySource::clearModels()
|
Chris@43
|
375 {
|
Chris@43
|
376 m_mutex.lock();
|
Chris@43
|
377
|
Chris@43
|
378 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
379 cout << "AudioCallbackPlaySource::clearModels()" << endl;
|
Chris@43
|
380 #endif
|
Chris@43
|
381
|
Chris@43
|
382 m_models.clear();
|
Chris@43
|
383
|
Chris@43
|
384 m_lastModelEndFrame = 0;
|
Chris@43
|
385
|
Chris@43
|
386 m_sourceSampleRate = 0;
|
Chris@43
|
387
|
Chris@43
|
388 m_mutex.unlock();
|
Chris@43
|
389
|
Chris@43
|
390 m_audioGenerator->clearModels();
|
Chris@93
|
391
|
Chris@93
|
392 clearRingBuffers();
|
Chris@43
|
393 }
|
Chris@43
|
394
|
Chris@43
|
395 void
|
Chris@366
|
396 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, int count)
|
Chris@43
|
397 {
|
Chris@43
|
398 if (!haveLock) m_mutex.lock();
|
Chris@43
|
399
|
Chris@445
|
400 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
401 cout << "clearRingBuffers" << endl;
|
Chris@445
|
402 #endif
|
Chris@397
|
403
|
Chris@93
|
404 rebuildRangeLists();
|
Chris@93
|
405
|
Chris@43
|
406 if (count == 0) {
|
Chris@595
|
407 if (m_writeBuffers) count = int(m_writeBuffers->size());
|
Chris@43
|
408 }
|
Chris@43
|
409
|
Chris@445
|
410 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
411 cout << "current playing frame = " << getCurrentPlayingFrame() << endl;
|
Chris@397
|
412
|
Chris@563
|
413 cout << "write buffer fill (before) = " << m_writeBufferFill << endl;
|
Chris@445
|
414 #endif
|
Chris@445
|
415
|
Chris@93
|
416 m_writeBufferFill = getCurrentBufferedFrame();
|
Chris@43
|
417
|
Chris@445
|
418 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
419 cout << "current buffered frame = " << m_writeBufferFill << endl;
|
Chris@445
|
420 #endif
|
Chris@397
|
421
|
Chris@43
|
422 if (m_readBuffers != m_writeBuffers) {
|
Chris@595
|
423 delete m_writeBuffers;
|
Chris@43
|
424 }
|
Chris@43
|
425
|
Chris@43
|
426 m_writeBuffers = new RingBufferVector;
|
Chris@43
|
427
|
Chris@366
|
428 for (int i = 0; i < count; ++i) {
|
Chris@595
|
429 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
|
Chris@43
|
430 }
|
Chris@43
|
431
|
Chris@442
|
432 m_audioGenerator->reset();
|
Chris@442
|
433
|
Chris@293
|
434 // cout << "AudioCallbackPlaySource::clearRingBuffers: Created "
|
Chris@595
|
435 // << count << " write buffers" << endl;
|
Chris@43
|
436
|
Chris@43
|
437 if (!haveLock) {
|
Chris@595
|
438 m_mutex.unlock();
|
Chris@43
|
439 }
|
Chris@43
|
440 }
|
Chris@43
|
441
|
Chris@43
|
442 void
|
Chris@434
|
443 AudioCallbackPlaySource::play(sv_frame_t startFrame)
|
Chris@43
|
444 {
|
Chris@540
|
445 if (!m_target) return;
|
Chris@540
|
446
|
Chris@414
|
447 if (!m_sourceSampleRate) {
|
Chris@563
|
448 SVCERR << "AudioCallbackPlaySource::play: No source sample rate available, not playing" << endl;
|
Chris@414
|
449 return;
|
Chris@414
|
450 }
|
Chris@414
|
451
|
Chris@43
|
452 if (m_viewManager->getPlaySelectionMode() &&
|
Chris@595
|
453 !m_viewManager->getSelections().empty()) {
|
Chris@60
|
454
|
Chris@563
|
455 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
456 cout << "AudioCallbackPlaySource::play: constraining frame " << startFrame << " to selection = ";
|
Chris@563
|
457 #endif
|
Chris@94
|
458
|
Chris@60
|
459 startFrame = m_viewManager->constrainFrameToSelection(startFrame);
|
Chris@60
|
460
|
Chris@563
|
461 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
462 cout << startFrame << endl;
|
Chris@563
|
463 #endif
|
Chris@94
|
464
|
Chris@43
|
465 } else {
|
Chris@454
|
466 if (startFrame < 0) {
|
Chris@454
|
467 startFrame = 0;
|
Chris@454
|
468 }
|
Chris@595
|
469 if (startFrame >= m_lastModelEndFrame) {
|
Chris@595
|
470 startFrame = 0;
|
Chris@595
|
471 }
|
Chris@43
|
472 }
|
Chris@43
|
473
|
Chris@132
|
474 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
475 cout << "play(" << startFrame << ") -> aligned playback model ";
|
Chris@132
|
476 #endif
|
Chris@60
|
477
|
Chris@60
|
478 startFrame = m_viewManager->alignReferenceToPlaybackFrame(startFrame);
|
Chris@60
|
479
|
Chris@189
|
480 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
481 cout << startFrame << endl;
|
Chris@189
|
482 #endif
|
Chris@60
|
483
|
Chris@43
|
484 // The fill thread will automatically empty its buffers before
|
Chris@43
|
485 // starting again if we have not so far been playing, but not if
|
Chris@43
|
486 // we're just re-seeking.
|
Chris@102
|
487 // NO -- we can end up playing some first -- always reset here
|
Chris@43
|
488
|
Chris@43
|
489 m_mutex.lock();
|
Chris@102
|
490
|
Chris@738
|
491 if (m_timeStretchWrapper) {
|
Chris@738
|
492 m_timeStretchWrapper->reset();
|
Chris@130
|
493 }
|
Chris@102
|
494
|
Chris@102
|
495 m_readBufferFill = m_writeBufferFill = startFrame;
|
Chris@102
|
496 if (m_readBuffers) {
|
Chris@366
|
497 for (int c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@102
|
498 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@132
|
499 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
500 cout << "reset ring buffer for channel " << c << endl;
|
Chris@132
|
501 #endif
|
Chris@102
|
502 if (rb) rb->reset();
|
Chris@102
|
503 }
|
Chris@43
|
504 }
|
Chris@102
|
505
|
Chris@43
|
506 m_mutex.unlock();
|
Chris@43
|
507
|
Chris@43
|
508 m_audioGenerator->reset();
|
Chris@43
|
509
|
Chris@94
|
510 m_playStartFrame = startFrame;
|
Chris@94
|
511 m_playStartFramePassed = false;
|
Chris@94
|
512 m_playStartedAt = RealTime::zeroTime;
|
Chris@94
|
513 if (m_target) {
|
Chris@94
|
514 m_playStartedAt = RealTime::fromSeconds(m_target->getCurrentTime());
|
Chris@94
|
515 }
|
Chris@94
|
516
|
Chris@43
|
517 bool changed = !m_playing;
|
Chris@91
|
518 m_lastRetrievalTimestamp = 0;
|
Chris@102
|
519 m_lastCurrentFrame = 0;
|
Chris@43
|
520 m_playing = true;
|
Chris@212
|
521
|
Chris@212
|
522 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
523 cout << "AudioCallbackPlaySource::play: awakening thread" << endl;
|
Chris@212
|
524 #endif
|
Chris@212
|
525
|
Chris@43
|
526 m_condition.wakeAll();
|
Chris@158
|
527 if (changed) {
|
Chris@158
|
528 emit playStatusChanged(m_playing);
|
Chris@158
|
529 emit activity(tr("Play from %1").arg
|
Chris@158
|
530 (RealTime::frame2RealTime
|
Chris@158
|
531 (m_playStartFrame, m_sourceSampleRate).toText().c_str()));
|
Chris@158
|
532 }
|
Chris@43
|
533 }
|
Chris@43
|
534
|
Chris@43
|
535 void
|
Chris@43
|
536 AudioCallbackPlaySource::stop()
|
Chris@43
|
537 {
|
Chris@212
|
538 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@233
|
539 SVDEBUG << "AudioCallbackPlaySource::stop()" << endl;
|
Chris@212
|
540 #endif
|
Chris@43
|
541 bool changed = m_playing;
|
Chris@43
|
542 m_playing = false;
|
Chris@212
|
543
|
Chris@212
|
544 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
545 cout << "AudioCallbackPlaySource::stop: awakening thread" << endl;
|
Chris@212
|
546 #endif
|
Chris@212
|
547
|
Chris@43
|
548 m_condition.wakeAll();
|
Chris@91
|
549 m_lastRetrievalTimestamp = 0;
|
Chris@158
|
550 if (changed) {
|
Chris@158
|
551 emit playStatusChanged(m_playing);
|
Chris@713
|
552 if (m_sourceSampleRate) {
|
Chris@713
|
553 emit activity(tr("Stop at %1").arg
|
Chris@713
|
554 (RealTime::frame2RealTime
|
Chris@713
|
555 (m_lastCurrentFrame, m_sourceSampleRate)
|
Chris@713
|
556 .toText().c_str()));
|
Chris@713
|
557 } else {
|
Chris@713
|
558 emit activity(tr("Stop"));
|
Chris@713
|
559 }
|
Chris@158
|
560 }
|
Chris@102
|
561 m_lastCurrentFrame = 0;
|
Chris@43
|
562 }
|
Chris@43
|
563
|
Chris@43
|
564 void
|
Chris@43
|
565 AudioCallbackPlaySource::selectionChanged()
|
Chris@43
|
566 {
|
Chris@43
|
567 if (m_viewManager->getPlaySelectionMode()) {
|
Chris@595
|
568 clearRingBuffers();
|
Chris@43
|
569 }
|
Chris@43
|
570 }
|
Chris@43
|
571
|
Chris@43
|
572 void
|
Chris@43
|
573 AudioCallbackPlaySource::playLoopModeChanged()
|
Chris@43
|
574 {
|
Chris@43
|
575 clearRingBuffers();
|
Chris@43
|
576 }
|
Chris@43
|
577
|
Chris@43
|
578 void
|
Chris@43
|
579 AudioCallbackPlaySource::playSelectionModeChanged()
|
Chris@43
|
580 {
|
Chris@43
|
581 if (!m_viewManager->getSelections().empty()) {
|
Chris@595
|
582 clearRingBuffers();
|
Chris@43
|
583 }
|
Chris@43
|
584 }
|
Chris@43
|
585
|
Chris@43
|
586 void
|
Chris@687
|
587 AudioCallbackPlaySource::playParametersChanged(int)
|
Chris@43
|
588 {
|
Chris@43
|
589 clearRingBuffers();
|
Chris@43
|
590 }
|
Chris@43
|
591
|
Chris@43
|
592 void
|
Chris@687
|
593 AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName)
|
Chris@43
|
594 {
|
Chris@43
|
595 }
|
Chris@43
|
596
|
Chris@43
|
597 void
|
Chris@43
|
598 AudioCallbackPlaySource::audioProcessingOverload()
|
Chris@43
|
599 {
|
Chris@563
|
600 SVCERR << "Audio processing overload!" << endl;
|
Chris@130
|
601
|
Chris@130
|
602 if (!m_playing) return;
|
Chris@130
|
603
|
Chris@739
|
604 if (m_auditioningEffectWrapper &&
|
Chris@739
|
605 !m_auditioningEffectWrapper->isBypassed()) {
|
Chris@739
|
606 m_auditioningEffectWrapper->setBypassed(true);
|
Chris@43
|
607 emit audioOverloadPluginDisabled();
|
Chris@130
|
608 return;
|
Chris@130
|
609 }
|
Chris@43
|
610 }
|
Chris@43
|
611
|
Chris@43
|
612 void
|
Chris@468
|
613 AudioCallbackPlaySource::setSystemPlaybackTarget(breakfastquay::SystemPlaybackTarget *target)
|
Chris@43
|
614 {
|
Chris@636
|
615 if (target == nullptr) {
|
Chris@559
|
616 // reset target-related facts and figures
|
Chris@559
|
617 m_deviceSampleRate = 0;
|
Chris@559
|
618 m_deviceChannelCount = 0;
|
Chris@559
|
619 }
|
Chris@91
|
620 m_target = target;
|
Chris@468
|
621 }
|
Chris@468
|
622
|
Chris@468
|
623 void
|
Chris@468
|
624 AudioCallbackPlaySource::setSystemPlaybackBlockSize(int size)
|
Chris@468
|
625 {
|
Chris@741
|
626 SVDEBUG << "AudioCallbackPlaySource::setTarget: Block size -> " << size << endl;
|
Chris@193
|
627 if (size != 0) {
|
Chris@193
|
628 m_blockSize = size;
|
Chris@193
|
629 }
|
Chris@193
|
630 if (size * 4 > m_ringBufferSize) {
|
Chris@472
|
631 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@741
|
632 SVCERR << "AudioCallbackPlaySource::setTarget: Buffer size "
|
Chris@741
|
633 << size << " > a quarter of ring buffer size "
|
Chris@741
|
634 << m_ringBufferSize << ", calling for more ring buffer"
|
Chris@741
|
635 << endl;
|
Chris@472
|
636 #endif
|
Chris@193
|
637 m_ringBufferSize = size * 4;
|
Chris@193
|
638 if (m_writeBuffers && !m_writeBuffers->empty()) {
|
Chris@193
|
639 clearRingBuffers();
|
Chris@193
|
640 }
|
Chris@193
|
641 }
|
Chris@43
|
642 }
|
Chris@43
|
643
|
Chris@366
|
644 int
|
Chris@43
|
645 AudioCallbackPlaySource::getTargetBlockSize() const
|
Chris@43
|
646 {
|
Chris@293
|
647 // cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << endl;
|
Chris@436
|
648 return int(m_blockSize);
|
Chris@43
|
649 }
|
Chris@43
|
650
|
Chris@43
|
651 void
|
Chris@468
|
652 AudioCallbackPlaySource::setSystemPlaybackLatency(int latency)
|
Chris@43
|
653 {
|
Chris@43
|
654 m_playLatency = latency;
|
Chris@43
|
655 }
|
Chris@43
|
656
|
Chris@434
|
657 sv_frame_t
|
Chris@43
|
658 AudioCallbackPlaySource::getTargetPlayLatency() const
|
Chris@43
|
659 {
|
Chris@43
|
660 return m_playLatency;
|
Chris@43
|
661 }
|
Chris@43
|
662
|
Chris@434
|
663 sv_frame_t
|
Chris@43
|
664 AudioCallbackPlaySource::getCurrentPlayingFrame()
|
Chris@43
|
665 {
|
Chris@91
|
666 // This method attempts to estimate which audio sample frame is
|
Chris@91
|
667 // "currently coming through the speakers".
|
Chris@91
|
668
|
Chris@553
|
669 sv_samplerate_t deviceRate = getDeviceSampleRate();
|
Chris@436
|
670 sv_frame_t latency = m_playLatency; // at target rate
|
Chris@402
|
671 RealTime latency_t = RealTime::zeroTime;
|
Chris@402
|
672
|
Chris@553
|
673 if (deviceRate != 0) {
|
Chris@553
|
674 latency_t = RealTime::frame2RealTime(latency, deviceRate);
|
Chris@402
|
675 }
|
Chris@93
|
676
|
Chris@93
|
677 return getCurrentFrame(latency_t);
|
Chris@93
|
678 }
|
Chris@93
|
679
|
Chris@434
|
680 sv_frame_t
|
Chris@93
|
681 AudioCallbackPlaySource::getCurrentBufferedFrame()
|
Chris@93
|
682 {
|
Chris@93
|
683 return getCurrentFrame(RealTime::zeroTime);
|
Chris@93
|
684 }
|
Chris@93
|
685
|
Chris@434
|
686 sv_frame_t
|
Chris@93
|
687 AudioCallbackPlaySource::getCurrentFrame(RealTime latency_t)
|
Chris@93
|
688 {
|
Chris@553
|
689 // The ring buffers contain data at the source sample rate and all
|
Chris@745
|
690 // processing here happens at this rate. Resampling only happens
|
Chris@745
|
691 // after the audio data leaves this class.
|
Chris@553
|
692
|
Chris@553
|
693 // (But because historically more than one sample rate could have
|
Chris@553
|
694 // been involved here, we do latency calculations using RealTime
|
Chris@553
|
695 // values instead of samples.)
|
Chris@43
|
696
|
Chris@553
|
697 sv_samplerate_t rate = getSourceSampleRate();
|
Chris@91
|
698
|
Chris@553
|
699 if (rate == 0) return 0;
|
Chris@91
|
700
|
Chris@366
|
701 int inbuffer = 0; // at target rate
|
Chris@91
|
702
|
Chris@366
|
703 for (int c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@595
|
704 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@595
|
705 if (rb) {
|
Chris@595
|
706 int here = rb->getReadSpace();
|
Chris@595
|
707 if (c == 0 || here < inbuffer) inbuffer = here;
|
Chris@595
|
708 }
|
Chris@43
|
709 }
|
Chris@43
|
710
|
Chris@436
|
711 sv_frame_t readBufferFill = m_readBufferFill;
|
Chris@436
|
712 sv_frame_t lastRetrievedBlockSize = m_lastRetrievedBlockSize;
|
Chris@91
|
713 double lastRetrievalTimestamp = m_lastRetrievalTimestamp;
|
Chris@91
|
714 double currentTime = 0.0;
|
Chris@91
|
715 if (m_target) currentTime = m_target->getCurrentTime();
|
Chris@91
|
716
|
Chris@102
|
717 bool looping = m_viewManager->getPlayLoopMode();
|
Chris@102
|
718
|
Chris@553
|
719 RealTime inbuffer_t = RealTime::frame2RealTime(inbuffer, rate);
|
Chris@91
|
720
|
Chris@91
|
721 // When the target has just requested a block from us, the last
|
Chris@91
|
722 // sample it obtained was our buffer fill frame count minus the
|
Chris@91
|
723 // amount of read space (converted back to source sample rate)
|
Chris@91
|
724 // remaining now. That sample is not expected to be played until
|
Chris@91
|
725 // the target's play latency has elapsed. By the time the
|
Chris@91
|
726 // following block is requested, that sample will be at the
|
Chris@91
|
727 // target's play latency minus the last requested block size away
|
Chris@91
|
728 // from being played.
|
Chris@91
|
729
|
Chris@91
|
730 RealTime sincerequest_t = RealTime::zeroTime;
|
Chris@91
|
731 RealTime lastretrieved_t = RealTime::zeroTime;
|
Chris@91
|
732
|
Chris@102
|
733 if (m_target &&
|
Chris@102
|
734 m_trustworthyTimestamps &&
|
Chris@102
|
735 lastRetrievalTimestamp != 0.0) {
|
Chris@91
|
736
|
Chris@553
|
737 lastretrieved_t = RealTime::frame2RealTime(lastRetrievedBlockSize, rate);
|
Chris@91
|
738
|
Chris@91
|
739 // calculate number of frames at target rate that have elapsed
|
Chris@91
|
740 // since the end of the last call to getSourceSamples
|
Chris@91
|
741
|
Chris@102
|
742 if (m_trustworthyTimestamps && !looping) {
|
Chris@91
|
743
|
Chris@102
|
744 // this adjustment seems to cause more problems when looping
|
Chris@102
|
745 double elapsed = currentTime - lastRetrievalTimestamp;
|
Chris@102
|
746
|
Chris@102
|
747 if (elapsed > 0.0) {
|
Chris@102
|
748 sincerequest_t = RealTime::fromSeconds(elapsed);
|
Chris@102
|
749 }
|
Chris@91
|
750 }
|
Chris@91
|
751
|
Chris@91
|
752 } else {
|
Chris@91
|
753
|
Chris@553
|
754 lastretrieved_t = RealTime::frame2RealTime(getTargetBlockSize(), rate);
|
Chris@62
|
755 }
|
Chris@91
|
756
|
Chris@553
|
757 RealTime bufferedto_t = RealTime::frame2RealTime(readBufferFill, rate);
|
Chris@91
|
758
|
Chris@91
|
759 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@742
|
760 cout << "\nbuffered to: " << bufferedto_t << ", in buffer: " << inbuffer_t << ", device latency: " << latency_t << "\n since request: " << sincerequest_t << ", last retrieved quantity: " << lastretrieved_t << endl;
|
Chris@91
|
761 #endif
|
Chris@43
|
762
|
Chris@93
|
763 // Normally the range lists should contain at least one item each
|
Chris@93
|
764 // -- if playback is unconstrained, that item should report the
|
Chris@93
|
765 // entire source audio duration.
|
Chris@43
|
766
|
Chris@93
|
767 if (m_rangeStarts.empty()) {
|
Chris@93
|
768 rebuildRangeLists();
|
Chris@93
|
769 }
|
Chris@92
|
770
|
Chris@93
|
771 if (m_rangeStarts.empty()) {
|
Chris@93
|
772 // this code is only used in case of error in rebuildRangeLists
|
Chris@93
|
773 RealTime playing_t = bufferedto_t
|
Chris@738
|
774 - latency_t - lastretrieved_t - inbuffer_t
|
Chris@93
|
775 + sincerequest_t;
|
Chris@193
|
776 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
|
Chris@553
|
777 sv_frame_t frame = RealTime::realTime2Frame(playing_t, rate);
|
Chris@93
|
778 return m_viewManager->alignPlaybackFrameToReference(frame);
|
Chris@93
|
779 }
|
Chris@43
|
780
|
Chris@91
|
781 int inRange = 0;
|
Chris@91
|
782 int index = 0;
|
Chris@91
|
783
|
Chris@366
|
784 for (int i = 0; i < (int)m_rangeStarts.size(); ++i) {
|
Chris@93
|
785 if (bufferedto_t >= m_rangeStarts[i]) {
|
Chris@93
|
786 inRange = index;
|
Chris@93
|
787 } else {
|
Chris@93
|
788 break;
|
Chris@93
|
789 }
|
Chris@93
|
790 ++index;
|
Chris@93
|
791 }
|
Chris@93
|
792
|
Chris@436
|
793 if (inRange >= int(m_rangeStarts.size())) {
|
Chris@436
|
794 inRange = int(m_rangeStarts.size())-1;
|
Chris@436
|
795 }
|
Chris@93
|
796
|
Chris@94
|
797 RealTime playing_t = bufferedto_t;
|
Chris@93
|
798
|
Chris@93
|
799 playing_t = playing_t
|
Chris@738
|
800 - latency_t - lastretrieved_t - inbuffer_t
|
Chris@93
|
801 + sincerequest_t;
|
Chris@94
|
802
|
Chris@94
|
803 // This rather gross little hack is used to ensure that latency
|
Chris@94
|
804 // compensation doesn't result in the playback pointer appearing
|
Chris@94
|
805 // to start earlier than the actual playback does. It doesn't
|
Chris@94
|
806 // work properly (hence the bail-out in the middle) because if we
|
Chris@94
|
807 // are playing a relatively short looped region, the playing time
|
Chris@94
|
808 // estimated from the buffer fill frame may have wrapped around
|
Chris@94
|
809 // the region boundary and end up being much smaller than the
|
Chris@94
|
810 // theoretical play start frame, perhaps even for the entire
|
Chris@94
|
811 // duration of playback!
|
Chris@94
|
812
|
Chris@94
|
813 if (!m_playStartFramePassed) {
|
Chris@553
|
814 RealTime playstart_t = RealTime::frame2RealTime(m_playStartFrame, rate);
|
Chris@94
|
815 if (playing_t < playstart_t) {
|
Chris@563
|
816 // cout << "playing_t " << playing_t << " < playstart_t "
|
Chris@293
|
817 // << playstart_t << endl;
|
Chris@741
|
818 if (m_playStartedAt + latency_t <
|
Chris@94
|
819 RealTime::fromSeconds(currentTime)) {
|
Chris@563
|
820 // cout << "but we've been playing for long enough that I think we should disregard it (it probably results from loop wrapping)" << endl;
|
Chris@94
|
821 m_playStartFramePassed = true;
|
Chris@94
|
822 } else {
|
Chris@94
|
823 playing_t = playstart_t;
|
Chris@94
|
824 }
|
Chris@94
|
825 } else {
|
Chris@94
|
826 m_playStartFramePassed = true;
|
Chris@94
|
827 }
|
Chris@94
|
828 }
|
Chris@163
|
829
|
Chris@163
|
830 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
831 cout << "playing_t " << playing_t;
|
Chris@163
|
832 #endif
|
Chris@94
|
833
|
Chris@94
|
834 playing_t = playing_t - m_rangeStarts[inRange];
|
Chris@93
|
835
|
Chris@93
|
836 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
837 cout << " as offset into range " << inRange << " (start =" << m_rangeStarts[inRange] << " duration =" << m_rangeDurations[inRange] << ") = " << playing_t << endl;
|
Chris@93
|
838 #endif
|
Chris@93
|
839
|
Chris@93
|
840 while (playing_t < RealTime::zeroTime) {
|
Chris@93
|
841
|
Chris@93
|
842 if (inRange == 0) {
|
Chris@93
|
843 if (looping) {
|
Chris@436
|
844 inRange = int(m_rangeStarts.size()) - 1;
|
Chris@93
|
845 } else {
|
Chris@93
|
846 break;
|
Chris@93
|
847 }
|
Chris@93
|
848 } else {
|
Chris@93
|
849 --inRange;
|
Chris@93
|
850 }
|
Chris@93
|
851
|
Chris@93
|
852 playing_t = playing_t + m_rangeDurations[inRange];
|
Chris@93
|
853 }
|
Chris@93
|
854
|
Chris@93
|
855 playing_t = playing_t + m_rangeStarts[inRange];
|
Chris@93
|
856
|
Chris@93
|
857 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
858 cout << " playing time: " << playing_t << endl;
|
Chris@93
|
859 #endif
|
Chris@93
|
860
|
Chris@93
|
861 if (!looping) {
|
Chris@366
|
862 if (inRange == (int)m_rangeStarts.size()-1 &&
|
Chris@93
|
863 playing_t >= m_rangeStarts[inRange] + m_rangeDurations[inRange]) {
|
Chris@563
|
864 cout << "Not looping, inRange " << inRange << " == rangeStarts.size()-1, playing_t " << playing_t << " >= m_rangeStarts[inRange] " << m_rangeStarts[inRange] << " + m_rangeDurations[inRange] " << m_rangeDurations[inRange] << " -- stopping" << endl;
|
Chris@93
|
865 stop();
|
Chris@93
|
866 }
|
Chris@93
|
867 }
|
Chris@93
|
868
|
Chris@93
|
869 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
|
Chris@93
|
870
|
Chris@553
|
871 sv_frame_t frame = RealTime::realTime2Frame(playing_t, rate);
|
Chris@102
|
872
|
Chris@102
|
873 if (m_lastCurrentFrame > 0 && !looping) {
|
Chris@102
|
874 if (frame < m_lastCurrentFrame) {
|
Chris@102
|
875 frame = m_lastCurrentFrame;
|
Chris@102
|
876 }
|
Chris@102
|
877 }
|
Chris@102
|
878
|
Chris@102
|
879 m_lastCurrentFrame = frame;
|
Chris@102
|
880
|
Chris@93
|
881 return m_viewManager->alignPlaybackFrameToReference(frame);
|
Chris@93
|
882 }
|
Chris@93
|
883
|
Chris@93
|
884 void
|
Chris@93
|
885 AudioCallbackPlaySource::rebuildRangeLists()
|
Chris@93
|
886 {
|
Chris@93
|
887 bool constrained = (m_viewManager->getPlaySelectionMode());
|
Chris@93
|
888
|
Chris@93
|
889 m_rangeStarts.clear();
|
Chris@93
|
890 m_rangeDurations.clear();
|
Chris@93
|
891
|
Chris@436
|
892 sv_samplerate_t sourceRate = getSourceSampleRate();
|
Chris@93
|
893 if (sourceRate == 0) return;
|
Chris@93
|
894
|
Chris@93
|
895 RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate);
|
Chris@93
|
896 if (end == RealTime::zeroTime) return;
|
Chris@93
|
897
|
Chris@93
|
898 if (!constrained) {
|
Chris@93
|
899 m_rangeStarts.push_back(RealTime::zeroTime);
|
Chris@93
|
900 m_rangeDurations.push_back(end);
|
Chris@93
|
901 return;
|
Chris@93
|
902 }
|
Chris@93
|
903
|
Chris@93
|
904 MultiSelection::SelectionList selections = m_viewManager->getSelections();
|
Chris@93
|
905 MultiSelection::SelectionList::const_iterator i;
|
Chris@93
|
906
|
Chris@93
|
907 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@233
|
908 SVDEBUG << "AudioCallbackPlaySource::rebuildRangeLists" << endl;
|
Chris@93
|
909 #endif
|
Chris@93
|
910
|
Chris@93
|
911 if (!selections.empty()) {
|
Chris@91
|
912
|
Chris@91
|
913 for (i = selections.begin(); i != selections.end(); ++i) {
|
Chris@91
|
914
|
Chris@91
|
915 RealTime start =
|
Chris@91
|
916 (RealTime::frame2RealTime
|
Chris@91
|
917 (m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
|
Chris@91
|
918 sourceRate));
|
Chris@91
|
919 RealTime duration =
|
Chris@91
|
920 (RealTime::frame2RealTime
|
Chris@91
|
921 (m_viewManager->alignReferenceToPlaybackFrame(i->getEndFrame()) -
|
Chris@91
|
922 m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
|
Chris@91
|
923 sourceRate));
|
Chris@91
|
924
|
Chris@93
|
925 m_rangeStarts.push_back(start);
|
Chris@93
|
926 m_rangeDurations.push_back(duration);
|
Chris@91
|
927 }
|
Chris@93
|
928 } else {
|
Chris@93
|
929 m_rangeStarts.push_back(RealTime::zeroTime);
|
Chris@93
|
930 m_rangeDurations.push_back(end);
|
Chris@43
|
931 }
|
Chris@43
|
932
|
Chris@93
|
933 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
934 cout << "Now have " << m_rangeStarts.size() << " play ranges" << endl;
|
Chris@91
|
935 #endif
|
Chris@43
|
936 }
|
Chris@43
|
937
|
Chris@43
|
938 void
|
Chris@43
|
939 AudioCallbackPlaySource::setOutputLevels(float left, float right)
|
Chris@43
|
940 {
|
Chris@574
|
941 if (left > m_outputLeft) m_outputLeft = left;
|
Chris@574
|
942 if (right > m_outputRight) m_outputRight = right;
|
Chris@580
|
943 m_levelsSet = true;
|
Chris@43
|
944 }
|
Chris@43
|
945
|
Chris@43
|
946 bool
|
Chris@43
|
947 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
|
Chris@43
|
948 {
|
Chris@43
|
949 left = m_outputLeft;
|
Chris@43
|
950 right = m_outputRight;
|
Chris@580
|
951 bool valid = m_levelsSet;
|
Chris@574
|
952 m_outputLeft = 0.f;
|
Chris@574
|
953 m_outputRight = 0.f;
|
Chris@580
|
954 m_levelsSet = false;
|
Chris@580
|
955 return valid;
|
Chris@43
|
956 }
|
Chris@43
|
957
|
Chris@43
|
958 void
|
Chris@468
|
959 AudioCallbackPlaySource::setSystemPlaybackSampleRate(int sr)
|
Chris@43
|
960 {
|
Chris@553
|
961 m_deviceSampleRate = sr;
|
Chris@43
|
962 }
|
Chris@43
|
963
|
Chris@43
|
964 void
|
Chris@559
|
965 AudioCallbackPlaySource::setSystemPlaybackChannelCount(int count)
|
Chris@43
|
966 {
|
Chris@559
|
967 m_deviceChannelCount = count;
|
Chris@43
|
968 }
|
Chris@43
|
969
|
Chris@43
|
970 void
|
Chris@739
|
971 AudioCallbackPlaySource::setAuditioningEffect(std::shared_ptr<Auditionable> a)
|
Chris@43
|
972 {
|
Chris@740
|
973 SVDEBUG << "AudioCallbackPlaySource::setAuditioningEffect(" << a << ")"
|
Chris@740
|
974 << endl;
|
Chris@740
|
975
|
Chris@739
|
976 auto plugin = std::dynamic_pointer_cast<RealTimePluginInstance>(a);
|
Chris@107
|
977 if (a && !plugin) {
|
Chris@563
|
978 SVCERR << "WARNING: AudioCallbackPlaySource::setAuditioningEffect: auditionable object " << a << " is not a real-time plugin instance" << endl;
|
Chris@107
|
979 }
|
Chris@204
|
980
|
Chris@204
|
981 m_mutex.lock();
|
Chris@739
|
982 m_auditioningEffectWrapper->setEffect(plugin);
|
Chris@739
|
983 m_auditioningEffectWrapper->setBypassed(false);
|
Chris@204
|
984 m_mutex.unlock();
|
Chris@737
|
985
|
Chris@737
|
986 SVDEBUG << "AudioCallbackPlaySource::setAuditioningEffect: set plugin to "
|
Chris@739
|
987 << plugin << endl;
|
Chris@43
|
988 }
|
Chris@43
|
989
|
Chris@43
|
990 void
|
Chris@682
|
991 AudioCallbackPlaySource::setSoloModelSet(std::set<ModelId> s)
|
Chris@43
|
992 {
|
Chris@43
|
993 m_audioGenerator->setSoloModelSet(s);
|
Chris@43
|
994 clearRingBuffers();
|
Chris@43
|
995 }
|
Chris@43
|
996
|
Chris@43
|
997 void
|
Chris@43
|
998 AudioCallbackPlaySource::clearSoloModelSet()
|
Chris@43
|
999 {
|
Chris@43
|
1000 m_audioGenerator->clearSoloModelSet();
|
Chris@43
|
1001 clearRingBuffers();
|
Chris@43
|
1002 }
|
Chris@43
|
1003
|
Chris@434
|
1004 sv_samplerate_t
|
Chris@553
|
1005 AudioCallbackPlaySource::getDeviceSampleRate() const
|
Chris@43
|
1006 {
|
Chris@553
|
1007 return m_deviceSampleRate;
|
Chris@43
|
1008 }
|
Chris@43
|
1009
|
Chris@366
|
1010 int
|
Chris@43
|
1011 AudioCallbackPlaySource::getSourceChannelCount() const
|
Chris@43
|
1012 {
|
Chris@43
|
1013 return m_sourceChannelCount;
|
Chris@43
|
1014 }
|
Chris@43
|
1015
|
Chris@366
|
1016 int
|
Chris@43
|
1017 AudioCallbackPlaySource::getTargetChannelCount() const
|
Chris@43
|
1018 {
|
Chris@43
|
1019 if (m_sourceChannelCount < 2) return 2;
|
Chris@43
|
1020 return m_sourceChannelCount;
|
Chris@43
|
1021 }
|
Chris@43
|
1022
|
Chris@559
|
1023 int
|
Chris@559
|
1024 AudioCallbackPlaySource::getDeviceChannelCount() const
|
Chris@559
|
1025 {
|
Chris@559
|
1026 return m_deviceChannelCount;
|
Chris@559
|
1027 }
|
Chris@559
|
1028
|
Chris@434
|
1029 sv_samplerate_t
|
Chris@43
|
1030 AudioCallbackPlaySource::getSourceSampleRate() const
|
Chris@43
|
1031 {
|
Chris@43
|
1032 return m_sourceSampleRate;
|
Chris@43
|
1033 }
|
Chris@43
|
1034
|
Chris@43
|
1035 void
|
Chris@436
|
1036 AudioCallbackPlaySource::setTimeStretch(double factor)
|
Chris@43
|
1037 {
|
Chris@738
|
1038 checkWrappers();
|
Chris@91
|
1039
|
Chris@738
|
1040 m_timeStretchWrapper->setTimeStretchRatio(factor);
|
Chris@738
|
1041
|
Chris@158
|
1042 emit activity(tr("Change time-stretch factor to %1").arg(factor));
|
Chris@43
|
1043 }
|
Chris@43
|
1044
|
Chris@471
|
1045 int
|
Chris@559
|
1046 AudioCallbackPlaySource::getSourceSamples(float *const *buffer,
|
Chris@559
|
1047 int requestedChannels,
|
Chris@559
|
1048 int count)
|
Chris@43
|
1049 {
|
Chris@559
|
1050 // In principle, the target will handle channel mapping in cases
|
Chris@559
|
1051 // where our channel count differs from the device's. But that
|
Chris@559
|
1052 // only holds if our channel count doesn't change -- i.e. if
|
Chris@559
|
1053 // getApplicationChannelCount() always returns the same value as
|
Chris@559
|
1054 // it did when the target was created, and if this function always
|
Chris@559
|
1055 // returns that number of channels.
|
Chris@559
|
1056 //
|
Chris@559
|
1057 // Unfortunately that can't hold for us -- we always have at least
|
Chris@559
|
1058 // 2 channels but if the user opens a new main model with more
|
Chris@559
|
1059 // channels than that (and more than the last main model) then our
|
Chris@559
|
1060 // target channel count necessarily gets increased.
|
Chris@559
|
1061 //
|
Chris@559
|
1062 // We have:
|
Chris@559
|
1063 //
|
Chris@559
|
1064 // getSourceChannelCount() -> number of channels available to
|
Chris@559
|
1065 // provide from real model data
|
Chris@559
|
1066 //
|
Chris@559
|
1067 // getTargetChannelCount() -> number we will actually provide;
|
Chris@559
|
1068 // same as getSourceChannelCount() except that it is always at
|
Chris@559
|
1069 // least 2
|
Chris@559
|
1070 //
|
Chris@559
|
1071 // getDeviceChannelCount() -> number the device will emit, usually
|
Chris@559
|
1072 // equal to the value of getTargetChannelCount() at the time the
|
Chris@559
|
1073 // device was initialised, unless the device could not provide
|
Chris@559
|
1074 // that number
|
Chris@559
|
1075 //
|
Chris@559
|
1076 // requestedChannels -> number the device is expecting from us,
|
Chris@559
|
1077 // always equal to the value of getTargetChannelCount() at the
|
Chris@559
|
1078 // time the device was initialised
|
Chris@559
|
1079 //
|
Chris@559
|
1080 // If the requested channel count is at least the target channel
|
Chris@559
|
1081 // count, then we go ahead and provide the target channels as
|
Chris@559
|
1082 // expected. We just zero any spare channels.
|
Chris@559
|
1083 //
|
Chris@559
|
1084 // If the requested channel count is smaller than the target
|
Chris@559
|
1085 // channel count, then we don't know what to do and we provide
|
Chris@559
|
1086 // nothing. This shouldn't happen as long as management is on the
|
Chris@559
|
1087 // ball -- we emit channelCountIncreased() when the target channel
|
Chris@559
|
1088 // count increases, and whatever code "owns" the driver should
|
Chris@559
|
1089 // have reopened the audio device when it got that signal. But
|
Chris@559
|
1090 // there's a race condition there, which we accommodate with this
|
Chris@559
|
1091 // check.
|
Chris@559
|
1092
|
Chris@559
|
1093 int channels = getTargetChannelCount();
|
Chris@559
|
1094
|
Chris@43
|
1095 if (!m_playing) {
|
Chris@193
|
1096 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1097 cout << "AudioCallbackPlaySource::getSourceSamples: Not playing" << endl;
|
Chris@193
|
1098 #endif
|
Chris@559
|
1099 v_zero_channels(buffer, requestedChannels, count);
|
Chris@595
|
1100 return 0;
|
Chris@43
|
1101 }
|
Chris@559
|
1102 if (requestedChannels < channels) {
|
Chris@559
|
1103 SVDEBUG << "AudioCallbackPlaySource::getSourceSamples: Not enough device channels (" << requestedChannels << ", need " << channels << "); hoping device is about to be reopened" << endl;
|
Chris@559
|
1104 v_zero_channels(buffer, requestedChannels, count);
|
Chris@559
|
1105 return 0;
|
Chris@559
|
1106 }
|
Chris@559
|
1107 if (requestedChannels > channels) {
|
Chris@559
|
1108 v_zero_channels(buffer + channels, requestedChannels - channels, count);
|
Chris@559
|
1109 }
|
Chris@43
|
1110
|
Chris@212
|
1111 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1112 cout << "AudioCallbackPlaySource::getSourceSamples: Playing" << endl;
|
Chris@212
|
1113 #endif
|
Chris@212
|
1114
|
Chris@43
|
1115 // Ensure that all buffers have at least the amount of data we
|
Chris@43
|
1116 // need -- else reduce the size of our requests correspondingly
|
Chris@43
|
1117
|
Chris@559
|
1118 for (int ch = 0; ch < channels; ++ch) {
|
Chris@43
|
1119
|
Chris@43
|
1120 RingBuffer<float> *rb = getReadRingBuffer(ch);
|
Chris@43
|
1121
|
Chris@43
|
1122 if (!rb) {
|
Chris@563
|
1123 SVCERR << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
|
Chris@43
|
1124 << "No ring buffer available for channel " << ch
|
Chris@293
|
1125 << ", returning no data here" << endl;
|
Chris@43
|
1126 count = 0;
|
Chris@43
|
1127 break;
|
Chris@43
|
1128 }
|
Chris@43
|
1129
|
Chris@366
|
1130 int rs = rb->getReadSpace();
|
Chris@43
|
1131 if (rs < count) {
|
Chris@43
|
1132 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1133 cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
|
Chris@43
|
1134 << "Ring buffer for channel " << ch << " has only "
|
Chris@193
|
1135 << rs << " (of " << count << ") samples available ("
|
Chris@193
|
1136 << "ring buffer size is " << rb->getSize() << ", write "
|
Chris@193
|
1137 << "space " << rb->getWriteSpace() << "), "
|
Chris@293
|
1138 << "reducing request size" << endl;
|
Chris@43
|
1139 #endif
|
Chris@43
|
1140 count = rs;
|
Chris@43
|
1141 }
|
Chris@43
|
1142 }
|
Chris@43
|
1143
|
Chris@471
|
1144 if (count == 0) return 0;
|
Chris@43
|
1145
|
Chris@91
|
1146 if (m_target) {
|
Chris@91
|
1147 m_lastRetrievedBlockSize = count;
|
Chris@91
|
1148 m_lastRetrievalTimestamp = m_target->getCurrentTime();
|
Chris@91
|
1149 }
|
Chris@43
|
1150
|
Chris@738
|
1151 int got = 0;
|
Chris@43
|
1152
|
Chris@563
|
1153 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@738
|
1154 cout << "channels == " << channels << endl;
|
Chris@563
|
1155 #endif
|
Chris@555
|
1156
|
Chris@738
|
1157 for (int ch = 0; ch < channels; ++ch) {
|
Chris@43
|
1158
|
Chris@738
|
1159 RingBuffer<float> *rb = getReadRingBuffer(ch);
|
Chris@43
|
1160
|
Chris@738
|
1161 if (rb) {
|
Chris@43
|
1162
|
Chris@738
|
1163 // this is marginally more likely to leave our channels in
|
Chris@738
|
1164 // sync after a processing failure than just passing "count":
|
Chris@738
|
1165 sv_frame_t request = count;
|
Chris@738
|
1166 if (ch > 0) request = got;
|
Chris@43
|
1167
|
Chris@738
|
1168 got = rb->read(buffer[ch], int(request));
|
Chris@595
|
1169
|
Chris@43
|
1170 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@738
|
1171 cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << endl;
|
Chris@43
|
1172 #endif
|
Chris@738
|
1173 }
|
Chris@43
|
1174
|
Chris@738
|
1175 for (int ch = 0; ch < channels; ++ch) {
|
Chris@738
|
1176 for (int i = got; i < count; ++i) {
|
Chris@738
|
1177 buffer[ch][i] = 0.0;
|
Chris@595
|
1178 }
|
Chris@595
|
1179 }
|
Chris@738
|
1180 }
|
Chris@43
|
1181
|
Chris@212
|
1182 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1183 cout << "AudioCallbackPlaySource::getSamples: awakening thread" << endl;
|
Chris@212
|
1184 #endif
|
Chris@212
|
1185
|
Chris@43
|
1186 m_condition.wakeAll();
|
Chris@43
|
1187
|
Chris@738
|
1188 return got;
|
Chris@43
|
1189 }
|
Chris@43
|
1190
|
Chris@43
|
1191 // Called from fill thread, m_playing true, mutex held
|
Chris@43
|
1192 bool
|
Chris@43
|
1193 AudioCallbackPlaySource::fillBuffers()
|
Chris@43
|
1194 {
|
Chris@636
|
1195 static float *tmp = nullptr;
|
Chris@436
|
1196 static sv_frame_t tmpSize = 0;
|
Chris@43
|
1197
|
Chris@434
|
1198 sv_frame_t space = 0;
|
Chris@366
|
1199 for (int c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@595
|
1200 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@595
|
1201 if (wb) {
|
Chris@595
|
1202 sv_frame_t spaceHere = wb->getWriteSpace();
|
Chris@595
|
1203 if (c == 0 || spaceHere < space) space = spaceHere;
|
Chris@595
|
1204 }
|
Chris@43
|
1205 }
|
Chris@43
|
1206
|
Chris@103
|
1207 if (space == 0) {
|
Chris@103
|
1208 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1209 cout << "AudioCallbackPlaySourceFillThread: no space to fill" << endl;
|
Chris@103
|
1210 #endif
|
Chris@103
|
1211 return false;
|
Chris@103
|
1212 }
|
Chris@43
|
1213
|
Chris@544
|
1214 // space is now the number of samples that can be written on each
|
Chris@544
|
1215 // channel's write ringbuffer
|
Chris@544
|
1216
|
Chris@434
|
1217 sv_frame_t f = m_writeBufferFill;
|
Chris@595
|
1218
|
Chris@43
|
1219 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
|
Chris@43
|
1220
|
Chris@43
|
1221 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@193
|
1222 if (!readWriteEqual) {
|
Chris@293
|
1223 cout << "AudioCallbackPlaySourceFillThread: note read buffers != write buffers" << endl;
|
Chris@193
|
1224 }
|
Chris@293
|
1225 cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << endl;
|
Chris@43
|
1226 #endif
|
Chris@43
|
1227
|
Chris@43
|
1228 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1229 cout << "buffered to " << f << " already" << endl;
|
Chris@43
|
1230 #endif
|
Chris@43
|
1231
|
Chris@366
|
1232 int channels = getTargetChannelCount();
|
Chris@43
|
1233
|
Chris@636
|
1234 static float **bufferPtrs = nullptr;
|
Chris@366
|
1235 static int bufferPtrCount = 0;
|
Chris@43
|
1236
|
Chris@43
|
1237 if (bufferPtrCount < channels) {
|
Chris@595
|
1238 if (bufferPtrs) delete[] bufferPtrs;
|
Chris@595
|
1239 bufferPtrs = new float *[channels];
|
Chris@595
|
1240 bufferPtrCount = channels;
|
Chris@43
|
1241 }
|
Chris@43
|
1242
|
Chris@436
|
1243 sv_frame_t generatorBlockSize = m_audioGenerator->getBlockSize();
|
Chris@43
|
1244
|
Chris@546
|
1245 // space must be a multiple of generatorBlockSize
|
Chris@546
|
1246 sv_frame_t reqSpace = space;
|
Chris@546
|
1247 space = (reqSpace / generatorBlockSize) * generatorBlockSize;
|
Chris@546
|
1248 if (space == 0) {
|
Chris@546
|
1249 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@546
|
1250 cout << "requested fill of " << reqSpace
|
Chris@546
|
1251 << " is less than generator block size of "
|
Chris@546
|
1252 << generatorBlockSize << ", leaving it" << endl;
|
Chris@546
|
1253 #endif
|
Chris@546
|
1254 return false;
|
Chris@43
|
1255 }
|
Chris@43
|
1256
|
Chris@546
|
1257 if (tmpSize < channels * space) {
|
Chris@546
|
1258 delete[] tmp;
|
Chris@546
|
1259 tmp = new float[channels * space];
|
Chris@546
|
1260 tmpSize = channels * space;
|
Chris@546
|
1261 }
|
Chris@43
|
1262
|
Chris@546
|
1263 for (int c = 0; c < channels; ++c) {
|
Chris@43
|
1264
|
Chris@546
|
1265 bufferPtrs[c] = tmp + c * space;
|
Chris@595
|
1266
|
Chris@546
|
1267 for (int i = 0; i < space; ++i) {
|
Chris@546
|
1268 tmp[c * space + i] = 0.0f;
|
Chris@546
|
1269 }
|
Chris@546
|
1270 }
|
Chris@43
|
1271
|
Chris@546
|
1272 sv_frame_t got = mixModels(f, space, bufferPtrs); // also modifies f
|
Chris@43
|
1273
|
Chris@546
|
1274 for (int c = 0; c < channels; ++c) {
|
Chris@43
|
1275
|
Chris@546
|
1276 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@546
|
1277 if (wb) {
|
Chris@546
|
1278 int actual = wb->write(bufferPtrs[c], int(got));
|
Chris@546
|
1279 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@546
|
1280 cout << "Wrote " << actual << " samples for ch " << c << ", now "
|
Chris@546
|
1281 << wb->getReadSpace() << " to read"
|
Chris@546
|
1282 << endl;
|
Chris@546
|
1283 #endif
|
Chris@546
|
1284 if (actual < got) {
|
Chris@563
|
1285 SVCERR << "WARNING: Buffer overrun in channel " << c
|
Chris@563
|
1286 << ": wrote " << actual << " of " << got
|
Chris@563
|
1287 << " samples" << endl;
|
Chris@546
|
1288 }
|
Chris@546
|
1289 }
|
Chris@546
|
1290 }
|
Chris@43
|
1291
|
Chris@546
|
1292 m_writeBufferFill = f;
|
Chris@546
|
1293 if (readWriteEqual) m_readBufferFill = f;
|
Chris@43
|
1294
|
Chris@163
|
1295 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
1296 cout << "Read buffer fill is now " << m_readBufferFill << ", write buffer fill "
|
Chris@563
|
1297 << m_writeBufferFill << endl;
|
Chris@163
|
1298 #endif
|
Chris@163
|
1299
|
Chris@546
|
1300 //!!! how do we know when ended? need to mark up a fully-buffered flag and check this if we find the buffers empty in getSourceSamples
|
Chris@43
|
1301
|
Chris@43
|
1302 return true;
|
Chris@43
|
1303 }
|
Chris@43
|
1304
|
Chris@434
|
1305 sv_frame_t
|
Chris@434
|
1306 AudioCallbackPlaySource::mixModels(sv_frame_t &frame, sv_frame_t count, float **buffers)
|
Chris@43
|
1307 {
|
Chris@434
|
1308 sv_frame_t processed = 0;
|
Chris@434
|
1309 sv_frame_t chunkStart = frame;
|
Chris@434
|
1310 sv_frame_t chunkSize = count;
|
Chris@434
|
1311 sv_frame_t selectionSize = 0;
|
Chris@434
|
1312 sv_frame_t nextChunkStart = chunkStart + chunkSize;
|
Chris@43
|
1313
|
Chris@43
|
1314 bool looping = m_viewManager->getPlayLoopMode();
|
Chris@43
|
1315 bool constrained = (m_viewManager->getPlaySelectionMode() &&
|
Chris@595
|
1316 !m_viewManager->getSelections().empty());
|
Chris@43
|
1317
|
Chris@366
|
1318 int channels = getTargetChannelCount();
|
Chris@43
|
1319
|
Chris@43
|
1320 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
1321 cout << "mixModels: start " << frame << ", size " << count << ", channels " << channels << endl;
|
Chris@43
|
1322 #endif
|
Chris@563
|
1323 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1324 if (constrained) {
|
Chris@563
|
1325 cout << "Manager has " << m_viewManager->getSelections().size() << " selection(s):" << endl;
|
Chris@563
|
1326 for (auto sel: m_viewManager->getSelections()) {
|
Chris@563
|
1327 cout << sel.getStartFrame() << " -> " << sel.getEndFrame()
|
Chris@563
|
1328 << " (" << (sel.getEndFrame() - sel.getStartFrame()) << " frames)"
|
Chris@563
|
1329 << endl;
|
Chris@563
|
1330 }
|
Chris@563
|
1331 }
|
Chris@563
|
1332 #endif
|
Chris@563
|
1333
|
Chris@636
|
1334 static float **chunkBufferPtrs = nullptr;
|
Chris@563
|
1335 static int chunkBufferPtrCount = 0;
|
Chris@43
|
1336
|
Chris@43
|
1337 if (chunkBufferPtrCount < channels) {
|
Chris@595
|
1338 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
|
Chris@595
|
1339 chunkBufferPtrs = new float *[channels];
|
Chris@595
|
1340 chunkBufferPtrCount = channels;
|
Chris@43
|
1341 }
|
Chris@43
|
1342
|
Chris@366
|
1343 for (int c = 0; c < channels; ++c) {
|
Chris@595
|
1344 chunkBufferPtrs[c] = buffers[c];
|
Chris@43
|
1345 }
|
Chris@43
|
1346
|
Chris@43
|
1347 while (processed < count) {
|
Chris@595
|
1348
|
Chris@595
|
1349 chunkSize = count - processed;
|
Chris@595
|
1350 nextChunkStart = chunkStart + chunkSize;
|
Chris@595
|
1351 selectionSize = 0;
|
Chris@43
|
1352
|
Chris@595
|
1353 sv_frame_t fadeIn = 0, fadeOut = 0;
|
Chris@43
|
1354
|
Chris@595
|
1355 if (constrained) {
|
Chris@60
|
1356
|
Chris@434
|
1357 sv_frame_t rChunkStart =
|
Chris@60
|
1358 m_viewManager->alignPlaybackFrameToReference(chunkStart);
|
Chris@595
|
1359
|
Chris@595
|
1360 Selection selection =
|
Chris@595
|
1361 m_viewManager->getContainingSelection(rChunkStart, true);
|
Chris@595
|
1362
|
Chris@595
|
1363 if (selection.isEmpty()) {
|
Chris@595
|
1364 if (looping) {
|
Chris@595
|
1365 selection = *m_viewManager->getSelections().begin();
|
Chris@595
|
1366 chunkStart = m_viewManager->alignReferenceToPlaybackFrame
|
Chris@60
|
1367 (selection.getStartFrame());
|
Chris@595
|
1368 fadeIn = 50;
|
Chris@595
|
1369 }
|
Chris@595
|
1370 }
|
Chris@43
|
1371
|
Chris@595
|
1372 if (selection.isEmpty()) {
|
Chris@43
|
1373
|
Chris@595
|
1374 chunkSize = 0;
|
Chris@595
|
1375 nextChunkStart = chunkStart;
|
Chris@43
|
1376
|
Chris@595
|
1377 } else {
|
Chris@43
|
1378
|
Chris@434
|
1379 sv_frame_t sf = m_viewManager->alignReferenceToPlaybackFrame
|
Chris@60
|
1380 (selection.getStartFrame());
|
Chris@434
|
1381 sv_frame_t ef = m_viewManager->alignReferenceToPlaybackFrame
|
Chris@60
|
1382 (selection.getEndFrame());
|
Chris@43
|
1383
|
Chris@595
|
1384 selectionSize = ef - sf;
|
Chris@60
|
1385
|
Chris@595
|
1386 if (chunkStart < sf) {
|
Chris@595
|
1387 chunkStart = sf;
|
Chris@595
|
1388 fadeIn = 50;
|
Chris@595
|
1389 }
|
Chris@43
|
1390
|
Chris@595
|
1391 nextChunkStart = chunkStart + chunkSize;
|
Chris@43
|
1392
|
Chris@595
|
1393 if (nextChunkStart >= ef) {
|
Chris@595
|
1394 nextChunkStart = ef;
|
Chris@595
|
1395 fadeOut = 50;
|
Chris@595
|
1396 }
|
Chris@43
|
1397
|
Chris@595
|
1398 chunkSize = nextChunkStart - chunkStart;
|
Chris@595
|
1399 }
|
Chris@595
|
1400
|
Chris@595
|
1401 } else if (looping && m_lastModelEndFrame > 0) {
|
Chris@43
|
1402
|
Chris@595
|
1403 if (chunkStart >= m_lastModelEndFrame) {
|
Chris@595
|
1404 chunkStart = 0;
|
Chris@595
|
1405 }
|
Chris@595
|
1406 if (chunkSize > m_lastModelEndFrame - chunkStart) {
|
Chris@595
|
1407 chunkSize = m_lastModelEndFrame - chunkStart;
|
Chris@595
|
1408 }
|
Chris@595
|
1409 nextChunkStart = chunkStart + chunkSize;
|
Chris@595
|
1410 }
|
Chris@43
|
1411
|
Chris@563
|
1412 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@595
|
1413 cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << endl;
|
Chris@563
|
1414 #endif
|
Chris@563
|
1415
|
Chris@595
|
1416 if (!chunkSize) {
|
Chris@595
|
1417 // We need to maintain full buffers so that the other
|
Chris@595
|
1418 // thread can tell where it's got to in the playback -- so
|
Chris@595
|
1419 // return the full amount here
|
Chris@595
|
1420 frame = frame + count;
|
Chris@562
|
1421 if (frame < nextChunkStart) {
|
Chris@562
|
1422 frame = nextChunkStart;
|
Chris@562
|
1423 }
|
Chris@562
|
1424 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
1425 cout << "mixModels: ending at " << nextChunkStart << ", returning frame as "
|
Chris@562
|
1426 << frame << endl;
|
Chris@562
|
1427 #endif
|
Chris@595
|
1428 return count;
|
Chris@595
|
1429 }
|
Chris@43
|
1430
|
Chris@43
|
1431 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
1432 cout << "mixModels: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << endl;
|
Chris@43
|
1433 #endif
|
Chris@43
|
1434
|
Chris@595
|
1435 if (selectionSize < 100) {
|
Chris@595
|
1436 fadeIn = 0;
|
Chris@595
|
1437 fadeOut = 0;
|
Chris@595
|
1438 } else if (selectionSize < 300) {
|
Chris@595
|
1439 if (fadeIn > 0) fadeIn = 10;
|
Chris@595
|
1440 if (fadeOut > 0) fadeOut = 10;
|
Chris@595
|
1441 }
|
Chris@43
|
1442
|
Chris@595
|
1443 if (fadeIn > 0) {
|
Chris@595
|
1444 if (processed * 2 < fadeIn) {
|
Chris@595
|
1445 fadeIn = processed * 2;
|
Chris@595
|
1446 }
|
Chris@595
|
1447 }
|
Chris@43
|
1448
|
Chris@595
|
1449 if (fadeOut > 0) {
|
Chris@595
|
1450 if ((count - processed - chunkSize) * 2 < fadeOut) {
|
Chris@595
|
1451 fadeOut = (count - processed - chunkSize) * 2;
|
Chris@595
|
1452 }
|
Chris@595
|
1453 }
|
Chris@43
|
1454
|
Chris@682
|
1455 for (std::set<ModelId>::iterator mi = m_models.begin();
|
Chris@595
|
1456 mi != m_models.end(); ++mi) {
|
Chris@595
|
1457
|
Chris@595
|
1458 (void) m_audioGenerator->mixModel(*mi, chunkStart,
|
Chris@366
|
1459 chunkSize, chunkBufferPtrs,
|
Chris@366
|
1460 fadeIn, fadeOut);
|
Chris@595
|
1461 }
|
Chris@43
|
1462
|
Chris@595
|
1463 for (int c = 0; c < channels; ++c) {
|
Chris@595
|
1464 chunkBufferPtrs[c] += chunkSize;
|
Chris@595
|
1465 }
|
Chris@43
|
1466
|
Chris@595
|
1467 processed += chunkSize;
|
Chris@595
|
1468 chunkStart = nextChunkStart;
|
Chris@43
|
1469 }
|
Chris@43
|
1470
|
Chris@43
|
1471 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@563
|
1472 cout << "mixModels returning " << processed << " frames to " << nextChunkStart << endl;
|
Chris@43
|
1473 #endif
|
Chris@43
|
1474
|
Chris@43
|
1475 frame = nextChunkStart;
|
Chris@43
|
1476 return processed;
|
Chris@43
|
1477 }
|
Chris@43
|
1478
|
Chris@43
|
1479 void
|
Chris@43
|
1480 AudioCallbackPlaySource::unifyRingBuffers()
|
Chris@43
|
1481 {
|
Chris@43
|
1482 if (m_readBuffers == m_writeBuffers) return;
|
Chris@43
|
1483
|
Chris@43
|
1484 // only unify if there will be something to read
|
Chris@366
|
1485 for (int c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@595
|
1486 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@595
|
1487 if (wb) {
|
Chris@595
|
1488 if (wb->getReadSpace() < m_blockSize * 2) {
|
Chris@595
|
1489 if ((m_writeBufferFill + m_blockSize * 2) <
|
Chris@595
|
1490 m_lastModelEndFrame) {
|
Chris@595
|
1491 // OK, we don't have enough and there's more to
|
Chris@595
|
1492 // read -- don't unify until we can do better
|
Chris@193
|
1493 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1494 cout << "AudioCallbackPlaySource::unifyRingBuffers: Not unifying: write buffer has less (" << wb->getReadSpace() << ") than " << m_blockSize*2 << " to read and write buffer fill (" << m_writeBufferFill << ") is not close to end frame (" << m_lastModelEndFrame << ")" << endl;
|
Chris@193
|
1495 #endif
|
Chris@595
|
1496 return;
|
Chris@595
|
1497 }
|
Chris@595
|
1498 }
|
Chris@595
|
1499 break;
|
Chris@595
|
1500 }
|
Chris@43
|
1501 }
|
Chris@43
|
1502
|
Chris@436
|
1503 sv_frame_t rf = m_readBufferFill;
|
Chris@43
|
1504 RingBuffer<float> *rb = getReadRingBuffer(0);
|
Chris@43
|
1505 if (rb) {
|
Chris@595
|
1506 int rs = rb->getReadSpace();
|
Chris@595
|
1507 //!!! incorrect when in non-contiguous selection, see comments elsewhere
|
Chris@595
|
1508 // cout << "rs = " << rs << endl;
|
Chris@595
|
1509 if (rs < rf) rf -= rs;
|
Chris@595
|
1510 else rf = 0;
|
Chris@43
|
1511 }
|
Chris@43
|
1512
|
Chris@193
|
1513 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1514 cout << "AudioCallbackPlaySource::unifyRingBuffers: m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << endl;
|
Chris@193
|
1515 #endif
|
Chris@43
|
1516
|
Chris@436
|
1517 sv_frame_t wf = m_writeBufferFill;
|
Chris@436
|
1518 sv_frame_t skip = 0;
|
Chris@366
|
1519 for (int c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@595
|
1520 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@595
|
1521 if (wb) {
|
Chris@595
|
1522 if (c == 0) {
|
Chris@595
|
1523
|
Chris@595
|
1524 int wrs = wb->getReadSpace();
|
Chris@595
|
1525 // cout << "wrs = " << wrs << endl;
|
Chris@43
|
1526
|
Chris@595
|
1527 if (wrs < wf) wf -= wrs;
|
Chris@595
|
1528 else wf = 0;
|
Chris@595
|
1529 // cout << "wf = " << wf << endl;
|
Chris@595
|
1530
|
Chris@595
|
1531 if (wf < rf) skip = rf - wf;
|
Chris@595
|
1532 if (skip == 0) break;
|
Chris@595
|
1533 }
|
Chris@43
|
1534
|
Chris@595
|
1535 // cout << "skipping " << skip << endl;
|
Chris@595
|
1536 wb->skip(int(skip));
|
Chris@595
|
1537 }
|
Chris@43
|
1538 }
|
Chris@595
|
1539
|
Chris@43
|
1540 m_bufferScavenger.claim(m_readBuffers);
|
Chris@43
|
1541 m_readBuffers = m_writeBuffers;
|
Chris@43
|
1542 m_readBufferFill = m_writeBufferFill;
|
Chris@193
|
1543 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@563
|
1544 cout << "unified" << endl;
|
Chris@193
|
1545 #endif
|
Chris@43
|
1546 }
|
Chris@43
|
1547
|
Chris@43
|
1548 void
|
Chris@43
|
1549 AudioCallbackPlaySource::FillThread::run()
|
Chris@43
|
1550 {
|
Chris@43
|
1551 AudioCallbackPlaySource &s(m_source);
|
Chris@43
|
1552
|
Chris@43
|
1553 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1554 cout << "AudioCallbackPlaySourceFillThread starting" << endl;
|
Chris@43
|
1555 #endif
|
Chris@43
|
1556
|
Chris@43
|
1557 s.m_mutex.lock();
|
Chris@43
|
1558
|
Chris@43
|
1559 bool previouslyPlaying = s.m_playing;
|
Chris@43
|
1560 bool work = false;
|
Chris@43
|
1561
|
Chris@43
|
1562 while (!s.m_exiting) {
|
Chris@43
|
1563
|
Chris@595
|
1564 s.unifyRingBuffers();
|
Chris@595
|
1565 s.m_bufferScavenger.scavenge();
|
Chris@43
|
1566 s.m_pluginScavenger.scavenge();
|
Chris@43
|
1567
|
Chris@595
|
1568 if (work && s.m_playing && s.getSourceSampleRate()) {
|
Chris@595
|
1569
|
Chris@43
|
1570 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
1571 cout << "AudioCallbackPlaySourceFillThread: not waiting" << endl;
|
Chris@43
|
1572 #endif
|
Chris@43
|
1573
|
Chris@595
|
1574 s.m_mutex.unlock();
|
Chris@595
|
1575 s.m_mutex.lock();
|
Chris@43
|
1576
|
Chris@595
|
1577 } else {
|
Chris@595
|
1578
|
Chris@595
|
1579 double ms = 100;
|
Chris@595
|
1580 if (s.getSourceSampleRate() > 0) {
|
Chris@595
|
1581 ms = double(s.m_ringBufferSize) / s.getSourceSampleRate() * 1000.0;
|
Chris@595
|
1582 }
|
Chris@595
|
1583
|
Chris@595
|
1584 if (s.m_playing) ms /= 10;
|
Chris@43
|
1585
|
Chris@43
|
1586 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1587 if (!s.m_playing) cout << endl;
|
Chris@595
|
1588 cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << endl;
|
Chris@43
|
1589 #endif
|
Chris@595
|
1590
|
Chris@595
|
1591 s.m_condition.wait(&s.m_mutex, int(ms));
|
Chris@595
|
1592 }
|
Chris@43
|
1593
|
Chris@43
|
1594 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
1595 cout << "AudioCallbackPlaySourceFillThread: awoken" << endl;
|
Chris@43
|
1596 #endif
|
Chris@43
|
1597
|
Chris@595
|
1598 work = false;
|
Chris@43
|
1599
|
Chris@595
|
1600 if (!s.getSourceSampleRate()) {
|
Chris@103
|
1601 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@293
|
1602 cout << "AudioCallbackPlaySourceFillThread: source sample rate is zero" << endl;
|
Chris@103
|
1603 #endif
|
Chris@103
|
1604 continue;
|
Chris@103
|
1605 }
|
Chris@43
|
1606
|
Chris@595
|
1607 bool playing = s.m_playing;
|
Chris@43
|
1608
|
Chris@595
|
1609 if (playing && !previouslyPlaying) {
|
Chris@43
|
1610 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@595
|
1611 cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << endl;
|
Chris@43
|
1612 #endif
|
Chris@595
|
1613 for (int c = 0; c < s.getTargetChannelCount(); ++c) {
|
Chris@595
|
1614 RingBuffer<float> *rb = s.getReadRingBuffer(c);
|
Chris@595
|
1615 if (rb) rb->reset();
|
Chris@595
|
1616 }
|
Chris@595
|
1617 }
|
Chris@595
|
1618 previouslyPlaying = playing;
|
Chris@43
|
1619
|
Chris@595
|
1620 work = s.fillBuffers();
|
Chris@43
|
1621 }
|
Chris@43
|
1622
|
Chris@43
|
1623 s.m_mutex.unlock();
|
Chris@43
|
1624 }
|
Chris@43
|
1625
|