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