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