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