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