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