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@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 if (m_converter) {
|
Chris@0
|
122 src_delete(m_converter);
|
Chris@0
|
123 m_converter = 0;
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 m_audioGenerator->addModel(model);
|
Chris@0
|
128
|
Chris@0
|
129 m_mutex.unlock();
|
Chris@0
|
130
|
Chris@0
|
131 if (!m_fillThread) {
|
Chris@0
|
132 m_fillThread = new AudioCallbackPlaySourceFillThread(*this);
|
Chris@0
|
133 m_fillThread->start();
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
137 std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl;
|
Chris@0
|
138 #endif
|
Chris@0
|
139
|
Chris@1
|
140 if (buffersChanged || srChanged) {
|
Chris@1
|
141 emit modelReplaced();
|
Chris@0
|
142 }
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 void
|
Chris@0
|
146 AudioCallbackPlaySource::removeModel(Model *model)
|
Chris@0
|
147 {
|
Chris@0
|
148 m_mutex.lock();
|
Chris@0
|
149
|
Chris@0
|
150 m_models.erase(model);
|
Chris@0
|
151
|
Chris@0
|
152 if (m_models.empty()) {
|
Chris@0
|
153 if (m_converter) {
|
Chris@0
|
154 src_delete(m_converter);
|
Chris@0
|
155 m_converter = 0;
|
Chris@0
|
156 }
|
Chris@0
|
157 m_sourceSampleRate = 0;
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 m_audioGenerator->removeModel(model);
|
Chris@0
|
161
|
Chris@0
|
162 m_mutex.unlock();
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 void
|
Chris@0
|
166 AudioCallbackPlaySource::clearModels()
|
Chris@0
|
167 {
|
Chris@0
|
168 m_mutex.lock();
|
Chris@0
|
169
|
Chris@0
|
170 m_models.clear();
|
Chris@0
|
171
|
Chris@0
|
172 if (m_converter) {
|
Chris@0
|
173 src_delete(m_converter);
|
Chris@0
|
174 m_converter = 0;
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 m_audioGenerator->clearModels();
|
Chris@0
|
178
|
Chris@0
|
179 m_sourceSampleRate = 0;
|
Chris@0
|
180
|
Chris@0
|
181 m_mutex.unlock();
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 void
|
Chris@0
|
185 AudioCallbackPlaySource::play(size_t startFrame)
|
Chris@0
|
186 {
|
Chris@0
|
187 // The fill thread will automatically empty its buffers before
|
Chris@0
|
188 // starting again if we have not so far been playing, but not if
|
Chris@0
|
189 // we're just re-seeking.
|
Chris@0
|
190
|
Chris@0
|
191 if (m_playing) {
|
Chris@0
|
192 m_mutex.lock();
|
Chris@0
|
193 m_bufferedToFrame = startFrame;
|
Chris@0
|
194 for (size_t c = 0; c < m_bufferCount; ++c) {
|
Chris@0
|
195 getRingBuffer(c).reset();
|
Chris@0
|
196 if (m_converter) src_reset(m_converter);
|
Chris@0
|
197 }
|
Chris@0
|
198 m_mutex.unlock();
|
Chris@0
|
199 } else {
|
Chris@0
|
200 m_bufferedToFrame = startFrame;
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 m_audioGenerator->reset();
|
Chris@0
|
204
|
Chris@0
|
205 m_playing = true;
|
Chris@0
|
206 m_condition.wakeAll();
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 void
|
Chris@0
|
210 AudioCallbackPlaySource::stop()
|
Chris@0
|
211 {
|
Chris@0
|
212 m_playing = false;
|
Chris@0
|
213 m_condition.wakeAll();
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 void
|
Chris@0
|
217 AudioCallbackPlaySource::setTargetBlockSize(size_t size)
|
Chris@0
|
218 {
|
Chris@0
|
219 std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl;
|
Chris@0
|
220 m_blockSize = size;
|
Chris@0
|
221 for (size_t i = 0; i < m_bufferCount; ++i) {
|
Chris@0
|
222 getRingBuffer(i).resize(m_ringBufferSize);
|
Chris@0
|
223 }
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 size_t
|
Chris@0
|
227 AudioCallbackPlaySource::getTargetBlockSize() const
|
Chris@0
|
228 {
|
Chris@0
|
229 std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
|
Chris@0
|
230 return m_blockSize;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 void
|
Chris@0
|
234 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
|
Chris@0
|
235 {
|
Chris@0
|
236 m_playLatency = latency;
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 size_t
|
Chris@0
|
240 AudioCallbackPlaySource::getTargetPlayLatency() const
|
Chris@0
|
241 {
|
Chris@0
|
242 return m_playLatency;
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 size_t
|
Chris@0
|
246 AudioCallbackPlaySource::getCurrentPlayingFrame()
|
Chris@0
|
247 {
|
Chris@0
|
248 bool resample = false;
|
Chris@0
|
249 double ratio = 1.0;
|
Chris@0
|
250
|
Chris@0
|
251 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@0
|
252 resample = true;
|
Chris@0
|
253 ratio = double(getSourceSampleRate()) / double(getTargetSampleRate());
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 size_t readSpace = 0;
|
Chris@0
|
257 for (size_t c = 0; c < getSourceChannelCount(); ++c) {
|
Chris@0
|
258 size_t spaceHere = getRingBuffer(c).getReadSpace();
|
Chris@0
|
259 if (c == 0 || spaceHere < readSpace) readSpace = spaceHere;
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@0
|
262 if (resample) {
|
Chris@0
|
263 readSpace = size_t(readSpace * ratio + 0.1);
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 size_t lastRequestedFrame = 0;
|
Chris@0
|
267 if (m_bufferedToFrame > readSpace) {
|
Chris@0
|
268 lastRequestedFrame = m_bufferedToFrame - readSpace;
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 size_t framePlaying = lastRequestedFrame;
|
Chris@0
|
272
|
Chris@0
|
273 size_t latency = m_playLatency;
|
Chris@0
|
274 if (resample) latency = size_t(m_playLatency * ratio + 0.1);
|
Chris@0
|
275
|
Chris@0
|
276 TimeStretcherData *timeStretcher = m_timeStretcher;
|
Chris@0
|
277 if (timeStretcher) {
|
Chris@0
|
278 latency += timeStretcher->getStretcher(0)->getProcessingLatency();
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 if (framePlaying > latency) {
|
Chris@0
|
282 framePlaying = framePlaying - latency;
|
Chris@0
|
283 } else {
|
Chris@0
|
284 framePlaying = 0;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
288 std::cout << "getCurrentPlayingFrame: readSpace " << readSpace << ", lastRequestedFrame " << lastRequestedFrame << ", framePlaying " << framePlaying << ", latency " << latency << std::endl;
|
Chris@0
|
289 #endif
|
Chris@0
|
290
|
Chris@0
|
291 return framePlaying;
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 void
|
Chris@0
|
295 AudioCallbackPlaySource::setOutputLevels(float left, float right)
|
Chris@0
|
296 {
|
Chris@0
|
297 m_outputLeft = left;
|
Chris@0
|
298 m_outputRight = right;
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 bool
|
Chris@0
|
302 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
|
Chris@0
|
303 {
|
Chris@0
|
304 left = m_outputLeft;
|
Chris@0
|
305 right = m_outputRight;
|
Chris@0
|
306 return true;
|
Chris@0
|
307 }
|
Chris@0
|
308
|
Chris@0
|
309 void
|
Chris@0
|
310 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
|
Chris@0
|
311 {
|
Chris@0
|
312 m_targetSampleRate = sr;
|
Chris@1
|
313
|
Chris@1
|
314 if (getSourceSampleRate() != getTargetSampleRate()) {
|
Chris@1
|
315
|
Chris@1
|
316 int err = 0;
|
Chris@1
|
317 m_converter = src_new(SRC_SINC_BEST_QUALITY, m_bufferCount, &err);
|
Chris@1
|
318 if (!m_converter) {
|
Chris@1
|
319 std::cerr
|
Chris@1
|
320 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
|
Chris@1
|
321 << src_strerror(err) << std::endl;
|
Chris@1
|
322 }
|
Chris@1
|
323
|
Chris@1
|
324 emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate());
|
Chris@1
|
325 }
|
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@1
|
546
|
Chris@1
|
547 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@1
|
548 std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
|
Chris@1
|
549 #endif
|
Chris@1
|
550
|
Chris@0
|
551 size_t channels = getSourceChannelCount();
|
Chris@0
|
552 size_t orig = space;
|
Chris@0
|
553 size_t got = 0;
|
Chris@0
|
554
|
Chris@0
|
555 static float **bufferPtrs = 0;
|
Chris@0
|
556 static size_t bufferPtrCount = 0;
|
Chris@0
|
557
|
Chris@0
|
558 if (bufferPtrCount < channels) {
|
Chris@0
|
559 if (bufferPtrs) delete[] bufferPtrs;
|
Chris@0
|
560 bufferPtrs = new float *[channels];
|
Chris@0
|
561 bufferPtrCount = channels;
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
|
Chris@0
|
565
|
Chris@1
|
566 if (resample && !m_converter) {
|
Chris@1
|
567 static bool warned = false;
|
Chris@1
|
568 if (!warned) {
|
Chris@1
|
569 std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
|
Chris@1
|
570 warned = true;
|
Chris@1
|
571 }
|
Chris@1
|
572 }
|
Chris@1
|
573
|
Chris@0
|
574 if (resample && m_converter) {
|
Chris@0
|
575
|
Chris@0
|
576 double ratio =
|
Chris@0
|
577 double(getTargetSampleRate()) / double(getSourceSampleRate());
|
Chris@0
|
578 orig = size_t(orig / ratio + 0.1);
|
Chris@0
|
579
|
Chris@0
|
580 // orig must be a multiple of generatorBlockSize
|
Chris@0
|
581 orig = (orig / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
582 if (orig == 0) return;
|
Chris@0
|
583
|
Chris@0
|
584 size_t work = std::max(orig, space);
|
Chris@0
|
585
|
Chris@0
|
586 // We only allocate one buffer, but we use it in two halves.
|
Chris@0
|
587 // We place the non-interleaved values in the second half of
|
Chris@0
|
588 // the buffer (orig samples for channel 0, orig samples for
|
Chris@0
|
589 // channel 1 etc), and then interleave them into the first
|
Chris@0
|
590 // half of the buffer. Then we resample back into the second
|
Chris@0
|
591 // half (interleaved) and de-interleave the results back to
|
Chris@0
|
592 // the start of the buffer for insertion into the ringbuffers.
|
Chris@0
|
593 // What a faff -- especially as we've already de-interleaved
|
Chris@0
|
594 // the audio data from the source file elsewhere before we
|
Chris@0
|
595 // even reach this point.
|
Chris@0
|
596
|
Chris@0
|
597 if (tmpSize < channels * work * 2) {
|
Chris@0
|
598 delete[] tmp;
|
Chris@0
|
599 tmp = new float[channels * work * 2];
|
Chris@0
|
600 tmpSize = channels * work * 2;
|
Chris@0
|
601 }
|
Chris@0
|
602
|
Chris@0
|
603 float *nonintlv = tmp + channels * work;
|
Chris@0
|
604 float *intlv = tmp;
|
Chris@0
|
605 float *srcout = tmp + channels * work;
|
Chris@0
|
606
|
Chris@0
|
607 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
608 for (size_t i = 0; i < orig; ++i) {
|
Chris@0
|
609 nonintlv[channels * i + c] = 0.0f;
|
Chris@0
|
610 }
|
Chris@0
|
611 }
|
Chris@0
|
612
|
Chris@0
|
613 for (std::set<Model *>::iterator mi = m_models.begin();
|
Chris@0
|
614 mi != m_models.end(); ++mi) {
|
Chris@0
|
615
|
Chris@0
|
616 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
617 bufferPtrs[c] = nonintlv + c * orig;
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 size_t gotHere = m_audioGenerator->mixModel
|
Chris@0
|
621 (*mi, f, orig, bufferPtrs);
|
Chris@0
|
622
|
Chris@0
|
623 got = std::max(got, gotHere);
|
Chris@0
|
624 }
|
Chris@0
|
625
|
Chris@0
|
626 // and interleave into first half
|
Chris@0
|
627 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
628 for (size_t i = 0; i < orig; ++i) {
|
Chris@0
|
629 float sample = 0;
|
Chris@0
|
630 if (i < got) {
|
Chris@0
|
631 sample = nonintlv[c * orig + i];
|
Chris@0
|
632 }
|
Chris@0
|
633 intlv[channels * i + c] = sample;
|
Chris@0
|
634 }
|
Chris@0
|
635 }
|
Chris@0
|
636
|
Chris@0
|
637 SRC_DATA data;
|
Chris@0
|
638 data.data_in = intlv;
|
Chris@0
|
639 data.data_out = srcout;
|
Chris@0
|
640 data.input_frames = orig;
|
Chris@0
|
641 data.output_frames = work;
|
Chris@0
|
642 data.src_ratio = ratio;
|
Chris@0
|
643 data.end_of_input = 0;
|
Chris@0
|
644
|
Chris@0
|
645 int err = src_process(m_converter, &data);
|
Chris@0
|
646 size_t toCopy = size_t(work * ratio + 0.1);
|
Chris@0
|
647
|
Chris@0
|
648 if (err) {
|
Chris@0
|
649 std::cerr
|
Chris@0
|
650 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
|
Chris@0
|
651 << src_strerror(err) << std::endl;
|
Chris@0
|
652 //!!! Then what?
|
Chris@0
|
653 } else {
|
Chris@0
|
654 got = data.input_frames_used;
|
Chris@0
|
655 toCopy = data.output_frames_gen;
|
Chris@0
|
656 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
657 std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
|
Chris@0
|
658 #endif
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
662 for (size_t i = 0; i < toCopy; ++i) {
|
Chris@0
|
663 tmp[i] = srcout[channels * i + c];
|
Chris@0
|
664 }
|
Chris@0
|
665 getRingBuffer(c).write(tmp, toCopy);
|
Chris@0
|
666 }
|
Chris@0
|
667
|
Chris@0
|
668 } else {
|
Chris@0
|
669
|
Chris@0
|
670 // space must be a multiple of generatorBlockSize
|
Chris@0
|
671 space = (space / generatorBlockSize) * generatorBlockSize;
|
Chris@0
|
672 if (space == 0) return;
|
Chris@0
|
673
|
Chris@0
|
674 if (tmpSize < channels * space) {
|
Chris@0
|
675 delete[] tmp;
|
Chris@0
|
676 tmp = new float[channels * space];
|
Chris@0
|
677 tmpSize = channels * space;
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
681
|
Chris@0
|
682 bufferPtrs[c] = tmp + c * space;
|
Chris@0
|
683
|
Chris@0
|
684 for (size_t i = 0; i < space; ++i) {
|
Chris@0
|
685 tmp[c * space + i] = 0.0f;
|
Chris@0
|
686 }
|
Chris@0
|
687 }
|
Chris@0
|
688
|
Chris@0
|
689 for (std::set<Model *>::iterator mi = m_models.begin();
|
Chris@0
|
690 mi != m_models.end(); ++mi) {
|
Chris@0
|
691
|
Chris@0
|
692 got = m_audioGenerator->mixModel
|
Chris@0
|
693 (*mi, f, space, bufferPtrs);
|
Chris@0
|
694 }
|
Chris@0
|
695
|
Chris@0
|
696 for (size_t c = 0; c < channels; ++c) {
|
Chris@0
|
697
|
Chris@0
|
698 got = getRingBuffer(c).write(bufferPtrs[c], space);
|
Chris@0
|
699
|
Chris@0
|
700 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
701 std::cerr << "Wrote " << got << " frames for ch " << c << ", now "
|
Chris@0
|
702 << getRingBuffer(c).getReadSpace() << " to read"
|
Chris@0
|
703 << std::endl;
|
Chris@0
|
704 #endif
|
Chris@0
|
705 }
|
Chris@0
|
706 }
|
Chris@0
|
707
|
Chris@0
|
708 m_bufferedToFrame = f + got;
|
Chris@0
|
709 }
|
Chris@0
|
710
|
Chris@0
|
711 void
|
Chris@0
|
712 AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run()
|
Chris@0
|
713 {
|
Chris@0
|
714 AudioCallbackPlaySource &s(m_source);
|
Chris@0
|
715
|
Chris@0
|
716 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
717 std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl;
|
Chris@0
|
718 #endif
|
Chris@0
|
719
|
Chris@0
|
720 s.m_mutex.lock();
|
Chris@0
|
721
|
Chris@0
|
722 bool previouslyPlaying = s.m_playing;
|
Chris@0
|
723
|
Chris@0
|
724 while (!s.m_exiting) {
|
Chris@0
|
725
|
Chris@0
|
726 s.m_timeStretcherScavenger.scavenge();
|
Chris@0
|
727
|
Chris@0
|
728 float ms = 100;
|
Chris@0
|
729 if (s.getSourceSampleRate() > 0) {
|
Chris@0
|
730 ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0;
|
Chris@0
|
731 }
|
Chris@0
|
732
|
Chris@0
|
733 if (!s.m_playing) ms *= 10;
|
Chris@0
|
734
|
Chris@0
|
735 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
736 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms/4 << "ms..." << std::endl;
|
Chris@0
|
737 #endif
|
Chris@0
|
738
|
Chris@0
|
739 s.m_condition.wait(&s.m_mutex, size_t(ms / 4));
|
Chris@0
|
740
|
Chris@0
|
741 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
742 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
|
Chris@0
|
743 #endif
|
Chris@0
|
744
|
Chris@0
|
745 if (!s.getSourceSampleRate()) continue;
|
Chris@0
|
746
|
Chris@0
|
747 bool playing = s.m_playing;
|
Chris@0
|
748
|
Chris@0
|
749 if (playing && !previouslyPlaying) {
|
Chris@0
|
750 #ifdef DEBUG_AUDIO_PLAY_SOURCE
|
Chris@0
|
751 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
|
Chris@0
|
752 #endif
|
Chris@0
|
753 for (size_t c = 0; c < s.getSourceChannelCount(); ++c) {
|
Chris@0
|
754 s.getRingBuffer(c).reset();
|
Chris@0
|
755 }
|
Chris@0
|
756 }
|
Chris@0
|
757 previouslyPlaying = playing;
|
Chris@0
|
758
|
Chris@0
|
759 if (!playing) continue;
|
Chris@0
|
760
|
Chris@0
|
761 s.fillBuffers();
|
Chris@0
|
762 }
|
Chris@0
|
763
|
Chris@0
|
764 s.m_mutex.unlock();
|
Chris@0
|
765 }
|
Chris@0
|
766
|
Chris@0
|
767
|
Chris@0
|
768
|
Chris@0
|
769 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
770 #include "AudioCallbackPlaySource.moc.cpp"
|
Chris@0
|
771 #endif
|
Chris@0
|
772
|