annotate audioio/AudioCallbackPlaySource.cpp @ 28:ca4ee67b2358

* Build fixes * Use Vamp plugin SDK library instead of objects directly
author Chris Cannam
date Thu, 06 Apr 2006 12:29:02 +0000
parents 4593a7ebad93
children dd2084d32652
rev   line source
Chris@19 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@21 4 Sonic Visualiser
Chris@21 5 An audio file viewer and annotation editor.
Chris@21 6 Centre for Digital Music, Queen Mary, University of London.
Chris@21 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@21 9 This program is free software; you can redistribute it and/or
Chris@21 10 modify it under the terms of the GNU General Public License as
Chris@21 11 published by the Free Software Foundation; either version 2 of the
Chris@21 12 License, or (at your option) any later version. See the file
Chris@21 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "AudioCallbackPlaySource.h"
Chris@0 17
Chris@0 18 #include "AudioGenerator.h"
Chris@0 19
Chris@0 20 #include "base/Model.h"
Chris@0 21 #include "base/ViewManager.h"
Chris@12 22 #include "base/PlayParameterRepository.h"
Chris@0 23 #include "model/DenseTimeValueModel.h"
Chris@0 24 #include "model/SparseOneDimensionalModel.h"
Chris@28 25 #include "IntegerTimeStretcher.h"
Chris@0 26
Chris@0 27 #include <iostream>
Chris@6 28 #include <cassert>
Chris@0 29
Chris@0 30 //#define DEBUG_AUDIO_PLAY_SOURCE 1
Chris@13 31 //#define DEBUG_AUDIO_PLAY_SOURCE_PLAYING 1
Chris@0 32
Chris@0 33 //const size_t AudioCallbackPlaySource::m_ringBufferSize = 102400;
Chris@0 34 const size_t AudioCallbackPlaySource::m_ringBufferSize = 131071;
Chris@0 35
Chris@0 36 AudioCallbackPlaySource::AudioCallbackPlaySource(ViewManager *manager) :
Chris@0 37 m_viewManager(manager),
Chris@0 38 m_audioGenerator(new AudioGenerator(manager)),
Chris@6 39 m_readBuffers(0),
Chris@6 40 m_writeBuffers(0),
Chris@13 41 m_readBufferFill(0),
Chris@13 42 m_writeBufferFill(0),
Chris@13 43 m_bufferScavenger(1),
Chris@6 44 m_sourceChannelCount(0),
Chris@0 45 m_blockSize(1024),
Chris@0 46 m_sourceSampleRate(0),
Chris@0 47 m_targetSampleRate(0),
Chris@0 48 m_playLatency(0),
Chris@0 49 m_playing(false),
Chris@0 50 m_exiting(false),
Chris@4 51 m_lastModelEndFrame(0),
Chris@0 52 m_outputLeft(0.0),
Chris@0 53 m_outputRight(0.0),
Chris@0 54 m_slowdownCounter(0),
Chris@0 55 m_timeStretcher(0),
Chris@0 56 m_fillThread(0),
Chris@0 57 m_converter(0)
Chris@0 58 {
Chris@0 59 m_viewManager->setAudioPlaySource(this);
Chris@3 60
Chris@3 61 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@3 62 this, SLOT(selectionChanged()));
Chris@3 63 connect(m_viewManager, SIGNAL(playLoopModeChanged()),
Chris@3 64 this, SLOT(playLoopModeChanged()));
Chris@3 65 connect(m_viewManager, SIGNAL(playSelectionModeChanged()),
Chris@3 66 this, SLOT(playSelectionModeChanged()));
Chris@12 67
Chris@12 68 connect(PlayParameterRepository::instance(),
Chris@12 69 SIGNAL(playParametersChanged(PlayParameters *)),
Chris@12 70 this, SLOT(playParametersChanged(PlayParameters *)));
Chris@0 71 }
Chris@0 72
Chris@0 73 AudioCallbackPlaySource::~AudioCallbackPlaySource()
Chris@0 74 {
Chris@0 75 m_exiting = true;
Chris@0 76
Chris@0 77 if (m_fillThread) {
Chris@0 78 m_condition.wakeAll();
Chris@0 79 m_fillThread->wait();
Chris@0 80 delete m_fillThread;
Chris@0 81 }
Chris@0 82
Chris@0 83 clearModels();
Chris@6 84
Chris@6 85 if (m_readBuffers != m_writeBuffers) {
Chris@6 86 delete m_readBuffers;
Chris@6 87 }
Chris@6 88
Chris@6 89 delete m_writeBuffers;
Chris@6 90
Chris@6 91 m_bufferScavenger.scavenge(true);
Chris@0 92 }
Chris@0 93
Chris@0 94 void
Chris@0 95 AudioCallbackPlaySource::addModel(Model *model)
Chris@0 96 {
Chris@10 97 bool canPlay = m_audioGenerator->addModel(model);
Chris@8 98
Chris@0 99 m_mutex.lock();
Chris@0 100
Chris@0 101 m_models.insert(model);
Chris@4 102 if (model->getEndFrame() > m_lastModelEndFrame) {
Chris@4 103 m_lastModelEndFrame = model->getEndFrame();
Chris@4 104 }
Chris@0 105
Chris@0 106 bool buffersChanged = false, srChanged = false;
Chris@0 107
Chris@0 108 if (m_sourceSampleRate == 0) {
Chris@0 109
Chris@0 110 m_sourceSampleRate = model->getSampleRate();
Chris@0 111 srChanged = true;
Chris@0 112
Chris@0 113 } else if (model->getSampleRate() != m_sourceSampleRate) {
Chris@0 114 std::cerr << "AudioCallbackPlaySource::addModel: ERROR: "
Chris@0 115 << "New model sample rate does not match" << std::endl
Chris@0 116 << "existing model(s) (new " << model->getSampleRate()
Chris@0 117 << " vs " << m_sourceSampleRate
Chris@0 118 << "), playback will be wrong"
Chris@0 119 << std::endl;
Chris@25 120 emit sampleRateMismatch(model->getSampleRate(), m_sourceSampleRate,
Chris@25 121 false);
Chris@0 122 }
Chris@0 123
Chris@0 124 size_t modelChannels = 1;
Chris@0 125 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 126 if (dtvm) modelChannels = dtvm->getChannelCount();
Chris@6 127 if (modelChannels > m_sourceChannelCount) {
Chris@6 128 m_sourceChannelCount = modelChannels;
Chris@0 129 }
Chris@0 130
Chris@6 131 std::cerr << "Adding model with " << modelChannels << " channels " << std::endl;
Chris@6 132
Chris@13 133 if (!m_writeBuffers || (m_writeBuffers->size() < getTargetChannelCount())) {
Chris@13 134 clearRingBuffers(true, getTargetChannelCount());
Chris@6 135 buffersChanged = true;
Chris@6 136 } else {
Chris@10 137 if (canPlay) clearRingBuffers(true);
Chris@0 138 }
Chris@0 139
Chris@0 140 if (buffersChanged || srChanged) {
Chris@0 141 if (m_converter) {
Chris@0 142 src_delete(m_converter);
Chris@0 143 m_converter = 0;
Chris@0 144 }
Chris@0 145 }
Chris@0 146
Chris@0 147 m_mutex.unlock();
Chris@0 148
Chris@13 149 m_audioGenerator->setTargetChannelCount(getTargetChannelCount());
Chris@13 150
Chris@0 151 if (!m_fillThread) {
Chris@0 152 m_fillThread = new AudioCallbackPlaySourceFillThread(*this);
Chris@0 153 m_fillThread->start();
Chris@0 154 }
Chris@0 155
Chris@0 156 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 157 std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl;
Chris@0 158 #endif
Chris@0 159
Chris@1 160 if (buffersChanged || srChanged) {
Chris@1 161 emit modelReplaced();
Chris@0 162 }
Chris@6 163
Chris@6 164 m_condition.wakeAll();
Chris@0 165 }
Chris@0 166
Chris@0 167 void
Chris@0 168 AudioCallbackPlaySource::removeModel(Model *model)
Chris@0 169 {
Chris@0 170 m_mutex.lock();
Chris@0 171
Chris@0 172 m_models.erase(model);
Chris@0 173
Chris@0 174 if (m_models.empty()) {
Chris@0 175 if (m_converter) {
Chris@0 176 src_delete(m_converter);
Chris@0 177 m_converter = 0;
Chris@0 178 }
Chris@0 179 m_sourceSampleRate = 0;
Chris@0 180 }
Chris@0 181
Chris@4 182 size_t lastEnd = 0;
Chris@4 183 for (std::set<Model *>::const_iterator i = m_models.begin();
Chris@4 184 i != m_models.end(); ++i) {
Chris@18 185 std::cerr << "AudioCallbackPlaySource::removeModel(" << model << "): checking end frame on model " << *i << std::endl;
Chris@4 186 if ((*i)->getEndFrame() > lastEnd) lastEnd = (*i)->getEndFrame();
Chris@18 187 std::cerr << "(done, lastEnd now " << lastEnd << ")" << std::endl;
Chris@4 188 }
Chris@4 189 m_lastModelEndFrame = lastEnd;
Chris@4 190
Chris@8 191 m_mutex.unlock();
Chris@8 192
Chris@0 193 m_audioGenerator->removeModel(model);
Chris@0 194
Chris@6 195 clearRingBuffers();
Chris@0 196 }
Chris@0 197
Chris@0 198 void
Chris@0 199 AudioCallbackPlaySource::clearModels()
Chris@0 200 {
Chris@0 201 m_mutex.lock();
Chris@0 202
Chris@0 203 m_models.clear();
Chris@0 204
Chris@0 205 if (m_converter) {
Chris@0 206 src_delete(m_converter);
Chris@0 207 m_converter = 0;
Chris@0 208 }
Chris@0 209
Chris@4 210 m_lastModelEndFrame = 0;
Chris@4 211
Chris@0 212 m_sourceSampleRate = 0;
Chris@0 213
Chris@0 214 m_mutex.unlock();
Chris@8 215
Chris@8 216 m_audioGenerator->clearModels();
Chris@0 217 }
Chris@0 218
Chris@0 219 void
Chris@6 220 AudioCallbackPlaySource::clearRingBuffers(bool haveLock, size_t count)
Chris@6 221 {
Chris@6 222 if (!haveLock) m_mutex.lock();
Chris@6 223
Chris@6 224 if (count == 0) {
Chris@6 225 if (m_writeBuffers) count = m_writeBuffers->size();
Chris@6 226 }
Chris@6 227
Chris@13 228 size_t sf = m_readBufferFill;
Chris@13 229 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@13 230 if (rb) {
Chris@13 231 //!!! This is incorrect if we're in a non-contiguous selection
Chris@13 232 //Same goes for all related code (subtracting the read space
Chris@13 233 //from the fill frame to try to establish where the effective
Chris@13 234 //pre-resample/timestretch read pointer is)
Chris@13 235 size_t rs = rb->getReadSpace();
Chris@13 236 if (rs < sf) sf -= rs;
Chris@13 237 else sf = 0;
Chris@13 238 }
Chris@13 239 m_writeBufferFill = sf;
Chris@13 240
Chris@6 241 if (m_readBuffers != m_writeBuffers) {
Chris@6 242 delete m_writeBuffers;
Chris@6 243 }
Chris@6 244
Chris@6 245 m_writeBuffers = new RingBufferVector;
Chris@6 246
Chris@6 247 for (size_t i = 0; i < count; ++i) {
Chris@6 248 m_writeBuffers->push_back(new RingBuffer<float>(m_ringBufferSize));
Chris@6 249 }
Chris@6 250
Chris@6 251 std::cerr << "AudioCallbackPlaySource::clearRingBuffers: Created "
Chris@6 252 << count << " write buffers" << std::endl;
Chris@6 253
Chris@6 254 if (!haveLock) {
Chris@6 255 m_mutex.unlock();
Chris@8 256 //!!! m_condition.wakeAll();
Chris@6 257 }
Chris@6 258 }
Chris@6 259
Chris@6 260 void
Chris@0 261 AudioCallbackPlaySource::play(size_t startFrame)
Chris@0 262 {
Chris@5 263 if (m_viewManager->getPlaySelectionMode() &&
Chris@5 264 !m_viewManager->getSelections().empty()) {
Chris@9 265 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@9 266 MultiSelection::SelectionList::iterator i = selections.begin();
Chris@4 267 if (i != selections.end()) {
Chris@4 268 if (startFrame < i->getStartFrame()) {
Chris@4 269 startFrame = i->getStartFrame();
Chris@4 270 } else {
Chris@9 271 MultiSelection::SelectionList::iterator j = selections.end();
Chris@4 272 --j;
Chris@4 273 if (startFrame >= j->getEndFrame()) {
Chris@4 274 startFrame = i->getStartFrame();
Chris@4 275 }
Chris@4 276 }
Chris@4 277 }
Chris@5 278 } else {
Chris@5 279 if (startFrame >= m_lastModelEndFrame) {
Chris@5 280 startFrame = 0;
Chris@5 281 }
Chris@4 282 }
Chris@4 283
Chris@0 284 // The fill thread will automatically empty its buffers before
Chris@0 285 // starting again if we have not so far been playing, but not if
Chris@0 286 // we're just re-seeking.
Chris@0 287
Chris@6 288 m_mutex.lock();
Chris@0 289 if (m_playing) {
Chris@13 290 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@6 291 if (m_readBuffers) {
Chris@13 292 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@6 293 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@6 294 if (rb) rb->reset();
Chris@6 295 }
Chris@0 296 }
Chris@6 297 if (m_converter) src_reset(m_converter);
Chris@0 298 } else {
Chris@6 299 if (m_converter) src_reset(m_converter);
Chris@13 300 m_readBufferFill = m_writeBufferFill = startFrame;
Chris@0 301 }
Chris@6 302 m_mutex.unlock();
Chris@0 303
Chris@0 304 m_audioGenerator->reset();
Chris@0 305
Chris@24 306 bool changed = !m_playing;
Chris@0 307 m_playing = true;
Chris@0 308 m_condition.wakeAll();
Chris@24 309 if (changed) emit playStatusChanged(m_playing);
Chris@0 310 }
Chris@0 311
Chris@0 312 void
Chris@0 313 AudioCallbackPlaySource::stop()
Chris@0 314 {
Chris@24 315 bool changed = m_playing;
Chris@0 316 m_playing = false;
Chris@0 317 m_condition.wakeAll();
Chris@24 318 if (changed) emit playStatusChanged(m_playing);
Chris@0 319 }
Chris@0 320
Chris@0 321 void
Chris@3 322 AudioCallbackPlaySource::selectionChanged()
Chris@3 323 {
Chris@3 324 if (m_viewManager->getPlaySelectionMode()) {
Chris@6 325 clearRingBuffers();
Chris@3 326 }
Chris@3 327 }
Chris@3 328
Chris@3 329 void
Chris@3 330 AudioCallbackPlaySource::playLoopModeChanged()
Chris@3 331 {
Chris@6 332 clearRingBuffers();
Chris@3 333 }
Chris@3 334
Chris@3 335 void
Chris@3 336 AudioCallbackPlaySource::playSelectionModeChanged()
Chris@3 337 {
Chris@3 338 if (!m_viewManager->getSelections().empty()) {
Chris@6 339 clearRingBuffers();
Chris@3 340 }
Chris@3 341 }
Chris@3 342
Chris@3 343 void
Chris@12 344 AudioCallbackPlaySource::playParametersChanged(PlayParameters *params)
Chris@12 345 {
Chris@12 346 clearRingBuffers();
Chris@12 347 }
Chris@12 348
Chris@12 349 void
Chris@0 350 AudioCallbackPlaySource::setTargetBlockSize(size_t size)
Chris@0 351 {
Chris@0 352 std::cerr << "AudioCallbackPlaySource::setTargetBlockSize() -> " << size << std::endl;
Chris@6 353 assert(size < m_ringBufferSize);
Chris@0 354 m_blockSize = size;
Chris@0 355 }
Chris@0 356
Chris@0 357 size_t
Chris@0 358 AudioCallbackPlaySource::getTargetBlockSize() const
Chris@0 359 {
Chris@0 360 std::cerr << "AudioCallbackPlaySource::getTargetBlockSize() -> " << m_blockSize << std::endl;
Chris@0 361 return m_blockSize;
Chris@0 362 }
Chris@0 363
Chris@0 364 void
Chris@0 365 AudioCallbackPlaySource::setTargetPlayLatency(size_t latency)
Chris@0 366 {
Chris@0 367 m_playLatency = latency;
Chris@0 368 }
Chris@0 369
Chris@0 370 size_t
Chris@0 371 AudioCallbackPlaySource::getTargetPlayLatency() const
Chris@0 372 {
Chris@0 373 return m_playLatency;
Chris@0 374 }
Chris@0 375
Chris@0 376 size_t
Chris@0 377 AudioCallbackPlaySource::getCurrentPlayingFrame()
Chris@0 378 {
Chris@0 379 bool resample = false;
Chris@0 380 double ratio = 1.0;
Chris@0 381
Chris@0 382 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@0 383 resample = true;
Chris@0 384 ratio = double(getSourceSampleRate()) / double(getTargetSampleRate());
Chris@0 385 }
Chris@0 386
Chris@0 387 size_t readSpace = 0;
Chris@13 388 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@6 389 RingBuffer<float> *rb = getReadRingBuffer(c);
Chris@6 390 if (rb) {
Chris@6 391 size_t spaceHere = rb->getReadSpace();
Chris@6 392 if (c == 0 || spaceHere < readSpace) readSpace = spaceHere;
Chris@6 393 }
Chris@0 394 }
Chris@0 395
Chris@0 396 if (resample) {
Chris@0 397 readSpace = size_t(readSpace * ratio + 0.1);
Chris@0 398 }
Chris@0 399
Chris@0 400 size_t latency = m_playLatency;
Chris@0 401 if (resample) latency = size_t(m_playLatency * ratio + 0.1);
Chris@0 402
Chris@0 403 TimeStretcherData *timeStretcher = m_timeStretcher;
Chris@0 404 if (timeStretcher) {
Chris@0 405 latency += timeStretcher->getStretcher(0)->getProcessingLatency();
Chris@0 406 }
Chris@0 407
Chris@3 408 latency += readSpace;
Chris@13 409 size_t bufferedFrame = m_readBufferFill;
Chris@3 410
Chris@5 411 bool looping = m_viewManager->getPlayLoopMode();
Chris@5 412 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@5 413 !m_viewManager->getSelections().empty());
Chris@5 414
Chris@3 415 size_t framePlaying = bufferedFrame;
Chris@5 416
Chris@5 417 if (looping && !constrained) {
Chris@5 418 while (framePlaying < latency) framePlaying += m_lastModelEndFrame;
Chris@4 419 }
Chris@3 420
Chris@5 421 if (framePlaying > latency) framePlaying -= latency;
Chris@5 422 else framePlaying = 0;
Chris@5 423
Chris@5 424 if (!constrained) {
Chris@5 425 if (!looping && framePlaying > m_lastModelEndFrame) {
Chris@5 426 framePlaying = m_lastModelEndFrame;
Chris@5 427 stop();
Chris@5 428 }
Chris@3 429 return framePlaying;
Chris@0 430 }
Chris@0 431
Chris@9 432 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@9 433 MultiSelection::SelectionList::const_iterator i;
Chris@3 434
Chris@4 435 i = selections.begin();
Chris@4 436 size_t rangeStart = i->getStartFrame();
Chris@4 437
Chris@4 438 i = selections.end();
Chris@4 439 --i;
Chris@4 440 size_t rangeEnd = i->getEndFrame();
Chris@4 441
Chris@3 442 for (i = selections.begin(); i != selections.end(); ++i) {
Chris@3 443 if (i->contains(bufferedFrame)) break;
Chris@3 444 }
Chris@3 445
Chris@3 446 size_t f = bufferedFrame;
Chris@3 447
Chris@4 448 // std::cerr << "getCurrentPlayingFrame: f=" << f << ", latency=" << latency << ", rangeEnd=" << rangeEnd << std::endl;
Chris@3 449
Chris@3 450 if (i == selections.end()) {
Chris@3 451 --i;
Chris@3 452 if (i->getEndFrame() + latency < f) {
Chris@4 453 // std::cerr << "framePlaying = " << framePlaying << ", rangeEnd = " << rangeEnd << std::endl;
Chris@4 454
Chris@5 455 if (!looping && (framePlaying > rangeEnd)) {
Chris@4 456 // std::cerr << "STOPPING" << std::endl;
Chris@4 457 stop();
Chris@4 458 return rangeEnd;
Chris@4 459 } else {
Chris@4 460 return framePlaying;
Chris@4 461 }
Chris@3 462 } else {
Chris@4 463 // std::cerr << "latency <- " << latency << "-(" << f << "-" << i->getEndFrame() << ")" << std::endl;
Chris@3 464 latency -= (f - i->getEndFrame());
Chris@3 465 f = i->getEndFrame();
Chris@3 466 }
Chris@3 467 }
Chris@3 468
Chris@4 469 // std::cerr << "i=(" << i->getStartFrame() << "," << i->getEndFrame() << ") f=" << f << ", latency=" << latency << std::endl;
Chris@3 470
Chris@3 471 while (latency > 0) {
Chris@3 472 size_t offset = f - i->getStartFrame();
Chris@3 473 if (offset >= latency) {
Chris@3 474 if (f > latency) {
Chris@3 475 framePlaying = f - latency;
Chris@3 476 } else {
Chris@3 477 framePlaying = 0;
Chris@3 478 }
Chris@3 479 break;
Chris@3 480 } else {
Chris@3 481 if (i == selections.begin()) {
Chris@5 482 if (looping) {
Chris@3 483 i = selections.end();
Chris@3 484 }
Chris@3 485 }
Chris@3 486 latency -= offset;
Chris@3 487 --i;
Chris@3 488 f = i->getEndFrame();
Chris@3 489 }
Chris@3 490 }
Chris@0 491
Chris@0 492 return framePlaying;
Chris@0 493 }
Chris@0 494
Chris@0 495 void
Chris@0 496 AudioCallbackPlaySource::setOutputLevels(float left, float right)
Chris@0 497 {
Chris@0 498 m_outputLeft = left;
Chris@0 499 m_outputRight = right;
Chris@0 500 }
Chris@0 501
Chris@0 502 bool
Chris@0 503 AudioCallbackPlaySource::getOutputLevels(float &left, float &right)
Chris@0 504 {
Chris@0 505 left = m_outputLeft;
Chris@0 506 right = m_outputRight;
Chris@0 507 return true;
Chris@0 508 }
Chris@0 509
Chris@0 510 void
Chris@0 511 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
Chris@0 512 {
Chris@0 513 m_targetSampleRate = sr;
Chris@1 514
Chris@1 515 if (getSourceSampleRate() != getTargetSampleRate()) {
Chris@1 516
Chris@1 517 int err = 0;
Chris@13 518 m_converter = src_new(SRC_SINC_BEST_QUALITY,
Chris@13 519 getTargetChannelCount(), &err);
Chris@1 520 if (!m_converter) {
Chris@1 521 std::cerr
Chris@1 522 << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
Chris@1 523 << src_strerror(err) << std::endl;
Chris@1 524
Chris@25 525 emit sampleRateMismatch(getSourceSampleRate(),
Chris@25 526 getTargetSampleRate(),
Chris@25 527 false);
Chris@25 528 } else {
Chris@25 529
Chris@25 530 emit sampleRateMismatch(getSourceSampleRate(),
Chris@25 531 getTargetSampleRate(),
Chris@25 532 true);
Chris@25 533 }
Chris@1 534 }
Chris@0 535 }
Chris@0 536
Chris@0 537 size_t
Chris@0 538 AudioCallbackPlaySource::getTargetSampleRate() const
Chris@0 539 {
Chris@0 540 if (m_targetSampleRate) return m_targetSampleRate;
Chris@0 541 else return getSourceSampleRate();
Chris@0 542 }
Chris@0 543
Chris@0 544 size_t
Chris@0 545 AudioCallbackPlaySource::getSourceChannelCount() const
Chris@0 546 {
Chris@6 547 return m_sourceChannelCount;
Chris@0 548 }
Chris@0 549
Chris@0 550 size_t
Chris@13 551 AudioCallbackPlaySource::getTargetChannelCount() const
Chris@13 552 {
Chris@13 553 if (m_sourceChannelCount < 2) return 2;
Chris@13 554 return m_sourceChannelCount;
Chris@13 555 }
Chris@13 556
Chris@13 557 size_t
Chris@0 558 AudioCallbackPlaySource::getSourceSampleRate() const
Chris@0 559 {
Chris@0 560 return m_sourceSampleRate;
Chris@0 561 }
Chris@0 562
Chris@0 563 AudioCallbackPlaySource::TimeStretcherData::TimeStretcherData(size_t channels,
Chris@0 564 size_t factor,
Chris@0 565 size_t blockSize) :
Chris@0 566 m_factor(factor),
Chris@0 567 m_blockSize(blockSize)
Chris@0 568 {
Chris@0 569 std::cerr << "TimeStretcherData::TimeStretcherData(" << channels << ", " << factor << ", " << blockSize << ")" << std::endl;
Chris@0 570
Chris@0 571 for (size_t ch = 0; ch < channels; ++ch) {
Chris@0 572 m_stretcher[ch] = StretcherBuffer
Chris@0 573 //!!! We really need to measure performance and work out
Chris@0 574 //what sort of quality level to use -- or at least to
Chris@0 575 //allow the user to configure it
Chris@0 576 (new IntegerTimeStretcher(factor, blockSize, 128),
Chris@0 577 new double[blockSize * factor]);
Chris@0 578 }
Chris@0 579 m_stretchInputBuffer = new double[blockSize];
Chris@0 580 }
Chris@0 581
Chris@0 582 AudioCallbackPlaySource::TimeStretcherData::~TimeStretcherData()
Chris@0 583 {
Chris@0 584 std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl;
Chris@0 585
Chris@0 586 while (!m_stretcher.empty()) {
Chris@0 587 delete m_stretcher.begin()->second.first;
Chris@0 588 delete[] m_stretcher.begin()->second.second;
Chris@0 589 m_stretcher.erase(m_stretcher.begin());
Chris@0 590 }
Chris@0 591 delete m_stretchInputBuffer;
Chris@0 592 }
Chris@0 593
Chris@0 594 IntegerTimeStretcher *
Chris@0 595 AudioCallbackPlaySource::TimeStretcherData::getStretcher(size_t channel)
Chris@0 596 {
Chris@0 597 return m_stretcher[channel].first;
Chris@0 598 }
Chris@0 599
Chris@0 600 double *
Chris@0 601 AudioCallbackPlaySource::TimeStretcherData::getOutputBuffer(size_t channel)
Chris@0 602 {
Chris@0 603 return m_stretcher[channel].second;
Chris@0 604 }
Chris@0 605
Chris@0 606 double *
Chris@0 607 AudioCallbackPlaySource::TimeStretcherData::getInputBuffer()
Chris@0 608 {
Chris@0 609 return m_stretchInputBuffer;
Chris@0 610 }
Chris@0 611
Chris@0 612 void
Chris@0 613 AudioCallbackPlaySource::TimeStretcherData::run(size_t channel)
Chris@0 614 {
Chris@0 615 getStretcher(channel)->process(getInputBuffer(),
Chris@0 616 getOutputBuffer(channel),
Chris@0 617 m_blockSize);
Chris@0 618 }
Chris@0 619
Chris@0 620 void
Chris@0 621 AudioCallbackPlaySource::setSlowdownFactor(size_t factor)
Chris@0 622 {
Chris@0 623 // Avoid locks -- create, assign, mark old one for scavenging
Chris@0 624 // later (as a call to getSourceSamples may still be using it)
Chris@0 625
Chris@0 626 TimeStretcherData *existingStretcher = m_timeStretcher;
Chris@0 627
Chris@0 628 if (existingStretcher && existingStretcher->getFactor() == factor) {
Chris@0 629 return;
Chris@0 630 }
Chris@0 631
Chris@0 632 if (factor > 1) {
Chris@0 633 TimeStretcherData *newStretcher = new TimeStretcherData
Chris@13 634 (getTargetChannelCount(), factor, getTargetBlockSize());
Chris@0 635 m_slowdownCounter = 0;
Chris@0 636 m_timeStretcher = newStretcher;
Chris@0 637 } else {
Chris@0 638 m_timeStretcher = 0;
Chris@0 639 }
Chris@0 640
Chris@0 641 if (existingStretcher) {
Chris@0 642 m_timeStretcherScavenger.claim(existingStretcher);
Chris@0 643 }
Chris@0 644 }
Chris@0 645
Chris@0 646 size_t
Chris@0 647 AudioCallbackPlaySource::getSourceSamples(size_t count, float **buffer)
Chris@0 648 {
Chris@0 649 if (!m_playing) {
Chris@13 650 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 651 for (size_t i = 0; i < count; ++i) {
Chris@0 652 buffer[ch][i] = 0.0;
Chris@0 653 }
Chris@0 654 }
Chris@0 655 return 0;
Chris@0 656 }
Chris@0 657
Chris@0 658 TimeStretcherData *timeStretcher = m_timeStretcher;
Chris@0 659
Chris@0 660 if (!timeStretcher || timeStretcher->getFactor() == 1) {
Chris@0 661
Chris@0 662 size_t got = 0;
Chris@0 663
Chris@13 664 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 665
Chris@6 666 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@0 667
Chris@6 668 if (rb) {
Chris@0 669
Chris@6 670 // this is marginally more likely to leave our channels in
Chris@6 671 // sync after a processing failure than just passing "count":
Chris@6 672 size_t request = count;
Chris@6 673 if (ch > 0) request = got;
Chris@6 674
Chris@6 675 got = rb->read(buffer[ch], request);
Chris@0 676
Chris@7 677 #ifdef DEBUG_AUDIO_PLAY_SOURCE_PLAYING
Chris@6 678 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", signalling for more (possibly)" << std::endl;
Chris@0 679 #endif
Chris@6 680 }
Chris@0 681
Chris@13 682 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@6 683 for (size_t i = got; i < count; ++i) {
Chris@6 684 buffer[ch][i] = 0.0;
Chris@6 685 }
Chris@0 686 }
Chris@0 687 }
Chris@0 688
Chris@0 689 m_condition.wakeAll();
Chris@0 690 return got;
Chris@0 691 }
Chris@0 692
Chris@0 693 if (m_slowdownCounter == 0) {
Chris@0 694
Chris@0 695 size_t got = 0;
Chris@0 696 double *ib = timeStretcher->getInputBuffer();
Chris@0 697
Chris@13 698 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 699
Chris@6 700 RingBuffer<float> *rb = getReadRingBuffer(ch);
Chris@6 701
Chris@6 702 if (rb) {
Chris@6 703
Chris@6 704 size_t request = count;
Chris@6 705 if (ch > 0) request = got; // see above
Chris@6 706 got = rb->read(buffer[ch], request);
Chris@0 707
Chris@0 708 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@6 709 std::cout << "AudioCallbackPlaySource::getSamples: got " << got << " samples on channel " << ch << ", running time stretcher" << std::endl;
Chris@0 710 #endif
Chris@0 711
Chris@6 712 for (size_t i = 0; i < count; ++i) {
Chris@6 713 ib[i] = buffer[ch][i];
Chris@6 714 }
Chris@6 715
Chris@6 716 timeStretcher->run(ch);
Chris@0 717 }
Chris@0 718 }
Chris@0 719
Chris@0 720 } else if (m_slowdownCounter >= timeStretcher->getFactor()) {
Chris@0 721 // reset this in case the factor has changed leaving the
Chris@0 722 // counter out of range
Chris@0 723 m_slowdownCounter = 0;
Chris@0 724 }
Chris@0 725
Chris@13 726 for (size_t ch = 0; ch < getTargetChannelCount(); ++ch) {
Chris@0 727
Chris@0 728 double *ob = timeStretcher->getOutputBuffer(ch);
Chris@0 729
Chris@0 730 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 731 std::cerr << "AudioCallbackPlaySource::getSamples: Copying from (" << (m_slowdownCounter * count) << "," << count << ") to buffer" << std::endl;
Chris@0 732 #endif
Chris@0 733
Chris@0 734 for (size_t i = 0; i < count; ++i) {
Chris@0 735 buffer[ch][i] = ob[m_slowdownCounter * count + i];
Chris@0 736 }
Chris@0 737 }
Chris@0 738
Chris@7 739 //!!! if (m_slowdownCounter == 0) m_condition.wakeAll();
Chris@0 740 m_slowdownCounter = (m_slowdownCounter + 1) % timeStretcher->getFactor();
Chris@0 741 return count;
Chris@0 742 }
Chris@0 743
Chris@6 744 // Called from fill thread, m_playing true, mutex held
Chris@7 745 bool
Chris@0 746 AudioCallbackPlaySource::fillBuffers()
Chris@0 747 {
Chris@0 748 static float *tmp = 0;
Chris@0 749 static size_t tmpSize = 0;
Chris@0 750
Chris@0 751 size_t space = 0;
Chris@13 752 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@6 753 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@6 754 if (wb) {
Chris@6 755 size_t spaceHere = wb->getWriteSpace();
Chris@6 756 if (c == 0 || spaceHere < space) space = spaceHere;
Chris@6 757 }
Chris@0 758 }
Chris@0 759
Chris@7 760 if (space == 0) return false;
Chris@4 761
Chris@13 762 size_t f = m_writeBufferFill;
Chris@13 763
Chris@13 764 bool readWriteEqual = (m_readBuffers == m_writeBuffers);
Chris@13 765
Chris@0 766 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 767 std::cout << "AudioCallbackPlaySourceFillThread: filling " << space << " frames" << std::endl;
Chris@0 768 #endif
Chris@0 769
Chris@0 770 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 771 std::cout << "buffered to " << f << " already" << std::endl;
Chris@0 772 #endif
Chris@0 773
Chris@0 774 bool resample = (getSourceSampleRate() != getTargetSampleRate());
Chris@1 775
Chris@1 776 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@1 777 std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
Chris@1 778 #endif
Chris@1 779
Chris@13 780 size_t channels = getTargetChannelCount();
Chris@13 781
Chris@0 782 size_t orig = space;
Chris@0 783 size_t got = 0;
Chris@0 784
Chris@0 785 static float **bufferPtrs = 0;
Chris@0 786 static size_t bufferPtrCount = 0;
Chris@0 787
Chris@0 788 if (bufferPtrCount < channels) {
Chris@0 789 if (bufferPtrs) delete[] bufferPtrs;
Chris@0 790 bufferPtrs = new float *[channels];
Chris@0 791 bufferPtrCount = channels;
Chris@0 792 }
Chris@0 793
Chris@0 794 size_t generatorBlockSize = m_audioGenerator->getBlockSize();
Chris@0 795
Chris@1 796 if (resample && !m_converter) {
Chris@1 797 static bool warned = false;
Chris@1 798 if (!warned) {
Chris@1 799 std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
Chris@1 800 warned = true;
Chris@1 801 }
Chris@1 802 }
Chris@1 803
Chris@0 804 if (resample && m_converter) {
Chris@0 805
Chris@0 806 double ratio =
Chris@0 807 double(getTargetSampleRate()) / double(getSourceSampleRate());
Chris@0 808 orig = size_t(orig / ratio + 0.1);
Chris@0 809
Chris@0 810 // orig must be a multiple of generatorBlockSize
Chris@0 811 orig = (orig / generatorBlockSize) * generatorBlockSize;
Chris@7 812 if (orig == 0) return false;
Chris@0 813
Chris@0 814 size_t work = std::max(orig, space);
Chris@0 815
Chris@0 816 // We only allocate one buffer, but we use it in two halves.
Chris@0 817 // We place the non-interleaved values in the second half of
Chris@0 818 // the buffer (orig samples for channel 0, orig samples for
Chris@0 819 // channel 1 etc), and then interleave them into the first
Chris@0 820 // half of the buffer. Then we resample back into the second
Chris@0 821 // half (interleaved) and de-interleave the results back to
Chris@0 822 // the start of the buffer for insertion into the ringbuffers.
Chris@0 823 // What a faff -- especially as we've already de-interleaved
Chris@0 824 // the audio data from the source file elsewhere before we
Chris@0 825 // even reach this point.
Chris@0 826
Chris@0 827 if (tmpSize < channels * work * 2) {
Chris@0 828 delete[] tmp;
Chris@0 829 tmp = new float[channels * work * 2];
Chris@0 830 tmpSize = channels * work * 2;
Chris@0 831 }
Chris@0 832
Chris@0 833 float *nonintlv = tmp + channels * work;
Chris@0 834 float *intlv = tmp;
Chris@0 835 float *srcout = tmp + channels * work;
Chris@0 836
Chris@0 837 for (size_t c = 0; c < channels; ++c) {
Chris@0 838 for (size_t i = 0; i < orig; ++i) {
Chris@0 839 nonintlv[channels * i + c] = 0.0f;
Chris@0 840 }
Chris@0 841 }
Chris@0 842
Chris@3 843 for (size_t c = 0; c < channels; ++c) {
Chris@3 844 bufferPtrs[c] = nonintlv + c * orig;
Chris@3 845 }
Chris@0 846
Chris@6 847 got = mixModels(f, orig, bufferPtrs);
Chris@0 848
Chris@0 849 // and interleave into first half
Chris@0 850 for (size_t c = 0; c < channels; ++c) {
Chris@6 851 for (size_t i = 0; i < got; ++i) {
Chris@6 852 float sample = nonintlv[c * got + i];
Chris@0 853 intlv[channels * i + c] = sample;
Chris@0 854 }
Chris@0 855 }
Chris@0 856
Chris@0 857 SRC_DATA data;
Chris@0 858 data.data_in = intlv;
Chris@0 859 data.data_out = srcout;
Chris@6 860 data.input_frames = got;
Chris@0 861 data.output_frames = work;
Chris@0 862 data.src_ratio = ratio;
Chris@0 863 data.end_of_input = 0;
Chris@0 864
Chris@0 865 int err = src_process(m_converter, &data);
Chris@6 866 // size_t toCopy = size_t(work * ratio + 0.1);
Chris@6 867 size_t toCopy = size_t(got * ratio + 0.1);
Chris@6 868
Chris@0 869 if (err) {
Chris@0 870 std::cerr
Chris@0 871 << "AudioCallbackPlaySourceFillThread: ERROR in samplerate conversion: "
Chris@0 872 << src_strerror(err) << std::endl;
Chris@0 873 //!!! Then what?
Chris@0 874 } else {
Chris@0 875 got = data.input_frames_used;
Chris@0 876 toCopy = data.output_frames_gen;
Chris@0 877 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 878 std::cerr << "Resampled " << got << " frames to " << toCopy << " frames" << std::endl;
Chris@0 879 #endif
Chris@0 880 }
Chris@0 881
Chris@0 882 for (size_t c = 0; c < channels; ++c) {
Chris@0 883 for (size_t i = 0; i < toCopy; ++i) {
Chris@0 884 tmp[i] = srcout[channels * i + c];
Chris@0 885 }
Chris@6 886 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@6 887 if (wb) wb->write(tmp, toCopy);
Chris@0 888 }
Chris@3 889
Chris@13 890 m_writeBufferFill = f;
Chris@13 891 if (readWriteEqual) m_readBufferFill = f;
Chris@13 892
Chris@0 893 } else {
Chris@0 894
Chris@0 895 // space must be a multiple of generatorBlockSize
Chris@0 896 space = (space / generatorBlockSize) * generatorBlockSize;
Chris@7 897 if (space == 0) return false;
Chris@0 898
Chris@0 899 if (tmpSize < channels * space) {
Chris@0 900 delete[] tmp;
Chris@0 901 tmp = new float[channels * space];
Chris@0 902 tmpSize = channels * space;
Chris@0 903 }
Chris@0 904
Chris@0 905 for (size_t c = 0; c < channels; ++c) {
Chris@0 906
Chris@0 907 bufferPtrs[c] = tmp + c * space;
Chris@3 908
Chris@0 909 for (size_t i = 0; i < space; ++i) {
Chris@0 910 tmp[c * space + i] = 0.0f;
Chris@0 911 }
Chris@0 912 }
Chris@0 913
Chris@6 914 size_t got = mixModels(f, space, bufferPtrs);
Chris@0 915
Chris@0 916 for (size_t c = 0; c < channels; ++c) {
Chris@0 917
Chris@6 918 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@6 919 if (wb) wb->write(bufferPtrs[c], got);
Chris@0 920
Chris@0 921 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@6 922 if (wb)
Chris@6 923 std::cerr << "Wrote " << got << " frames for ch " << c << ", now "
Chris@6 924 << wb->getReadSpace() << " to read"
Chris@6 925 << std::endl;
Chris@0 926 #endif
Chris@0 927 }
Chris@3 928
Chris@13 929 m_writeBufferFill = f;
Chris@13 930 if (readWriteEqual) m_readBufferFill = f;
Chris@13 931
Chris@3 932 //!!! 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@0 933 }
Chris@7 934
Chris@7 935 return true;
Chris@3 936 }
Chris@3 937
Chris@6 938 size_t
Chris@3 939 AudioCallbackPlaySource::mixModels(size_t &frame, size_t count, float **buffers)
Chris@3 940 {
Chris@3 941 size_t processed = 0;
Chris@3 942 size_t chunkStart = frame;
Chris@3 943 size_t chunkSize = count;
Chris@6 944 size_t selectionSize = 0;
Chris@3 945 size_t nextChunkStart = chunkStart + chunkSize;
Chris@0 946
Chris@5 947 bool looping = m_viewManager->getPlayLoopMode();
Chris@5 948 bool constrained = (m_viewManager->getPlaySelectionMode() &&
Chris@5 949 !m_viewManager->getSelections().empty());
Chris@3 950
Chris@3 951 static float **chunkBufferPtrs = 0;
Chris@3 952 static size_t chunkBufferPtrCount = 0;
Chris@13 953 size_t channels = getTargetChannelCount();
Chris@3 954
Chris@3 955 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@3 956 std::cerr << "Selection playback: start " << frame << ", size " << count <<", channels " << channels << std::endl;
Chris@3 957 #endif
Chris@3 958
Chris@3 959 if (chunkBufferPtrCount < channels) {
Chris@3 960 if (chunkBufferPtrs) delete[] chunkBufferPtrs;
Chris@3 961 chunkBufferPtrs = new float *[channels];
Chris@3 962 chunkBufferPtrCount = channels;
Chris@3 963 }
Chris@3 964
Chris@3 965 for (size_t c = 0; c < channels; ++c) {
Chris@3 966 chunkBufferPtrs[c] = buffers[c];
Chris@3 967 }
Chris@3 968
Chris@3 969 while (processed < count) {
Chris@3 970
Chris@3 971 chunkSize = count - processed;
Chris@3 972 nextChunkStart = chunkStart + chunkSize;
Chris@6 973 selectionSize = 0;
Chris@3 974
Chris@4 975 size_t fadeIn = 0, fadeOut = 0;
Chris@4 976
Chris@5 977 if (constrained) {
Chris@3 978
Chris@3 979 Selection selection =
Chris@3 980 m_viewManager->getContainingSelection(chunkStart, true);
Chris@3 981
Chris@3 982 if (selection.isEmpty()) {
Chris@5 983 if (looping) {
Chris@3 984 selection = *m_viewManager->getSelections().begin();
Chris@3 985 chunkStart = selection.getStartFrame();
Chris@4 986 fadeIn = 50;
Chris@3 987 }
Chris@3 988 }
Chris@3 989
Chris@3 990 if (selection.isEmpty()) {
Chris@3 991
Chris@3 992 chunkSize = 0;
Chris@3 993 nextChunkStart = chunkStart;
Chris@3 994
Chris@3 995 } else {
Chris@3 996
Chris@6 997 selectionSize =
Chris@6 998 selection.getEndFrame() -
Chris@6 999 selection.getStartFrame();
Chris@6 1000
Chris@3 1001 if (chunkStart < selection.getStartFrame()) {
Chris@3 1002 chunkStart = selection.getStartFrame();
Chris@4 1003 fadeIn = 50;
Chris@3 1004 }
Chris@3 1005
Chris@4 1006 nextChunkStart = chunkStart + chunkSize;
Chris@4 1007
Chris@6 1008 if (nextChunkStart >= selection.getEndFrame()) {
Chris@4 1009 nextChunkStart = selection.getEndFrame();
Chris@4 1010 fadeOut = 50;
Chris@4 1011 }
Chris@3 1012
Chris@3 1013 chunkSize = nextChunkStart - chunkStart;
Chris@3 1014 }
Chris@4 1015
Chris@5 1016 } else if (looping && m_lastModelEndFrame > 0) {
Chris@4 1017
Chris@4 1018 if (chunkStart >= m_lastModelEndFrame) {
Chris@4 1019 chunkStart = 0;
Chris@4 1020 }
Chris@4 1021 if (chunkSize > m_lastModelEndFrame - chunkStart) {
Chris@4 1022 chunkSize = m_lastModelEndFrame - chunkStart;
Chris@4 1023 }
Chris@4 1024 nextChunkStart = chunkStart + chunkSize;
Chris@3 1025 }
Chris@3 1026
Chris@6 1027 // std::cerr << "chunkStart " << chunkStart << ", chunkSize " << chunkSize << ", nextChunkStart " << nextChunkStart << ", frame " << frame << ", count " << count << ", processed " << processed << std::endl;
Chris@6 1028
Chris@3 1029 if (!chunkSize) {
Chris@3 1030 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@3 1031 std::cerr << "Ending selection playback at " << nextChunkStart << std::endl;
Chris@3 1032 #endif
Chris@3 1033 // We need to maintain full buffers so that the other
Chris@3 1034 // thread can tell where it's got to in the playback -- so
Chris@3 1035 // return the full amount here
Chris@3 1036 frame = frame + count;
Chris@6 1037 return count;
Chris@3 1038 }
Chris@3 1039
Chris@3 1040 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@3 1041 std::cerr << "Selection playback: chunk at " << chunkStart << " -> " << nextChunkStart << " (size " << chunkSize << ")" << std::endl;
Chris@3 1042 #endif
Chris@3 1043
Chris@3 1044 size_t got = 0;
Chris@3 1045
Chris@6 1046 if (selectionSize < 100) {
Chris@4 1047 fadeIn = 0;
Chris@4 1048 fadeOut = 0;
Chris@6 1049 } else if (selectionSize < 300) {
Chris@4 1050 if (fadeIn > 0) fadeIn = 10;
Chris@4 1051 if (fadeOut > 0) fadeOut = 10;
Chris@4 1052 }
Chris@4 1053
Chris@4 1054 if (fadeIn > 0) {
Chris@4 1055 if (processed * 2 < fadeIn) {
Chris@4 1056 fadeIn = processed * 2;
Chris@4 1057 }
Chris@4 1058 }
Chris@4 1059
Chris@4 1060 if (fadeOut > 0) {
Chris@6 1061 if ((count - processed - chunkSize) * 2 < fadeOut) {
Chris@6 1062 fadeOut = (count - processed - chunkSize) * 2;
Chris@4 1063 }
Chris@4 1064 }
Chris@4 1065
Chris@3 1066 for (std::set<Model *>::iterator mi = m_models.begin();
Chris@3 1067 mi != m_models.end(); ++mi) {
Chris@3 1068
Chris@3 1069 got = m_audioGenerator->mixModel(*mi, chunkStart,
Chris@4 1070 chunkSize, chunkBufferPtrs,
Chris@4 1071 fadeIn, fadeOut);
Chris@3 1072 }
Chris@3 1073
Chris@3 1074 for (size_t c = 0; c < channels; ++c) {
Chris@3 1075 chunkBufferPtrs[c] += chunkSize;
Chris@3 1076 }
Chris@3 1077
Chris@3 1078 processed += chunkSize;
Chris@3 1079 chunkStart = nextChunkStart;
Chris@3 1080 }
Chris@3 1081
Chris@3 1082 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@7 1083 std::cerr << "Returning selection playback " << processed << " frames to " << nextChunkStart << std::endl;
Chris@3 1084 #endif
Chris@3 1085
Chris@3 1086 frame = nextChunkStart;
Chris@6 1087 return processed;
Chris@3 1088 }
Chris@0 1089
Chris@0 1090 void
Chris@13 1091 AudioCallbackPlaySource::unifyRingBuffers()
Chris@13 1092 {
Chris@13 1093 if (m_readBuffers == m_writeBuffers) return;
Chris@13 1094
Chris@13 1095 // only unify if there will be something to read
Chris@13 1096 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@13 1097 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@13 1098 if (wb) {
Chris@13 1099 if (wb->getReadSpace() < m_blockSize * 2) {
Chris@13 1100 if ((m_writeBufferFill + m_blockSize * 2) <
Chris@13 1101 m_lastModelEndFrame) {
Chris@13 1102 // OK, we don't have enough and there's more to
Chris@13 1103 // read -- don't unify until we can do better
Chris@13 1104 return;
Chris@13 1105 }
Chris@13 1106 }
Chris@13 1107 break;
Chris@13 1108 }
Chris@13 1109 }
Chris@13 1110
Chris@13 1111 size_t rf = m_readBufferFill;
Chris@13 1112 RingBuffer<float> *rb = getReadRingBuffer(0);
Chris@13 1113 if (rb) {
Chris@13 1114 size_t rs = rb->getReadSpace();
Chris@13 1115 //!!! incorrect when in non-contiguous selection, see comments elsewhere
Chris@13 1116 // std::cerr << "rs = " << rs << std::endl;
Chris@13 1117 if (rs < rf) rf -= rs;
Chris@13 1118 else rf = 0;
Chris@13 1119 }
Chris@13 1120
Chris@13 1121 //std::cerr << "m_readBufferFill = " << m_readBufferFill << ", rf = " << rf << ", m_writeBufferFill = " << m_writeBufferFill << std::endl;
Chris@13 1122
Chris@13 1123 size_t wf = m_writeBufferFill;
Chris@13 1124 size_t skip = 0;
Chris@13 1125 for (size_t c = 0; c < getTargetChannelCount(); ++c) {
Chris@13 1126 RingBuffer<float> *wb = getWriteRingBuffer(c);
Chris@13 1127 if (wb) {
Chris@13 1128 if (c == 0) {
Chris@13 1129
Chris@13 1130 size_t wrs = wb->getReadSpace();
Chris@13 1131 // std::cerr << "wrs = " << wrs << std::endl;
Chris@13 1132
Chris@13 1133 if (wrs < wf) wf -= wrs;
Chris@13 1134 else wf = 0;
Chris@13 1135 // std::cerr << "wf = " << wf << std::endl;
Chris@13 1136
Chris@13 1137 if (wf < rf) skip = rf - wf;
Chris@13 1138 if (skip == 0) break;
Chris@13 1139 }
Chris@13 1140
Chris@13 1141 // std::cerr << "skipping " << skip << std::endl;
Chris@13 1142 wb->skip(skip);
Chris@13 1143 }
Chris@13 1144 }
Chris@13 1145
Chris@13 1146 m_bufferScavenger.claim(m_readBuffers);
Chris@13 1147 m_readBuffers = m_writeBuffers;
Chris@13 1148 m_readBufferFill = m_writeBufferFill;
Chris@13 1149 std::cerr << "unified" << std::endl;
Chris@13 1150 }
Chris@13 1151
Chris@13 1152 void
Chris@0 1153 AudioCallbackPlaySource::AudioCallbackPlaySourceFillThread::run()
Chris@0 1154 {
Chris@0 1155 AudioCallbackPlaySource &s(m_source);
Chris@0 1156
Chris@0 1157 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1158 std::cerr << "AudioCallbackPlaySourceFillThread starting" << std::endl;
Chris@0 1159 #endif
Chris@0 1160
Chris@0 1161 s.m_mutex.lock();
Chris@0 1162
Chris@0 1163 bool previouslyPlaying = s.m_playing;
Chris@7 1164 bool work = false;
Chris@0 1165
Chris@0 1166 while (!s.m_exiting) {
Chris@0 1167
Chris@13 1168 s.unifyRingBuffers();
Chris@6 1169 s.m_bufferScavenger.scavenge();
Chris@0 1170 s.m_timeStretcherScavenger.scavenge();
Chris@0 1171
Chris@7 1172 if (work && s.m_playing && s.getSourceSampleRate()) {
Chris@24 1173
Chris@24 1174 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@24 1175 std::cout << "AudioCallbackPlaySourceFillThread: not waiting" << std::endl;
Chris@24 1176 #endif
Chris@7 1177
Chris@7 1178 s.m_mutex.unlock();
Chris@7 1179 s.m_mutex.lock();
Chris@7 1180
Chris@7 1181 } else {
Chris@7 1182
Chris@7 1183 float ms = 100;
Chris@7 1184 if (s.getSourceSampleRate() > 0) {
Chris@7 1185 ms = float(m_ringBufferSize) / float(s.getSourceSampleRate()) * 1000.0;
Chris@7 1186 }
Chris@7 1187
Chris@7 1188 if (s.m_playing) ms /= 10;
Chris@7 1189
Chris@7 1190 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@7 1191 std::cout << "AudioCallbackPlaySourceFillThread: waiting for " << ms << "ms..." << std::endl;
Chris@7 1192 #endif
Chris@7 1193
Chris@7 1194 s.m_condition.wait(&s.m_mutex, size_t(ms));
Chris@0 1195 }
Chris@0 1196
Chris@0 1197 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1198 std::cout << "AudioCallbackPlaySourceFillThread: awoken" << std::endl;
Chris@0 1199 #endif
Chris@0 1200
Chris@7 1201 work = false;
Chris@7 1202
Chris@0 1203 if (!s.getSourceSampleRate()) continue;
Chris@0 1204
Chris@0 1205 bool playing = s.m_playing;
Chris@0 1206
Chris@0 1207 if (playing && !previouslyPlaying) {
Chris@0 1208 #ifdef DEBUG_AUDIO_PLAY_SOURCE
Chris@0 1209 std::cout << "AudioCallbackPlaySourceFillThread: playback state changed, resetting" << std::endl;
Chris@0 1210 #endif
Chris@13 1211 for (size_t c = 0; c < s.getTargetChannelCount(); ++c) {
Chris@6 1212 RingBuffer<float> *rb = s.getReadRingBuffer(c);
Chris@6 1213 if (rb) rb->reset();
Chris@0 1214 }
Chris@0 1215 }
Chris@0 1216 previouslyPlaying = playing;
Chris@0 1217
Chris@7 1218 work = s.fillBuffers();
Chris@0 1219 }
Chris@0 1220
Chris@0 1221 s.m_mutex.unlock();
Chris@0 1222 }
Chris@0 1223
Chris@0 1224
Chris@0 1225
Chris@0 1226 #ifdef INCLUDE_MOCFILES
Chris@0 1227 #include "AudioCallbackPlaySource.moc.cpp"
Chris@0 1228 #endif
Chris@0 1229