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