annotate audioio/AudioCallbackPlaySource.cpp @ 99:cd5cbdecc82d sv1-v1.2

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