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