annotate audioio/AudioCallbackPlaySource.cpp @ 93:737b373246b5

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