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