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