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@0
|
5 Chris Cannam, Queen Mary University of London, 2005
|
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@0
|
38 m_outputLeft(0.0),
|
Chris@0
|
39 m_outputRight(0.0),
|
Chris@0
|
40 m_slowdownCounter(0),
|
Chris@0
|
41 m_timeStretcher(0),
|
Chris@0
|
42 m_fillThread(0),
|
Chris@0
|
43 m_converter(0)
|
Chris@0
|
44 {
|
Chris@0
|
45 // preallocate some slots, to avoid reallocation in an
|
Chris@0
|
46 // un-thread-safe manner later
|
Chris@0
|
47 while (m_buffers.size() < 20) m_buffers.push_back(0);
|
Chris@0
|
48
|
Chris@0
|
49 m_viewManager->setAudioPlaySource(this);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 AudioCallbackPlaySource::~AudioCallbackPlaySource()
|
Chris@0
|
53 {
|
Chris@0
|
54 m_exiting = true;
|
Chris@0
|
55
|
Chris@0
|
56 if (m_fillThread) {
|
Chris@0
|
57 m_condition.wakeAll();
|
Chris@0
|
58 m_fillThread->wait();
|
Chris@0
|
59 delete m_fillThread;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 clearModels();
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 void
|
Chris@0
|
66 AudioCallbackPlaySource::addModel(Model *model)
|
Chris@0
|
67 {
|
Chris@0
|
68 m_mutex.lock();
|
Chris@0
|
69
|
Chris@0
|
70 m_models.insert(model);
|
Chris@0
|
71
|
Chris@0
|
72 bool buffersChanged = false, srChanged = false;
|
Chris@0
|
73
|
Chris@0
|
74 if (m_sourceSampleRate == 0) {
|
Chris@0
|
75
|
Chris@0
|
76 m_sourceSampleRate = model->getSampleRate();
|
Chris@0
|
77 srChanged = true;
|
Chris@0
|
78
|
Chris@0
|
79 } else if (model->getSampleRate() != m_sourceSampleRate) {
|
Chris@0
|
80 std::cerr << "AudioCallbackPlaySource::addModel: ERROR: "
|
Chris@0
|
81 << "New model sample rate does not match" << std::endl
|
Chris@0
|
82 << "existing model(s) (new " << model->getSampleRate()
|
Chris@0
|
83 << " vs " << m_sourceSampleRate
|
Chris@0
|
84 << "), playback will be wrong"
|
Chris@0
|
85 << std::endl;
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 size_t sz = m_ringBufferSize;
|
Chris@0
|
89 if (m_bufferCount > 0) {
|
Chris@0
|
90 sz = m_buffers[0]->getSize();
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 size_t modelChannels = 1;
|
Chris@0
|
94 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
|
Chris@0
|
95 if (dtvm) modelChannels = dtvm->getChannelCount();
|
Chris@0
|
96
|
Chris@0
|
97 while (m_bufferCount < modelChannels) {
|
Chris@0
|
98
|
Chris@0
|
99 if (m_buffers.size() < modelChannels) {
|
Chris@0
|
100 // This is a hideously chancy operation -- the RT thread
|
Chris@0
|
101 // could be using this vector. We allocated several slots
|
Chris@0
|
102 // in the ctor to avoid exactly this, but if we ever end
|
Chris@0
|
103 // up with more channels than that (!) then we're just
|
Chris@0
|
104 // going to have to risk it
|
Chris@0
|
105 m_buffers.push_back(new RingBuffer<float>(sz));
|
Chris@0
|
106
|
Chris@0
|
107 } else {
|
Chris@0
|
108 // The usual case
|
Chris@0
|
109 m_buffers[m_bufferCount] = new RingBuffer<float>(sz);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 ++m_bufferCount;
|
Chris@0
|
113 buffersChanged = true;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 if (buffersChanged) {
|
Chris@0
|
117 m_audioGenerator->setTargetChannelCount(m_bufferCount);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 if (buffersChanged || srChanged) {
|
Chris@0
|
121
|
Chris@0
|
122 if (m_converter) {
|
Chris@0
|
123 src_delete(m_converter);
|
Chris@0
|
124 m_converter = 0;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@0
|
128
|
Chris@0
|
129 int err = 0;
|
Chris@0
|
130 m_converter = src_new(SRC_SINC_FASTEST, m_bufferCount, &err);
|
Chris@0
|
131 if (!m_converter) {
|
Chris@0
|
132 std::cerr
|
Chris@0
|
133 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
|
Chris@0
|
134 << src_strerror(err) << std::endl;
|
Chris@0
|
135 }
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 m_audioGenerator->addModel(model);
|
Chris@0
|
140
|
Chris@0
|
141 m_mutex.unlock();
|
Chris@0
|
142
|
Chris@0
|
143 if (!m_fillThread) {
|
Chris@0
|
144 m_fillThread = new AudioCallbackPlaySourceFillThread(*this);
|
Chris@0
|
145 m_fillThread->start();
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
149 std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl;
|
Chris@0
|
150 #endif
|
Chris@0
|
151 emit modelReplaced();
|
Chris@0
|
152
|
Chris@0
|
153 if (srChanged && (getSourceSampleRate() != getTargetSampleRate())) {
|
Chris@0
|
154 emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate());
|
Chris@0
|
155 }
|
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@0
|
173 m_audioGenerator->removeModel(model);
|
Chris@0
|
174
|
Chris@0
|
175 m_mutex.unlock();
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 void
|
Chris@0
|
179 AudioCallbackPlaySource::clearModels()
|
Chris@0
|
180 {
|
Chris@0
|
181 m_mutex.lock();
|
Chris@0
|
182
|
Chris@0
|
183 m_models.clear();
|
Chris@0
|
184
|
Chris@0
|
185 if (m_converter) {
|
Chris@0
|
186 src_delete(m_converter);
|
Chris@0
|
187 m_converter = 0;
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 m_audioGenerator->clearModels();
|
Chris@0
|
191
|
Chris@0
|
192 m_sourceSampleRate = 0;
|
Chris@0
|
193
|
Chris@0
|
194 m_mutex.unlock();
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 void
|
Chris@0
|
198 AudioCallbackPlaySource::play(size_t startFrame)
|
Chris@0
|
199 {
|
Chris@0
|
200 // The fill thread will automatically empty its buffers before
|
Chris@0
|
201 // starting again if we have not so far been playing, but not if
|
Chris@0
|
202 // we're just re-seeking.
|
Chris@0
|
203
|
Chris@0
|
204 if (m_playing) {
|
Chris@0
|
205 m_mutex.lock();
|
Chris@0
|
206 m_bufferedToFrame = startFrame;
|
Chris@0
|
207 for (size_t c = 0; c < m_bufferCount; ++c) {
|
Chris@0
|
208 getRingBuffer(c).reset();
|
Chris@0
|
209 if (m_converter) src_reset(m_converter);
|
Chris@0
|
210 }
|
Chris@0
|
211 m_mutex.unlock();
|
Chris@0
|
212 } else {
|
Chris@0
|
213 m_bufferedToFrame = startFrame;
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 m_audioGenerator->reset();
|
Chris@0
|
217
|
Chris@0
|
218 m_playing = true;
|
Chris@0
|
219 m_condition.wakeAll();
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 void
|
Chris@0
|
223 AudioCallbackPlaySource::stop()
|
Chris@0
|
224 {
|
Chris@0
|
225 m_playing = false;
|
Chris@0
|
226 m_condition.wakeAll();
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 void
|
Chris@0
|
230 AudioCallbackPlaySource::setTargetBlockSize(size_t size)
|
Chris@0
|
231 {
|
Chris@0
|
232 std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl;
|
Chris@0
|
233 m_blockSize = size;
|
Chris@0
|
234 for (size_t i = 0; i < m_bufferCount; ++i) {
|
Chris@0
|
235 getRingBuffer(i).resize(m_ringBufferSize);
|
Chris@0
|
236 }
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 size_t
|
Chris@0
|
240 AudioCallbackPlaySource::getTargetBlockSize() const
|
Chris@0
|
241 {
|
Chris@0
|
242 std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
|
Chris@0
|
243 return m_blockSize;
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 void
|
Chris@0
|
247 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
|
Chris@0
|
248 {
|
Chris@0
|
249 m_playLatency = latency;
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 size_t
|
Chris@0
|
253 AudioCallbackPlaySource::getTargetPlayLatency() const
|
Chris@0
|
254 {
|
Chris@0
|
255 return m_playLatency;
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 size_t
|
Chris@0
|
259 AudioCallbackPlaySource::getCurrentPlayingFrame()
|
Chris@0
|
260 {
|
Chris@0
|
261 bool resample = false;
|
Chris@0
|
262 double ratio = 1.0;
|
Chris@0
|
263
|
Chris@0
|
264 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@0
|
265 resample = true;
|
Chris@0
|
266 ratio = double(getSourceSampleRate()) / double(getTargetSampleRate());
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 size_t readSpace = 0;
|
Chris@0
|
270 for (size_t c = 0; c < getSourceChannelCount(); ++c) {
|
Chris@0
|
271 size_t spaceHere = getRingBuffer(c).getReadSpace();
|
Chris@0
|
272 if (c == 0 || spaceHere < readSpace) readSpace = spaceHere;
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 if (resample) {
|
Chris@0
|
276 readSpace = size_t(readSpace * ratio + 0.1);
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 size_t lastRequestedFrame = 0;
|
Chris@0
|
280 if (m_bufferedToFrame > readSpace) {
|
Chris@0
|
281 lastRequestedFrame = m_bufferedToFrame - readSpace;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 size_t framePlaying = lastRequestedFrame;
|
Chris@0
|
285
|
Chris@0
|
286 size_t latency = m_playLatency;
|
Chris@0
|
287 if (resample) latency = size_t(m_playLatency * ratio + 0.1);
|
Chris@0
|
288
|
Chris@0
|
289 TimeStretcherData *timeStretcher = m_timeStretcher;
|
Chris@0
|
290 if (timeStretcher) {
|
Chris@0
|
291 latency += timeStretcher->getStretcher(0)->getProcessingLatency();
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 if (framePlaying > latency) {
|
Chris@0
|
295 framePlaying = framePlaying - latency;
|
Chris@0
|
296 } else {
|
Chris@0
|
297 framePlaying = 0;
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
301 std::cout << "getCurrentPlayingFrame: readSpace " << readSpace << ", lastRequestedFrame " << lastRequestedFrame << ", framePlaying " << framePlaying << ", latency " << latency << std::endl;
|
Chris@0
|
302 #endif
|
Chris@0
|
303
|
Chris@0
|
304 return framePlaying;
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 void
|
Chris@0
|
308 AudioCallbackPlaySource::setOutputLevels(float left, float right)
|
Chris@0
|
309 {
|
Chris@0
|
310 m_outputLeft = left;
|
Chris@0
|
311 m_outputRight = right;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 bool
|
Chris@0
|
315 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
|
Chris@0
|
316 {
|
Chris@0
|
317 left = m_outputLeft;
|
Chris@0
|
318 right = m_outputRight;
|
Chris@0
|
319 return true;
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 void
|
Chris@0
|
323 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
|
Chris@0
|
324 {
|
Chris@0
|
325 m_targetSampleRate = sr;
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 size_t
|
Chris@0
|
329 AudioCallbackPlaySource::getTargetSampleRate() const
|
Chris@0
|
330 {
|
Chris@0
|
331 if (m_targetSampleRate) return m_targetSampleRate;
|
Chris@0
|
332 else return getSourceSampleRate();
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 size_t
|
Chris@0
|
336 AudioCallbackPlaySource::getSourceChannelCount() const
|
Chris@0
|
337 {
|
Chris@0
|
338 return m_bufferCount;
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 size_t
|
Chris@0
|
342 AudioCallbackPlaySource::getSourceSampleRate() const
|
Chris@0
|
343 {
|
Chris@0
|
344 return m_sourceSampleRate;
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 AudioCallbackPlaySource::TimeStretcherData::TimeStretcherData(size_t channels,
|
Chris@0
|
348 size_t factor,
|
Chris@0
|
349 size_t blockSize) :
|
Chris@0
|
350 m_factor(factor),
|
Chris@0
|
351 m_blockSize(blockSize)
|
Chris@0
|
352 {
|
Chris@0
|
353 std::cerr << "TimeStretcherData::TimeStretcherData(" << channels << ", " << factor << ", " << blockSize << ")" << std::endl;
|
Chris@0
|
354
|
Chris@0
|
355 for (size_t ch = 0; ch < channels; ++ch) {
|
Chris@0
|
356 m_stretcher[ch] = StretcherBuffer
|
Chris@0
|
357 //!!! We really need to measure performance and work out
|
Chris@0
|
358 //what sort of quality level to use -- or at least to
|
Chris@0
|
359 //allow the user to configure it
|
Chris@0
|
360 (new IntegerTimeStretcher(factor, blockSize, 128),
|
Chris@0
|
361 new double[blockSize * factor]);
|
Chris@0
|
362 }
|
Chris@0
|
363 m_stretchInputBuffer = new double[blockSize];
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 AudioCallbackPlaySource::TimeStretcherData::~TimeStretcherData()
|
Chris@0
|
367 {
|
Chris@0
|
368 std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl;
|
Chris@0
|
369
|
Chris@0
|
370 while (!m_stretcher.empty()) {
|
Chris@0
|
371 delete m_stretcher.begin()->second.first;
|
Chris@0
|
372 delete[] m_stretcher.begin()->second.second;
|
Chris@0
|
373 m_stretcher.erase(m_stretcher.begin());
|
Chris@0
|
374 }
|
Chris@0
|
375 delete m_stretchInputBuffer;
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 IntegerTimeStretcher *
|
Chris@0
|
379 AudioCallbackPlaySource::TimeStretcherData::getStretcher(size_t channel)
|
Chris@0
|
380 {
|
Chris@0
|
381 return m_stretcher[channel].first;
|
Chris@0
|
382 }
|
Chris@0
|
383
|
Chris@0
|
384 double *
|
Chris@0
|
385 AudioCallbackPlaySource::TimeStretcherData::getOutputBuffer(size_t channel)
|
Chris@0
|
386 {
|
Chris@0
|
387 return m_stretcher[channel].second;
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 double *
|
Chris@0
|
391 AudioCallbackPlaySource::TimeStretcherData::getInputBuffer()
|
Chris@0
|
392 {
|
Chris@0
|
393 return m_stretchInputBuffer;
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 void
|
Chris@0
|
397 AudioCallbackPlaySource::TimeStretcherData::run(size_t channel)
|
Chris@0
|
398 {
|
Chris@0
|
399 getStretcher(channel)->process(getInputBuffer(),
|
Chris@0
|
400 getOutputBuffer(channel),
|
Chris@0
|
401 m_blockSize);
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 void
|
Chris@0
|
405 AudioCallbackPlaySource::setSlowdownFactor(size_t factor)
|
Chris@0
|
406 {
|
Chris@0
|
407 // Avoid locks -- create, assign, mark old one for scavenging
|
Chris@0
|
408 // later (as a call to getSourceSamples may still be using it)
|
Chris@0
|
409
|
Chris@0
|
410 TimeStretcherData *existingStretcher = m_timeStretcher;
|
Chris@0
|
411
|
Chris@0
|
412 if (existingStretcher && existingStretcher->getFactor() == factor) {
|
Chris@0
|
413 return;
|
Chris@0
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 if (factor > 1) {
|
Chris@0
|
417 TimeStretcherData *newStretcher = new TimeStretcherData
|
Chris@0
|
418 (getSourceChannelCount(), factor, getTargetBlockSize());
|
Chris@0
|
419 m_slowdownCounter = 0;
|
Chris@0
|
420 m_timeStretcher = newStretcher;
|
Chris@0
|
421 } else {
|
Chris@0
|
422 m_timeStretcher = 0;
|
Chris@0
|
423 }
|
Chris@0
|
424
|
Chris@0
|
425 if (existingStretcher) {
|
Chris@0
|
426 m_timeStretcherScavenger.claim(existingStretcher);
|
Chris@0
|
427 }
|
Chris@0
|
428 }
|
Chris@0
|
429
|
Chris@0
|
430 size_t
|
Chris@0
|
431 AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer)
|
Chris@0
|
432 {
|
Chris@0
|
433 if (!m_playing) {
|
Chris@0
|
434 for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) {
|
Chris@0
|
435 for (size_t i = 0; i < count; ++i) {
|
Chris@0
|
436 buffer[ch][i] = 0.0;
|
Chris@0
|
437 }
|
Chris@0
|
438 }
|
Chris@0
|
439 return 0;
|
Chris@0
|
440 }
|
Chris@0
|
441
|
Chris@0
|
442 TimeStretcherData *timeStretcher = m_timeStretcher;
|
Chris@0
|
443
|
Chris@0
|
444 if (!timeStretcher || timeStretcher->getFactor() == 1) {
|
Chris@0
|
445
|
Chris@0
|
446 size_t got = 0;
|
Chris@0
|
447
|
Chris@0
|
448 for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) {
|
Chris@0
|
449
|
Chris@0
|
450 RingBuffer<float> &rb = *m_buffers[ch];
|
Chris@0
|
451
|
Chris@0
|
452 // this is marginally more likely to leave our channels in
|
Chris@0
|
453 // sync after a processing failure than just passing "count":
|
Chris@0
|
454 size_t request = count;
|
Chris@0
|
455 if (ch > 0) request = got;
|
Chris@0
|
456
|
Chris@0
|
457 got = rb.read(buffer[ch], request);
|
Chris@0
|
458
|
Chris@0
|
459 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
460 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", signalling for more (possibly)" << std::endl;
|
Chris@0
|
461 #endif
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464 for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) {
|
Chris@0
|
465 for (size_t i = got; i < count; ++i) {
|
Chris@0
|
466 buffer[ch][i] = 0.0;
|
Chris@0
|
467 }
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 m_condition.wakeAll();
|
Chris@0
|
471 return got;
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 if (m_slowdownCounter == 0) {
|
Chris@0
|
475
|
Chris@0
|
476 size_t got = 0;
|
Chris@0
|
477 double *ib = timeStretcher->getInputBuffer();
|
Chris@0
|
478
|
Chris@0
|
479 for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) {
|
Chris@0
|
480
|
Chris@0
|
481 RingBuffer<float> &rb = *m_buffers[ch];
|
Chris@0
|
482 size_t request = count;
|
Chris@0
|
483 if (ch > 0) request = got; // see above
|
Chris@0
|
484 got = rb.read(buffer[ch], request);
|
Chris@0
|
485
|
Chris@0
|
486 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
487 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", running time stretcher" << std::endl;
|
Chris@0
|
488 #endif
|
Chris@0
|
489
|
Chris@0
|
490 for (size_t i = 0; i < count; ++i) {
|
Chris@0
|
491 ib[i] = buffer[ch][i];
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 timeStretcher->run(ch);
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 } else if (m_slowdownCounter >= timeStretcher->getFactor()) {
|
Chris@0
|
498 // reset this in case the factor has changed leaving the
|
Chris@0
|
499 // counter out of range
|
Chris@0
|
500 m_slowdownCounter = 0;
|
Chris@0
|
501 }
|
Chris@0
|
502
|
Chris@0
|
503 for (size_t ch = 0; ch < getSourceChannelCount(); ++ch) {
|
Chris@0
|
504
|
Chris@0
|
505 double *ob = timeStretcher->getOutputBuffer(ch);
|
Chris@0
|
506
|
Chris@0
|
507 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
508 std::cerr << "AudioCallbackPlaySource::getSamples: Copying from (" << (m_slowdownCounter * count) << "," << count << ") to buffer" << std::endl;
|
Chris@0
|
509 #endif
|
Chris@0
|
510
|
Chris@0
|
511 for (size_t i = 0; i < count; ++i) {
|
Chris@0
|
512 buffer[ch][i] = ob[m_slowdownCounter * count + i];
|
Chris@0
|
513 }
|
Chris@0
|
514 }
|
Chris@0
|
515
|
Chris@0
|
516 if (m_slowdownCounter == 0) m_condition.wakeAll();
|
Chris@0
|
517 m_slowdownCounter = (m_slowdownCounter + 1) % timeStretcher->getFactor();
|
Chris@0
|
518 return count;
|
Chris@0
|
519 }
|
Chris@0
|
520
|
Chris@0
|
521 void
|
Chris@0
|
522 AudioCallbackPlaySource::fillBuffers()
|
Chris@0
|
523 {
|
Chris@0
|
524 static float *tmp = 0;
|
Chris@0
|
525 static size_t tmpSize = 0;
|
Chris@0
|
526
|
Chris@0
|
527 size_t space = 0;
|
Chris@0
|
528 for (size_t c = 0; c < m_bufferCount; ++c) {
|
Chris@0
|
529 size_t spaceHere = getRingBuffer(c).getWriteSpace();
|
Chris@0
|
530 if (c == 0 || spaceHere < space) space = spaceHere;
|
Chris@0
|
531 }
|
Chris@0
|
532
|
Chris@0
|
533 if (space == 0) return;
|
Chris@0
|
534
|
Chris@0
|
535 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
536 std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl;
|
Chris@0
|
537 #endif
|
Chris@0
|
538
|
Chris@0
|
539 size_t f = m_bufferedToFrame;
|
Chris@0
|
540
|
Chris@0
|
541 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
542 std::cout << "buffered to " << f << " already" << std::endl;
|
Chris@0
|
543 #endif
|
Chris@0
|
544
|
Chris@0
|
545 bool resample = (getSourceSampleRate() != getTargetSampleRate());
|
Chris@0
|
546 size_t channels = getSourceChannelCount();
|
Chris@0
|
547 size_t orig = space;
|
Chris@0
|
548 size_t got = 0;
|
Chris@0
|
549
|
Chris@0
|
550 static float **bufferPtrs = 0;
|
Chris@0
|
551 static size_t bufferPtrCount = 0;
|
Chris@0
|
552
|
Chris@0
|
553 if (bufferPtrCount < channels) {
|
Chris@0
|
554 if (bufferPtrs) delete[] bufferPtrs;
|
Chris@0
|
555 bufferPtrs = new float *[channels];
|
Chris@0
|
556 bufferPtrCount = channels;
|
Chris@0
|
557 }
|
Chris@0
|
558
|
Chris@0
|
559 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
|
Chris@0
|
560
|
Chris@0
|
561 if (resample && m_converter) {
|
Chris@0
|
562
|
Chris@0
|
563 double ratio =
|
Chris@0
|
564 double(getTargetSampleRate()) / double(getSourceSampleRate());
|
Chris@0
|
565 orig = size_t(orig / ratio + 0.1);
|
Chris@0
|
566
|
Chris@0
|
567 // orig must be a multiple of generatorBlockSize
|
Chris@0
|
568 orig = (orig / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
569 if (orig == 0) return;
|
Chris@0
|
570
|
Chris@0
|
571 size_t work = std::max(orig, space);
|
Chris@0
|
572
|
Chris@0
|
573 // We only allocate one buffer, but we use it in two halves.
|
Chris@0
|
574 // We place the non-interleaved values in the second half of
|
Chris@0
|
575 // the buffer (orig samples for channel 0, orig samples for
|
Chris@0
|
576 // channel 1 etc), and then interleave them into the first
|
Chris@0
|
577 // half of the buffer. Then we resample back into the second
|
Chris@0
|
578 // half (interleaved) and de-interleave the results back to
|
Chris@0
|
579 // the start of the buffer for insertion into the ringbuffers.
|
Chris@0
|
580 // What a faff -- especially as we've already de-interleaved
|
Chris@0
|
581 // the audio data from the source file elsewhere before we
|
Chris@0
|
582 // even reach this point.
|
Chris@0
|
583
|
Chris@0
|
584 if (tmpSize < channels * work * 2) {
|
Chris@0
|
585 delete[] tmp;
|
Chris@0
|
586 tmp = new float[channels * work * 2];
|
Chris@0
|
587 tmpSize = channels * work * 2;
|
Chris@0
|
588 }
|
Chris@0
|
589
|
Chris@0
|
590 float *nonintlv = tmp + channels * work;
|
Chris@0
|
591 float *intlv = tmp;
|
Chris@0
|
592 float *srcout = tmp + channels * work;
|
Chris@0
|
593
|
Chris@0
|
594 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
595 for (size_t i = 0; i < orig; ++i) {
|
Chris@0
|
596 nonintlv[channels * i + c] = 0.0f;
|
Chris@0
|
597 }
|
Chris@0
|
598 }
|
Chris@0
|
599
|
Chris@0
|
600 for (std::set<Model *>::iterator mi = m_models.begin();
|
Chris@0
|
601 mi != m_models.end(); ++mi) {
|
Chris@0
|
602
|
Chris@0
|
603 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
604 bufferPtrs[c] = nonintlv + c * orig;
|
Chris@0
|
605 }
|
Chris@0
|
606
|
Chris@0
|
607 size_t gotHere = m_audioGenerator->mixModel
|
Chris@0
|
608 (*mi, f, orig, bufferPtrs);
|
Chris@0
|
609
|
Chris@0
|
610 got = std::max(got, gotHere);
|
Chris@0
|
611 }
|
Chris@0
|
612
|
Chris@0
|
613 // and interleave into first half
|
Chris@0
|
614 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
615 for (size_t i = 0; i < orig; ++i) {
|
Chris@0
|
616 float sample = 0;
|
Chris@0
|
617 if (i < got) {
|
Chris@0
|
618 sample = nonintlv[c * orig + i];
|
Chris@0
|
619 }
|
Chris@0
|
620 intlv[channels * i + c] = sample;
|
Chris@0
|
621 }
|
Chris@0
|
622 }
|
Chris@0
|
623
|
Chris@0
|
624 SRC_DATA data;
|
Chris@0
|
625 data.data_in = intlv;
|
Chris@0
|
626 data.data_out = srcout;
|
Chris@0
|
627 data.input_frames = orig;
|
Chris@0
|
628 data.output_frames = work;
|
Chris@0
|
629 data.src_ratio = ratio;
|
Chris@0
|
630 data.end_of_input = 0;
|
Chris@0
|
631
|
Chris@0
|
632 int err = src_process(m_converter, &data);
|
Chris@0
|
633 size_t toCopy = size_t(work * ratio + 0.1);
|
Chris@0
|
634
|
Chris@0
|
635 if (err) {
|
Chris@0
|
636 std::cerr
|
Chris@0
|
637 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
|
Chris@0
|
638 << src_strerror(err) << std::endl;
|
Chris@0
|
639 //!!! Then what?
|
Chris@0
|
640 } else {
|
Chris@0
|
641 got = data.input_frames_used;
|
Chris@0
|
642 toCopy = data.output_frames_gen;
|
Chris@0
|
643 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
644 std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
|
Chris@0
|
645 #endif
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
649 for (size_t i = 0; i < toCopy; ++i) {
|
Chris@0
|
650 tmp[i] = srcout[channels * i + c];
|
Chris@0
|
651 }
|
Chris@0
|
652 getRingBuffer(c).write(tmp, toCopy);
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 } else {
|
Chris@0
|
656
|
Chris@0
|
657 // space must be a multiple of generatorBlockSize
|
Chris@0
|
658 space = (space / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
659 if (space == 0) return;
|
Chris@0
|
660
|
Chris@0
|
661 if (tmpSize < channels * space) {
|
Chris@0
|
662 delete[] tmp;
|
Chris@0
|
663 tmp = new float[channels * space];
|
Chris@0
|
664 tmpSize = channels * space;
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
668
|
Chris@0
|
669 bufferPtrs[c] = tmp + c * space;
|
Chris@0
|
670
|
Chris@0
|
671 for (size_t i = 0; i < space; ++i) {
|
Chris@0
|
672 tmp[c * space + i] = 0.0f;
|
Chris@0
|
673 }
|
Chris@0
|
674 }
|
Chris@0
|
675
|
Chris@0
|
676 for (std::set<Model *>::iterator mi = m_models.begin();
|
Chris@0
|
677 mi != m_models.end(); ++mi) {
|
Chris@0
|
678
|
Chris@0
|
679 got = m_audioGenerator->mixModel
|
Chris@0
|
680 (*mi, f, space, bufferPtrs);
|
Chris@0
|
681 }
|
Chris@0
|
682
|
Chris@0
|
683 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
684
|
Chris@0
|
685 got = getRingBuffer(c).write(bufferPtrs[c], space);
|
Chris@0
|
686
|
Chris@0
|
687 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
688 std::cerr << "Wrote " << got << " frames for ch " << c << ", now "
|
Chris@0
|
689 << getRingBuffer(c).getReadSpace() << " to read"
|
Chris@0
|
690 << std::endl;
|
Chris@0
|
691 #endif
|
Chris@0
|
692 }
|
Chris@0
|
693 }
|
Chris@0
|
694
|
Chris@0
|
695 m_bufferedToFrame = f + got;
|
Chris@0
|
696 }
|
Chris@0
|
697
|
Chris@0
|
698 void
|
Chris@0
|
699 AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run()
|
Chris@0
|
700 {
|
Chris@0
|
701 AudioCallbackPlaySource &s(m_source);
|
Chris@0
|
702
|
Chris@0
|
703 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
704 std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl;
|
Chris@0
|
705 #endif
|
Chris@0
|
706
|
Chris@0
|
707 s.m_mutex.lock();
|
Chris@0
|
708
|
Chris@0
|
709 bool previouslyPlaying = s.m_playing;
|
Chris@0
|
710
|
Chris@0
|
711 while (!s.m_exiting) {
|
Chris@0
|
712
|
Chris@0
|
713 s.m_timeStretcherScavenger.scavenge();
|
Chris@0
|
714
|
Chris@0
|
715 float ms = 100;
|
Chris@0
|
716 if (s.getSourceSampleRate() > 0) {
|
Chris@0
|
717 ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0;
|
Chris@0
|
718 }
|
Chris@0
|
719
|
Chris@0
|
720 if (!s.m_playing) ms *= 10;
|
Chris@0
|
721
|
Chris@0
|
722 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
723 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms/4 << "ms..." << std::endl;
|
Chris@0
|
724 #endif
|
Chris@0
|
725
|
Chris@0
|
726 s.m_condition.wait(&s.m_mutex, size_t(ms / 4));
|
Chris@0
|
727
|
Chris@0
|
728 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
729 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
|
Chris@0
|
730 #endif
|
Chris@0
|
731
|
Chris@0
|
732 if (!s.getSourceSampleRate()) continue;
|
Chris@0
|
733
|
Chris@0
|
734 bool playing = s.m_playing;
|
Chris@0
|
735
|
Chris@0
|
736 if (playing && !previouslyPlaying) {
|
Chris@0
|
737 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
738 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
|
Chris@0
|
739 #endif
|
Chris@0
|
740 for (size_t c = 0; c < s.getSourceChannelCount(); ++c) {
|
Chris@0
|
741 s.getRingBuffer(c).reset();
|
Chris@0
|
742 }
|
Chris@0
|
743 }
|
Chris@0
|
744 previouslyPlaying = playing;
|
Chris@0
|
745
|
Chris@0
|
746 if (!playing) continue;
|
Chris@0
|
747
|
Chris@0
|
748 s.fillBuffers();
|
Chris@0
|
749 }
|
Chris@0
|
750
|
Chris@0
|
751 s.m_mutex.unlock();
|
Chris@0
|
752 }
|
Chris@0
|
753
|
Chris@0
|
754
|
Chris@0
|
755
|
Chris@0
|
756 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
757 #include "AudioCallbackPlaySource.moc.cpp"
|
Chris@0
|
758 #endif
|
Chris@0
|
759
|