annotate audioio/AudioGenerator.cpp @ 302:febd49356ef3 tonioni

Make the default zoom level somewhat configurable
author Chris Cannam
date Wed, 04 Dec 2013 18:22:28 +0000
parents ae1eedd6951f
children 9716c75499ef
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.
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 "AudioGenerator.h"
Chris@43 17
Chris@43 18 #include "base/TempDirectory.h"
Chris@43 19 #include "base/PlayParameters.h"
Chris@43 20 #include "base/PlayParameterRepository.h"
Chris@43 21 #include "base/Pitch.h"
Chris@43 22 #include "base/Exceptions.h"
Chris@43 23
Chris@43 24 #include "data/model/NoteModel.h"
Chris@278 25 #include "data/model/FlexiNoteModel.h"
Chris@43 26 #include "data/model/DenseTimeValueModel.h"
Chris@43 27 #include "data/model/SparseOneDimensionalModel.h"
Chris@299 28 #include "data/model/NoteData.h"
Chris@43 29
Chris@43 30 #include "plugin/RealTimePluginFactory.h"
Chris@43 31 #include "plugin/RealTimePluginInstance.h"
Chris@43 32 #include "plugin/PluginIdentifier.h"
Chris@43 33 #include "plugin/PluginXml.h"
Chris@43 34 #include "plugin/api/alsa/seq_event.h"
Chris@43 35
Chris@43 36 #include <iostream>
Chris@167 37 #include <cmath>
Chris@43 38
Chris@43 39 #include <QDir>
Chris@43 40 #include <QFile>
Chris@43 41
Chris@43 42 const size_t
Chris@43 43 AudioGenerator::m_pluginBlockSize = 2048;
Chris@43 44
Chris@43 45 QString
Chris@43 46 AudioGenerator::m_sampleDir = "";
Chris@43 47
Chris@43 48 //#define DEBUG_AUDIO_GENERATOR 1
Chris@43 49
Chris@43 50 AudioGenerator::AudioGenerator() :
Chris@43 51 m_sourceSampleRate(0),
Chris@43 52 m_targetChannelCount(1),
Chris@43 53 m_soloing(false)
Chris@43 54 {
Chris@108 55 initialiseSampleDir();
Chris@43 56
Chris@43 57 connect(PlayParameterRepository::getInstance(),
Chris@108 58 SIGNAL(playPluginIdChanged(const Playable *, QString)),
Chris@43 59 this,
Chris@108 60 SLOT(playPluginIdChanged(const Playable *, QString)));
Chris@108 61
Chris@108 62 connect(PlayParameterRepository::getInstance(),
Chris@108 63 SIGNAL(playPluginConfigurationChanged(const Playable *, QString)),
Chris@108 64 this,
Chris@108 65 SLOT(playPluginConfigurationChanged(const Playable *, QString)));
Chris@43 66 }
Chris@43 67
Chris@43 68 AudioGenerator::~AudioGenerator()
Chris@43 69 {
Chris@177 70 #ifdef DEBUG_AUDIO_GENERATOR
Chris@233 71 SVDEBUG << "AudioGenerator::~AudioGenerator" << endl;
Chris@177 72 #endif
Chris@43 73 }
Chris@43 74
Chris@108 75 void
Chris@108 76 AudioGenerator::initialiseSampleDir()
Chris@43 77 {
Chris@108 78 if (m_sampleDir != "") return;
Chris@108 79
Chris@108 80 try {
Chris@108 81 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
Chris@108 82 } catch (DirectoryCreationFailed f) {
Chris@293 83 cerr << "WARNING: AudioGenerator::initialiseSampleDir:"
Chris@108 84 << " Failed to create temporary sample directory"
Chris@293 85 << endl;
Chris@108 86 m_sampleDir = "";
Chris@108 87 return;
Chris@108 88 }
Chris@108 89
Chris@108 90 QDir sampleResourceDir(":/samples", "*.wav");
Chris@108 91
Chris@108 92 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
Chris@108 93
Chris@108 94 QString fileName(sampleResourceDir[i]);
Chris@108 95 QFile file(sampleResourceDir.filePath(fileName));
Chris@151 96 QString target = QDir(m_sampleDir).filePath(fileName);
Chris@108 97
Chris@151 98 if (!file.copy(target)) {
Chris@293 99 cerr << "WARNING: AudioGenerator::getSampleDir: "
Chris@294 100 << "Unable to copy " << fileName
Chris@108 101 << " into temporary directory \""
Chris@293 102 << m_sampleDir << "\"" << endl;
Chris@151 103 } else {
Chris@151 104 QFile tf(target);
Chris@151 105 tf.setPermissions(tf.permissions() |
Chris@151 106 QFile::WriteOwner |
Chris@151 107 QFile::WriteUser);
Chris@108 108 }
Chris@43 109 }
Chris@43 110 }
Chris@43 111
Chris@43 112 bool
Chris@43 113 AudioGenerator::addModel(Model *model)
Chris@43 114 {
Chris@43 115 if (m_sourceSampleRate == 0) {
Chris@43 116
Chris@43 117 m_sourceSampleRate = model->getSampleRate();
Chris@43 118
Chris@43 119 } else {
Chris@43 120
Chris@43 121 DenseTimeValueModel *dtvm =
Chris@43 122 dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 123
Chris@43 124 if (dtvm) {
Chris@43 125 m_sourceSampleRate = model->getSampleRate();
Chris@43 126 return true;
Chris@43 127 }
Chris@43 128 }
Chris@43 129
Chris@43 130 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@43 131 if (plugin) {
Chris@43 132 QMutexLocker locker(&m_mutex);
Chris@43 133 m_synthMap[model] = plugin;
Chris@43 134 return true;
Chris@43 135 }
Chris@43 136
Chris@43 137 return false;
Chris@43 138 }
Chris@43 139
Chris@43 140 void
Chris@108 141 AudioGenerator::playPluginIdChanged(const Playable *playable, QString)
Chris@43 142 {
Chris@108 143 const Model *model = dynamic_cast<const Model *>(playable);
Chris@108 144 if (!model) {
Chris@293 145 cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable "
Chris@108 146 << playable << " is not a supported model type"
Chris@293 147 << endl;
Chris@108 148 return;
Chris@108 149 }
Chris@108 150
Chris@43 151 if (m_synthMap.find(model) == m_synthMap.end()) return;
Chris@43 152
Chris@43 153 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@43 154 if (plugin) {
Chris@43 155 QMutexLocker locker(&m_mutex);
Chris@43 156 delete m_synthMap[model];
Chris@43 157 m_synthMap[model] = plugin;
Chris@43 158 }
Chris@43 159 }
Chris@43 160
Chris@43 161 void
Chris@108 162 AudioGenerator::playPluginConfigurationChanged(const Playable *playable,
Chris@43 163 QString configurationXml)
Chris@43 164 {
Chris@233 165 // SVDEBUG << "AudioGenerator::playPluginConfigurationChanged" << endl;
Chris@43 166
Chris@108 167 const Model *model = dynamic_cast<const Model *>(playable);
Chris@108 168 if (!model) {
Chris@293 169 cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable "
Chris@108 170 << playable << " is not a supported model type"
Chris@293 171 << endl;
Chris@108 172 return;
Chris@108 173 }
Chris@108 174
Chris@43 175 if (m_synthMap.find(model) == m_synthMap.end()) {
Chris@233 176 SVDEBUG << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << endl;
Chris@43 177 return;
Chris@43 178 }
Chris@43 179
Chris@43 180 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@43 181 if (plugin) {
Chris@43 182 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@43 183 }
Chris@43 184 }
Chris@43 185
Chris@43 186 void
Chris@43 187 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin)
Chris@43 188 {
Chris@108 189 if (m_sampleDir != "") {
Chris@108 190 plugin->configure("sampledir", m_sampleDir.toStdString());
Chris@108 191 }
Chris@43 192 }
Chris@43 193
Chris@43 194 RealTimePluginInstance *
Chris@43 195 AudioGenerator::loadPluginFor(const Model *model)
Chris@43 196 {
Chris@43 197 QString pluginId, configurationXml;
Chris@43 198
Chris@108 199 const Playable *playable = model;
Chris@108 200 if (!playable || !playable->canPlay()) return 0;
Chris@108 201
Chris@43 202 PlayParameters *parameters =
Chris@108 203 PlayParameterRepository::getInstance()->getPlayParameters(playable);
Chris@43 204 if (parameters) {
Chris@43 205 pluginId = parameters->getPlayPluginId();
Chris@43 206 configurationXml = parameters->getPlayPluginConfiguration();
Chris@43 207 }
Chris@43 208
Chris@276 209 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): id = " << pluginId << std::endl;
Chris@276 210
Chris@276 211 if (pluginId == "") {
Chris@276 212 SVDEBUG << "AudioGenerator::loadPluginFor(" << model << "): parameters contain empty plugin ID, skipping" << endl;
Chris@276 213 return 0;
Chris@276 214 }
Chris@43 215
Chris@43 216 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
Chris@43 217 if (!plugin) return 0;
Chris@43 218
Chris@276 219 std::cerr << "AudioGenerator::loadPluginFor(" << model << "): loaded plugin "
Chris@276 220 << plugin << std::endl;
Chris@276 221
Chris@43 222 if (configurationXml != "") {
Chris@43 223 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@179 224 setSampleDir(plugin);
Chris@43 225 }
Chris@43 226
Chris@108 227 configurationXml = PluginXml(plugin).toXmlString();
Chris@108 228
Chris@43 229 if (parameters) {
Chris@43 230 parameters->setPlayPluginId(pluginId);
Chris@43 231 parameters->setPlayPluginConfiguration(configurationXml);
Chris@43 232 }
Chris@43 233
Chris@43 234 return plugin;
Chris@43 235 }
Chris@43 236
Chris@43 237 RealTimePluginInstance *
Chris@43 238 AudioGenerator::loadPlugin(QString pluginId, QString program)
Chris@43 239 {
Chris@43 240 RealTimePluginFactory *factory =
Chris@43 241 RealTimePluginFactory::instanceFor(pluginId);
Chris@43 242
Chris@43 243 if (!factory) {
Chris@293 244 cerr << "Failed to get plugin factory" << endl;
Chris@274 245 return 0;
Chris@43 246 }
Chris@43 247
Chris@43 248 RealTimePluginInstance *instance =
Chris@43 249 factory->instantiatePlugin
Chris@43 250 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
Chris@43 251
Chris@43 252 if (!instance) {
Chris@293 253 cerr << "Failed to instantiate plugin " << pluginId << endl;
Chris@43 254 return 0;
Chris@43 255 }
Chris@43 256
Chris@43 257 setSampleDir(instance);
Chris@43 258
Chris@43 259 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
Chris@43 260 instance->setParameterValue(i, instance->getParameterDefault(i));
Chris@43 261 }
Chris@43 262 std::string defaultProgram = instance->getProgram(0, 0);
Chris@43 263 if (defaultProgram != "") {
Chris@295 264 cerr << "first selecting default program " << defaultProgram << endl;
Chris@43 265 instance->selectProgram(defaultProgram);
Chris@43 266 }
Chris@43 267 if (program != "") {
Chris@295 268 cerr << "now selecting desired program " << program << endl;
Chris@43 269 instance->selectProgram(program.toStdString());
Chris@43 270 }
Chris@43 271 instance->setIdealChannelCount(m_targetChannelCount); // reset!
Chris@43 272
Chris@43 273 return instance;
Chris@43 274 }
Chris@43 275
Chris@43 276 void
Chris@43 277 AudioGenerator::removeModel(Model *model)
Chris@43 278 {
Chris@43 279 SparseOneDimensionalModel *sodm =
Chris@43 280 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@43 281 if (!sodm) return; // nothing to do
Chris@43 282
Chris@43 283 QMutexLocker locker(&m_mutex);
Chris@43 284
Chris@43 285 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
Chris@43 286
Chris@43 287 RealTimePluginInstance *instance = m_synthMap[sodm];
Chris@43 288 m_synthMap.erase(sodm);
Chris@43 289 delete instance;
Chris@43 290 }
Chris@43 291
Chris@43 292 void
Chris@43 293 AudioGenerator::clearModels()
Chris@43 294 {
Chris@43 295 QMutexLocker locker(&m_mutex);
Chris@43 296 while (!m_synthMap.empty()) {
Chris@43 297 RealTimePluginInstance *instance = m_synthMap.begin()->second;
Chris@43 298 m_synthMap.erase(m_synthMap.begin());
Chris@43 299 delete instance;
Chris@43 300 }
Chris@43 301 }
Chris@43 302
Chris@43 303 void
Chris@43 304 AudioGenerator::reset()
Chris@43 305 {
Chris@43 306 QMutexLocker locker(&m_mutex);
Chris@43 307 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@43 308 if (i->second) {
Chris@43 309 i->second->silence();
Chris@43 310 i->second->discardEvents();
Chris@43 311 }
Chris@43 312 }
Chris@43 313
Chris@43 314 m_noteOffs.clear();
Chris@43 315 }
Chris@43 316
Chris@43 317 void
Chris@43 318 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
Chris@43 319 {
Chris@43 320 if (m_targetChannelCount == targetChannelCount) return;
Chris@43 321
Chris@233 322 // SVDEBUG << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << endl;
Chris@43 323
Chris@43 324 QMutexLocker locker(&m_mutex);
Chris@43 325 m_targetChannelCount = targetChannelCount;
Chris@43 326
Chris@43 327 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@43 328 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
Chris@43 329 }
Chris@43 330 }
Chris@43 331
Chris@43 332 size_t
Chris@43 333 AudioGenerator::getBlockSize() const
Chris@43 334 {
Chris@43 335 return m_pluginBlockSize;
Chris@43 336 }
Chris@43 337
Chris@43 338 void
Chris@43 339 AudioGenerator::setSoloModelSet(std::set<Model *> s)
Chris@43 340 {
Chris@43 341 QMutexLocker locker(&m_mutex);
Chris@43 342
Chris@43 343 m_soloModelSet = s;
Chris@43 344 m_soloing = true;
Chris@43 345 }
Chris@43 346
Chris@43 347 void
Chris@43 348 AudioGenerator::clearSoloModelSet()
Chris@43 349 {
Chris@43 350 QMutexLocker locker(&m_mutex);
Chris@43 351
Chris@43 352 m_soloModelSet.clear();
Chris@43 353 m_soloing = false;
Chris@43 354 }
Chris@43 355
Chris@43 356 size_t
Chris@43 357 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
Chris@43 358 float **buffer, size_t fadeIn, size_t fadeOut)
Chris@43 359 {
Chris@43 360 if (m_sourceSampleRate == 0) {
Chris@293 361 cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << endl;
Chris@43 362 return frameCount;
Chris@43 363 }
Chris@43 364
Chris@43 365 QMutexLocker locker(&m_mutex);
Chris@43 366
Chris@108 367 Playable *playable = model;
Chris@108 368 if (!playable || !playable->canPlay()) return frameCount;
Chris@108 369
Chris@43 370 PlayParameters *parameters =
Chris@108 371 PlayParameterRepository::getInstance()->getPlayParameters(playable);
Chris@43 372 if (!parameters) return frameCount;
Chris@43 373
Chris@43 374 bool playing = !parameters->isPlayMuted();
Chris@43 375 if (!playing) {
Chris@43 376 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 377 cout << "AudioGenerator::mixModel(" << model << "): muted" << endl;
Chris@43 378 #endif
Chris@43 379 return frameCount;
Chris@43 380 }
Chris@43 381
Chris@43 382 if (m_soloing) {
Chris@43 383 if (m_soloModelSet.find(model) == m_soloModelSet.end()) {
Chris@43 384 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 385 cout << "AudioGenerator::mixModel(" << model << "): not one of the solo'd models" << endl;
Chris@43 386 #endif
Chris@43 387 return frameCount;
Chris@43 388 }
Chris@43 389 }
Chris@43 390
Chris@43 391 float gain = parameters->getPlayGain();
Chris@43 392 float pan = parameters->getPlayPan();
Chris@43 393
Chris@43 394 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@43 395 if (dtvm) {
Chris@43 396 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
Chris@43 397 buffer, gain, pan, fadeIn, fadeOut);
Chris@43 398 }
Chris@43 399
Chris@279 400 bool synthetic =
Chris@279 401 (qobject_cast<SparseOneDimensionalModel *>(model) ||
Chris@278 402 qobject_cast<NoteModel *>(model) ||
Chris@278 403 qobject_cast<FlexiNoteModel *>(model));
Chris@43 404
Chris@279 405 if (synthetic) {
Chris@275 406 return mixSyntheticNoteModel(model, startFrame, frameCount,
Chris@275 407 buffer, gain, pan, fadeIn, fadeOut);
Chris@43 408 }
Chris@43 409
Chris@276 410 std::cerr << "AudioGenerator::mixModel: WARNING: Model " << model << " of type " << model->getTypeName() << " is marked as playable, but I have no mechanism to play it" << std::endl;
Chris@276 411
Chris@43 412 return frameCount;
Chris@43 413 }
Chris@43 414
Chris@43 415 size_t
Chris@43 416 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
Chris@43 417 size_t startFrame, size_t frames,
Chris@43 418 float **buffer, float gain, float pan,
Chris@43 419 size_t fadeIn, size_t fadeOut)
Chris@43 420 {
Chris@80 421 static float **channelBuffer = 0;
Chris@80 422 static size_t channelBufSiz = 0;
Chris@80 423 static size_t channelBufCount = 0;
Chris@43 424
Chris@43 425 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
Chris@43 426
Chris@80 427 size_t modelChannels = dtvm->getChannelCount();
Chris@80 428
Chris@80 429 if (channelBufSiz < totalFrames || channelBufCount < modelChannels) {
Chris@80 430
Chris@80 431 for (size_t c = 0; c < channelBufCount; ++c) {
Chris@80 432 delete[] channelBuffer[c];
Chris@80 433 }
Chris@80 434
Chris@43 435 delete[] channelBuffer;
Chris@80 436 channelBuffer = new float *[modelChannels];
Chris@80 437
Chris@80 438 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 439 channelBuffer[c] = new float[totalFrames];
Chris@80 440 }
Chris@80 441
Chris@80 442 channelBufCount = modelChannels;
Chris@43 443 channelBufSiz = totalFrames;
Chris@43 444 }
Chris@80 445
Chris@43 446 size_t got = 0;
Chris@80 447
Chris@80 448 if (startFrame >= fadeIn/2) {
Chris@80 449 got = dtvm->getData(0, modelChannels - 1,
Chris@80 450 startFrame - fadeIn/2,
Chris@80 451 frames + fadeOut/2 + fadeIn/2,
Chris@80 452 channelBuffer);
Chris@80 453 } else {
Chris@80 454 size_t missing = fadeIn/2 - startFrame;
Chris@80 455
Chris@80 456 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 457 channelBuffer[c] += missing;
Chris@80 458 }
Chris@80 459
Chris@80 460 got = dtvm->getData(0, modelChannels - 1,
Chris@80 461 startFrame,
Chris@80 462 frames + fadeOut/2,
Chris@80 463 channelBuffer);
Chris@80 464
Chris@80 465 for (size_t c = 0; c < modelChannels; ++c) {
Chris@80 466 channelBuffer[c] -= missing;
Chris@80 467 }
Chris@80 468
Chris@80 469 got += missing;
Chris@80 470 }
Chris@43 471
Chris@43 472 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@43 473
Chris@80 474 size_t sourceChannel = (c % modelChannels);
Chris@43 475
Chris@233 476 // SVDEBUG << "mixing channel " << c << " from source channel " << sourceChannel << endl;
Chris@43 477
Chris@43 478 float channelGain = gain;
Chris@43 479 if (pan != 0.0) {
Chris@43 480 if (c == 0) {
Chris@43 481 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@43 482 } else {
Chris@43 483 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@43 484 }
Chris@43 485 }
Chris@43 486
Chris@43 487 for (size_t i = 0; i < fadeIn/2; ++i) {
Chris@43 488 float *back = buffer[c];
Chris@43 489 back -= fadeIn/2;
Chris@80 490 back[i] += (channelGain * channelBuffer[sourceChannel][i] * i) / fadeIn;
Chris@43 491 }
Chris@43 492
Chris@43 493 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
Chris@43 494 float mult = channelGain;
Chris@43 495 if (i < fadeIn/2) {
Chris@43 496 mult = (mult * i) / fadeIn;
Chris@43 497 }
Chris@43 498 if (i > frames - fadeOut/2) {
Chris@43 499 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
Chris@43 500 }
Chris@80 501 float val = channelBuffer[sourceChannel][i];
Chris@80 502 if (i >= got) val = 0.f;
Chris@80 503 buffer[c][i] += mult * val;
Chris@43 504 }
Chris@43 505 }
Chris@43 506
Chris@43 507 return got;
Chris@43 508 }
Chris@43 509
Chris@43 510 size_t
Chris@275 511 AudioGenerator::mixSyntheticNoteModel(Model *model,
Chris@275 512 size_t startFrame, size_t frames,
Chris@275 513 float **buffer, float gain, float pan,
Chris@275 514 size_t /* fadeIn */,
Chris@275 515 size_t /* fadeOut */)
Chris@43 516 {
Chris@275 517 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@43 518 if (!plugin) return 0;
Chris@43 519
Chris@43 520 size_t latency = plugin->getLatency();
Chris@43 521 size_t blocks = frames / m_pluginBlockSize;
Chris@43 522
Chris@43 523 //!!! hang on -- the fact that the audio callback play source's
Chris@43 524 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@43 525 //that we always get called for a multiple of it here (because it
Chris@43 526 //also depends on the JACK block size). how should we ensure that
Chris@43 527 //all models write the same amount in to the mix, and that we
Chris@43 528 //always have a multiple of the plugin buffer size? I guess this
Chris@43 529 //class has to be queryable for the plugin buffer size & the
Chris@43 530 //callback play source has to use that as a multiple for all the
Chris@43 531 //calls to mixModel
Chris@43 532
Chris@43 533 size_t got = blocks * m_pluginBlockSize;
Chris@43 534
Chris@43 535 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 536 cout << "mixModel [synthetic note]: frames " << frames
Chris@293 537 << ", blocks " << blocks << endl;
Chris@43 538 #endif
Chris@43 539
Chris@43 540 snd_seq_event_t onEv;
Chris@43 541 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@43 542 onEv.data.note.channel = 0;
Chris@43 543
Chris@43 544 snd_seq_event_t offEv;
Chris@43 545 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@43 546 offEv.data.note.channel = 0;
Chris@43 547 offEv.data.note.velocity = 0;
Chris@43 548
Chris@275 549 NoteOffSet &noteOffs = m_noteOffs[model];
Chris@43 550
Chris@43 551 for (size_t i = 0; i < blocks; ++i) {
Chris@43 552
Chris@43 553 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@43 554
Chris@299 555 NoteList notes;
Chris@299 556 NoteExportable *exportable = dynamic_cast<NoteExportable *>(model);
Chris@299 557 if (exportable) {
Chris@299 558 notes = exportable->getNotes(reqStart + latency,
Chris@299 559 reqStart + latency + m_pluginBlockSize);
Chris@299 560 }
Chris@43 561
Chris@43 562 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@43 563 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@43 564
Chris@275 565 for (NoteList::const_iterator ni = notes.begin();
Chris@275 566 ni != notes.end(); ++ni) {
Chris@43 567
Chris@275 568 size_t noteFrame = ni->start;
Chris@43 569
Chris@275 570 if (noteFrame >= latency) noteFrame -= latency;
Chris@43 571
Chris@275 572 if (noteFrame < reqStart ||
Chris@275 573 noteFrame >= reqStart + m_pluginBlockSize) continue;
Chris@43 574
Chris@43 575 while (noteOffs.begin() != noteOffs.end() &&
Chris@275 576 noteOffs.begin()->frame <= noteFrame) {
Chris@43 577
Chris@43 578 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@43 579 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@43 580
Chris@43 581 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@43 582
Chris@43 583 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 584 cerr << "mixModel [synthetic]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl;
Chris@43 585 #endif
Chris@43 586
Chris@43 587 plugin->sendEvent(eventTime, &offEv);
Chris@43 588 noteOffs.erase(noteOffs.begin());
Chris@43 589 }
Chris@43 590
Chris@43 591 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@275 592 (noteFrame, m_sourceSampleRate);
Chris@43 593
Chris@275 594 if (ni->isMidiPitchQuantized) {
Chris@275 595 onEv.data.note.note = ni->midiPitch;
Chris@275 596 } else {
Chris@275 597 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 598 cerr << "mixModel [synthetic]: non-pitch-quantized notes are not supported [yet], quantizing" << endl;
Chris@275 599 #endif
Chris@275 600 onEv.data.note.note = Pitch::getPitchForFrequency(ni->frequency);
Chris@275 601 }
Chris@275 602
Chris@275 603 onEv.data.note.velocity = ni->velocity;
Chris@275 604
Chris@43 605 plugin->sendEvent(eventTime, &onEv);
Chris@43 606
Chris@43 607 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 608 cout << "mixModel [synthetic]: note at frame " << noteFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << endl;
Chris@43 609 #endif
Chris@43 610
Chris@275 611 noteOffs.insert
Chris@275 612 (NoteOff(onEv.data.note.note, noteFrame + ni->duration));
Chris@43 613 }
Chris@43 614
Chris@43 615 while (noteOffs.begin() != noteOffs.end() &&
Chris@43 616 noteOffs.begin()->frame <=
Chris@43 617 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@43 618
Chris@43 619 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@43 620 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@43 621
Chris@43 622 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@43 623
Chris@43 624 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 625 cerr << "mixModel [synthetic]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << endl;
Chris@43 626 #endif
Chris@43 627
Chris@43 628 plugin->sendEvent(eventTime, &offEv);
Chris@43 629 noteOffs.erase(noteOffs.begin());
Chris@43 630 }
Chris@43 631
Chris@43 632 plugin->run(blockTime);
Chris@43 633 float **outs = plugin->getAudioOutputBuffers();
Chris@43 634
Chris@43 635 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@43 636 #ifdef DEBUG_AUDIO_GENERATOR
Chris@293 637 cout << "mixModel [synthetic]: adding " << m_pluginBlockSize << " samples from plugin output " << c << endl;
Chris@43 638 #endif
Chris@43 639
Chris@43 640 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@43 641
Chris@43 642 float channelGain = gain;
Chris@43 643 if (pan != 0.0) {
Chris@43 644 if (c == 0) {
Chris@43 645 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@43 646 } else {
Chris@43 647 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@43 648 }
Chris@43 649 }
Chris@43 650
Chris@43 651 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@43 652 buffer[c][i * m_pluginBlockSize + j] +=
Chris@43 653 channelGain * outs[sourceChannel][j];
Chris@43 654 }
Chris@43 655 }
Chris@43 656 }
Chris@43 657
Chris@43 658 return got;
Chris@43 659 }