annotate audio/AudioCallbackPlaySource.cpp @ 546:4de547a5905c 3.0-integration

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