annotate audioio/AudioCallbackPlaySource.cpp @ 5:2edc0757ca75

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