annotate audio/AudioCallbackPlaySource.cpp @ 765:eae885290abc

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