annotate audioio/AudioCallbackPlaySource.cpp @ 463:ad998a2fe9e2

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