annotate audioio/AudioCallbackPlaySource.cpp @ 200:2c33d6bbea15

* Set LIBS properly when optional pkg-config package found
author Chris Cannam
date Wed, 22 Sep 2010 12:28:37 +0100
parents d9c21e7bff21
children 5ee9e6bc21eb
rev   line source
Chris@43 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@43 2
Chris@43 3 /*
Chris@43 4 Sonic Visualiser
Chris@43 5 An audio file viewer and annotation editor.
Chris@43 6 Centre for Digital Music, Queen Mary, University of London.
Chris@43 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@43 8
Chris@43 9 This program is free software; you can redistribute it and/or
Chris@43 10 modify it under the terms of the GNU General Public License as
Chris@43 11 published by the Free Software Foundation; either version 2 of the
Chris@43 12 License, or (at your option) any later version. See the file
Chris@43 13 COPYING included with this distribution for more information.
Chris@43 14 */
Chris@43 15
Chris@43 16 #include "AudioCallbackPlaySource.h"
Chris@43 17
Chris@43 18 #include "AudioGenerator.h"
Chris@43 19
Chris@43 20 #include "data/model/Model.h"
Chris@105 21 #include "base/ViewManagerBase.h"
Chris@43 22 #include "base/PlayParameterRepository.h"
Chris@43 23 #include "base/Preferences.h"
Chris@43 24 #include "data/model/DenseTimeValueModel.h"
Chris@43 25 #include "data/model/WaveFileModel.h"
Chris@43 26 #include "data/model/SparseOneDimensionalModel.h"
Chris@43 27 #include "plugin/RealTimePluginInstance.h"
Chris@62 28
Chris@91 29 #include "AudioCallbackPlayTarget.h"
Chris@91 30
Chris@62 31 #include <rubberband/RubberBandStretcher.h>
Chris@62 32 using namespace RubberBand;
Chris@43 33
Chris@43 34 #include <iostream>
Chris@43 35 #include <cassert>
Chris@43 36
Chris@174 37 //#define DEBUG_AUDIO_PLAY_SOURCE 1
Chris@43 38 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
Chris@43 39
Chris@193 40 static const size_t DEFAULT_RING_BUFFER_SIZE = 131071;
Chris@43 41
Chris@105 42 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManagerBase *manager,
Chris@57 43 QString clientName) :
Chris@43 44 m_viewManager(manager),
Chris@43 45 m_audioGenerator(new AudioGenerator()),
Chris@57 46 m_clientName(clientName),
Chris@43 47 m_readBuffers(0),
Chris@43 48 m_writeBuffers(0),
Chris@43 49 m_readBufferFill(0),
Chris@43 50 m_writeBufferFill(0),
Chris@43 51 m_bufferScavenger(1),
Chris@43 52 m_sourceChannelCount(0),
Chris@43 53 m_blockSize(1024),
Chris@43 54 m_sourceSampleRate(0),
Chris@43 55 m_targetSampleRate(0),
Chris@43 56 m_playLatency(0),
Chris@91 57 m_target(0),
Chris@91 58 m_lastRetrievalTimestamp(0.0),
Chris@91 59 m_lastRetrievedBlockSize(0),
Chris@102 60 m_trustworthyTimestamps(true),
Chris@102 61 m_lastCurrentFrame(0),
Chris@43 62 m_playing(false),
Chris@43 63 m_exiting(false),
Chris@43 64 m_lastModelEndFrame(0),
Chris@193 65 m_ringBufferSize(DEFAULT_RING_BUFFER_SIZE),
Chris@43 66 m_outputLeft(0.0),
Chris@43 67 m_outputRight(0.0),
Chris@43 68 m_auditioningPlugin(0),
Chris@43 69 m_auditioningPluginBypassed(false),
Chris@94 70 m_playStartFrame(0),
Chris@94 71 m_playStartFramePassed(false),
Chris@43 72 m_timeStretcher(0),
Chris@130 73 m_monoStretcher(0),
Chris@91 74 m_stretchRatio(1.0),
Chris@91 75 m_stretcherInputCount(0),
Chris@91 76 m_stretcherInputs(0),
Chris@91 77 m_stretcherInputSizes(0),
Chris@43 78 m_fillThread(0),
Chris@43 79 m_converter(0),
Chris@43 80 m_crapConverter(0),
Chris@43 81 m_resampleQuality(Preferences::getInstance()->getResampleQuality())
Chris@43 82 {
Chris@43 83 m_viewManager->setAudioPlaySource(this);
Chris@43 84
Chris@43 85 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@43 86 this, SLOT(selectionChanged()));
Chris@43 87 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
Chris@43 88 this, SLOT(playLoopModeChanged()));
Chris@43 89 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
Chris@43 90 this, SLOT(playSelectionModeChanged()));
Chris@43 91
Chris@43 92 connect(PlayParameterRepository::getInstance(),
Chris@43 93 SIGNAL(playParametersChanged(PlayParameters *)),
Chris@43 94 this, SLOT(playParametersChanged(PlayParameters *)));
Chris@43 95
Chris@43 96 connect(Preferences::getInstance(),
Chris@43 97 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@43 98 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@43 99 }
Chris@43 100
Chris@43 101 AudioCallbackPlaySource::~AudioCallbackPlaySource()
Chris@43 102 {
Chris@177 103 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@177 104 std::cerr << "AudioCallbackPlaySource::~AudioCallbackPlaySource entering" << std::endl;
Chris@177 105 #endif
Chris@43 106 m_exiting = true;
Chris@43 107
Chris@43 108 if (m_fillThread) {
Chris@43 109 m_condition.wakeAll();
Chris@43 110 m_fillThread->wait();
Chris@43 111 delete m_fillThread;
Chris@43 112 }
Chris@43 113
Chris@43 114 clearModels();
Chris@43 115
Chris@43 116 if (m_readBuffers != m_writeBuffers) {
Chris@43 117 delete m_readBuffers;
Chris@43 118 }
Chris@43 119
Chris@43 120 delete m_writeBuffers;
Chris@43 121
Chris@43 122 delete m_audioGenerator;
Chris@43 123
Chris@91 124 for (size_t i = 0; i < m_stretcherInputCount; ++i) {
Chris@91 125 delete[] m_stretcherInputs[i];
Chris@91 126 }
Chris@91 127 delete[] m_stretcherInputSizes;
Chris@91 128 delete[] m_stretcherInputs;
Chris@91 129
Chris@130 130 delete m_timeStretcher;
Chris@130 131 delete m_monoStretcher;
Chris@130 132
Chris@43 133 m_bufferScavenger.scavenge(true);
Chris@43 134 m_pluginScavenger.scavenge(true);
Chris@177 135 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@177 136 std::cerr << "AudioCallbackPlaySource::~AudioCallbackPlaySource finishing" << std::endl;
Chris@177 137 #endif
Chris@43 138 }
Chris@43 139
Chris@43 140 void
Chris@43 141 AudioCallbackPlaySource::addModel(Model *model)
Chris@43 142 {
Chris@43 143 if (m_models.find(model) != m_models.end()) return;
Chris@43 144
Chris@43 145 bool canPlay = m_audioGenerator->addModel(model);
Chris@43 146
Chris@43 147 m_mutex.lock();
Chris@43 148
Chris@43 149 m_models.insert(model);
Chris@43 150 if (model->getEndFrame() > m_lastModelEndFrame) {
Chris@43 151 m_lastModelEndFrame = model->getEndFrame();
Chris@43 152 }
Chris@43 153
Chris@43 154 bool buffersChanged = false, srChanged = false;
Chris@43 155
Chris@43 156 size_t modelChannels = 1;
Chris@43 157 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 158 if (dtvm) modelChannels = dtvm->getChannelCount();
Chris@43 159 if (modelChannels > m_sourceChannelCount) {
Chris@43 160 m_sourceChannelCount = modelChannels;
Chris@43 161 }
Chris@43 162
Chris@43 163 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@103 164 std::cout << "Adding model with " << modelChannels << " channels at rate " << model->getSampleRate() << std::endl;
Chris@43 165 #endif
Chris@43 166
Chris@43 167 if (m_sourceSampleRate == 0) {
Chris@43 168
Chris@43 169 m_sourceSampleRate = model->getSampleRate();
Chris@43 170 srChanged = true;
Chris@43 171
Chris@43 172 } else if (model->getSampleRate() != m_sourceSampleRate) {
Chris@43 173
Chris@43 174 // If this is a dense time-value model and we have no other, we
Chris@43 175 // can just switch to this model's sample rate
Chris@43 176
Chris@43 177 if (dtvm) {
Chris@43 178
Chris@43 179 bool conflicting = false;
Chris@43 180
Chris@43 181 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@43 182 i != m_models.end(); ++i) {
Chris@43 183 // Only wave file models can be considered conflicting --
Chris@43 184 // writable wave file models are derived and we shouldn't
Chris@43 185 // take their rates into account. Also, don't give any
Chris@43 186 // particular weight to a file that's already playing at
Chris@43 187 // the wrong rate anyway
Chris@43 188 WaveFileModel *wfm = dynamic_cast<WaveFileModel *>(*i);
Chris@43 189 if (wfm && wfm != dtvm &&
Chris@43 190 wfm->getSampleRate() != model->getSampleRate() &&
Chris@43 191 wfm->getSampleRate() == m_sourceSampleRate) {
Chris@43 192 std::cerr << "AudioCallbackPlaySource::addModel: Conflicting wave file model " << *i << " found" << std::endl;
Chris@43 193 conflicting = true;
Chris@43 194 break;
Chris@43 195 }
Chris@43 196 }
Chris@43 197
Chris@43 198 if (conflicting) {
Chris@43 199
Chris@43 200 std::cerr << "AudioCallbackPlaySource::addModel: ERROR: "
Chris@43 201 << "New model sample rate does not match" << std::endl
Chris@43 202 << "existing model(s) (new " << model->getSampleRate()
Chris@43 203 << " vs " << m_sourceSampleRate
Chris@43 204 << "), playback will be wrong"
Chris@43 205 << std::endl;
Chris@43 206
Chris@43 207 emit sampleRateMismatch(model->getSampleRate(),
Chris@43 208 m_sourceSampleRate,
Chris@43 209 false);
Chris@43 210 } else {
Chris@43 211 m_sourceSampleRate = model->getSampleRate();
Chris@43 212 srChanged = true;
Chris@43 213 }
Chris@43 214 }
Chris@43 215 }
Chris@43 216
Chris@43 217 if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) {
Chris@43 218 clearRingBuffers(true, getTargetChannelCount());
Chris@43 219 buffersChanged = true;
Chris@43 220 } else {
Chris@43 221 if (canPlay) clearRingBuffers(true);
Chris@43 222 }
Chris@43 223
Chris@43 224 if (buffersChanged || srChanged) {
Chris@43 225 if (m_converter) {
Chris@43 226 src_delete(m_converter);
Chris@43 227 src_delete(m_crapConverter);
Chris@43 228 m_converter = 0;
Chris@43 229 m_crapConverter = 0;
Chris@43 230 }
Chris@43 231 }
Chris@43 232
Chris@164 233 rebuildRangeLists();
Chris@164 234
Chris@43 235 m_mutex.unlock();
Chris@43 236
Chris@43 237 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
Chris@43 238
Chris@43 239 if (!m_fillThread) {
Chris@43 240 m_fillThread = new FillThread(*this);
Chris@43 241 m_fillThread->start();
Chris@43 242 }
Chris@43 243
Chris@43 244 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 245 std::cout << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s) -- emitting modelReplaced" << std::endl;
Chris@43 246 #endif
Chris@43 247
Chris@43 248 if (buffersChanged || srChanged) {
Chris@43 249 emit modelReplaced();
Chris@43 250 }
Chris@43 251
Chris@43 252 connect(model, SIGNAL(modelChanged(size_t, size_t)),
Chris@43 253 this, SLOT(modelChanged(size_t, size_t)));
Chris@43 254
Chris@43 255 m_condition.wakeAll();
Chris@43 256 }
Chris@43 257
Chris@43 258 void
Chris@43 259 AudioCallbackPlaySource::modelChanged(size_t startFrame, size_t endFrame)
Chris@43 260 {
Chris@43 261 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 262 std::cerr << "AudioCallbackPlaySource::modelChanged(" << startFrame << "," << endFrame << ")" << std::endl;
Chris@43 263 #endif
Chris@93 264 if (endFrame > m_lastModelEndFrame) {
Chris@93 265 m_lastModelEndFrame = endFrame;
Chris@99 266 rebuildRangeLists();
Chris@93 267 }
Chris@43 268 }
Chris@43 269
Chris@43 270 void
Chris@43 271 AudioCallbackPlaySource::removeModel(Model *model)
Chris@43 272 {
Chris@43 273 m_mutex.lock();
Chris@43 274
Chris@43 275 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 276 std::cout << "AudioCallbackPlaySource::removeModel(" << model << ")" << std::endl;
Chris@43 277 #endif
Chris@43 278
Chris@43 279 disconnect(model, SIGNAL(modelChanged(size_t, size_t)),
Chris@43 280 this, SLOT(modelChanged(size_t, size_t)));
Chris@43 281
Chris@43 282 m_models.erase(model);
Chris@43 283
Chris@43 284 if (m_models.empty()) {
Chris@43 285 if (m_converter) {
Chris@43 286 src_delete(m_converter);
Chris@43 287 src_delete(m_crapConverter);
Chris@43 288 m_converter = 0;
Chris@43 289 m_crapConverter = 0;
Chris@43 290 }
Chris@43 291 m_sourceSampleRate = 0;
Chris@43 292 }
Chris@43 293
Chris@43 294 size_t lastEnd = 0;
Chris@43 295 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@43 296 i != m_models.end(); ++i) {
Chris@164 297 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@164 298 std::cout << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl;
Chris@164 299 #endif
Chris@43 300 if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame();
Chris@164 301 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@164 302 std::cout << "(done, lastEnd now " << lastEnd << ")" << std::endl;
Chris@164 303 #endif
Chris@43 304 }
Chris@43 305 m_lastModelEndFrame = lastEnd;
Chris@43 306
Chris@43 307 m_mutex.unlock();
Chris@43 308
Chris@43 309 m_audioGenerator->removeModel(model);
Chris@43 310
Chris@43 311 clearRingBuffers();
Chris@43 312 }
Chris@43 313
Chris@43 314 void
Chris@43 315 AudioCallbackPlaySource::clearModels()
Chris@43 316 {
Chris@43 317 m_mutex.lock();
Chris@43 318
Chris@43 319 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 320 std::cout << "AudioCallbackPlaySource::clearModels()" << std::endl;
Chris@43 321 #endif
Chris@43 322
Chris@43 323 m_models.clear();
Chris@43 324
Chris@43 325 if (m_converter) {
Chris@43 326 src_delete(m_converter);
Chris@43 327 src_delete(m_crapConverter);
Chris@43 328 m_converter = 0;
Chris@43 329 m_crapConverter = 0;
Chris@43 330 }
Chris@43 331
Chris@43 332 m_lastModelEndFrame = 0;
Chris@43 333
Chris@43 334 m_sourceSampleRate = 0;
Chris@43 335
Chris@43 336 m_mutex.unlock();
Chris@43 337
Chris@43 338 m_audioGenerator->clearModels();
Chris@93 339
Chris@93 340 clearRingBuffers();
Chris@43 341 }
Chris@43 342
Chris@43 343 void
Chris@43 344 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count)
Chris@43 345 {
Chris@43 346 if (!haveLock) m_mutex.lock();
Chris@43 347
Chris@93 348 rebuildRangeLists();
Chris@93 349
Chris@43 350 if (count == 0) {
Chris@43 351 if (m_writeBuffers) count = m_writeBuffers->size();
Chris@43 352 }
Chris@43 353
Chris@93 354 m_writeBufferFill = getCurrentBufferedFrame();
Chris@43 355
Chris@43 356 if (m_readBuffers != m_writeBuffers) {
Chris@43 357 delete m_writeBuffers;
Chris@43 358 }
Chris@43 359
Chris@43 360 m_writeBuffers = new RingBufferVector;
Chris@43 361
Chris@43 362 for (size_t i = 0; i < count; ++i) {
Chris@43 363 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
Chris@43 364 }
Chris@43 365
Chris@43 366 // std::cout << "AudioCallbackPlaySource::clearRingBuffers: Created "
Chris@43 367 // << count << " write buffers" << std::endl;
Chris@43 368
Chris@43 369 if (!haveLock) {
Chris@43 370 m_mutex.unlock();
Chris@43 371 }
Chris@43 372 }
Chris@43 373
Chris@43 374 void
Chris@43 375 AudioCallbackPlaySource::play(size_t startFrame)
Chris@43 376 {
Chris@43 377 if (m_viewManager->getPlaySelectionMode() &&
Chris@43 378 !m_viewManager->getSelections().empty()) {
Chris@60 379
Chris@94 380 std::cerr << "AudioCallbackPlaySource::play: constraining frame " << startFrame << " to selection = ";
Chris@94 381
Chris@60 382 startFrame = m_viewManager->constrainFrameToSelection(startFrame);
Chris@60 383
Chris@94 384 std::cerr << startFrame << std::endl;
Chris@94 385
Chris@43 386 } else {
Chris@43 387 if (startFrame >= m_lastModelEndFrame) {
Chris@43 388 startFrame = 0;
Chris@43 389 }
Chris@43 390 }
Chris@43 391
Chris@132 392 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@60 393 std::cerr << "play(" << startFrame << ") -> playback model ";
Chris@132 394 #endif
Chris@60 395
Chris@60 396 startFrame = m_viewManager->alignReferenceToPlaybackFrame(startFrame);
Chris@60 397
Chris@189 398 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@60 399 std::cerr << startFrame << std::endl;
Chris@189 400 #endif
Chris@60 401
Chris@43 402 // The fill thread will automatically empty its buffers before
Chris@43 403 // starting again if we have not so far been playing, but not if
Chris@43 404 // we're just re-seeking.
Chris@102 405 // NO -- we can end up playing some first -- always reset here
Chris@43 406
Chris@43 407 m_mutex.lock();
Chris@102 408
Chris@91 409 if (m_timeStretcher) {
Chris@91 410 m_timeStretcher->reset();
Chris@91 411 }
Chris@130 412 if (m_monoStretcher) {
Chris@130 413 m_monoStretcher->reset();
Chris@130 414 }
Chris@102 415
Chris@102 416 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@102 417 if (m_readBuffers) {
Chris@102 418 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@102 419 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@132 420 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@102 421 std::cerr << "reset ring buffer for channel " << c << std::endl;
Chris@132 422 #endif
Chris@102 423 if (rb) rb->reset();
Chris@102 424 }
Chris@43 425 }
Chris@102 426 if (m_converter) src_reset(m_converter);
Chris@102 427 if (m_crapConverter) src_reset(m_crapConverter);
Chris@102 428
Chris@43 429 m_mutex.unlock();
Chris@43 430
Chris@43 431 m_audioGenerator->reset();
Chris@43 432
Chris@94 433 m_playStartFrame = startFrame;
Chris@94 434 m_playStartFramePassed = false;
Chris@94 435 m_playStartedAt = RealTime::zeroTime;
Chris@94 436 if (m_target) {
Chris@94 437 m_playStartedAt = RealTime::fromSeconds(m_target->getCurrentTime());
Chris@94 438 }
Chris@94 439
Chris@43 440 bool changed = !m_playing;
Chris@91 441 m_lastRetrievalTimestamp = 0;
Chris@102 442 m_lastCurrentFrame = 0;
Chris@43 443 m_playing = true;
Chris@43 444 m_condition.wakeAll();
Chris@158 445 if (changed) {
Chris@158 446 emit playStatusChanged(m_playing);
Chris@158 447 emit activity(tr("Play from %1").arg
Chris@158 448 (RealTime::frame2RealTime
Chris@158 449 (m_playStartFrame, m_sourceSampleRate).toText().c_str()));
Chris@158 450 }
Chris@43 451 }
Chris@43 452
Chris@43 453 void
Chris@43 454 AudioCallbackPlaySource::stop()
Chris@43 455 {
Chris@43 456 bool changed = m_playing;
Chris@43 457 m_playing = false;
Chris@43 458 m_condition.wakeAll();
Chris@91 459 m_lastRetrievalTimestamp = 0;
Chris@158 460 if (changed) {
Chris@158 461 emit playStatusChanged(m_playing);
Chris@158 462 emit activity(tr("Stop at %1").arg
Chris@158 463 (RealTime::frame2RealTime
Chris@158 464 (m_lastCurrentFrame, m_sourceSampleRate).toText().c_str()));
Chris@158 465 }
Chris@102 466 m_lastCurrentFrame = 0;
Chris@43 467 }
Chris@43 468
Chris@43 469 void
Chris@43 470 AudioCallbackPlaySource::selectionChanged()
Chris@43 471 {
Chris@43 472 if (m_viewManager->getPlaySelectionMode()) {
Chris@43 473 clearRingBuffers();
Chris@43 474 }
Chris@43 475 }
Chris@43 476
Chris@43 477 void
Chris@43 478 AudioCallbackPlaySource::playLoopModeChanged()
Chris@43 479 {
Chris@43 480 clearRingBuffers();
Chris@43 481 }
Chris@43 482
Chris@43 483 void
Chris@43 484 AudioCallbackPlaySource::playSelectionModeChanged()
Chris@43 485 {
Chris@43 486 if (!m_viewManager->getSelections().empty()) {
Chris@43 487 clearRingBuffers();
Chris@43 488 }
Chris@43 489 }
Chris@43 490
Chris@43 491 void
Chris@43 492 AudioCallbackPlaySource::playParametersChanged(PlayParameters *)
Chris@43 493 {
Chris@43 494 clearRingBuffers();
Chris@43 495 }
Chris@43 496
Chris@43 497 void
Chris@43 498 AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName n)
Chris@43 499 {
Chris@43 500 if (n == "Resample Quality") {
Chris@43 501 setResampleQuality(Preferences::getInstance()->getResampleQuality());
Chris@43 502 }
Chris@43 503 }
Chris@43 504
Chris@43 505 void
Chris@43 506 AudioCallbackPlaySource::audioProcessingOverload()
Chris@43 507 {
Chris@130 508 std::cerr << "Audio processing overload!" << std::endl;
Chris@130 509
Chris@130 510 if (!m_playing) return;
Chris@130 511
Chris@43 512 RealTimePluginInstance *ap = m_auditioningPlugin;
Chris@130 513 if (ap && !m_auditioningPluginBypassed) {
Chris@43 514 m_auditioningPluginBypassed = true;
Chris@43 515 emit audioOverloadPluginDisabled();
Chris@130 516 return;
Chris@130 517 }
Chris@130 518
Chris@130 519 if (m_timeStretcher &&
Chris@130 520 m_timeStretcher->getTimeRatio() < 1.0 &&
Chris@130 521 m_stretcherInputCount > 1 &&
Chris@130 522 m_monoStretcher && !m_stretchMono) {
Chris@130 523 m_stretchMono = true;
Chris@130 524 emit audioTimeStretchMultiChannelDisabled();
Chris@130 525 return;
Chris@43 526 }
Chris@43 527 }
Chris@43 528
Chris@43 529 void
Chris@91 530 AudioCallbackPlaySource::setTarget(AudioCallbackPlayTarget *target, size_t size)
Chris@43 531 {
Chris@91 532 m_target = target;
Chris@193 533 std::cout << "AudioCallbackPlaySource::setTarget: Block size -> " << size << std::endl;
Chris@193 534 if (size != 0) {
Chris@193 535 m_blockSize = size;
Chris@193 536 }
Chris@193 537 if (size * 4 > m_ringBufferSize) {
Chris@193 538 std::cerr << "AudioCallbackPlaySource::setTarget: Buffer size "
Chris@193 539 << size << " > a quarter of ring buffer size "
Chris@193 540 << m_ringBufferSize << ", calling for more ring buffer"
Chris@193 541 << std::endl;
Chris@193 542 m_ringBufferSize = size * 4;
Chris@193 543 if (m_writeBuffers && !m_writeBuffers->empty()) {
Chris@193 544 clearRingBuffers();
Chris@193 545 }
Chris@193 546 }
Chris@43 547 }
Chris@43 548
Chris@43 549 size_t
Chris@43 550 AudioCallbackPlaySource::getTargetBlockSize() const
Chris@43 551 {
Chris@43 552 // std::cout << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
Chris@43 553 return m_blockSize;
Chris@43 554 }
Chris@43 555
Chris@43 556 void
Chris@43 557 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
Chris@43 558 {
Chris@43 559 m_playLatency = latency;
Chris@43 560 }
Chris@43 561
Chris@43 562 size_t
Chris@43 563 AudioCallbackPlaySource::getTargetPlayLatency() const
Chris@43 564 {
Chris@43 565 return m_playLatency;
Chris@43 566 }
Chris@43 567
Chris@43 568 size_t
Chris@43 569 AudioCallbackPlaySource::getCurrentPlayingFrame()
Chris@43 570 {
Chris@91 571 // This method attempts to estimate which audio sample frame is
Chris@91 572 // "currently coming through the speakers".
Chris@91 573
Chris@93 574 size_t targetRate = getTargetSampleRate();
Chris@93 575 size_t latency = m_playLatency; // at target rate
Chris@93 576 RealTime latency_t = RealTime::frame2RealTime(latency, targetRate);
Chris@93 577
Chris@93 578 return getCurrentFrame(latency_t);
Chris@93 579 }
Chris@93 580
Chris@93 581 size_t
Chris@93 582 AudioCallbackPlaySource::getCurrentBufferedFrame()
Chris@93 583 {
Chris@93 584 return getCurrentFrame(RealTime::zeroTime);
Chris@93 585 }
Chris@93 586
Chris@93 587 size_t
Chris@93 588 AudioCallbackPlaySource::getCurrentFrame(RealTime latency_t)
Chris@93 589 {
Chris@43 590 bool resample = false;
Chris@91 591 double resampleRatio = 1.0;
Chris@43 592
Chris@91 593 // We resample when filling the ring buffer, and time-stretch when
Chris@91 594 // draining it. The buffer contains data at the "target rate" and
Chris@91 595 // the latency provided by the target is also at the target rate.
Chris@91 596 // Because of the multiple rates involved, we do the actual
Chris@91 597 // calculation using RealTime instead.
Chris@43 598
Chris@91 599 size_t sourceRate = getSourceSampleRate();
Chris@91 600 size_t targetRate = getTargetSampleRate();
Chris@91 601
Chris@91 602 if (sourceRate == 0 || targetRate == 0) return 0;
Chris@91 603
Chris@91 604 size_t inbuffer = 0; // at target rate
Chris@91 605
Chris@43 606 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 607 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@43 608 if (rb) {
Chris@91 609 size_t here = rb->getReadSpace();
Chris@91 610 if (c == 0 || here < inbuffer) inbuffer = here;
Chris@43 611 }
Chris@43 612 }
Chris@43 613
Chris@91 614 size_t readBufferFill = m_readBufferFill;
Chris@91 615 size_t lastRetrievedBlockSize = m_lastRetrievedBlockSize;
Chris@91 616 double lastRetrievalTimestamp = m_lastRetrievalTimestamp;
Chris@91 617 double currentTime = 0.0;
Chris@91 618 if (m_target) currentTime = m_target->getCurrentTime();
Chris@91 619
Chris@102 620 bool looping = m_viewManager->getPlayLoopMode();
Chris@102 621
Chris@91 622 RealTime inbuffer_t = RealTime::frame2RealTime(inbuffer, targetRate);
Chris@91 623
Chris@91 624 size_t stretchlat = 0;
Chris@91 625 double timeRatio = 1.0;
Chris@91 626
Chris@91 627 if (m_timeStretcher) {
Chris@91 628 stretchlat = m_timeStretcher->getLatency();
Chris@91 629 timeRatio = m_timeStretcher->getTimeRatio();
Chris@43 630 }
Chris@43 631
Chris@91 632 RealTime stretchlat_t = RealTime::frame2RealTime(stretchlat, targetRate);
Chris@43 633
Chris@91 634 // When the target has just requested a block from us, the last
Chris@91 635 // sample it obtained was our buffer fill frame count minus the
Chris@91 636 // amount of read space (converted back to source sample rate)
Chris@91 637 // remaining now. That sample is not expected to be played until
Chris@91 638 // the target's play latency has elapsed. By the time the
Chris@91 639 // following block is requested, that sample will be at the
Chris@91 640 // target's play latency minus the last requested block size away
Chris@91 641 // from being played.
Chris@91 642
Chris@91 643 RealTime sincerequest_t = RealTime::zeroTime;
Chris@91 644 RealTime lastretrieved_t = RealTime::zeroTime;
Chris@91 645
Chris@102 646 if (m_target &&
Chris@102 647 m_trustworthyTimestamps &&
Chris@102 648 lastRetrievalTimestamp != 0.0) {
Chris@91 649
Chris@91 650 lastretrieved_t = RealTime::frame2RealTime
Chris@91 651 (lastRetrievedBlockSize, targetRate);
Chris@91 652
Chris@91 653 // calculate number of frames at target rate that have elapsed
Chris@91 654 // since the end of the last call to getSourceSamples
Chris@91 655
Chris@102 656 if (m_trustworthyTimestamps && !looping) {
Chris@91 657
Chris@102 658 // this adjustment seems to cause more problems when looping
Chris@102 659 double elapsed = currentTime - lastRetrievalTimestamp;
Chris@102 660
Chris@102 661 if (elapsed > 0.0) {
Chris@102 662 sincerequest_t = RealTime::fromSeconds(elapsed);
Chris@102 663 }
Chris@91 664 }
Chris@91 665
Chris@91 666 } else {
Chris@91 667
Chris@91 668 lastretrieved_t = RealTime::frame2RealTime
Chris@91 669 (getTargetBlockSize(), targetRate);
Chris@62 670 }
Chris@91 671
Chris@91 672 RealTime bufferedto_t = RealTime::frame2RealTime(readBufferFill, sourceRate);
Chris@91 673
Chris@91 674 if (timeRatio != 1.0) {
Chris@91 675 lastretrieved_t = lastretrieved_t / timeRatio;
Chris@91 676 sincerequest_t = sincerequest_t / timeRatio;
Chris@163 677 latency_t = latency_t / timeRatio;
Chris@43 678 }
Chris@43 679
Chris@91 680 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@163 681 std::cerr << "\nbuffered to: " << bufferedto_t << ", in buffer: " << inbuffer_t << ", time ratio " << timeRatio << "\n stretcher latency: " << stretchlat_t << ", device latency: " << latency_t << "\n since request: " << sincerequest_t << ", last retrieved quantity: " << lastretrieved_t << std::endl;
Chris@91 682 #endif
Chris@43 683
Chris@91 684 RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate);
Chris@60 685
Chris@93 686 // Normally the range lists should contain at least one item each
Chris@93 687 // -- if playback is unconstrained, that item should report the
Chris@93 688 // entire source audio duration.
Chris@43 689
Chris@93 690 if (m_rangeStarts.empty()) {
Chris@93 691 rebuildRangeLists();
Chris@93 692 }
Chris@92 693
Chris@93 694 if (m_rangeStarts.empty()) {
Chris@93 695 // this code is only used in case of error in rebuildRangeLists
Chris@93 696 RealTime playing_t = bufferedto_t
Chris@93 697 - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t
Chris@93 698 + sincerequest_t;
Chris@193 699 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
Chris@93 700 size_t frame = RealTime::realTime2Frame(playing_t, sourceRate);
Chris@93 701 return m_viewManager->alignPlaybackFrameToReference(frame);
Chris@93 702 }
Chris@43 703
Chris@91 704 int inRange = 0;
Chris@91 705 int index = 0;
Chris@91 706
Chris@93 707 for (size_t i = 0; i < m_rangeStarts.size(); ++i) {
Chris@93 708 if (bufferedto_t >= m_rangeStarts[i]) {
Chris@93 709 inRange = index;
Chris@93 710 } else {
Chris@93 711 break;
Chris@93 712 }
Chris@93 713 ++index;
Chris@93 714 }
Chris@93 715
Chris@93 716 if (inRange >= m_rangeStarts.size()) inRange = m_rangeStarts.size()-1;
Chris@93 717
Chris@94 718 RealTime playing_t = bufferedto_t;
Chris@93 719
Chris@93 720 playing_t = playing_t
Chris@93 721 - latency_t - stretchlat_t - lastretrieved_t - inbuffer_t
Chris@93 722 + sincerequest_t;
Chris@94 723
Chris@94 724 // This rather gross little hack is used to ensure that latency
Chris@94 725 // compensation doesn't result in the playback pointer appearing
Chris@94 726 // to start earlier than the actual playback does. It doesn't
Chris@94 727 // work properly (hence the bail-out in the middle) because if we
Chris@94 728 // are playing a relatively short looped region, the playing time
Chris@94 729 // estimated from the buffer fill frame may have wrapped around
Chris@94 730 // the region boundary and end up being much smaller than the
Chris@94 731 // theoretical play start frame, perhaps even for the entire
Chris@94 732 // duration of playback!
Chris@94 733
Chris@94 734 if (!m_playStartFramePassed) {
Chris@94 735 RealTime playstart_t = RealTime::frame2RealTime(m_playStartFrame,
Chris@94 736 sourceRate);
Chris@94 737 if (playing_t < playstart_t) {
Chris@132 738 // std::cerr << "playing_t " << playing_t << " < playstart_t "
Chris@132 739 // << playstart_t << std::endl;
Chris@122 740 if (/*!!! sincerequest_t > RealTime::zeroTime && */
Chris@94 741 m_playStartedAt + latency_t + stretchlat_t <
Chris@94 742 RealTime::fromSeconds(currentTime)) {
Chris@176 743 // std::cerr << "but we've been playing for long enough that I think we should disregard it (it probably results from loop wrapping)" << std::endl;
Chris@94 744 m_playStartFramePassed = true;
Chris@94 745 } else {
Chris@94 746 playing_t = playstart_t;
Chris@94 747 }
Chris@94 748 } else {
Chris@94 749 m_playStartFramePassed = true;
Chris@94 750 }
Chris@94 751 }
Chris@163 752
Chris@163 753 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@163 754 std::cerr << "playing_t " << playing_t;
Chris@163 755 #endif
Chris@94 756
Chris@94 757 playing_t = playing_t - m_rangeStarts[inRange];
Chris@93 758
Chris@93 759 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@163 760 std::cerr << " as offset into range " << inRange << " (start =" << m_rangeStarts[inRange] << " duration =" << m_rangeDurations[inRange] << ") = " << playing_t << std::endl;
Chris@93 761 #endif
Chris@93 762
Chris@93 763 while (playing_t < RealTime::zeroTime) {
Chris@93 764
Chris@93 765 if (inRange == 0) {
Chris@93 766 if (looping) {
Chris@93 767 inRange = m_rangeStarts.size() - 1;
Chris@93 768 } else {
Chris@93 769 break;
Chris@93 770 }
Chris@93 771 } else {
Chris@93 772 --inRange;
Chris@93 773 }
Chris@93 774
Chris@93 775 playing_t = playing_t + m_rangeDurations[inRange];
Chris@93 776 }
Chris@93 777
Chris@93 778 playing_t = playing_t + m_rangeStarts[inRange];
Chris@93 779
Chris@93 780 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@93 781 std::cerr << " playing time: " << playing_t << std::endl;
Chris@93 782 #endif
Chris@93 783
Chris@93 784 if (!looping) {
Chris@93 785 if (inRange == m_rangeStarts.size()-1 &&
Chris@93 786 playing_t >= m_rangeStarts[inRange] + m_rangeDurations[inRange]) {
Chris@96 787 std::cerr << "Not looping, inRange " << inRange << " == rangeStarts.size()-1, playing_t " << playing_t << " >= m_rangeStarts[inRange] " << m_rangeStarts[inRange] << " + m_rangeDurations[inRange] " << m_rangeDurations[inRange] << " -- stopping" << std::endl;
Chris@93 788 stop();
Chris@93 789 }
Chris@93 790 }
Chris@93 791
Chris@93 792 if (playing_t < RealTime::zeroTime) playing_t = RealTime::zeroTime;
Chris@93 793
Chris@93 794 size_t frame = RealTime::realTime2Frame(playing_t, sourceRate);
Chris@102 795
Chris@102 796 if (m_lastCurrentFrame > 0 && !looping) {
Chris@102 797 if (frame < m_lastCurrentFrame) {
Chris@102 798 frame = m_lastCurrentFrame;
Chris@102 799 }
Chris@102 800 }
Chris@102 801
Chris@102 802 m_lastCurrentFrame = frame;
Chris@102 803
Chris@93 804 return m_viewManager->alignPlaybackFrameToReference(frame);
Chris@93 805 }
Chris@93 806
Chris@93 807 void
Chris@93 808 AudioCallbackPlaySource::rebuildRangeLists()
Chris@93 809 {
Chris@93 810 bool constrained = (m_viewManager->getPlaySelectionMode());
Chris@93 811
Chris@93 812 m_rangeStarts.clear();
Chris@93 813 m_rangeDurations.clear();
Chris@93 814
Chris@93 815 size_t sourceRate = getSourceSampleRate();
Chris@93 816 if (sourceRate == 0) return;
Chris@93 817
Chris@93 818 RealTime end = RealTime::frame2RealTime(m_lastModelEndFrame, sourceRate);
Chris@93 819 if (end == RealTime::zeroTime) return;
Chris@93 820
Chris@93 821 if (!constrained) {
Chris@93 822 m_rangeStarts.push_back(RealTime::zeroTime);
Chris@93 823 m_rangeDurations.push_back(end);
Chris@93 824 return;
Chris@93 825 }
Chris@93 826
Chris@93 827 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@93 828 MultiSelection::SelectionList::const_iterator i;
Chris@93 829
Chris@93 830 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@93 831 std::cerr << "AudioCallbackPlaySource::rebuildRangeLists" << std::endl;
Chris@93 832 #endif
Chris@93 833
Chris@93 834 if (!selections.empty()) {
Chris@91 835
Chris@91 836 for (i = selections.begin(); i != selections.end(); ++i) {
Chris@91 837
Chris@91 838 RealTime start =
Chris@91 839 (RealTime::frame2RealTime
Chris@91 840 (m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
Chris@91 841 sourceRate));
Chris@91 842 RealTime duration =
Chris@91 843 (RealTime::frame2RealTime
Chris@91 844 (m_viewManager->alignReferenceToPlaybackFrame(i->getEndFrame()) -
Chris@91 845 m_viewManager->alignReferenceToPlaybackFrame(i->getStartFrame()),
Chris@91 846 sourceRate));
Chris@91 847
Chris@93 848 m_rangeStarts.push_back(start);
Chris@93 849 m_rangeDurations.push_back(duration);
Chris@91 850 }
Chris@93 851 } else {
Chris@93 852 m_rangeStarts.push_back(RealTime::zeroTime);
Chris@93 853 m_rangeDurations.push_back(end);
Chris@43 854 }
Chris@43 855
Chris@93 856 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@93 857 std::cerr << "Now have " << m_rangeStarts.size() << " play ranges" << std::endl;
Chris@91 858 #endif
Chris@43 859 }
Chris@43 860
Chris@43 861 void
Chris@43 862 AudioCallbackPlaySource::setOutputLevels(float left, float right)
Chris@43 863 {
Chris@43 864 m_outputLeft = left;
Chris@43 865 m_outputRight = right;
Chris@43 866 }
Chris@43 867
Chris@43 868 bool
Chris@43 869 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
Chris@43 870 {
Chris@43 871 left = m_outputLeft;
Chris@43 872 right = m_outputRight;
Chris@43 873 return true;
Chris@43 874 }
Chris@43 875
Chris@43 876 void
Chris@43 877 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
Chris@43 878 {
Chris@43 879 m_targetSampleRate = sr;
Chris@43 880 initialiseConverter();
Chris@43 881 }
Chris@43 882
Chris@43 883 void
Chris@43 884 AudioCallbackPlaySource::initialiseConverter()
Chris@43 885 {
Chris@43 886 m_mutex.lock();
Chris@43 887
Chris@43 888 if (m_converter) {
Chris@43 889 src_delete(m_converter);
Chris@43 890 src_delete(m_crapConverter);
Chris@43 891 m_converter = 0;
Chris@43 892 m_crapConverter = 0;
Chris@43 893 }
Chris@43 894
Chris@43 895 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@43 896
Chris@43 897 int err = 0;
Chris@43 898
Chris@43 899 m_converter = src_new(m_resampleQuality == 2 ? SRC_SINC_BEST_QUALITY :
Chris@43 900 m_resampleQuality == 1 ? SRC_SINC_MEDIUM_QUALITY :
Chris@43 901 m_resampleQuality == 0 ? SRC_SINC_FASTEST :
Chris@43 902 SRC_SINC_MEDIUM_QUALITY,
Chris@43 903 getTargetChannelCount(), &err);
Chris@43 904
Chris@43 905 if (m_converter) {
Chris@43 906 m_crapConverter = src_new(SRC_LINEAR,
Chris@43 907 getTargetChannelCount(),
Chris@43 908 &err);
Chris@43 909 }
Chris@43 910
Chris@43 911 if (!m_converter || !m_crapConverter) {
Chris@43 912 std::cerr
Chris@43 913 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
Chris@43 914 << src_strerror(err) << std::endl;
Chris@43 915
Chris@43 916 if (m_converter) {
Chris@43 917 src_delete(m_converter);
Chris@43 918 m_converter = 0;
Chris@43 919 }
Chris@43 920
Chris@43 921 if (m_crapConverter) {
Chris@43 922 src_delete(m_crapConverter);
Chris@43 923 m_crapConverter = 0;
Chris@43 924 }
Chris@43 925
Chris@43 926 m_mutex.unlock();
Chris@43 927
Chris@43 928 emit sampleRateMismatch(getSourceSampleRate(),
Chris@43 929 getTargetSampleRate(),
Chris@43 930 false);
Chris@43 931 } else {
Chris@43 932
Chris@43 933 m_mutex.unlock();
Chris@43 934
Chris@43 935 emit sampleRateMismatch(getSourceSampleRate(),
Chris@43 936 getTargetSampleRate(),
Chris@43 937 true);
Chris@43 938 }
Chris@43 939 } else {
Chris@43 940 m_mutex.unlock();
Chris@43 941 }
Chris@43 942 }
Chris@43 943
Chris@43 944 void
Chris@43 945 AudioCallbackPlaySource::setResampleQuality(int q)
Chris@43 946 {
Chris@43 947 if (q == m_resampleQuality) return;
Chris@43 948 m_resampleQuality = q;
Chris@43 949
Chris@43 950 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 951 std::cerr << "AudioCallbackPlaySource::setResampleQuality: setting to "
Chris@43 952 << m_resampleQuality << std::endl;
Chris@43 953 #endif
Chris@43 954
Chris@43 955 initialiseConverter();
Chris@43 956 }
Chris@43 957
Chris@43 958 void
Chris@107 959 AudioCallbackPlaySource::setAuditioningEffect(Auditionable *a)
Chris@43 960 {
Chris@107 961 RealTimePluginInstance *plugin = dynamic_cast<RealTimePluginInstance *>(a);
Chris@107 962 if (a && !plugin) {
Chris@107 963 std::cerr << "WARNING: AudioCallbackPlaySource::setAuditioningEffect: auditionable object " << a << " is not a real-time plugin instance" << std::endl;
Chris@107 964 }
Chris@43 965 RealTimePluginInstance *formerPlugin = m_auditioningPlugin;
Chris@43 966 m_auditioningPlugin = plugin;
Chris@43 967 m_auditioningPluginBypassed = false;
Chris@43 968 if (formerPlugin) m_pluginScavenger.claim(formerPlugin);
Chris@43 969 }
Chris@43 970
Chris@43 971 void
Chris@43 972 AudioCallbackPlaySource::setSoloModelSet(std::set<Model *> s)
Chris@43 973 {
Chris@43 974 m_audioGenerator->setSoloModelSet(s);
Chris@43 975 clearRingBuffers();
Chris@43 976 }
Chris@43 977
Chris@43 978 void
Chris@43 979 AudioCallbackPlaySource::clearSoloModelSet()
Chris@43 980 {
Chris@43 981 m_audioGenerator->clearSoloModelSet();
Chris@43 982 clearRingBuffers();
Chris@43 983 }
Chris@43 984
Chris@43 985 size_t
Chris@43 986 AudioCallbackPlaySource::getTargetSampleRate() const
Chris@43 987 {
Chris@43 988 if (m_targetSampleRate) return m_targetSampleRate;
Chris@43 989 else return getSourceSampleRate();
Chris@43 990 }
Chris@43 991
Chris@43 992 size_t
Chris@43 993 AudioCallbackPlaySource::getSourceChannelCount() const
Chris@43 994 {
Chris@43 995 return m_sourceChannelCount;
Chris@43 996 }
Chris@43 997
Chris@43 998 size_t
Chris@43 999 AudioCallbackPlaySource::getTargetChannelCount() const
Chris@43 1000 {
Chris@43 1001 if (m_sourceChannelCount < 2) return 2;
Chris@43 1002 return m_sourceChannelCount;
Chris@43 1003 }
Chris@43 1004
Chris@43 1005 size_t
Chris@43 1006 AudioCallbackPlaySource::getSourceSampleRate() const
Chris@43 1007 {
Chris@43 1008 return m_sourceSampleRate;
Chris@43 1009 }
Chris@43 1010
Chris@43 1011 void
Chris@91 1012 AudioCallbackPlaySource::setTimeStretch(float factor)
Chris@43 1013 {
Chris@91 1014 m_stretchRatio = factor;
Chris@91 1015
Chris@91 1016 if (m_timeStretcher || (factor == 1.f)) {
Chris@91 1017 // stretch ratio will be set in next process call if appropriate
Chris@62 1018 } else {
Chris@91 1019 m_stretcherInputCount = getTargetChannelCount();
Chris@62 1020 RubberBandStretcher *stretcher = new RubberBandStretcher
Chris@62 1021 (getTargetSampleRate(),
Chris@91 1022 m_stretcherInputCount,
Chris@62 1023 RubberBandStretcher::OptionProcessRealTime,
Chris@62 1024 factor);
Chris@130 1025 RubberBandStretcher *monoStretcher = new RubberBandStretcher
Chris@130 1026 (getTargetSampleRate(),
Chris@130 1027 1,
Chris@130 1028 RubberBandStretcher::OptionProcessRealTime,
Chris@130 1029 factor);
Chris@91 1030 m_stretcherInputs = new float *[m_stretcherInputCount];
Chris@91 1031 m_stretcherInputSizes = new size_t[m_stretcherInputCount];
Chris@91 1032 for (size_t c = 0; c < m_stretcherInputCount; ++c) {
Chris@91 1033 m_stretcherInputSizes[c] = 16384;
Chris@91 1034 m_stretcherInputs[c] = new float[m_stretcherInputSizes[c]];
Chris@91 1035 }
Chris@130 1036 m_monoStretcher = monoStretcher;
Chris@62 1037 m_timeStretcher = stretcher;
Chris@62 1038 }
Chris@158 1039
Chris@158 1040 emit activity(tr("Change time-stretch factor to %1").arg(factor));
Chris@43 1041 }
Chris@43 1042
Chris@43 1043 size_t
Chris@130 1044 AudioCallbackPlaySource::getSourceSamples(size_t ucount, float **buffer)
Chris@43 1045 {
Chris@130 1046 int count = ucount;
Chris@130 1047
Chris@43 1048 if (!m_playing) {
Chris@193 1049 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@193 1050 std::cerr << "AudioCallbackPlaySource::getSourceSamples: Not playing" << std::endl;
Chris@193 1051 #endif
Chris@43 1052 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@130 1053 for (int i = 0; i < count; ++i) {
Chris@43 1054 buffer[ch][i] = 0.0;
Chris@43 1055 }
Chris@43 1056 }
Chris@43 1057 return 0;
Chris@43 1058 }
Chris@43 1059
Chris@43 1060 // Ensure that all buffers have at least the amount of data we
Chris@43 1061 // need -- else reduce the size of our requests correspondingly
Chris@43 1062
Chris@43 1063 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@43 1064
Chris@43 1065 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@43 1066
Chris@43 1067 if (!rb) {
Chris@43 1068 std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@43 1069 << "No ring buffer available for channel " << ch
Chris@43 1070 << ", returning no data here" << std::endl;
Chris@43 1071 count = 0;
Chris@43 1072 break;
Chris@43 1073 }
Chris@43 1074
Chris@43 1075 size_t rs = rb->getReadSpace();
Chris@43 1076 if (rs < count) {
Chris@43 1077 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1078 std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: "
Chris@43 1079 << "Ring buffer for channel " << ch << " has only "
Chris@193 1080 << rs << " (of " << count << ") samples available ("
Chris@193 1081 << "ring buffer size is " << rb->getSize() << ", write "
Chris@193 1082 << "space " << rb->getWriteSpace() << "), "
Chris@43 1083 << "reducing request size" << std::endl;
Chris@43 1084 #endif
Chris@43 1085 count = rs;
Chris@43 1086 }
Chris@43 1087 }
Chris@43 1088
Chris@43 1089 if (count == 0) return 0;
Chris@43 1090
Chris@62 1091 RubberBandStretcher *ts = m_timeStretcher;
Chris@130 1092 RubberBandStretcher *ms = m_monoStretcher;
Chris@130 1093
Chris@62 1094 float ratio = ts ? ts->getTimeRatio() : 1.f;
Chris@91 1095
Chris@91 1096 if (ratio != m_stretchRatio) {
Chris@91 1097 if (!ts) {
Chris@91 1098 std::cerr << "WARNING: AudioCallbackPlaySource::getSourceSamples: Time ratio change to " << m_stretchRatio << " is pending, but no stretcher is set" << std::endl;
Chris@91 1099 m_stretchRatio = 1.f;
Chris@91 1100 } else {
Chris@91 1101 ts->setTimeRatio(m_stretchRatio);
Chris@130 1102 if (ms) ms->setTimeRatio(m_stretchRatio);
Chris@130 1103 if (m_stretchRatio >= 1.0) m_stretchMono = false;
Chris@130 1104 }
Chris@130 1105 }
Chris@130 1106
Chris@130 1107 int stretchChannels = m_stretcherInputCount;
Chris@130 1108 if (m_stretchMono) {
Chris@130 1109 if (ms) {
Chris@130 1110 ts = ms;
Chris@130 1111 stretchChannels = 1;
Chris@130 1112 } else {
Chris@130 1113 m_stretchMono = false;
Chris@91 1114 }
Chris@91 1115 }
Chris@91 1116
Chris@91 1117 if (m_target) {
Chris@91 1118 m_lastRetrievedBlockSize = count;
Chris@91 1119 m_lastRetrievalTimestamp = m_target->getCurrentTime();
Chris@91 1120 }
Chris@43 1121
Chris@62 1122 if (!ts || ratio == 1.f) {
Chris@43 1123
Chris@130 1124 int got = 0;
Chris@43 1125
Chris@43 1126 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@43 1127
Chris@43 1128 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@43 1129
Chris@43 1130 if (rb) {
Chris@43 1131
Chris@43 1132 // this is marginally more likely to leave our channels in
Chris@43 1133 // sync after a processing failure than just passing "count":
Chris@43 1134 size_t request = count;
Chris@43 1135 if (ch > 0) request = got;
Chris@43 1136
Chris@43 1137 got = rb->read(buffer[ch], request);
Chris@43 1138
Chris@43 1139 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@43 1140 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " (of " << count << ") samples on channel " << ch << ", signalling for more (possibly)" << std::endl;
Chris@43 1141 #endif
Chris@43 1142 }
Chris@43 1143
Chris@43 1144 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@130 1145 for (int i = got; i < count; ++i) {
Chris@43 1146 buffer[ch][i] = 0.0;
Chris@43 1147 }
Chris@43 1148 }
Chris@43 1149 }
Chris@43 1150
Chris@43 1151 applyAuditioningEffect(count, buffer);
Chris@43 1152
Chris@43 1153 m_condition.wakeAll();
Chris@91 1154
Chris@43 1155 return got;
Chris@43 1156 }
Chris@43 1157
Chris@62 1158 size_t channels = getTargetChannelCount();
Chris@91 1159 size_t available;
Chris@91 1160 int warned = 0;
Chris@91 1161 size_t fedToStretcher = 0;
Chris@43 1162
Chris@91 1163 // The input block for a given output is approx output / ratio,
Chris@91 1164 // but we can't predict it exactly, for an adaptive timestretcher.
Chris@91 1165
Chris@91 1166 while ((available = ts->available()) < count) {
Chris@91 1167
Chris@91 1168 size_t reqd = lrintf((count - available) / ratio);
Chris@91 1169 reqd = std::max(reqd, ts->getSamplesRequired());
Chris@91 1170 if (reqd == 0) reqd = 1;
Chris@91 1171
Chris@91 1172 size_t got = reqd;
Chris@91 1173
Chris@91 1174 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@91 1175 std::cerr << "reqd = " <<reqd << ", channels = " << channels << ", ic = " << m_stretcherInputCount << std::endl;
Chris@62 1176 #endif
Chris@43 1177
Chris@91 1178 for (size_t c = 0; c < channels; ++c) {
Chris@131 1179 if (c >= m_stretcherInputCount) continue;
Chris@91 1180 if (reqd > m_stretcherInputSizes[c]) {
Chris@91 1181 if (c == 0) {
Chris@91 1182 std::cerr << "WARNING: resizing stretcher input buffer from " << m_stretcherInputSizes[c] << " to " << (reqd * 2) << std::endl;
Chris@91 1183 }
Chris@91 1184 delete[] m_stretcherInputs[c];
Chris@91 1185 m_stretcherInputSizes[c] = reqd * 2;
Chris@91 1186 m_stretcherInputs[c] = new float[m_stretcherInputSizes[c]];
Chris@91 1187 }
Chris@91 1188 }
Chris@43 1189
Chris@91 1190 for (size_t c = 0; c < channels; ++c) {
Chris@131 1191 if (c >= m_stretcherInputCount) continue;
Chris@91 1192 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@91 1193 if (rb) {
Chris@130 1194 size_t gotHere;
Chris@130 1195 if (stretchChannels == 1 && c > 0) {
Chris@130 1196 gotHere = rb->readAdding(m_stretcherInputs[0], got);
Chris@130 1197 } else {
Chris@130 1198 gotHere = rb->read(m_stretcherInputs[c], got);
Chris@130 1199 }
Chris@91 1200 if (gotHere < got) got = gotHere;
Chris@91 1201
Chris@91 1202 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@91 1203 if (c == 0) {
Chris@91 1204 std::cerr << "feeding stretcher: got " << gotHere
Chris@91 1205 << ", " << rb->getReadSpace() << " remain" << std::endl;
Chris@91 1206 }
Chris@62 1207 #endif
Chris@43 1208
Chris@91 1209 } else {
Chris@91 1210 std::cerr << "WARNING: No ring buffer available for channel " << c << " in stretcher input block" << std::endl;
Chris@43 1211 }
Chris@43 1212 }
Chris@43 1213
Chris@43 1214 if (got < reqd) {
Chris@43 1215 std::cerr << "WARNING: Read underrun in playback ("
Chris@43 1216 << got << " < " << reqd << ")" << std::endl;
Chris@43 1217 }
Chris@43 1218
Chris@91 1219 ts->process(m_stretcherInputs, got, false);
Chris@91 1220
Chris@91 1221 fedToStretcher += got;
Chris@43 1222
Chris@43 1223 if (got == 0) break;
Chris@43 1224
Chris@62 1225 if (ts->available() == available) {
Chris@43 1226 std::cerr << "WARNING: AudioCallbackPlaySource::getSamples: Added " << got << " samples to time stretcher, created no new available output samples (warned = " << warned << ")" << std::endl;
Chris@43 1227 if (++warned == 5) break;
Chris@43 1228 }
Chris@43 1229 }
Chris@43 1230
Chris@62 1231 ts->retrieve(buffer, count);
Chris@43 1232
Chris@130 1233 for (int c = stretchChannels; c < getTargetChannelCount(); ++c) {
Chris@130 1234 for (int i = 0; i < count; ++i) {
Chris@130 1235 buffer[c][i] = buffer[0][i];
Chris@130 1236 }
Chris@130 1237 }
Chris@130 1238
Chris@43 1239 applyAuditioningEffect(count, buffer);
Chris@43 1240
Chris@43 1241 m_condition.wakeAll();
Chris@43 1242
Chris@43 1243 return count;
Chris@43 1244 }
Chris@43 1245
Chris@43 1246 void
Chris@43 1247 AudioCallbackPlaySource::applyAuditioningEffect(size_t count, float **buffers)
Chris@43 1248 {
Chris@43 1249 if (m_auditioningPluginBypassed) return;
Chris@43 1250 RealTimePluginInstance *plugin = m_auditioningPlugin;
Chris@43 1251 if (!plugin) return;
Chris@43 1252
Chris@43 1253 if (plugin->getAudioInputCount() != getTargetChannelCount()) {
Chris@43 1254 // std::cerr << "plugin input count " << plugin->getAudioInputCount()
Chris@43 1255 // << " != our channel count " << getTargetChannelCount()
Chris@43 1256 // << std::endl;
Chris@43 1257 return;
Chris@43 1258 }
Chris@43 1259 if (plugin->getAudioOutputCount() != getTargetChannelCount()) {
Chris@43 1260 // std::cerr << "plugin output count " << plugin->getAudioOutputCount()
Chris@43 1261 // << " != our channel count " << getTargetChannelCount()
Chris@43 1262 // << std::endl;
Chris@43 1263 return;
Chris@43 1264 }
Chris@102 1265 if (plugin->getBufferSize() < count) {
Chris@43 1266 // std::cerr << "plugin buffer size " << plugin->getBufferSize()
Chris@102 1267 // << " < our block size " << count
Chris@43 1268 // << std::endl;
Chris@43 1269 return;
Chris@43 1270 }
Chris@43 1271
Chris@43 1272 float **ib = plugin->getAudioInputBuffers();
Chris@43 1273 float **ob = plugin->getAudioOutputBuffers();
Chris@43 1274
Chris@43 1275 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1276 for (size_t i = 0; i < count; ++i) {
Chris@43 1277 ib[c][i] = buffers[c][i];
Chris@43 1278 }
Chris@43 1279 }
Chris@43 1280
Chris@102 1281 plugin->run(Vamp::RealTime::zeroTime, count);
Chris@43 1282
Chris@43 1283 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1284 for (size_t i = 0; i < count; ++i) {
Chris@43 1285 buffers[c][i] = ob[c][i];
Chris@43 1286 }
Chris@43 1287 }
Chris@43 1288 }
Chris@43 1289
Chris@43 1290 // Called from fill thread, m_playing true, mutex held
Chris@43 1291 bool
Chris@43 1292 AudioCallbackPlaySource::fillBuffers()
Chris@43 1293 {
Chris@43 1294 static float *tmp = 0;
Chris@43 1295 static size_t tmpSize = 0;
Chris@43 1296
Chris@43 1297 size_t space = 0;
Chris@43 1298 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1299 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1300 if (wb) {
Chris@43 1301 size_t spaceHere = wb->getWriteSpace();
Chris@43 1302 if (c == 0 || spaceHere < space) space = spaceHere;
Chris@43 1303 }
Chris@43 1304 }
Chris@43 1305
Chris@103 1306 if (space == 0) {
Chris@103 1307 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@103 1308 std::cout << "AudioCallbackPlaySourceFillThread: no space to fill" << std::endl;
Chris@103 1309 #endif
Chris@103 1310 return false;
Chris@103 1311 }
Chris@43 1312
Chris@43 1313 size_t f = m_writeBufferFill;
Chris@43 1314
Chris@43 1315 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
Chris@43 1316
Chris@43 1317 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@193 1318 if (!readWriteEqual) {
Chris@193 1319 std::cout << "AudioCallbackPlaySourceFillThread: note read buffers != write buffers" << std::endl;
Chris@193 1320 }
Chris@43 1321 std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl;
Chris@43 1322 #endif
Chris@43 1323
Chris@43 1324 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1325 std::cout << "buffered to " << f << " already" << std::endl;
Chris@43 1326 #endif
Chris@43 1327
Chris@43 1328 bool resample = (getSourceSampleRate() != getTargetSampleRate());
Chris@43 1329
Chris@43 1330 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1331 std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
Chris@43 1332 #endif
Chris@43 1333
Chris@43 1334 size_t channels = getTargetChannelCount();
Chris@43 1335
Chris@43 1336 size_t orig = space;
Chris@43 1337 size_t got = 0;
Chris@43 1338
Chris@43 1339 static float **bufferPtrs = 0;
Chris@43 1340 static size_t bufferPtrCount = 0;
Chris@43 1341
Chris@43 1342 if (bufferPtrCount < channels) {
Chris@43 1343 if (bufferPtrs) delete[] bufferPtrs;
Chris@43 1344 bufferPtrs = new float *[channels];
Chris@43 1345 bufferPtrCount = channels;
Chris@43 1346 }
Chris@43 1347
Chris@43 1348 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
Chris@43 1349
Chris@43 1350 if (resample && !m_converter) {
Chris@43 1351 static bool warned = false;
Chris@43 1352 if (!warned) {
Chris@43 1353 std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
Chris@43 1354 warned = true;
Chris@43 1355 }
Chris@43 1356 }
Chris@43 1357
Chris@43 1358 if (resample && m_converter) {
Chris@43 1359
Chris@43 1360 double ratio =
Chris@43 1361 double(getTargetSampleRate()) / double(getSourceSampleRate());
Chris@43 1362 orig = size_t(orig / ratio + 0.1);
Chris@43 1363
Chris@43 1364 // orig must be a multiple of generatorBlockSize
Chris@43 1365 orig = (orig / generatorBlockSize) * generatorBlockSize;
Chris@43 1366 if (orig == 0) return false;
Chris@43 1367
Chris@43 1368 size_t work = std::max(orig, space);
Chris@43 1369
Chris@43 1370 // We only allocate one buffer, but we use it in two halves.
Chris@43 1371 // We place the non-interleaved values in the second half of
Chris@43 1372 // the buffer (orig samples for channel 0, orig samples for
Chris@43 1373 // channel 1 etc), and then interleave them into the first
Chris@43 1374 // half of the buffer. Then we resample back into the second
Chris@43 1375 // half (interleaved) and de-interleave the results back to
Chris@43 1376 // the start of the buffer for insertion into the ringbuffers.
Chris@43 1377 // What a faff -- especially as we've already de-interleaved
Chris@43 1378 // the audio data from the source file elsewhere before we
Chris@43 1379 // even reach this point.
Chris@43 1380
Chris@43 1381 if (tmpSize < channels * work * 2) {
Chris@43 1382 delete[] tmp;
Chris@43 1383 tmp = new float[channels * work * 2];
Chris@43 1384 tmpSize = channels * work * 2;
Chris@43 1385 }
Chris@43 1386
Chris@43 1387 float *nonintlv = tmp + channels * work;
Chris@43 1388 float *intlv = tmp;
Chris@43 1389 float *srcout = tmp + channels * work;
Chris@43 1390
Chris@43 1391 for (size_t c = 0; c < channels; ++c) {
Chris@43 1392 for (size_t i = 0; i < orig; ++i) {
Chris@43 1393 nonintlv[channels * i + c] = 0.0f;
Chris@43 1394 }
Chris@43 1395 }
Chris@43 1396
Chris@43 1397 for (size_t c = 0; c < channels; ++c) {
Chris@43 1398 bufferPtrs[c] = nonintlv + c * orig;
Chris@43 1399 }
Chris@43 1400
Chris@163 1401 got = mixModels(f, orig, bufferPtrs); // also modifies f
Chris@43 1402
Chris@43 1403 // and interleave into first half
Chris@43 1404 for (size_t c = 0; c < channels; ++c) {
Chris@43 1405 for (size_t i = 0; i < got; ++i) {
Chris@43 1406 float sample = nonintlv[c * got + i];
Chris@43 1407 intlv[channels * i + c] = sample;
Chris@43 1408 }
Chris@43 1409 }
Chris@43 1410
Chris@43 1411 SRC_DATA data;
Chris@43 1412 data.data_in = intlv;
Chris@43 1413 data.data_out = srcout;
Chris@43 1414 data.input_frames = got;
Chris@43 1415 data.output_frames = work;
Chris@43 1416 data.src_ratio = ratio;
Chris@43 1417 data.end_of_input = 0;
Chris@43 1418
Chris@43 1419 int err = 0;
Chris@43 1420
Chris@62 1421 if (m_timeStretcher && m_timeStretcher->getTimeRatio() < 0.4) {
Chris@43 1422 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1423 std::cout << "Using crappy converter" << std::endl;
Chris@43 1424 #endif
Chris@43 1425 err = src_process(m_crapConverter, &data);
Chris@43 1426 } else {
Chris@43 1427 err = src_process(m_converter, &data);
Chris@43 1428 }
Chris@43 1429
Chris@43 1430 size_t toCopy = size_t(got * ratio + 0.1);
Chris@43 1431
Chris@43 1432 if (err) {
Chris@43 1433 std::cerr
Chris@43 1434 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
Chris@43 1435 << src_strerror(err) << std::endl;
Chris@43 1436 //!!! Then what?
Chris@43 1437 } else {
Chris@43 1438 got = data.input_frames_used;
Chris@43 1439 toCopy = data.output_frames_gen;
Chris@43 1440 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1441 std::cout << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
Chris@43 1442 #endif
Chris@43 1443 }
Chris@43 1444
Chris@43 1445 for (size_t c = 0; c < channels; ++c) {
Chris@43 1446 for (size_t i = 0; i < toCopy; ++i) {
Chris@43 1447 tmp[i] = srcout[channels * i + c];
Chris@43 1448 }
Chris@43 1449 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1450 if (wb) wb->write(tmp, toCopy);
Chris@43 1451 }
Chris@43 1452
Chris@43 1453 m_writeBufferFill = f;
Chris@43 1454 if (readWriteEqual) m_readBufferFill = f;
Chris@43 1455
Chris@43 1456 } else {
Chris@43 1457
Chris@43 1458 // space must be a multiple of generatorBlockSize
Chris@195 1459 size_t reqSpace = space;
Chris@195 1460 space = (reqSpace / generatorBlockSize) * generatorBlockSize;
Chris@91 1461 if (space == 0) {
Chris@91 1462 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@195 1463 std::cout << "requested fill of " << reqSpace
Chris@195 1464 << " is less than generator block size of "
Chris@91 1465 << generatorBlockSize << ", leaving it" << std::endl;
Chris@91 1466 #endif
Chris@91 1467 return false;
Chris@91 1468 }
Chris@43 1469
Chris@43 1470 if (tmpSize < channels * space) {
Chris@43 1471 delete[] tmp;
Chris@43 1472 tmp = new float[channels * space];
Chris@43 1473 tmpSize = channels * space;
Chris@43 1474 }
Chris@43 1475
Chris@43 1476 for (size_t c = 0; c < channels; ++c) {
Chris@43 1477
Chris@43 1478 bufferPtrs[c] = tmp + c * space;
Chris@43 1479
Chris@43 1480 for (size_t i = 0; i < space; ++i) {
Chris@43 1481 tmp[c * space + i] = 0.0f;
Chris@43 1482 }
Chris@43 1483 }
Chris@43 1484
Chris@163 1485 size_t got = mixModels(f, space, bufferPtrs); // also modifies f
Chris@43 1486
Chris@43 1487 for (size_t c = 0; c < channels; ++c) {
Chris@43 1488
Chris@43 1489 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1490 if (wb) {
Chris@43 1491 size_t actual = wb->write(bufferPtrs[c], got);
Chris@43 1492 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1493 std::cout << "Wrote " << actual << " samples for ch " << c << ", now "
Chris@43 1494 << wb->getReadSpace() << " to read"
Chris@43 1495 << std::endl;
Chris@43 1496 #endif
Chris@43 1497 if (actual < got) {
Chris@43 1498 std::cerr << "WARNING: Buffer overrun in channel " << c
Chris@43 1499 << ": wrote " << actual << " of " << got
Chris@43 1500 << " samples" << std::endl;
Chris@43 1501 }
Chris@43 1502 }
Chris@43 1503 }
Chris@43 1504
Chris@43 1505 m_writeBufferFill = f;
Chris@43 1506 if (readWriteEqual) m_readBufferFill = f;
Chris@43 1507
Chris@163 1508 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@163 1509 std::cout << "Read buffer fill is now " << m_readBufferFill << std::endl;
Chris@163 1510 #endif
Chris@163 1511
Chris@43 1512 //!!! 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@43 1513 }
Chris@43 1514
Chris@43 1515 return true;
Chris@43 1516 }
Chris@43 1517
Chris@43 1518 size_t
Chris@43 1519 AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers)
Chris@43 1520 {
Chris@43 1521 size_t processed = 0;
Chris@43 1522 size_t chunkStart = frame;
Chris@43 1523 size_t chunkSize = count;
Chris@43 1524 size_t selectionSize = 0;
Chris@43 1525 size_t nextChunkStart = chunkStart + chunkSize;
Chris@43 1526
Chris@43 1527 bool looping = m_viewManager->getPlayLoopMode();
Chris@43 1528 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@43 1529 !m_viewManager->getSelections().empty());
Chris@43 1530
Chris@43 1531 static float **chunkBufferPtrs = 0;
Chris@43 1532 static size_t chunkBufferPtrCount = 0;
Chris@43 1533 size_t channels = getTargetChannelCount();
Chris@43 1534
Chris@43 1535 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1536 std::cout << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl;
Chris@43 1537 #endif
Chris@43 1538
Chris@43 1539 if (chunkBufferPtrCount < channels) {
Chris@43 1540 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
Chris@43 1541 chunkBufferPtrs = new float *[channels];
Chris@43 1542 chunkBufferPtrCount = channels;
Chris@43 1543 }
Chris@43 1544
Chris@43 1545 for (size_t c = 0; c < channels; ++c) {
Chris@43 1546 chunkBufferPtrs[c] = buffers[c];
Chris@43 1547 }
Chris@43 1548
Chris@43 1549 while (processed < count) {
Chris@43 1550
Chris@43 1551 chunkSize = count - processed;
Chris@43 1552 nextChunkStart = chunkStart + chunkSize;
Chris@43 1553 selectionSize = 0;
Chris@43 1554
Chris@43 1555 size_t fadeIn = 0, fadeOut = 0;
Chris@43 1556
Chris@43 1557 if (constrained) {
Chris@60 1558
Chris@60 1559 size_t rChunkStart =
Chris@60 1560 m_viewManager->alignPlaybackFrameToReference(chunkStart);
Chris@43 1561
Chris@43 1562 Selection selection =
Chris@60 1563 m_viewManager->getContainingSelection(rChunkStart, true);
Chris@43 1564
Chris@43 1565 if (selection.isEmpty()) {
Chris@43 1566 if (looping) {
Chris@43 1567 selection = *m_viewManager->getSelections().begin();
Chris@60 1568 chunkStart = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1569 (selection.getStartFrame());
Chris@43 1570 fadeIn = 50;
Chris@43 1571 }
Chris@43 1572 }
Chris@43 1573
Chris@43 1574 if (selection.isEmpty()) {
Chris@43 1575
Chris@43 1576 chunkSize = 0;
Chris@43 1577 nextChunkStart = chunkStart;
Chris@43 1578
Chris@43 1579 } else {
Chris@43 1580
Chris@60 1581 size_t sf = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1582 (selection.getStartFrame());
Chris@60 1583 size_t ef = m_viewManager->alignReferenceToPlaybackFrame
Chris@60 1584 (selection.getEndFrame());
Chris@43 1585
Chris@60 1586 selectionSize = ef - sf;
Chris@60 1587
Chris@60 1588 if (chunkStart < sf) {
Chris@60 1589 chunkStart = sf;
Chris@43 1590 fadeIn = 50;
Chris@43 1591 }
Chris@43 1592
Chris@43 1593 nextChunkStart = chunkStart + chunkSize;
Chris@43 1594
Chris@60 1595 if (nextChunkStart >= ef) {
Chris@60 1596 nextChunkStart = ef;
Chris@43 1597 fadeOut = 50;
Chris@43 1598 }
Chris@43 1599
Chris@43 1600 chunkSize = nextChunkStart - chunkStart;
Chris@43 1601 }
Chris@43 1602
Chris@43 1603 } else if (looping && m_lastModelEndFrame > 0) {
Chris@43 1604
Chris@43 1605 if (chunkStart >= m_lastModelEndFrame) {
Chris@43 1606 chunkStart = 0;
Chris@43 1607 }
Chris@43 1608 if (chunkSize > m_lastModelEndFrame - chunkStart) {
Chris@43 1609 chunkSize = m_lastModelEndFrame - chunkStart;
Chris@43 1610 }
Chris@43 1611 nextChunkStart = chunkStart + chunkSize;
Chris@43 1612 }
Chris@43 1613
Chris@43 1614 // std::cout << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl;
Chris@43 1615
Chris@43 1616 if (!chunkSize) {
Chris@43 1617 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1618 std::cout << "Ending selection playback at " << nextChunkStart << std::endl;
Chris@43 1619 #endif
Chris@43 1620 // We need to maintain full buffers so that the other
Chris@43 1621 // thread can tell where it's got to in the playback -- so
Chris@43 1622 // return the full amount here
Chris@43 1623 frame = frame + count;
Chris@43 1624 return count;
Chris@43 1625 }
Chris@43 1626
Chris@43 1627 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1628 std::cout << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl;
Chris@43 1629 #endif
Chris@43 1630
Chris@43 1631 size_t got = 0;
Chris@43 1632
Chris@43 1633 if (selectionSize < 100) {
Chris@43 1634 fadeIn = 0;
Chris@43 1635 fadeOut = 0;
Chris@43 1636 } else if (selectionSize < 300) {
Chris@43 1637 if (fadeIn > 0) fadeIn = 10;
Chris@43 1638 if (fadeOut > 0) fadeOut = 10;
Chris@43 1639 }
Chris@43 1640
Chris@43 1641 if (fadeIn > 0) {
Chris@43 1642 if (processed * 2 < fadeIn) {
Chris@43 1643 fadeIn = processed * 2;
Chris@43 1644 }
Chris@43 1645 }
Chris@43 1646
Chris@43 1647 if (fadeOut > 0) {
Chris@43 1648 if ((count - processed - chunkSize) * 2 < fadeOut) {
Chris@43 1649 fadeOut = (count - processed - chunkSize) * 2;
Chris@43 1650 }
Chris@43 1651 }
Chris@43 1652
Chris@43 1653 for (std::set<Model *>::iterator mi = m_models.begin();
Chris@43 1654 mi != m_models.end(); ++mi) {
Chris@43 1655
Chris@43 1656 got = m_audioGenerator->mixModel(*mi, chunkStart,
Chris@43 1657 chunkSize, chunkBufferPtrs,
Chris@43 1658 fadeIn, fadeOut);
Chris@43 1659 }
Chris@43 1660
Chris@43 1661 for (size_t c = 0; c < channels; ++c) {
Chris@43 1662 chunkBufferPtrs[c] += chunkSize;
Chris@43 1663 }
Chris@43 1664
Chris@43 1665 processed += chunkSize;
Chris@43 1666 chunkStart = nextChunkStart;
Chris@43 1667 }
Chris@43 1668
Chris@43 1669 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1670 std::cout << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl;
Chris@43 1671 #endif
Chris@43 1672
Chris@43 1673 frame = nextChunkStart;
Chris@43 1674 return processed;
Chris@43 1675 }
Chris@43 1676
Chris@43 1677 void
Chris@43 1678 AudioCallbackPlaySource::unifyRingBuffers()
Chris@43 1679 {
Chris@43 1680 if (m_readBuffers == m_writeBuffers) return;
Chris@43 1681
Chris@43 1682 // only unify if there will be something to read
Chris@43 1683 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1684 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1685 if (wb) {
Chris@43 1686 if (wb->getReadSpace() < m_blockSize * 2) {
Chris@43 1687 if ((m_writeBufferFill + m_blockSize * 2) <
Chris@43 1688 m_lastModelEndFrame) {
Chris@43 1689 // OK, we don't have enough and there's more to
Chris@43 1690 // read -- don't unify until we can do better
Chris@193 1691 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@193 1692 std::cerr << "AudioCallbackPlaySource::unifyRingBuffers: Not unifying: write buffer has less (" << wb->getReadSpace() << ") than " << m_blockSize*2 << " to read and write buffer fill (" << m_writeBufferFill << ") is not close to end frame (" << m_lastModelEndFrame << ")" << std::endl;
Chris@193 1693 #endif
Chris@43 1694 return;
Chris@43 1695 }
Chris@43 1696 }
Chris@43 1697 break;
Chris@43 1698 }
Chris@43 1699 }
Chris@43 1700
Chris@43 1701 size_t rf = m_readBufferFill;
Chris@43 1702 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@43 1703 if (rb) {
Chris@43 1704 size_t rs = rb->getReadSpace();
Chris@43 1705 //!!! incorrect when in non-contiguous selection, see comments elsewhere
Chris@43 1706 // std::cout << "rs = " << rs << std::endl;
Chris@43 1707 if (rs < rf) rf -= rs;
Chris@43 1708 else rf = 0;
Chris@43 1709 }
Chris@43 1710
Chris@193 1711 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@193 1712 std::cerr << "AudioCallbackPlaySource::unifyRingBuffers: m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl;
Chris@193 1713 #endif
Chris@43 1714
Chris@43 1715 size_t wf = m_writeBufferFill;
Chris@43 1716 size_t skip = 0;
Chris@43 1717 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@43 1718 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@43 1719 if (wb) {
Chris@43 1720 if (c == 0) {
Chris@43 1721
Chris@43 1722 size_t wrs = wb->getReadSpace();
Chris@43 1723 // std::cout << "wrs = " << wrs << std::endl;
Chris@43 1724
Chris@43 1725 if (wrs < wf) wf -= wrs;
Chris@43 1726 else wf = 0;
Chris@43 1727 // std::cout << "wf = " << wf << std::endl;
Chris@43 1728
Chris@43 1729 if (wf < rf) skip = rf - wf;
Chris@43 1730 if (skip == 0) break;
Chris@43 1731 }
Chris@43 1732
Chris@43 1733 // std::cout << "skipping " << skip << std::endl;
Chris@43 1734 wb->skip(skip);
Chris@43 1735 }
Chris@43 1736 }
Chris@43 1737
Chris@43 1738 m_bufferScavenger.claim(m_readBuffers);
Chris@43 1739 m_readBuffers = m_writeBuffers;
Chris@43 1740 m_readBufferFill = m_writeBufferFill;
Chris@193 1741 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@193 1742 std::cerr << "unified" << std::endl;
Chris@193 1743 #endif
Chris@43 1744 }
Chris@43 1745
Chris@43 1746 void
Chris@43 1747 AudioCallbackPlaySource::FillThread::run()
Chris@43 1748 {
Chris@43 1749 AudioCallbackPlaySource &s(m_source);
Chris@43 1750
Chris@43 1751 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1752 std::cout << "AudioCallbackPlaySourceFillThread starting" << std::endl;
Chris@43 1753 #endif
Chris@43 1754
Chris@43 1755 s.m_mutex.lock();
Chris@43 1756
Chris@43 1757 bool previouslyPlaying = s.m_playing;
Chris@43 1758 bool work = false;
Chris@43 1759
Chris@43 1760 while (!s.m_exiting) {
Chris@43 1761
Chris@43 1762 s.unifyRingBuffers();
Chris@43 1763 s.m_bufferScavenger.scavenge();
Chris@43 1764 s.m_pluginScavenger.scavenge();
Chris@43 1765
Chris@43 1766 if (work && s.m_playing && s.getSourceSampleRate()) {
Chris@43 1767
Chris@43 1768 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1769 std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl;
Chris@43 1770 #endif
Chris@43 1771
Chris@43 1772 s.m_mutex.unlock();
Chris@43 1773 s.m_mutex.lock();
Chris@43 1774
Chris@43 1775 } else {
Chris@43 1776
Chris@43 1777 float ms = 100;
Chris@43 1778 if (s.getSourceSampleRate() > 0) {
Chris@193 1779 ms = float(s.m_ringBufferSize) /
Chris@193 1780 float(s.getSourceSampleRate()) * 1000.0;
Chris@43 1781 }
Chris@43 1782
Chris@43 1783 if (s.m_playing) ms /= 10;
Chris@43 1784
Chris@43 1785 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1786 if (!s.m_playing) std::cout << std::endl;
Chris@43 1787 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl;
Chris@43 1788 #endif
Chris@43 1789
Chris@43 1790 s.m_condition.wait(&s.m_mutex, size_t(ms));
Chris@43 1791 }
Chris@43 1792
Chris@43 1793 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1794 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
Chris@43 1795 #endif
Chris@43 1796
Chris@43 1797 work = false;
Chris@43 1798
Chris@103 1799 if (!s.getSourceSampleRate()) {
Chris@103 1800 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@103 1801 std::cout << "AudioCallbackPlaySourceFillThread: source sample rate is zero" << std::endl;
Chris@103 1802 #endif
Chris@103 1803 continue;
Chris@103 1804 }
Chris@43 1805
Chris@43 1806 bool playing = s.m_playing;
Chris@43 1807
Chris@43 1808 if (playing && !previouslyPlaying) {
Chris@43 1809 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@43 1810 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
Chris@43 1811 #endif
Chris@43 1812 for (size_t c = 0; c < s.getTargetChannelCount(); ++c) {
Chris@43 1813 RingBuffer<float> *rb = s.getReadRingBuffer(c);
Chris@43 1814 if (rb) rb->reset();
Chris@43 1815 }
Chris@43 1816 }
Chris@43 1817 previouslyPlaying = playing;
Chris@43 1818
Chris@43 1819 work = s.fillBuffers();
Chris@43 1820 }
Chris@43 1821
Chris@43 1822 s.m_mutex.unlock();
Chris@43 1823 }
Chris@43 1824