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