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