annotate audioio/AudioCallbackPlaySource.cpp @ 389:8cfc71c8ec4b

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