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