Chris@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 Sonic Visualiser
|
Chris@0
|
5 An audio file viewer and annotation editor.
|
Chris@0
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@0
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@0
|
9 This program is free software; you can redistribute it and/or
|
Chris@0
|
10 modify it under the terms of the GNU General Public License as
|
Chris@0
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@0
|
12 License, or (at your option) any later version. See the file
|
Chris@0
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "AudioCallbackPlaySource.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include "AudioGenerator.h"
|
Chris@0
|
19
|
Chris@1
|
20 #include "data/model/Model.h"
|
Chris@1
|
21 #include "view/ViewManager.h"
|
Chris@0
|
22 #include "base/PlayParameterRepository.h"
|
Chris@32
|
23 #include "base/Preferences.h"
|
Chris@1
|
24 #include "data/model/DenseTimeValueModel.h"
|
Chris@1
|
25 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@41
|
26 #include "plugin/RealTimePluginInstance.h"
|
Chris@14
|
27 #include "PhaseVocoderTimeStretcher.h"
|
Chris@0
|
28
|
Chris@0
|
29 #include <iostream>
|
Chris@0
|
30 #include <cassert>
|
Chris@0
|
31
|
Chris@0
|
32 //#define DEBUG_AUDIO_PLAY_SOURCE 1
|
Chris@14
|
33 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
|
Chris@0
|
34
|
Chris@0
|
35 //const size_t AudioCallbackPlaySource::m_ringBufferSize = 102400;
|
Chris@0
|
36 const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071;
|
Chris@0
|
37
|
Chris@0
|
38 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) :
|
Chris@0
|
39 m_viewManager(manager),
|
Chris@0
|
40 m_audioGenerator(new AudioGenerator()),
|
Chris@0
|
41 m_readBuffers(0),
|
Chris@0
|
42 m_writeBuffers(0),
|
Chris@0
|
43 m_readBufferFill(0),
|
Chris@0
|
44 m_writeBufferFill(0),
|
Chris@0
|
45 m_bufferScavenger(1),
|
Chris@0
|
46 m_sourceChannelCount(0),
|
Chris@0
|
47 m_blockSize(1024),
|
Chris@0
|
48 m_sourceSampleRate(0),
|
Chris@0
|
49 m_targetSampleRate(0),
|
Chris@0
|
50 m_playLatency(0),
|
Chris@0
|
51 m_playing(false),
|
Chris@0
|
52 m_exiting(false),
|
Chris@0
|
53 m_lastModelEndFrame(0),
|
Chris@0
|
54 m_outputLeft(0.0),
|
Chris@0
|
55 m_outputRight(0.0),
|
Chris@41
|
56 m_auditioningPlugin(0),
|
Chris@0
|
57 m_timeStretcher(0),
|
Chris@0
|
58 m_fillThread(0),
|
Chris@32
|
59 m_converter(0),
|
Chris@32
|
60 m_crapConverter(0),
|
Chris@32
|
61 m_resampleQuality(Preferences::getInstance()->getResampleQuality())
|
Chris@0
|
62 {
|
Chris@0
|
63 m_viewManager->setAudioPlaySource(this);
|
Chris@0
|
64
|
Chris@0
|
65 connect(m_viewManager, SIGNAL(selectionChanged()),
|
Chris@0
|
66 this, SLOT(selectionChanged()));
|
Chris@0
|
67 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
|
Chris@0
|
68 this, SLOT(playLoopModeChanged()));
|
Chris@0
|
69 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
|
Chris@0
|
70 this, SLOT(playSelectionModeChanged()));
|
Chris@0
|
71
|
Chris@0
|
72 connect(PlayParameterRepository::getInstance(),
|
Chris@0
|
73 SIGNAL(playParametersChanged(PlayParameters *)),
|
Chris@0
|
74 this, SLOT(playParametersChanged(PlayParameters *)));
|
Chris@32
|
75
|
Chris@32
|
76 connect(Preferences::getInstance(),
|
Chris@32
|
77 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
|
Chris@32
|
78 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 AudioCallbackPlaySource::~AudioCallbackPlaySource()
|
Chris@0
|
82 {
|
Chris@0
|
83 m_exiting = true;
|
Chris@0
|
84
|
Chris@0
|
85 if (m_fillThread) {
|
Chris@0
|
86 m_condition.wakeAll();
|
Chris@0
|
87 m_fillThread->wait();
|
Chris@0
|
88 delete m_fillThread;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 clearModels();
|
Chris@0
|
92
|
Chris@0
|
93 if (m_readBuffers != m_writeBuffers) {
|
Chris@0
|
94 delete m_readBuffers;
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 delete m_writeBuffers;
|
Chris@0
|
98
|
Chris@0
|
99 delete m_audioGenerator;
|
Chris@0
|
100
|
Chris@0
|
101 m_bufferScavenger.scavenge(true);
|
Chris@41
|
102 m_pluginScavenger.scavenge(true);
|
Chris@41
|
103 m_timeStretcherScavenger.scavenge(true);
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 void
|
Chris@0
|
107 AudioCallbackPlaySource::addModel(Model *model)
|
Chris@0
|
108 {
|
Chris@0
|
109 if (m_models.find(model) != m_models.end()) return;
|
Chris@0
|
110
|
Chris@0
|
111 bool canPlay = m_audioGenerator->addModel(model);
|
Chris@0
|
112
|
Chris@0
|
113 m_mutex.lock();
|
Chris@0
|
114
|
Chris@0
|
115 m_models.insert(model);
|
Chris@0
|
116 if (model->getEndFrame() > m_lastModelEndFrame) {
|
Chris@0
|
117 m_lastModelEndFrame = model->getEndFrame();
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 bool buffersChanged = false, srChanged = false;
|
Chris@0
|
121
|
Chris@0
|
122 size_t modelChannels = 1;
|
Chris@0
|
123 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
124 if (dtvm) modelChannels = dtvm->getChannelCount();
|
Chris@0
|
125 if (modelChannels > m_sourceChannelCount) {
|
Chris@0
|
126 m_sourceChannelCount = modelChannels;
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 // std::cerr << "Adding model with " << modelChannels << " channels " << std::endl;
|
Chris@0
|
130
|
Chris@0
|
131 if (m_sourceSampleRate == 0) {
|
Chris@0
|
132
|
Chris@0
|
133 m_sourceSampleRate = model->getSampleRate();
|
Chris@0
|
134 srChanged = true;
|
Chris@0
|
135
|
Chris@0
|
136 } else if (model->getSampleRate() != m_sourceSampleRate) {
|
Chris@0
|
137
|
Chris@0
|
138 // If this is a dense time-value model and we have no other, we
|
Chris@0
|
139 // can just switch to this model's sample rate
|
Chris@0
|
140
|
Chris@0
|
141 if (dtvm) {
|
Chris@0
|
142
|
Chris@0
|
143 bool conflicting = false;
|
Chris@0
|
144
|
Chris@0
|
145 for (std::set<Model *>::const_iterator i = m_models.begin();
|
Chris@0
|
146 i != m_models.end(); ++i) {
|
Chris@0
|
147 if (*i != dtvm && dynamic_cast<DenseTimeValueModel *>(*i)) {
|
Chris@0
|
148 std::cerr << "AudioCallbackPlaySource::addModel: Conflicting dense time-value model " << *i << " found" << std::endl;
|
Chris@0
|
149 conflicting = true;
|
Chris@0
|
150 break;
|
Chris@0
|
151 }
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 if (conflicting) {
|
Chris@0
|
155
|
Chris@0
|
156 std::cerr << "AudioCallbackPlaySource::addModel: ERROR: "
|
Chris@0
|
157 << "New model sample rate does not match" << std::endl
|
Chris@0
|
158 << "existing model(s) (new " << model->getSampleRate()
|
Chris@0
|
159 << " vs " << m_sourceSampleRate
|
Chris@0
|
160 << "), playback will be wrong"
|
Chris@0
|
161 << std::endl;
|
Chris@0
|
162
|
Chris@0
|
163 emit sampleRateMismatch(model->getSampleRate(), m_sourceSampleRate,
|
Chris@0
|
164 false);
|
Chris@0
|
165 } else {
|
Chris@0
|
166 m_sourceSampleRate = model->getSampleRate();
|
Chris@0
|
167 srChanged = true;
|
Chris@0
|
168 }
|
Chris@0
|
169 }
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) {
|
Chris@0
|
173 clearRingBuffers(true, getTargetChannelCount());
|
Chris@0
|
174 buffersChanged = true;
|
Chris@0
|
175 } else {
|
Chris@0
|
176 if (canPlay) clearRingBuffers(true);
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 if (buffersChanged || srChanged) {
|
Chris@0
|
180 if (m_converter) {
|
Chris@0
|
181 src_delete(m_converter);
|
Chris@32
|
182 src_delete(m_crapConverter);
|
Chris@0
|
183 m_converter = 0;
|
Chris@32
|
184 m_crapConverter = 0;
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 m_mutex.unlock();
|
Chris@0
|
189
|
Chris@0
|
190 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
|
Chris@0
|
191
|
Chris@0
|
192 if (!m_fillThread) {
|
Chris@0
|
193 m_fillThread = new AudioCallbackPlaySourceFillThread(*this);
|
Chris@0
|
194 m_fillThread->start();
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
198 std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl;
|
Chris@0
|
199 #endif
|
Chris@0
|
200
|
Chris@0
|
201 if (buffersChanged || srChanged) {
|
Chris@0
|
202 emit modelReplaced();
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 m_condition.wakeAll();
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 void
|
Chris@0
|
209 AudioCallbackPlaySource::removeModel(Model *model)
|
Chris@0
|
210 {
|
Chris@0
|
211 m_mutex.lock();
|
Chris@0
|
212
|
Chris@0
|
213 m_models.erase(model);
|
Chris@0
|
214
|
Chris@0
|
215 if (m_models.empty()) {
|
Chris@0
|
216 if (m_converter) {
|
Chris@0
|
217 src_delete(m_converter);
|
Chris@32
|
218 src_delete(m_crapConverter);
|
Chris@0
|
219 m_converter = 0;
|
Chris@32
|
220 m_crapConverter = 0;
|
Chris@0
|
221 }
|
Chris@0
|
222 m_sourceSampleRate = 0;
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 size_t lastEnd = 0;
|
Chris@0
|
226 for (std::set<Model *>::const_iterator i = m_models.begin();
|
Chris@0
|
227 i != m_models.end(); ++i) {
|
Chris@0
|
228 // std::cerr << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl;
|
Chris@0
|
229 if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame();
|
Chris@0
|
230 // std::cerr << "(done, lastEnd now " << lastEnd << ")" << std::endl;
|
Chris@0
|
231 }
|
Chris@0
|
232 m_lastModelEndFrame = lastEnd;
|
Chris@0
|
233
|
Chris@0
|
234 m_mutex.unlock();
|
Chris@0
|
235
|
Chris@0
|
236 m_audioGenerator->removeModel(model);
|
Chris@0
|
237
|
Chris@0
|
238 clearRingBuffers();
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 void
|
Chris@0
|
242 AudioCallbackPlaySource::clearModels()
|
Chris@0
|
243 {
|
Chris@0
|
244 m_mutex.lock();
|
Chris@0
|
245
|
Chris@0
|
246 m_models.clear();
|
Chris@0
|
247
|
Chris@0
|
248 if (m_converter) {
|
Chris@0
|
249 src_delete(m_converter);
|
Chris@32
|
250 src_delete(m_crapConverter);
|
Chris@0
|
251 m_converter = 0;
|
Chris@32
|
252 m_crapConverter = 0;
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 m_lastModelEndFrame = 0;
|
Chris@0
|
256
|
Chris@0
|
257 m_sourceSampleRate = 0;
|
Chris@0
|
258
|
Chris@0
|
259 m_mutex.unlock();
|
Chris@0
|
260
|
Chris@0
|
261 m_audioGenerator->clearModels();
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 void
|
Chris@0
|
265 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count)
|
Chris@0
|
266 {
|
Chris@0
|
267 if (!haveLock) m_mutex.lock();
|
Chris@0
|
268
|
Chris@0
|
269 if (count == 0) {
|
Chris@0
|
270 if (m_writeBuffers) count = m_writeBuffers->size();
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 size_t sf = m_readBufferFill;
|
Chris@0
|
274 RingBuffer<float> *rb = getReadRingBuffer(0);
|
Chris@0
|
275 if (rb) {
|
Chris@0
|
276 //!!! This is incorrect if we're in a non-contiguous selection
|
Chris@0
|
277 //Same goes for all related code (subtracting the read space
|
Chris@0
|
278 //from the fill frame to try to establish where the effective
|
Chris@0
|
279 //pre-resample/timestretch read pointer is)
|
Chris@0
|
280 size_t rs = rb->getReadSpace();
|
Chris@0
|
281 if (rs < sf) sf -= rs;
|
Chris@0
|
282 else sf = 0;
|
Chris@0
|
283 }
|
Chris@0
|
284 m_writeBufferFill = sf;
|
Chris@0
|
285
|
Chris@0
|
286 if (m_readBuffers != m_writeBuffers) {
|
Chris@0
|
287 delete m_writeBuffers;
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 m_writeBuffers = new RingBufferVector;
|
Chris@0
|
291
|
Chris@0
|
292 for (size_t i = 0; i < count; ++i) {
|
Chris@0
|
293 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 // std::cerr << "AudioCallbackPlaySource::clearRingBuffers: Created "
|
Chris@0
|
297 // << count << " write buffers" << std::endl;
|
Chris@0
|
298
|
Chris@0
|
299 if (!haveLock) {
|
Chris@0
|
300 m_mutex.unlock();
|
Chris@0
|
301 }
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 void
|
Chris@0
|
305 AudioCallbackPlaySource::play(size_t startFrame)
|
Chris@0
|
306 {
|
Chris@0
|
307 if (m_viewManager->getPlaySelectionMode() &&
|
Chris@0
|
308 !m_viewManager->getSelections().empty()) {
|
Chris@0
|
309 MultiSelection::SelectionList selections = m_viewManager->getSelections();
|
Chris@0
|
310 MultiSelection::SelectionList::iterator i = selections.begin();
|
Chris@0
|
311 if (i != selections.end()) {
|
Chris@0
|
312 if (startFrame < i->getStartFrame()) {
|
Chris@0
|
313 startFrame = i->getStartFrame();
|
Chris@0
|
314 } else {
|
Chris@0
|
315 MultiSelection::SelectionList::iterator j = selections.end();
|
Chris@0
|
316 --j;
|
Chris@0
|
317 if (startFrame >= j->getEndFrame()) {
|
Chris@0
|
318 startFrame = i->getStartFrame();
|
Chris@0
|
319 }
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322 } else {
|
Chris@0
|
323 if (startFrame >= m_lastModelEndFrame) {
|
Chris@0
|
324 startFrame = 0;
|
Chris@0
|
325 }
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 // The fill thread will automatically empty its buffers before
|
Chris@0
|
329 // starting again if we have not so far been playing, but not if
|
Chris@0
|
330 // we're just re-seeking.
|
Chris@0
|
331
|
Chris@0
|
332 m_mutex.lock();
|
Chris@0
|
333 if (m_playing) {
|
Chris@0
|
334 m_readBufferFill = m_writeBufferFill = startFrame;
|
Chris@0
|
335 if (m_readBuffers) {
|
Chris@0
|
336 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@0
|
337 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@0
|
338 if (rb) rb->reset();
|
Chris@0
|
339 }
|
Chris@0
|
340 }
|
Chris@0
|
341 if (m_converter) src_reset(m_converter);
|
Chris@32
|
342 if (m_crapConverter) src_reset(m_crapConverter);
|
Chris@0
|
343 } else {
|
Chris@0
|
344 if (m_converter) src_reset(m_converter);
|
Chris@32
|
345 if (m_crapConverter) src_reset(m_crapConverter);
|
Chris@0
|
346 m_readBufferFill = m_writeBufferFill = startFrame;
|
Chris@0
|
347 }
|
Chris@0
|
348 m_mutex.unlock();
|
Chris@0
|
349
|
Chris@0
|
350 m_audioGenerator->reset();
|
Chris@0
|
351
|
Chris@0
|
352 bool changed = !m_playing;
|
Chris@0
|
353 m_playing = true;
|
Chris@0
|
354 m_condition.wakeAll();
|
Chris@0
|
355 if (changed) emit playStatusChanged(m_playing);
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358 void
|
Chris@0
|
359 AudioCallbackPlaySource::stop()
|
Chris@0
|
360 {
|
Chris@0
|
361 bool changed = m_playing;
|
Chris@0
|
362 m_playing = false;
|
Chris@0
|
363 m_condition.wakeAll();
|
Chris@0
|
364 if (changed) emit playStatusChanged(m_playing);
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 void
|
Chris@0
|
368 AudioCallbackPlaySource::selectionChanged()
|
Chris@0
|
369 {
|
Chris@0
|
370 if (m_viewManager->getPlaySelectionMode()) {
|
Chris@0
|
371 clearRingBuffers();
|
Chris@0
|
372 }
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 void
|
Chris@0
|
376 AudioCallbackPlaySource::playLoopModeChanged()
|
Chris@0
|
377 {
|
Chris@0
|
378 clearRingBuffers();
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 void
|
Chris@0
|
382 AudioCallbackPlaySource::playSelectionModeChanged()
|
Chris@0
|
383 {
|
Chris@0
|
384 if (!m_viewManager->getSelections().empty()) {
|
Chris@0
|
385 clearRingBuffers();
|
Chris@0
|
386 }
|
Chris@0
|
387 }
|
Chris@0
|
388
|
Chris@0
|
389 void
|
Chris@0
|
390 AudioCallbackPlaySource::playParametersChanged(PlayParameters *params)
|
Chris@0
|
391 {
|
Chris@0
|
392 clearRingBuffers();
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 void
|
Chris@32
|
396 AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n)
|
Chris@32
|
397 {
|
Chris@32
|
398 if (n == "Resample Quality") {
|
Chris@32
|
399 setResampleQuality(Preferences::getInstance()->getResampleQuality());
|
Chris@32
|
400 }
|
Chris@32
|
401 }
|
Chris@32
|
402
|
Chris@32
|
403 void
|
Chris@0
|
404 AudioCallbackPlaySource::setTargetBlockSize(size_t size)
|
Chris@0
|
405 {
|
Chris@0
|
406 // std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl;
|
Chris@0
|
407 assert(size < m_ringBufferSize);
|
Chris@0
|
408 m_blockSize = size;
|
Chris@0
|
409 }
|
Chris@0
|
410
|
Chris@0
|
411 size_t
|
Chris@0
|
412 AudioCallbackPlaySource::getTargetBlockSize() const
|
Chris@0
|
413 {
|
Chris@0
|
414 // std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
|
Chris@0
|
415 return m_blockSize;
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 void
|
Chris@0
|
419 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
|
Chris@0
|
420 {
|
Chris@0
|
421 m_playLatency = latency;
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 size_t
|
Chris@0
|
425 AudioCallbackPlaySource::getTargetPlayLatency() const
|
Chris@0
|
426 {
|
Chris@0
|
427 return m_playLatency;
|
Chris@0
|
428 }
|
Chris@0
|
429
|
Chris@0
|
430 size_t
|
Chris@0
|
431 AudioCallbackPlaySource::getCurrentPlayingFrame()
|
Chris@0
|
432 {
|
Chris@0
|
433 bool resample = false;
|
Chris@0
|
434 double ratio = 1.0;
|
Chris@0
|
435
|
Chris@0
|
436 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@0
|
437 resample = true;
|
Chris@0
|
438 ratio = double(getSourceSampleRate()) / double(getTargetSampleRate());
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 size_t readSpace = 0;
|
Chris@0
|
442 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@0
|
443 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@0
|
444 if (rb) {
|
Chris@0
|
445 size_t spaceHere = rb->getReadSpace();
|
Chris@0
|
446 if (c == 0 || spaceHere < readSpace) readSpace = spaceHere;
|
Chris@0
|
447 }
|
Chris@0
|
448 }
|
Chris@0
|
449
|
Chris@0
|
450 if (resample) {
|
Chris@0
|
451 readSpace = size_t(readSpace * ratio + 0.1);
|
Chris@0
|
452 }
|
Chris@0
|
453
|
Chris@0
|
454 size_t latency = m_playLatency;
|
Chris@0
|
455 if (resample) latency = size_t(m_playLatency * ratio + 0.1);
|
Chris@16
|
456
|
Chris@16
|
457 PhaseVocoderTimeStretcher *timeStretcher = m_timeStretcher;
|
Chris@0
|
458 if (timeStretcher) {
|
Chris@16
|
459 latency += timeStretcher->getProcessingLatency();
|
Chris@0
|
460 }
|
Chris@0
|
461
|
Chris@0
|
462 latency += readSpace;
|
Chris@0
|
463 size_t bufferedFrame = m_readBufferFill;
|
Chris@0
|
464
|
Chris@0
|
465 bool looping = m_viewManager->getPlayLoopMode();
|
Chris@0
|
466 bool constrained = (m_viewManager->getPlaySelectionMode() &&
|
Chris@0
|
467 !m_viewManager->getSelections().empty());
|
Chris@0
|
468
|
Chris@0
|
469 size_t framePlaying = bufferedFrame;
|
Chris@0
|
470
|
Chris@0
|
471 if (looping && !constrained) {
|
Chris@0
|
472 while (framePlaying < latency) framePlaying += m_lastModelEndFrame;
|
Chris@0
|
473 }
|
Chris@0
|
474
|
Chris@0
|
475 if (framePlaying > latency) framePlaying -= latency;
|
Chris@0
|
476 else framePlaying = 0;
|
Chris@0
|
477
|
Chris@0
|
478 if (!constrained) {
|
Chris@0
|
479 if (!looping && framePlaying > m_lastModelEndFrame) {
|
Chris@0
|
480 framePlaying = m_lastModelEndFrame;
|
Chris@0
|
481 stop();
|
Chris@0
|
482 }
|
Chris@0
|
483 return framePlaying;
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 MultiSelection::SelectionList selections = m_viewManager->getSelections();
|
Chris@0
|
487 MultiSelection::SelectionList::const_iterator i;
|
Chris@0
|
488
|
Chris@0
|
489 i = selections.begin();
|
Chris@0
|
490 size_t rangeStart = i->getStartFrame();
|
Chris@0
|
491
|
Chris@0
|
492 i = selections.end();
|
Chris@0
|
493 --i;
|
Chris@0
|
494 size_t rangeEnd = i->getEndFrame();
|
Chris@0
|
495
|
Chris@0
|
496 for (i = selections.begin(); i != selections.end(); ++i) {
|
Chris@0
|
497 if (i->contains(bufferedFrame)) break;
|
Chris@0
|
498 }
|
Chris@0
|
499
|
Chris@0
|
500 size_t f = bufferedFrame;
|
Chris@0
|
501
|
Chris@0
|
502 // std::cerr << "getCurrentPlayingFrame: f=" << f << ", latency=" << latency << ", rangeEnd=" << rangeEnd << std::endl;
|
Chris@0
|
503
|
Chris@0
|
504 if (i == selections.end()) {
|
Chris@0
|
505 --i;
|
Chris@0
|
506 if (i->getEndFrame() + latency < f) {
|
Chris@0
|
507 // std::cerr << "framePlaying = " << framePlaying << ", rangeEnd = " << rangeEnd << std::endl;
|
Chris@0
|
508
|
Chris@0
|
509 if (!looping && (framePlaying > rangeEnd)) {
|
Chris@0
|
510 // std::cerr << "STOPPING" << std::endl;
|
Chris@0
|
511 stop();
|
Chris@0
|
512 return rangeEnd;
|
Chris@0
|
513 } else {
|
Chris@0
|
514 return framePlaying;
|
Chris@0
|
515 }
|
Chris@0
|
516 } else {
|
Chris@0
|
517 // std::cerr << "latency <- " << latency << "-(" << f << "-" << i->getEndFrame() << ")" << std::endl;
|
Chris@0
|
518 latency -= (f - i->getEndFrame());
|
Chris@0
|
519 f = i->getEndFrame();
|
Chris@0
|
520 }
|
Chris@0
|
521 }
|
Chris@0
|
522
|
Chris@0
|
523 // std::cerr << "i=(" << i->getStartFrame() << "," << i->getEndFrame() << ") f=" << f << ", latency=" << latency << std::endl;
|
Chris@0
|
524
|
Chris@0
|
525 while (latency > 0) {
|
Chris@0
|
526 size_t offset = f - i->getStartFrame();
|
Chris@0
|
527 if (offset >= latency) {
|
Chris@0
|
528 if (f > latency) {
|
Chris@0
|
529 framePlaying = f - latency;
|
Chris@0
|
530 } else {
|
Chris@0
|
531 framePlaying = 0;
|
Chris@0
|
532 }
|
Chris@0
|
533 break;
|
Chris@0
|
534 } else {
|
Chris@0
|
535 if (i == selections.begin()) {
|
Chris@0
|
536 if (looping) {
|
Chris@0
|
537 i = selections.end();
|
Chris@0
|
538 }
|
Chris@0
|
539 }
|
Chris@0
|
540 latency -= offset;
|
Chris@0
|
541 --i;
|
Chris@0
|
542 f = i->getEndFrame();
|
Chris@0
|
543 }
|
Chris@0
|
544 }
|
Chris@0
|
545
|
Chris@0
|
546 return framePlaying;
|
Chris@0
|
547 }
|
Chris@0
|
548
|
Chris@0
|
549 void
|
Chris@0
|
550 AudioCallbackPlaySource::setOutputLevels(float left, float right)
|
Chris@0
|
551 {
|
Chris@0
|
552 m_outputLeft = left;
|
Chris@0
|
553 m_outputRight = right;
|
Chris@0
|
554 }
|
Chris@0
|
555
|
Chris@0
|
556 bool
|
Chris@0
|
557 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
|
Chris@0
|
558 {
|
Chris@0
|
559 left = m_outputLeft;
|
Chris@0
|
560 right = m_outputRight;
|
Chris@0
|
561 return true;
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 void
|
Chris@0
|
565 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
|
Chris@0
|
566 {
|
Chris@0
|
567 m_targetSampleRate = sr;
|
Chris@32
|
568 initialiseConverter();
|
Chris@32
|
569 }
|
Chris@32
|
570
|
Chris@32
|
571 void
|
Chris@32
|
572 AudioCallbackPlaySource::initialiseConverter()
|
Chris@32
|
573 {
|
Chris@32
|
574 m_mutex.lock();
|
Chris@32
|
575
|
Chris@32
|
576 if (m_converter) {
|
Chris@32
|
577 src_delete(m_converter);
|
Chris@32
|
578 src_delete(m_crapConverter);
|
Chris@32
|
579 m_converter = 0;
|
Chris@32
|
580 m_crapConverter = 0;
|
Chris@32
|
581 }
|
Chris@0
|
582
|
Chris@0
|
583 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@0
|
584
|
Chris@0
|
585 int err = 0;
|
Chris@32
|
586
|
Chris@32
|
587 m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY :
|
Chris@32
|
588 m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY :
|
Chris@32
|
589 m_resampleQuality == 0 ? SRC_SINC_FASTEST :
|
Chris@32
|
590 SRC_SINC_MEDIUM_QUALITY,
|
Chris@0
|
591 getTargetChannelCount(), &err);
|
Chris@32
|
592
|
Chris@32
|
593 if (m_converter) {
|
Chris@32
|
594 m_crapConverter = src_new(SRC_LINEAR,
|
Chris@32
|
595 getTargetChannelCount(),
|
Chris@32
|
596 &err);
|
Chris@32
|
597 }
|
Chris@32
|
598
|
Chris@32
|
599 if (!m_converter || !m_crapConverter) {
|
Chris@0
|
600 std::cerr
|
Chris@0
|
601 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
|
Chris@0
|
602 << src_strerror(err) << std::endl;
|
Chris@0
|
603
|
Chris@32
|
604 if (m_converter) {
|
Chris@32
|
605 src_delete(m_converter);
|
Chris@32
|
606 m_converter = 0;
|
Chris@32
|
607 }
|
Chris@32
|
608
|
Chris@32
|
609 if (m_crapConverter) {
|
Chris@32
|
610 src_delete(m_crapConverter);
|
Chris@32
|
611 m_crapConverter = 0;
|
Chris@32
|
612 }
|
Chris@32
|
613
|
Chris@32
|
614 m_mutex.unlock();
|
Chris@32
|
615
|
Chris@0
|
616 emit sampleRateMismatch(getSourceSampleRate(),
|
Chris@0
|
617 getTargetSampleRate(),
|
Chris@0
|
618 false);
|
Chris@0
|
619 } else {
|
Chris@0
|
620
|
Chris@32
|
621 m_mutex.unlock();
|
Chris@32
|
622
|
Chris@0
|
623 emit sampleRateMismatch(getSourceSampleRate(),
|
Chris@0
|
624 getTargetSampleRate(),
|
Chris@0
|
625 true);
|
Chris@0
|
626 }
|
Chris@32
|
627 } else {
|
Chris@32
|
628 m_mutex.unlock();
|
Chris@0
|
629 }
|
Chris@0
|
630 }
|
Chris@0
|
631
|
Chris@32
|
632 void
|
Chris@32
|
633 AudioCallbackPlaySource::setResampleQuality(int q)
|
Chris@32
|
634 {
|
Chris@32
|
635 if (q == m_resampleQuality) return;
|
Chris@32
|
636 m_resampleQuality = q;
|
Chris@32
|
637
|
Chris@32
|
638 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@32
|
639 std::cerr << "AudioCallbackPlaySource::setResampleQuality: setting to "
|
Chris@32
|
640 << m_resampleQuality << std::endl;
|
Chris@32
|
641 #endif
|
Chris@32
|
642
|
Chris@32
|
643 initialiseConverter();
|
Chris@32
|
644 }
|
Chris@32
|
645
|
Chris@41
|
646 void
|
Chris@41
|
647 AudioCallbackPlaySource::setAuditioningPlugin(RealTimePluginInstance *plugin)
|
Chris@41
|
648 {
|
Chris@41
|
649 RealTimePluginInstance *formerPlugin = m_auditioningPlugin;
|
Chris@41
|
650 m_auditioningPlugin = plugin;
|
Chris@41
|
651 if (formerPlugin) m_pluginScavenger.claim(formerPlugin);
|
Chris@41
|
652 }
|
Chris@41
|
653
|
Chris@0
|
654 size_t
|
Chris@0
|
655 AudioCallbackPlaySource::getTargetSampleRate() const
|
Chris@0
|
656 {
|
Chris@0
|
657 if (m_targetSampleRate) return m_targetSampleRate;
|
Chris@0
|
658 else return getSourceSampleRate();
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 size_t
|
Chris@0
|
662 AudioCallbackPlaySource::getSourceChannelCount() const
|
Chris@0
|
663 {
|
Chris@0
|
664 return m_sourceChannelCount;
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 size_t
|
Chris@0
|
668 AudioCallbackPlaySource::getTargetChannelCount() const
|
Chris@0
|
669 {
|
Chris@0
|
670 if (m_sourceChannelCount < 2) return 2;
|
Chris@0
|
671 return m_sourceChannelCount;
|
Chris@0
|
672 }
|
Chris@0
|
673
|
Chris@0
|
674 size_t
|
Chris@0
|
675 AudioCallbackPlaySource::getSourceSampleRate() const
|
Chris@0
|
676 {
|
Chris@0
|
677 return m_sourceSampleRate;
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 void
|
Chris@26
|
681 AudioCallbackPlaySource::setTimeStretch(float factor, bool sharpen, bool mono)
|
Chris@0
|
682 {
|
Chris@0
|
683 // Avoid locks -- create, assign, mark old one for scavenging
|
Chris@0
|
684 // later (as a call to getSourceSamples may still be using it)
|
Chris@0
|
685
|
Chris@16
|
686 PhaseVocoderTimeStretcher *existingStretcher = m_timeStretcher;
|
Chris@0
|
687
|
Chris@26
|
688 size_t channels = getTargetChannelCount();
|
Chris@26
|
689 if (mono) channels = 1;
|
Chris@26
|
690
|
Chris@16
|
691 if (existingStretcher &&
|
Chris@16
|
692 existingStretcher->getRatio() == factor &&
|
Chris@26
|
693 existingStretcher->getSharpening() == sharpen &&
|
Chris@26
|
694 existingStretcher->getChannelCount() == channels) {
|
Chris@0
|
695 return;
|
Chris@0
|
696 }
|
Chris@0
|
697
|
Chris@12
|
698 if (factor != 1) {
|
Chris@25
|
699
|
Chris@25
|
700 if (existingStretcher &&
|
Chris@26
|
701 existingStretcher->getSharpening() == sharpen &&
|
Chris@26
|
702 existingStretcher->getChannelCount() == channels) {
|
Chris@25
|
703 existingStretcher->setRatio(factor);
|
Chris@25
|
704 return;
|
Chris@25
|
705 }
|
Chris@25
|
706
|
Chris@16
|
707 PhaseVocoderTimeStretcher *newStretcher = new PhaseVocoderTimeStretcher
|
Chris@22
|
708 (getTargetSampleRate(),
|
Chris@26
|
709 channels,
|
Chris@16
|
710 factor,
|
Chris@16
|
711 sharpen,
|
Chris@31
|
712 getTargetBlockSize());
|
Chris@26
|
713
|
Chris@0
|
714 m_timeStretcher = newStretcher;
|
Chris@26
|
715
|
Chris@0
|
716 } else {
|
Chris@0
|
717 m_timeStretcher = 0;
|
Chris@0
|
718 }
|
Chris@0
|
719
|
Chris@0
|
720 if (existingStretcher) {
|
Chris@0
|
721 m_timeStretcherScavenger.claim(existingStretcher);
|
Chris@0
|
722 }
|
Chris@0
|
723 }
|
Chris@26
|
724
|
Chris@0
|
725 size_t
|
Chris@0
|
726 AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer)
|
Chris@0
|
727 {
|
Chris@0
|
728 if (!m_playing) {
|
Chris@0
|
729 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
|
Chris@0
|
730 for (size_t i = 0; i < count; ++i) {
|
Chris@0
|
731 buffer[ch][i] = 0.0;
|
Chris@0
|
732 }
|
Chris@0
|
733 }
|
Chris@0
|
734 return 0;
|
Chris@0
|
735 }
|
Chris@0
|
736
|
Chris@16
|
737 PhaseVocoderTimeStretcher *ts = m_timeStretcher;
|
Chris@0
|
738
|
Chris@16
|
739 if (!ts || ts->getRatio() == 1) {
|
Chris@0
|
740
|
Chris@0
|
741 size_t got = 0;
|
Chris@0
|
742
|
Chris@0
|
743 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
|
Chris@0
|
744
|
Chris@0
|
745 RingBuffer<float> *rb = getReadRingBuffer(ch);
|
Chris@0
|
746
|
Chris@0
|
747 if (rb) {
|
Chris@0
|
748
|
Chris@0
|
749 // this is marginally more likely to leave our channels in
|
Chris@0
|
750 // sync after a processing failure than just passing "count":
|
Chris@0
|
751 size_t request = count;
|
Chris@0
|
752 if (ch > 0) request = got;
|
Chris@0
|
753
|
Chris@0
|
754 got = rb->read(buffer[ch], request);
|
Chris@0
|
755
|
Chris@0
|
756 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
|
Chris@0
|
757 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", signalling for more (possibly)" << std::endl;
|
Chris@0
|
758 #endif
|
Chris@0
|
759 }
|
Chris@0
|
760
|
Chris@0
|
761 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
|
Chris@0
|
762 for (size_t i = got; i < count; ++i) {
|
Chris@0
|
763 buffer[ch][i] = 0.0;
|
Chris@0
|
764 }
|
Chris@0
|
765 }
|
Chris@0
|
766 }
|
Chris@0
|
767
|
Chris@41
|
768 applyAuditioningEffect(count, buffer);
|
Chris@41
|
769
|
Chris@0
|
770 m_condition.wakeAll();
|
Chris@0
|
771 return got;
|
Chris@0
|
772 }
|
Chris@0
|
773
|
Chris@16
|
774 float ratio = ts->getRatio();
|
Chris@0
|
775
|
Chris@16
|
776 // std::cout << "ratio = " << ratio << std::endl;
|
Chris@0
|
777
|
Chris@26
|
778 size_t channels = getTargetChannelCount();
|
Chris@26
|
779 bool mix = (channels > 1 && ts->getChannelCount() == 1);
|
Chris@26
|
780
|
Chris@16
|
781 size_t available;
|
Chris@0
|
782
|
Chris@31
|
783 int warned = 0;
|
Chris@31
|
784
|
Chris@31
|
785
|
Chris@31
|
786
|
Chris@31
|
787 //!!!
|
Chris@31
|
788 // We want output blocks of e.g. 1024 (probably fixed, certainly
|
Chris@31
|
789 // bounded). We can provide input blocks of any size (unbounded)
|
Chris@31
|
790 // at the timestretcher's request. The input block for a given
|
Chris@31
|
791 // output is approx output / ratio, but we can't predict it
|
Chris@31
|
792 // exactly, for an adaptive timestretcher. The stretcher will
|
Chris@31
|
793 // need some additional buffer space.
|
Chris@31
|
794
|
Chris@31
|
795
|
Chris@31
|
796
|
Chris@31
|
797
|
Chris@16
|
798 while ((available = ts->getAvailableOutputSamples()) < count) {
|
Chris@0
|
799
|
Chris@16
|
800 size_t reqd = lrintf((count - available) / ratio);
|
Chris@16
|
801 reqd = std::max(reqd, ts->getRequiredInputSamples());
|
Chris@16
|
802 if (reqd == 0) reqd = 1;
|
Chris@16
|
803
|
Chris@16
|
804 float *ib[channels];
|
Chris@0
|
805
|
Chris@16
|
806 size_t got = reqd;
|
Chris@0
|
807
|
Chris@26
|
808 if (mix) {
|
Chris@26
|
809 for (size_t c = 0; c < channels; ++c) {
|
Chris@26
|
810 if (c == 0) ib[c] = new float[reqd]; //!!! fix -- this is a rt function
|
Chris@26
|
811 else ib[c] = 0;
|
Chris@26
|
812 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@26
|
813 if (rb) {
|
Chris@26
|
814 size_t gotHere;
|
Chris@26
|
815 if (c > 0) gotHere = rb->readAdding(ib[0], got);
|
Chris@26
|
816 else gotHere = rb->read(ib[0], got);
|
Chris@26
|
817 if (gotHere < got) got = gotHere;
|
Chris@26
|
818 }
|
Chris@26
|
819 }
|
Chris@26
|
820 } else {
|
Chris@26
|
821 for (size_t c = 0; c < channels; ++c) {
|
Chris@26
|
822 ib[c] = new float[reqd]; //!!! fix -- this is a rt function
|
Chris@26
|
823 RingBuffer<float> *rb = getReadRingBuffer(c);
|
Chris@26
|
824 if (rb) {
|
Chris@26
|
825 size_t gotHere = rb->read(ib[c], got);
|
Chris@26
|
826 if (gotHere < got) got = gotHere;
|
Chris@26
|
827 }
|
Chris@16
|
828 }
|
Chris@16
|
829 }
|
Chris@0
|
830
|
Chris@16
|
831 if (got < reqd) {
|
Chris@16
|
832 std::cerr << "WARNING: Read underrun in playback ("
|
Chris@16
|
833 << got << " < " << reqd << ")" << std::endl;
|
Chris@16
|
834 }
|
Chris@16
|
835
|
Chris@16
|
836 ts->putInput(ib, got);
|
Chris@16
|
837
|
Chris@16
|
838 for (size_t c = 0; c < channels; ++c) {
|
Chris@16
|
839 delete[] ib[c];
|
Chris@16
|
840 }
|
Chris@16
|
841
|
Chris@16
|
842 if (got == 0) break;
|
Chris@16
|
843
|
Chris@16
|
844 if (ts->getAvailableOutputSamples() == available) {
|
Chris@31
|
845 std::cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << std::endl;
|
Chris@31
|
846 if (++warned == 5) break;
|
Chris@16
|
847 }
|
Chris@0
|
848 }
|
Chris@0
|
849
|
Chris@16
|
850 ts->getOutput(buffer, count);
|
Chris@0
|
851
|
Chris@26
|
852 if (mix) {
|
Chris@26
|
853 for (size_t c = 1; c < channels; ++c) {
|
Chris@26
|
854 for (size_t i = 0; i < count; ++i) {
|
Chris@26
|
855 buffer[c][i] = buffer[0][i] / channels;
|
Chris@26
|
856 }
|
Chris@26
|
857 }
|
Chris@26
|
858 for (size_t i = 0; i < count; ++i) {
|
Chris@26
|
859 buffer[0][i] /= channels;
|
Chris@26
|
860 }
|
Chris@26
|
861 }
|
Chris@26
|
862
|
Chris@41
|
863 applyAuditioningEffect(count, buffer);
|
Chris@41
|
864
|
Chris@16
|
865 m_condition.wakeAll();
|
Chris@12
|
866
|
Chris@0
|
867 return count;
|
Chris@0
|
868 }
|
Chris@0
|
869
|
Chris@41
|
870 void
|
Chris@41
|
871 AudioCallbackPlaySource::applyAuditioningEffect(size_t count, float **buffers)
|
Chris@41
|
872 {
|
Chris@41
|
873 RealTimePluginInstance *plugin = m_auditioningPlugin;
|
Chris@41
|
874 if (!plugin) return;
|
Chris@41
|
875
|
Chris@41
|
876 if (plugin->getAudioInputCount() != getTargetChannelCount()) {
|
Chris@41
|
877 std::cerr << "plugin input count " << plugin->getAudioInputCount()
|
Chris@41
|
878 << " != our channel count " << getTargetChannelCount()
|
Chris@41
|
879 << std::endl;
|
Chris@41
|
880 return;
|
Chris@41
|
881 }
|
Chris@41
|
882 if (plugin->getAudioOutputCount() != getTargetChannelCount()) {
|
Chris@41
|
883 std::cerr << "plugin output count " << plugin->getAudioOutputCount()
|
Chris@41
|
884 << " != our channel count " << getTargetChannelCount()
|
Chris@41
|
885 << std::endl;
|
Chris@41
|
886 return;
|
Chris@41
|
887 }
|
Chris@41
|
888 if (plugin->getBufferSize() != count) {
|
Chris@41
|
889 std::cerr << "plugin buffer size " << plugin->getBufferSize()
|
Chris@41
|
890 << " != our block size " << count
|
Chris@41
|
891 << std::endl;
|
Chris@41
|
892 return;
|
Chris@41
|
893 }
|
Chris@41
|
894
|
Chris@41
|
895 float **ib = plugin->getAudioInputBuffers();
|
Chris@41
|
896 float **ob = plugin->getAudioOutputBuffers();
|
Chris@41
|
897
|
Chris@41
|
898 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@41
|
899 for (size_t i = 0; i < count; ++i) {
|
Chris@41
|
900 ib[c][i] = buffers[c][i];
|
Chris@41
|
901 }
|
Chris@41
|
902 }
|
Chris@41
|
903
|
Chris@41
|
904 plugin->run(Vamp::RealTime::zeroTime);
|
Chris@41
|
905
|
Chris@41
|
906 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@41
|
907 for (size_t i = 0; i < count; ++i) {
|
Chris@41
|
908 buffers[c][i] = ob[c][i];
|
Chris@41
|
909 }
|
Chris@41
|
910 }
|
Chris@41
|
911 }
|
Chris@41
|
912
|
Chris@0
|
913 // Called from fill thread, m_playing true, mutex held
|
Chris@0
|
914 bool
|
Chris@0
|
915 AudioCallbackPlaySource::fillBuffers()
|
Chris@0
|
916 {
|
Chris@0
|
917 static float *tmp = 0;
|
Chris@0
|
918 static size_t tmpSize = 0;
|
Chris@0
|
919
|
Chris@0
|
920 size_t space = 0;
|
Chris@0
|
921 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@0
|
922 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@0
|
923 if (wb) {
|
Chris@0
|
924 size_t spaceHere = wb->getWriteSpace();
|
Chris@0
|
925 if (c == 0 || spaceHere < space) space = spaceHere;
|
Chris@0
|
926 }
|
Chris@0
|
927 }
|
Chris@0
|
928
|
Chris@0
|
929 if (space == 0) return false;
|
Chris@0
|
930
|
Chris@0
|
931 size_t f = m_writeBufferFill;
|
Chris@0
|
932
|
Chris@0
|
933 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
|
Chris@0
|
934
|
Chris@0
|
935 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
936 std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl;
|
Chris@0
|
937 #endif
|
Chris@0
|
938
|
Chris@0
|
939 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
940 std::cout << "buffered to " << f << " already" << std::endl;
|
Chris@0
|
941 #endif
|
Chris@0
|
942
|
Chris@0
|
943 bool resample = (getSourceSampleRate() != getTargetSampleRate());
|
Chris@0
|
944
|
Chris@0
|
945 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
946 std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
|
Chris@0
|
947 #endif
|
Chris@0
|
948
|
Chris@0
|
949 size_t channels = getTargetChannelCount();
|
Chris@0
|
950
|
Chris@0
|
951 size_t orig = space;
|
Chris@0
|
952 size_t got = 0;
|
Chris@0
|
953
|
Chris@0
|
954 static float **bufferPtrs = 0;
|
Chris@0
|
955 static size_t bufferPtrCount = 0;
|
Chris@0
|
956
|
Chris@0
|
957 if (bufferPtrCount < channels) {
|
Chris@0
|
958 if (bufferPtrs) delete[] bufferPtrs;
|
Chris@0
|
959 bufferPtrs = new float *[channels];
|
Chris@0
|
960 bufferPtrCount = channels;
|
Chris@0
|
961 }
|
Chris@0
|
962
|
Chris@0
|
963 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
|
Chris@0
|
964
|
Chris@0
|
965 if (resample && !m_converter) {
|
Chris@0
|
966 static bool warned = false;
|
Chris@0
|
967 if (!warned) {
|
Chris@0
|
968 std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
|
Chris@0
|
969 warned = true;
|
Chris@0
|
970 }
|
Chris@0
|
971 }
|
Chris@0
|
972
|
Chris@0
|
973 if (resample && m_converter) {
|
Chris@0
|
974
|
Chris@0
|
975 double ratio =
|
Chris@0
|
976 double(getTargetSampleRate()) / double(getSourceSampleRate());
|
Chris@0
|
977 orig = size_t(orig / ratio + 0.1);
|
Chris@0
|
978
|
Chris@0
|
979 // orig must be a multiple of generatorBlockSize
|
Chris@0
|
980 orig = (orig / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
981 if (orig == 0) return false;
|
Chris@0
|
982
|
Chris@0
|
983 size_t work = std::max(orig, space);
|
Chris@0
|
984
|
Chris@0
|
985 // We only allocate one buffer, but we use it in two halves.
|
Chris@0
|
986 // We place the non-interleaved values in the second half of
|
Chris@0
|
987 // the buffer (orig samples for channel 0, orig samples for
|
Chris@0
|
988 // channel 1 etc), and then interleave them into the first
|
Chris@0
|
989 // half of the buffer. Then we resample back into the second
|
Chris@0
|
990 // half (interleaved) and de-interleave the results back to
|
Chris@0
|
991 // the start of the buffer for insertion into the ringbuffers.
|
Chris@0
|
992 // What a faff -- especially as we've already de-interleaved
|
Chris@0
|
993 // the audio data from the source file elsewhere before we
|
Chris@0
|
994 // even reach this point.
|
Chris@0
|
995
|
Chris@0
|
996 if (tmpSize < channels * work * 2) {
|
Chris@0
|
997 delete[] tmp;
|
Chris@0
|
998 tmp = new float[channels * work * 2];
|
Chris@0
|
999 tmpSize = channels * work * 2;
|
Chris@0
|
1000 }
|
Chris@0
|
1001
|
Chris@0
|
1002 float *nonintlv = tmp + channels * work;
|
Chris@0
|
1003 float *intlv = tmp;
|
Chris@0
|
1004 float *srcout = tmp + channels * work;
|
Chris@0
|
1005
|
Chris@0
|
1006 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1007 for (size_t i = 0; i < orig; ++i) {
|
Chris@0
|
1008 nonintlv[channels * i + c] = 0.0f;
|
Chris@0
|
1009 }
|
Chris@0
|
1010 }
|
Chris@0
|
1011
|
Chris@0
|
1012 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1013 bufferPtrs[c] = nonintlv + c * orig;
|
Chris@0
|
1014 }
|
Chris@0
|
1015
|
Chris@0
|
1016 got = mixModels(f, orig, bufferPtrs);
|
Chris@0
|
1017
|
Chris@0
|
1018 // and interleave into first half
|
Chris@0
|
1019 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1020 for (size_t i = 0; i < got; ++i) {
|
Chris@0
|
1021 float sample = nonintlv[c * got + i];
|
Chris@0
|
1022 intlv[channels * i + c] = sample;
|
Chris@0
|
1023 }
|
Chris@0
|
1024 }
|
Chris@0
|
1025
|
Chris@0
|
1026 SRC_DATA data;
|
Chris@0
|
1027 data.data_in = intlv;
|
Chris@0
|
1028 data.data_out = srcout;
|
Chris@0
|
1029 data.input_frames = got;
|
Chris@0
|
1030 data.output_frames = work;
|
Chris@0
|
1031 data.src_ratio = ratio;
|
Chris@0
|
1032 data.end_of_input = 0;
|
Chris@0
|
1033
|
Chris@32
|
1034 int err = 0;
|
Chris@32
|
1035
|
Chris@32
|
1036 if (m_timeStretcher && m_timeStretcher->getRatio() < 0.4) {
|
Chris@32
|
1037 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@32
|
1038 std::cerr << "Using crappy converter" << std::endl;
|
Chris@32
|
1039 #endif
|
Chris@32
|
1040 src_process(m_crapConverter, &data);
|
Chris@32
|
1041 } else {
|
Chris@32
|
1042 src_process(m_converter, &data);
|
Chris@32
|
1043 }
|
Chris@32
|
1044
|
Chris@0
|
1045 size_t toCopy = size_t(got * ratio + 0.1);
|
Chris@0
|
1046
|
Chris@0
|
1047 if (err) {
|
Chris@0
|
1048 std::cerr
|
Chris@0
|
1049 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
|
Chris@0
|
1050 << src_strerror(err) << std::endl;
|
Chris@0
|
1051 //!!! Then what?
|
Chris@0
|
1052 } else {
|
Chris@0
|
1053 got = data.input_frames_used;
|
Chris@0
|
1054 toCopy = data.output_frames_gen;
|
Chris@0
|
1055 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1056 std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
|
Chris@0
|
1057 #endif
|
Chris@0
|
1058 }
|
Chris@0
|
1059
|
Chris@0
|
1060 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1061 for (size_t i = 0; i < toCopy; ++i) {
|
Chris@0
|
1062 tmp[i] = srcout[channels * i + c];
|
Chris@0
|
1063 }
|
Chris@0
|
1064 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@0
|
1065 if (wb) wb->write(tmp, toCopy);
|
Chris@0
|
1066 }
|
Chris@0
|
1067
|
Chris@0
|
1068 m_writeBufferFill = f;
|
Chris@0
|
1069 if (readWriteEqual) m_readBufferFill = f;
|
Chris@0
|
1070
|
Chris@0
|
1071 } else {
|
Chris@0
|
1072
|
Chris@0
|
1073 // space must be a multiple of generatorBlockSize
|
Chris@0
|
1074 space = (space / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
1075 if (space == 0) return false;
|
Chris@0
|
1076
|
Chris@0
|
1077 if (tmpSize < channels * space) {
|
Chris@0
|
1078 delete[] tmp;
|
Chris@0
|
1079 tmp = new float[channels * space];
|
Chris@0
|
1080 tmpSize = channels * space;
|
Chris@0
|
1081 }
|
Chris@0
|
1082
|
Chris@0
|
1083 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1084
|
Chris@0
|
1085 bufferPtrs[c] = tmp + c * space;
|
Chris@0
|
1086
|
Chris@0
|
1087 for (size_t i = 0; i < space; ++i) {
|
Chris@0
|
1088 tmp[c * space + i] = 0.0f;
|
Chris@0
|
1089 }
|
Chris@0
|
1090 }
|
Chris@0
|
1091
|
Chris@0
|
1092 size_t got = mixModels(f, space, bufferPtrs);
|
Chris@0
|
1093
|
Chris@0
|
1094 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1095
|
Chris@0
|
1096 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@0
|
1097 if (wb) wb->write(bufferPtrs[c], got);
|
Chris@0
|
1098
|
Chris@0
|
1099 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1100 if (wb)
|
Chris@0
|
1101 std::cerr << "Wrote " << got << " frames for ch " << c << ", now "
|
Chris@0
|
1102 << wb->getReadSpace() << " to read"
|
Chris@0
|
1103 << std::endl;
|
Chris@0
|
1104 #endif
|
Chris@0
|
1105 }
|
Chris@0
|
1106
|
Chris@0
|
1107 m_writeBufferFill = f;
|
Chris@0
|
1108 if (readWriteEqual) m_readBufferFill = f;
|
Chris@0
|
1109
|
Chris@0
|
1110 //!!! 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@0
|
1111 }
|
Chris@0
|
1112
|
Chris@0
|
1113 return true;
|
Chris@0
|
1114 }
|
Chris@0
|
1115
|
Chris@0
|
1116 size_t
|
Chris@0
|
1117 AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers)
|
Chris@0
|
1118 {
|
Chris@0
|
1119 size_t processed = 0;
|
Chris@0
|
1120 size_t chunkStart = frame;
|
Chris@0
|
1121 size_t chunkSize = count;
|
Chris@0
|
1122 size_t selectionSize = 0;
|
Chris@0
|
1123 size_t nextChunkStart = chunkStart + chunkSize;
|
Chris@0
|
1124
|
Chris@0
|
1125 bool looping = m_viewManager->getPlayLoopMode();
|
Chris@0
|
1126 bool constrained = (m_viewManager->getPlaySelectionMode() &&
|
Chris@0
|
1127 !m_viewManager->getSelections().empty());
|
Chris@0
|
1128
|
Chris@0
|
1129 static float **chunkBufferPtrs = 0;
|
Chris@0
|
1130 static size_t chunkBufferPtrCount = 0;
|
Chris@0
|
1131 size_t channels = getTargetChannelCount();
|
Chris@0
|
1132
|
Chris@0
|
1133 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1134 std::cerr << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl;
|
Chris@0
|
1135 #endif
|
Chris@0
|
1136
|
Chris@0
|
1137 if (chunkBufferPtrCount < channels) {
|
Chris@0
|
1138 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
|
Chris@0
|
1139 chunkBufferPtrs = new float *[channels];
|
Chris@0
|
1140 chunkBufferPtrCount = channels;
|
Chris@0
|
1141 }
|
Chris@0
|
1142
|
Chris@0
|
1143 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1144 chunkBufferPtrs[c] = buffers[c];
|
Chris@0
|
1145 }
|
Chris@0
|
1146
|
Chris@0
|
1147 while (processed < count) {
|
Chris@0
|
1148
|
Chris@0
|
1149 chunkSize = count - processed;
|
Chris@0
|
1150 nextChunkStart = chunkStart + chunkSize;
|
Chris@0
|
1151 selectionSize = 0;
|
Chris@0
|
1152
|
Chris@0
|
1153 size_t fadeIn = 0, fadeOut = 0;
|
Chris@0
|
1154
|
Chris@0
|
1155 if (constrained) {
|
Chris@0
|
1156
|
Chris@0
|
1157 Selection selection =
|
Chris@0
|
1158 m_viewManager->getContainingSelection(chunkStart, true);
|
Chris@0
|
1159
|
Chris@0
|
1160 if (selection.isEmpty()) {
|
Chris@0
|
1161 if (looping) {
|
Chris@0
|
1162 selection = *m_viewManager->getSelections().begin();
|
Chris@0
|
1163 chunkStart = selection.getStartFrame();
|
Chris@0
|
1164 fadeIn = 50;
|
Chris@0
|
1165 }
|
Chris@0
|
1166 }
|
Chris@0
|
1167
|
Chris@0
|
1168 if (selection.isEmpty()) {
|
Chris@0
|
1169
|
Chris@0
|
1170 chunkSize = 0;
|
Chris@0
|
1171 nextChunkStart = chunkStart;
|
Chris@0
|
1172
|
Chris@0
|
1173 } else {
|
Chris@0
|
1174
|
Chris@0
|
1175 selectionSize =
|
Chris@0
|
1176 selection.getEndFrame() -
|
Chris@0
|
1177 selection.getStartFrame();
|
Chris@0
|
1178
|
Chris@0
|
1179 if (chunkStart < selection.getStartFrame()) {
|
Chris@0
|
1180 chunkStart = selection.getStartFrame();
|
Chris@0
|
1181 fadeIn = 50;
|
Chris@0
|
1182 }
|
Chris@0
|
1183
|
Chris@0
|
1184 nextChunkStart = chunkStart + chunkSize;
|
Chris@0
|
1185
|
Chris@0
|
1186 if (nextChunkStart >= selection.getEndFrame()) {
|
Chris@0
|
1187 nextChunkStart = selection.getEndFrame();
|
Chris@0
|
1188 fadeOut = 50;
|
Chris@0
|
1189 }
|
Chris@0
|
1190
|
Chris@0
|
1191 chunkSize = nextChunkStart - chunkStart;
|
Chris@0
|
1192 }
|
Chris@0
|
1193
|
Chris@0
|
1194 } else if (looping && m_lastModelEndFrame > 0) {
|
Chris@0
|
1195
|
Chris@0
|
1196 if (chunkStart >= m_lastModelEndFrame) {
|
Chris@0
|
1197 chunkStart = 0;
|
Chris@0
|
1198 }
|
Chris@0
|
1199 if (chunkSize > m_lastModelEndFrame - chunkStart) {
|
Chris@0
|
1200 chunkSize = m_lastModelEndFrame - chunkStart;
|
Chris@0
|
1201 }
|
Chris@0
|
1202 nextChunkStart = chunkStart + chunkSize;
|
Chris@0
|
1203 }
|
Chris@0
|
1204
|
Chris@0
|
1205 // std::cerr << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl;
|
Chris@0
|
1206
|
Chris@0
|
1207 if (!chunkSize) {
|
Chris@0
|
1208 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1209 std::cerr << "Ending selection playback at " << nextChunkStart << std::endl;
|
Chris@0
|
1210 #endif
|
Chris@0
|
1211 // We need to maintain full buffers so that the other
|
Chris@0
|
1212 // thread can tell where it's got to in the playback -- so
|
Chris@0
|
1213 // return the full amount here
|
Chris@0
|
1214 frame = frame + count;
|
Chris@0
|
1215 return count;
|
Chris@0
|
1216 }
|
Chris@0
|
1217
|
Chris@0
|
1218 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1219 std::cerr << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl;
|
Chris@0
|
1220 #endif
|
Chris@0
|
1221
|
Chris@0
|
1222 size_t got = 0;
|
Chris@0
|
1223
|
Chris@0
|
1224 if (selectionSize < 100) {
|
Chris@0
|
1225 fadeIn = 0;
|
Chris@0
|
1226 fadeOut = 0;
|
Chris@0
|
1227 } else if (selectionSize < 300) {
|
Chris@0
|
1228 if (fadeIn > 0) fadeIn = 10;
|
Chris@0
|
1229 if (fadeOut > 0) fadeOut = 10;
|
Chris@0
|
1230 }
|
Chris@0
|
1231
|
Chris@0
|
1232 if (fadeIn > 0) {
|
Chris@0
|
1233 if (processed * 2 < fadeIn) {
|
Chris@0
|
1234 fadeIn = processed * 2;
|
Chris@0
|
1235 }
|
Chris@0
|
1236 }
|
Chris@0
|
1237
|
Chris@0
|
1238 if (fadeOut > 0) {
|
Chris@0
|
1239 if ((count - processed - chunkSize) * 2 < fadeOut) {
|
Chris@0
|
1240 fadeOut = (count - processed - chunkSize) * 2;
|
Chris@0
|
1241 }
|
Chris@0
|
1242 }
|
Chris@0
|
1243
|
Chris@0
|
1244 for (std::set<Model *>::iterator mi = m_models.begin();
|
Chris@0
|
1245 mi != m_models.end(); ++mi) {
|
Chris@0
|
1246
|
Chris@0
|
1247 got = m_audioGenerator->mixModel(*mi, chunkStart,
|
Chris@0
|
1248 chunkSize, chunkBufferPtrs,
|
Chris@0
|
1249 fadeIn, fadeOut);
|
Chris@0
|
1250 }
|
Chris@0
|
1251
|
Chris@0
|
1252 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
1253 chunkBufferPtrs[c] += chunkSize;
|
Chris@0
|
1254 }
|
Chris@0
|
1255
|
Chris@0
|
1256 processed += chunkSize;
|
Chris@0
|
1257 chunkStart = nextChunkStart;
|
Chris@0
|
1258 }
|
Chris@0
|
1259
|
Chris@0
|
1260 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1261 std::cerr << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl;
|
Chris@0
|
1262 #endif
|
Chris@0
|
1263
|
Chris@0
|
1264 frame = nextChunkStart;
|
Chris@0
|
1265 return processed;
|
Chris@0
|
1266 }
|
Chris@0
|
1267
|
Chris@0
|
1268 void
|
Chris@0
|
1269 AudioCallbackPlaySource::unifyRingBuffers()
|
Chris@0
|
1270 {
|
Chris@0
|
1271 if (m_readBuffers == m_writeBuffers) return;
|
Chris@0
|
1272
|
Chris@0
|
1273 // only unify if there will be something to read
|
Chris@0
|
1274 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@0
|
1275 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@0
|
1276 if (wb) {
|
Chris@0
|
1277 if (wb->getReadSpace() < m_blockSize * 2) {
|
Chris@0
|
1278 if ((m_writeBufferFill + m_blockSize * 2) <
|
Chris@0
|
1279 m_lastModelEndFrame) {
|
Chris@0
|
1280 // OK, we don't have enough and there's more to
|
Chris@0
|
1281 // read -- don't unify until we can do better
|
Chris@0
|
1282 return;
|
Chris@0
|
1283 }
|
Chris@0
|
1284 }
|
Chris@0
|
1285 break;
|
Chris@0
|
1286 }
|
Chris@0
|
1287 }
|
Chris@0
|
1288
|
Chris@0
|
1289 size_t rf = m_readBufferFill;
|
Chris@0
|
1290 RingBuffer<float> *rb = getReadRingBuffer(0);
|
Chris@0
|
1291 if (rb) {
|
Chris@0
|
1292 size_t rs = rb->getReadSpace();
|
Chris@0
|
1293 //!!! incorrect when in non-contiguous selection, see comments elsewhere
|
Chris@0
|
1294 // std::cerr << "rs = " << rs << std::endl;
|
Chris@0
|
1295 if (rs < rf) rf -= rs;
|
Chris@0
|
1296 else rf = 0;
|
Chris@0
|
1297 }
|
Chris@0
|
1298
|
Chris@0
|
1299 //std::cerr << "m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl;
|
Chris@0
|
1300
|
Chris@0
|
1301 size_t wf = m_writeBufferFill;
|
Chris@0
|
1302 size_t skip = 0;
|
Chris@0
|
1303 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
|
Chris@0
|
1304 RingBuffer<float> *wb = getWriteRingBuffer(c);
|
Chris@0
|
1305 if (wb) {
|
Chris@0
|
1306 if (c == 0) {
|
Chris@0
|
1307
|
Chris@0
|
1308 size_t wrs = wb->getReadSpace();
|
Chris@0
|
1309 // std::cerr << "wrs = " << wrs << std::endl;
|
Chris@0
|
1310
|
Chris@0
|
1311 if (wrs < wf) wf -= wrs;
|
Chris@0
|
1312 else wf = 0;
|
Chris@0
|
1313 // std::cerr << "wf = " << wf << std::endl;
|
Chris@0
|
1314
|
Chris@0
|
1315 if (wf < rf) skip = rf - wf;
|
Chris@0
|
1316 if (skip == 0) break;
|
Chris@0
|
1317 }
|
Chris@0
|
1318
|
Chris@0
|
1319 // std::cerr << "skipping " << skip << std::endl;
|
Chris@0
|
1320 wb->skip(skip);
|
Chris@0
|
1321 }
|
Chris@0
|
1322 }
|
Chris@0
|
1323
|
Chris@0
|
1324 m_bufferScavenger.claim(m_readBuffers);
|
Chris@0
|
1325 m_readBuffers = m_writeBuffers;
|
Chris@0
|
1326 m_readBufferFill = m_writeBufferFill;
|
Chris@0
|
1327 // std::cerr << "unified" << std::endl;
|
Chris@0
|
1328 }
|
Chris@0
|
1329
|
Chris@0
|
1330 void
|
Chris@0
|
1331 AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run()
|
Chris@0
|
1332 {
|
Chris@0
|
1333 AudioCallbackPlaySource &s(m_source);
|
Chris@0
|
1334
|
Chris@0
|
1335 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1336 std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl;
|
Chris@0
|
1337 #endif
|
Chris@0
|
1338
|
Chris@0
|
1339 s.m_mutex.lock();
|
Chris@0
|
1340
|
Chris@0
|
1341 bool previouslyPlaying = s.m_playing;
|
Chris@0
|
1342 bool work = false;
|
Chris@0
|
1343
|
Chris@0
|
1344 while (!s.m_exiting) {
|
Chris@0
|
1345
|
Chris@0
|
1346 s.unifyRingBuffers();
|
Chris@0
|
1347 s.m_bufferScavenger.scavenge();
|
Chris@41
|
1348 s.m_pluginScavenger.scavenge();
|
Chris@0
|
1349 s.m_timeStretcherScavenger.scavenge();
|
Chris@0
|
1350
|
Chris@0
|
1351 if (work && s.m_playing && s.getSourceSampleRate()) {
|
Chris@0
|
1352
|
Chris@0
|
1353 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1354 std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl;
|
Chris@0
|
1355 #endif
|
Chris@0
|
1356
|
Chris@0
|
1357 s.m_mutex.unlock();
|
Chris@0
|
1358 s.m_mutex.lock();
|
Chris@0
|
1359
|
Chris@0
|
1360 } else {
|
Chris@0
|
1361
|
Chris@0
|
1362 float ms = 100;
|
Chris@0
|
1363 if (s.getSourceSampleRate() > 0) {
|
Chris@0
|
1364 ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0;
|
Chris@0
|
1365 }
|
Chris@0
|
1366
|
Chris@0
|
1367 if (s.m_playing) ms /= 10;
|
Chris@0
|
1368
|
Chris@0
|
1369 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1370 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl;
|
Chris@0
|
1371 #endif
|
Chris@0
|
1372
|
Chris@0
|
1373 s.m_condition.wait(&s.m_mutex, size_t(ms));
|
Chris@0
|
1374 }
|
Chris@0
|
1375
|
Chris@0
|
1376 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1377 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
|
Chris@0
|
1378 #endif
|
Chris@0
|
1379
|
Chris@0
|
1380 work = false;
|
Chris@0
|
1381
|
Chris@0
|
1382 if (!s.getSourceSampleRate()) continue;
|
Chris@0
|
1383
|
Chris@0
|
1384 bool playing = s.m_playing;
|
Chris@0
|
1385
|
Chris@0
|
1386 if (playing && !previouslyPlaying) {
|
Chris@0
|
1387 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
1388 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
|
Chris@0
|
1389 #endif
|
Chris@0
|
1390 for (size_t c = 0; c < s.getTargetChannelCount(); ++c) {
|
Chris@0
|
1391 RingBuffer<float> *rb = s.getReadRingBuffer(c);
|
Chris@0
|
1392 if (rb) rb->reset();
|
Chris@0
|
1393 }
|
Chris@0
|
1394 }
|
Chris@0
|
1395 previouslyPlaying = playing;
|
Chris@0
|
1396
|
Chris@0
|
1397 work = s.fillBuffers();
|
Chris@0
|
1398 }
|
Chris@0
|
1399
|
Chris@0
|
1400 s.m_mutex.unlock();
|
Chris@0
|
1401 }
|
Chris@0
|
1402
|
Chris@0
|
1403
|
Chris@0
|
1404
|
Chris@0
|
1405 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
1406 #include "AudioCallbackPlaySource.moc.cpp"
|
Chris@0
|
1407 #endif
|
Chris@0
|
1408
|