annotate audioio/AudioGenerator.cpp @ 32:42989c2e621b

* Fix crash in spectrogram layer when replacing model * Change sample player's sample path to a single sample directory. This makes it immune to OS path separator differences, and ensures it can reject configure calls that try to set a nonexistent directory. Reloading play parameters (e.g. sample program) should now work. * some win32 fixes.
author Chris Cannam
date Wed, 26 Apr 2006 16:00:13 +0000
parents b1112948724f
children f6b15e0b6fde
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 "AudioGenerator.h"
Chris@0 17
Chris@30 18 #include "base/TempDirectory.h"
Chris@0 19 #include "base/PlayParameters.h"
Chris@11 20 #include "base/PlayParameterRepository.h"
Chris@0 21
Chris@11 22 #include "model/NoteModel.h"
Chris@0 23 #include "model/DenseTimeValueModel.h"
Chris@0 24 #include "model/SparseOneDimensionalModel.h"
Chris@0 25
Chris@0 26 #include "plugin/RealTimePluginFactory.h"
Chris@0 27 #include "plugin/RealTimePluginInstance.h"
Chris@0 28 #include "plugin/PluginIdentifier.h"
Chris@26 29 #include "plugin/PluginXml.h"
Chris@0 30 #include "plugin/api/alsa/seq_event.h"
Chris@0 31
Chris@0 32 #include <iostream>
Martin@14 33 #include <math.h>
Chris@0 34
Chris@29 35 #include <QDir>
Chris@29 36 #include <QFile>
Chris@29 37
Chris@0 38 const size_t
Chris@0 39 AudioGenerator::m_pluginBlockSize = 2048;
Chris@0 40
Chris@30 41 QString
Chris@30 42 AudioGenerator::m_sampleDir = "";
Chris@30 43
Chris@5 44 //#define DEBUG_AUDIO_GENERATOR 1
Chris@0 45
Chris@30 46 AudioGenerator::AudioGenerator() :
Chris@0 47 m_sourceSampleRate(0),
Chris@0 48 m_targetChannelCount(1)
Chris@0 49 {
Chris@22 50 connect(PlayParameterRepository::instance(),
Chris@22 51 SIGNAL(playPluginIdChanged(const Model *, QString)),
Chris@22 52 this,
Chris@22 53 SLOT(playPluginIdChanged(const Model *, QString)));
Chris@22 54
Chris@22 55 connect(PlayParameterRepository::instance(),
Chris@22 56 SIGNAL(playPluginConfigurationChanged(const Model *, QString)),
Chris@22 57 this,
Chris@22 58 SLOT(playPluginConfigurationChanged(const Model *, QString)));
Chris@0 59 }
Chris@0 60
Chris@0 61 AudioGenerator::~AudioGenerator()
Chris@0 62 {
Chris@0 63 }
Chris@0 64
Chris@10 65 bool
Chris@13 66 AudioGenerator::canPlay(const Model *model)
Chris@13 67 {
Chris@13 68 if (dynamic_cast<const DenseTimeValueModel *>(model) ||
Chris@13 69 dynamic_cast<const SparseOneDimensionalModel *>(model) ||
Chris@13 70 dynamic_cast<const NoteModel *>(model)) {
Chris@13 71 return true;
Chris@13 72 } else {
Chris@13 73 return false;
Chris@13 74 }
Chris@13 75 }
Chris@13 76
Chris@13 77 bool
Chris@0 78 AudioGenerator::addModel(Model *model)
Chris@0 79 {
Chris@0 80 if (m_sourceSampleRate == 0) {
Chris@0 81
Chris@0 82 m_sourceSampleRate = model->getSampleRate();
Chris@0 83
Chris@0 84 } else {
Chris@0 85
Chris@0 86 DenseTimeValueModel *dtvm =
Chris@0 87 dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 88
Chris@0 89 if (dtvm) {
Chris@0 90 m_sourceSampleRate = model->getSampleRate();
Chris@10 91 return true;
Chris@0 92 }
Chris@0 93 }
Chris@0 94
Chris@22 95 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@22 96 if (plugin) {
Chris@22 97 QMutexLocker locker(&m_mutex);
Chris@22 98 m_synthMap[model] = plugin;
Chris@22 99 return true;
Chris@11 100 }
Chris@0 101
Chris@22 102 return false;
Chris@22 103 }
Chris@22 104
Chris@22 105 void
Chris@22 106 AudioGenerator::playPluginIdChanged(const Model *model, QString)
Chris@22 107 {
Chris@22 108 if (m_synthMap.find(model) == m_synthMap.end()) return;
Chris@22 109
Chris@22 110 RealTimePluginInstance *plugin = loadPluginFor(model);
Chris@22 111 if (plugin) {
Chris@22 112 QMutexLocker locker(&m_mutex);
Chris@22 113 delete m_synthMap[model];
Chris@22 114 m_synthMap[model] = plugin;
Chris@22 115 }
Chris@22 116 }
Chris@22 117
Chris@22 118 void
Chris@22 119 AudioGenerator::playPluginConfigurationChanged(const Model *model,
Chris@22 120 QString configurationXml)
Chris@22 121 {
Chris@31 122 std::cerr << "AudioGenerator::playPluginConfigurationChanged" << std::endl;
Chris@31 123
Chris@31 124 if (m_synthMap.find(model) == m_synthMap.end()) {
Chris@31 125 std::cerr << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << std::endl;
Chris@31 126 return;
Chris@31 127 }
Chris@22 128
Chris@22 129 RealTimePluginInstance *plugin = m_synthMap[model];
Chris@22 130 if (plugin) {
Chris@26 131 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@22 132 }
Chris@22 133 }
Chris@22 134
Chris@22 135 QString
Chris@22 136 AudioGenerator::getDefaultPlayPluginId(const Model *model)
Chris@22 137 {
Chris@22 138 const SparseOneDimensionalModel *sodm =
Chris@22 139 dynamic_cast<const SparseOneDimensionalModel *>(model);
Chris@22 140 if (sodm) {
Chris@22 141 return QString("dssi:%1:sample_player").
Chris@22 142 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
Chris@22 143 }
Chris@22 144
Chris@22 145 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
Chris@11 146 if (nm) {
Chris@22 147 return QString("dssi:%1:sample_player").
Chris@22 148 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
Chris@22 149 }
Chris@22 150
Chris@22 151 return "";
Chris@22 152 }
Chris@22 153
Chris@22 154 QString
Chris@22 155 AudioGenerator::getDefaultPlayPluginConfiguration(const Model *model)
Chris@22 156 {
Chris@30 157 QString program = "";
Chris@30 158
Chris@22 159 const SparseOneDimensionalModel *sodm =
Chris@22 160 dynamic_cast<const SparseOneDimensionalModel *>(model);
Chris@22 161 if (sodm) {
Chris@30 162 program = "cowbell";
Chris@11 163 }
Chris@11 164
Chris@22 165 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
Chris@22 166 if (nm) {
Chris@30 167 program = "piano";
Chris@30 168 }
Chris@30 169
Chris@30 170 if (program == "") return "";
Chris@30 171
Chris@30 172 return
Chris@30 173 QString("<plugin configuration=\"%1\" program=\"%2\"/>")
Chris@30 174 .arg(XmlExportable::encodeEntities
Chris@32 175 (QString("sampledir=%1")
Chris@32 176 .arg(PluginXml::encodeConfigurationChars(getSampleDir()))))
Chris@30 177 .arg(XmlExportable::encodeEntities(program));
Chris@22 178 }
Chris@22 179
Chris@29 180 QString
Chris@29 181 AudioGenerator::getSampleDir()
Chris@29 182 {
Chris@29 183 if (m_sampleDir != "") return m_sampleDir;
Chris@29 184
Chris@30 185 try {
Chris@30 186 m_sampleDir = TempDirectory::instance()->getSubDirectoryPath("samples");
Chris@30 187 } catch (TempDirectory::DirectoryCreationFailed f) {
Chris@29 188 std::cerr << "WARNING: AudioGenerator::getSampleDir: Failed to create "
Chris@30 189 << "temporary sample directory" << std::endl;
Chris@30 190 m_sampleDir = "";
Chris@30 191 return "";
Chris@29 192 }
Chris@29 193
Chris@29 194 QDir sampleResourceDir(":/samples", "*.wav");
Chris@29 195
Chris@29 196 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
Chris@29 197
Chris@29 198 QString fileName(sampleResourceDir[i]);
Chris@29 199 QFile file(sampleResourceDir.filePath(fileName));
Chris@29 200
Chris@29 201 if (!file.copy(QDir(m_sampleDir).filePath(fileName))) {
Chris@29 202 std::cerr << "WARNING: AudioGenerator::getSampleDir: "
Chris@29 203 << "Unable to copy " << fileName.toStdString()
Chris@29 204 << " into temporary directory \""
Chris@29 205 << m_sampleDir.toStdString() << "\"" << std::endl;
Chris@29 206 }
Chris@29 207 }
Chris@29 208
Chris@29 209 return m_sampleDir;
Chris@29 210 }
Chris@29 211
Chris@32 212 void
Chris@32 213 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin)
Chris@29 214 {
Chris@32 215 plugin->configure("sampledir", getSampleDir().toStdString());
Chris@29 216 }
Chris@29 217
Chris@22 218 RealTimePluginInstance *
Chris@22 219 AudioGenerator::loadPluginFor(const Model *model)
Chris@22 220 {
Chris@22 221 QString pluginId, configurationXml;
Chris@22 222
Chris@22 223 PlayParameters *parameters =
Chris@22 224 PlayParameterRepository::instance()->getPlayParameters(model);
Chris@22 225 if (parameters) {
Chris@22 226 pluginId = parameters->getPlayPluginId();
Chris@22 227 configurationXml = parameters->getPlayPluginConfiguration();
Chris@22 228 }
Chris@22 229
Chris@22 230 if (pluginId == "") {
Chris@22 231 pluginId = getDefaultPlayPluginId(model);
Chris@22 232 configurationXml = getDefaultPlayPluginConfiguration(model);
Chris@22 233 }
Chris@22 234
Chris@22 235 if (pluginId == "") return 0;
Chris@22 236
Chris@22 237 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
Chris@29 238 if (!plugin) return 0;
Chris@29 239
Chris@22 240 if (configurationXml != "") {
Chris@26 241 PluginXml(plugin).setParametersFromXml(configurationXml);
Chris@22 242 }
Chris@22 243
Chris@22 244 if (parameters) {
Chris@22 245 parameters->setPlayPluginId(pluginId);
Chris@22 246 parameters->setPlayPluginConfiguration(configurationXml);
Chris@22 247 }
Chris@22 248
Chris@22 249 return plugin;
Chris@11 250 }
Chris@11 251
Chris@11 252 RealTimePluginInstance *
Chris@11 253 AudioGenerator::loadPlugin(QString pluginId, QString program)
Chris@11 254 {
Chris@0 255 RealTimePluginFactory *factory =
Chris@0 256 RealTimePluginFactory::instanceFor(pluginId);
Chris@0 257
Chris@0 258 if (!factory) {
Chris@0 259 std::cerr << "Failed to get plugin factory" << std::endl;
Chris@10 260 return false;
Chris@0 261 }
Chris@0 262
Chris@0 263 RealTimePluginInstance *instance =
Chris@0 264 factory->instantiatePlugin
Chris@0 265 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
Chris@0 266
Chris@29 267 if (!instance) {
Chris@29 268 std::cerr << "Failed to instantiate plugin " << pluginId.toStdString() << std::endl;
Chris@0 269 }
Chris@10 270
Chris@32 271 setSampleDir(instance);
Chris@29 272
Chris@29 273 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
Chris@29 274 instance->setParameterValue(i, instance->getParameterDefault(i));
Chris@29 275 }
Chris@29 276 std::string defaultProgram = instance->getProgram(0, 0);
Chris@29 277 if (defaultProgram != "") {
Chris@29 278 std::cerr << "first selecting default program " << defaultProgram << std::endl;
Chris@29 279 instance->selectProgram(defaultProgram);
Chris@29 280 }
Chris@29 281 if (program != "") {
Chris@29 282 std::cerr << "now selecting desired program " << program.toStdString() << std::endl;
Chris@29 283 instance->selectProgram(program.toStdString());
Chris@29 284 }
Chris@29 285 instance->setIdealChannelCount(m_targetChannelCount); // reset!
Chris@29 286
Chris@11 287 return instance;
Chris@0 288 }
Chris@0 289
Chris@0 290 void
Chris@0 291 AudioGenerator::removeModel(Model *model)
Chris@0 292 {
Chris@0 293 SparseOneDimensionalModel *sodm =
Chris@0 294 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@0 295 if (!sodm) return; // nothing to do
Chris@0 296
Chris@8 297 QMutexLocker locker(&m_mutex);
Chris@8 298
Chris@0 299 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
Chris@0 300
Chris@0 301 RealTimePluginInstance *instance = m_synthMap[sodm];
Chris@0 302 m_synthMap.erase(sodm);
Chris@0 303 delete instance;
Chris@0 304 }
Chris@0 305
Chris@0 306 void
Chris@0 307 AudioGenerator::clearModels()
Chris@0 308 {
Chris@8 309 QMutexLocker locker(&m_mutex);
Chris@0 310 while (!m_synthMap.empty()) {
Chris@0 311 RealTimePluginInstance *instance = m_synthMap.begin()->second;
Chris@0 312 m_synthMap.erase(m_synthMap.begin());
Chris@0 313 delete instance;
Chris@0 314 }
Chris@0 315 }
Chris@0 316
Chris@0 317 void
Chris@0 318 AudioGenerator::reset()
Chris@0 319 {
Chris@8 320 QMutexLocker locker(&m_mutex);
Chris@0 321 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 322 if (i->second) {
Chris@0 323 i->second->silence();
Chris@0 324 i->second->discardEvents();
Chris@0 325 }
Chris@0 326 }
Chris@0 327
Chris@0 328 m_noteOffs.clear();
Chris@0 329 }
Chris@0 330
Chris@0 331 void
Chris@0 332 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
Chris@0 333 {
Chris@13 334 if (m_targetChannelCount == targetChannelCount) return;
Chris@13 335
Chris@13 336 std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::endl;
Chris@13 337
Chris@8 338 QMutexLocker locker(&m_mutex);
Chris@0 339 m_targetChannelCount = targetChannelCount;
Chris@0 340
Chris@0 341 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 342 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
Chris@0 343 }
Chris@0 344 }
Chris@0 345
Chris@0 346 size_t
Chris@0 347 AudioGenerator::getBlockSize() const
Chris@0 348 {
Chris@0 349 return m_pluginBlockSize;
Chris@0 350 }
Chris@0 351
Chris@0 352 size_t
Chris@0 353 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
Chris@4 354 float **buffer, size_t fadeIn, size_t fadeOut)
Chris@0 355 {
Chris@0 356 if (m_sourceSampleRate == 0) {
Chris@0 357 std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl;
Chris@0 358 return frameCount;
Chris@0 359 }
Chris@0 360
Chris@8 361 QMutexLocker locker(&m_mutex);
Chris@8 362
Chris@11 363 PlayParameters *parameters =
Chris@11 364 PlayParameterRepository::instance()->getPlayParameters(model);
Chris@0 365 if (!parameters) return frameCount;
Chris@0 366
Chris@0 367 bool playing = !parameters->isPlayMuted();
Chris@0 368 if (!playing) return frameCount;
Chris@0 369
Chris@0 370 float gain = parameters->getPlayGain();
Chris@0 371 float pan = parameters->getPlayPan();
Chris@0 372
Chris@0 373 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 374 if (dtvm) {
Chris@0 375 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
Chris@4 376 buffer, gain, pan, fadeIn, fadeOut);
Chris@0 377 }
Chris@0 378
Chris@0 379 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@0 380 (model);
Chris@0 381 if (sodm) {
Chris@0 382 return mixSparseOneDimensionalModel(sodm, startFrame, frameCount,
Chris@4 383 buffer, gain, pan, fadeIn, fadeOut);
Chris@0 384 }
Chris@0 385
Chris@11 386 NoteModel *nm = dynamic_cast<NoteModel *>(model);
Chris@11 387 if (nm) {
Chris@11 388 return mixNoteModel(nm, startFrame, frameCount,
Chris@11 389 buffer, gain, pan, fadeIn, fadeOut);
Chris@11 390 }
Chris@11 391
Chris@0 392 return frameCount;
Chris@0 393 }
Chris@0 394
Chris@0 395 size_t
Chris@0 396 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
Chris@0 397 size_t startFrame, size_t frames,
Chris@4 398 float **buffer, float gain, float pan,
Chris@4 399 size_t fadeIn, size_t fadeOut)
Chris@0 400 {
Chris@0 401 static float *channelBuffer = 0;
Chris@0 402 static size_t channelBufSiz = 0;
Chris@5 403
Chris@4 404 size_t totalFrames = frames + fadeIn/2 + fadeOut/2;
Chris@4 405
Chris@4 406 if (channelBufSiz < totalFrames) {
Chris@0 407 delete[] channelBuffer;
Chris@4 408 channelBuffer = new float[totalFrames];
Chris@4 409 channelBufSiz = totalFrames;
Chris@0 410 }
Chris@0 411
Chris@0 412 size_t got = 0;
Chris@13 413 size_t prevChannel = 999;
Chris@0 414
Chris@13 415 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@4 416
Chris@13 417 size_t sourceChannel = (c % dtvm->getChannelCount());
Chris@13 418
Chris@13 419 // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::endl;
Chris@13 420
Chris@13 421 float channelGain = gain;
Chris@13 422 if (pan != 0.0) {
Chris@13 423 if (c == 0) {
Chris@13 424 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 425 } else {
Chris@13 426 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 427 }
Chris@13 428 }
Chris@13 429
Chris@13 430 if (prevChannel != sourceChannel) {
Chris@13 431 if (startFrame >= fadeIn/2) {
Chris@13 432 got = dtvm->getValues
Chris@13 433 (sourceChannel,
Chris@13 434 startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
Chris@13 435 channelBuffer);
Chris@13 436 } else {
Chris@13 437 size_t missing = fadeIn/2 - startFrame;
Chris@13 438 got = dtvm->getValues
Chris@13 439 (sourceChannel,
Chris@13 440 0, startFrame + frames + fadeOut/2,
Chris@13 441 channelBuffer + missing);
Chris@13 442 }
Chris@13 443 }
Chris@13 444 prevChannel = sourceChannel;
Chris@4 445
Chris@4 446 for (size_t i = 0; i < fadeIn/2; ++i) {
Chris@4 447 float *back = buffer[c];
Chris@4 448 back -= fadeIn/2;
Chris@13 449 back[i] += (channelGain * channelBuffer[i] * i) / fadeIn;
Chris@4 450 }
Chris@4 451
Chris@4 452 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
Chris@13 453 float mult = channelGain;
Chris@4 454 if (i < fadeIn/2) {
Chris@4 455 mult = (mult * i) / fadeIn;
Chris@4 456 }
Chris@4 457 if (i > frames - fadeOut/2) {
Chris@4 458 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
Chris@4 459 }
Chris@4 460 buffer[c][i] += mult * channelBuffer[i];
Chris@0 461 }
Chris@0 462 }
Chris@0 463
Chris@0 464 return got;
Chris@0 465 }
Chris@11 466
Chris@0 467 size_t
Chris@0 468 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
Chris@0 469 size_t startFrame, size_t frames,
Chris@4 470 float **buffer, float gain, float pan,
Chris@4 471 size_t /* fadeIn */,
Chris@4 472 size_t /* fadeOut */)
Chris@0 473 {
Chris@0 474 RealTimePluginInstance *plugin = m_synthMap[sodm];
Chris@0 475 if (!plugin) return 0;
Chris@0 476
Chris@0 477 size_t latency = plugin->getLatency();
Chris@0 478 size_t blocks = frames / m_pluginBlockSize;
Chris@0 479
Chris@0 480 //!!! hang on -- the fact that the audio callback play source's
Chris@0 481 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@0 482 //that we always get called for a multiple of it here (because it
Chris@0 483 //also depends on the JACK block size). how should we ensure that
Chris@0 484 //all models write the same amount in to the mix, and that we
Chris@0 485 //always have a multiple of the plugin buffer size? I guess this
Chris@0 486 //class has to be queryable for the plugin buffer size & the
Chris@0 487 //callback play source has to use that as a multiple for all the
Chris@0 488 //calls to mixModel
Chris@0 489
Chris@0 490 size_t got = blocks * m_pluginBlockSize;
Chris@0 491
Chris@0 492 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 493 std::cout << "mixModel [sparse]: frames " << frames
Chris@0 494 << ", blocks " << blocks << std::endl;
Chris@0 495 #endif
Chris@0 496
Chris@0 497 snd_seq_event_t onEv;
Chris@0 498 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@0 499 onEv.data.note.channel = 0;
Chris@0 500 onEv.data.note.note = 64;
Chris@0 501 onEv.data.note.velocity = 127;
Chris@0 502
Chris@0 503 snd_seq_event_t offEv;
Chris@0 504 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@0 505 offEv.data.note.channel = 0;
Chris@0 506 offEv.data.note.velocity = 0;
Chris@0 507
Chris@0 508 NoteOffSet &noteOffs = m_noteOffs[sodm];
Chris@0 509
Chris@0 510 for (size_t i = 0; i < blocks; ++i) {
Chris@0 511
Chris@0 512 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@0 513
Chris@0 514 SparseOneDimensionalModel::PointList points =
Chris@5 515 sodm->getPoints(reqStart + latency,
Chris@0 516 reqStart + latency + m_pluginBlockSize);
Chris@0 517
Chris@26 518 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@0 519 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@0 520
Chris@0 521 for (SparseOneDimensionalModel::PointList::iterator pli =
Chris@0 522 points.begin(); pli != points.end(); ++pli) {
Chris@0 523
Chris@0 524 size_t pliFrame = pli->frame;
Chris@5 525
Chris@0 526 if (pliFrame >= latency) pliFrame -= latency;
Chris@0 527
Chris@5 528 if (pliFrame < reqStart ||
Chris@5 529 pliFrame >= reqStart + m_pluginBlockSize) continue;
Chris@5 530
Chris@0 531 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 532 noteOffs.begin()->frame <= pliFrame) {
Chris@0 533
Chris@26 534 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 535 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 536
Chris@0 537 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@5 538
Chris@5 539 #ifdef DEBUG_AUDIO_GENERATOR
Chris@5 540 std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@5 541 #endif
Chris@5 542
Chris@0 543 plugin->sendEvent(eventTime, &offEv);
Chris@0 544 noteOffs.erase(noteOffs.begin());
Chris@0 545 }
Chris@0 546
Chris@26 547 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 548 (pliFrame, m_sourceSampleRate);
Chris@0 549
Chris@0 550 plugin->sendEvent(eventTime, &onEv);
Chris@0 551
Chris@0 552 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 553 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
Chris@0 554 #endif
Chris@0 555
Chris@0 556 size_t duration = 7000; // frames [for now]
Chris@0 557 NoteOff noff;
Chris@0 558 noff.pitch = onEv.data.note.note;
Chris@0 559 noff.frame = pliFrame + duration;
Chris@0 560 noteOffs.insert(noff);
Chris@0 561 }
Chris@0 562
Chris@0 563 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 564 noteOffs.begin()->frame <=
Chris@0 565 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@0 566
Chris@26 567 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@0 568 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 569
Chris@0 570 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@5 571
Chris@5 572 #ifdef DEBUG_AUDIO_GENERATOR
Chris@5 573 std::cerr << "mixModel [sparse]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@5 574 #endif
Chris@5 575
Chris@0 576 plugin->sendEvent(eventTime, &offEv);
Chris@0 577 noteOffs.erase(noteOffs.begin());
Chris@0 578 }
Chris@0 579
Chris@0 580 plugin->run(blockTime);
Chris@0 581 float **outs = plugin->getAudioOutputBuffers();
Chris@0 582
Chris@13 583 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@0 584 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 585 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
Chris@0 586 #endif
Chris@0 587
Chris@13 588 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@13 589
Chris@13 590 float channelGain = gain;
Chris@13 591 if (pan != 0.0) {
Chris@13 592 if (c == 0) {
Chris@13 593 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 594 } else {
Chris@13 595 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 596 }
Chris@13 597 }
Chris@13 598
Chris@0 599 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@13 600 buffer[c][i * m_pluginBlockSize + j] +=
Chris@13 601 channelGain * outs[sourceChannel][j];
Chris@0 602 }
Chris@0 603 }
Chris@0 604 }
Chris@0 605
Chris@0 606 return got;
Chris@0 607 }
Chris@0 608
Chris@11 609
Chris@11 610 //!!! mucho duplication with above -- refactor
Chris@11 611 size_t
Chris@11 612 AudioGenerator::mixNoteModel(NoteModel *nm,
Chris@11 613 size_t startFrame, size_t frames,
Chris@11 614 float **buffer, float gain, float pan,
Chris@11 615 size_t /* fadeIn */,
Chris@11 616 size_t /* fadeOut */)
Chris@11 617 {
Chris@11 618 RealTimePluginInstance *plugin = m_synthMap[nm];
Chris@11 619 if (!plugin) return 0;
Chris@11 620
Chris@11 621 size_t latency = plugin->getLatency();
Chris@11 622 size_t blocks = frames / m_pluginBlockSize;
Chris@11 623
Chris@11 624 //!!! hang on -- the fact that the audio callback play source's
Chris@11 625 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@11 626 //that we always get called for a multiple of it here (because it
Chris@11 627 //also depends on the JACK block size). how should we ensure that
Chris@11 628 //all models write the same amount in to the mix, and that we
Chris@11 629 //always have a multiple of the plugin buffer size? I guess this
Chris@11 630 //class has to be queryable for the plugin buffer size & the
Chris@11 631 //callback play source has to use that as a multiple for all the
Chris@11 632 //calls to mixModel
Chris@11 633
Chris@11 634 size_t got = blocks * m_pluginBlockSize;
Chris@11 635
Chris@11 636 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 637 std::cout << "mixModel [note]: frames " << frames
Chris@11 638 << ", blocks " << blocks << std::endl;
Chris@11 639 #endif
Chris@11 640
Chris@11 641 snd_seq_event_t onEv;
Chris@11 642 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@11 643 onEv.data.note.channel = 0;
Chris@11 644 onEv.data.note.note = 64;
Chris@11 645 onEv.data.note.velocity = 127;
Chris@11 646
Chris@11 647 snd_seq_event_t offEv;
Chris@11 648 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@11 649 offEv.data.note.channel = 0;
Chris@11 650 offEv.data.note.velocity = 0;
Chris@11 651
Chris@11 652 NoteOffSet &noteOffs = m_noteOffs[nm];
Chris@11 653
Chris@11 654 for (size_t i = 0; i < blocks; ++i) {
Chris@11 655
Chris@11 656 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@11 657
Chris@11 658 NoteModel::PointList points =
Chris@11 659 nm->getPoints(reqStart + latency,
Chris@11 660 reqStart + latency + m_pluginBlockSize);
Chris@11 661
Chris@26 662 Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime
Chris@11 663 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@11 664
Chris@11 665 for (NoteModel::PointList::iterator pli =
Chris@11 666 points.begin(); pli != points.end(); ++pli) {
Chris@11 667
Chris@11 668 size_t pliFrame = pli->frame;
Chris@11 669
Chris@11 670 if (pliFrame >= latency) pliFrame -= latency;
Chris@11 671
Chris@11 672 if (pliFrame < reqStart ||
Chris@11 673 pliFrame >= reqStart + m_pluginBlockSize) continue;
Chris@11 674
Chris@11 675 while (noteOffs.begin() != noteOffs.end() &&
Chris@11 676 noteOffs.begin()->frame <= pliFrame) {
Chris@11 677
Chris@26 678 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 679 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@11 680
Chris@11 681 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@11 682
Chris@11 683 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 684 std::cerr << "mixModel [note]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@11 685 #endif
Chris@11 686
Chris@11 687 plugin->sendEvent(eventTime, &offEv);
Chris@11 688 noteOffs.erase(noteOffs.begin());
Chris@11 689 }
Chris@11 690
Chris@26 691 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 692 (pliFrame, m_sourceSampleRate);
Chris@11 693
Chris@11 694 onEv.data.note.note = lrintf(pli->value);
Chris@11 695
Chris@11 696 plugin->sendEvent(eventTime, &onEv);
Chris@11 697
Chris@11 698 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 699 std::cout << "mixModel [note]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
Chris@11 700 #endif
Chris@11 701
Chris@11 702 size_t duration = pli->duration;
Chris@11 703 NoteOff noff;
Chris@11 704 noff.pitch = onEv.data.note.note;
Chris@11 705 noff.frame = pliFrame + duration;
Chris@11 706 noteOffs.insert(noff);
Chris@11 707 }
Chris@11 708
Chris@11 709 while (noteOffs.begin() != noteOffs.end() &&
Chris@11 710 noteOffs.begin()->frame <=
Chris@11 711 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@11 712
Chris@26 713 Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime
Chris@11 714 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@11 715
Chris@11 716 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@11 717
Chris@11 718 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 719 std::cerr << "mixModel [note]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << std::endl;
Chris@11 720 #endif
Chris@11 721
Chris@11 722 plugin->sendEvent(eventTime, &offEv);
Chris@11 723 noteOffs.erase(noteOffs.begin());
Chris@11 724 }
Chris@11 725
Chris@11 726 plugin->run(blockTime);
Chris@11 727 float **outs = plugin->getAudioOutputBuffers();
Chris@11 728
Chris@13 729 for (size_t c = 0; c < m_targetChannelCount; ++c) {
Chris@11 730 #ifdef DEBUG_AUDIO_GENERATOR
Chris@11 731 std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
Chris@11 732 #endif
Chris@11 733
Chris@13 734 size_t sourceChannel = (c % plugin->getAudioOutputCount());
Chris@13 735
Chris@13 736 float channelGain = gain;
Chris@13 737 if (pan != 0.0) {
Chris@13 738 if (c == 0) {
Chris@13 739 if (pan > 0.0) channelGain *= 1.0 - pan;
Chris@13 740 } else {
Chris@13 741 if (pan < 0.0) channelGain *= pan + 1.0;
Chris@13 742 }
Chris@13 743 }
Chris@13 744
Chris@11 745 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@13 746 buffer[c][i * m_pluginBlockSize + j] +=
Chris@13 747 channelGain * outs[sourceChannel][j];
Chris@11 748 }
Chris@11 749 }
Chris@11 750 }
Chris@11 751
Chris@11 752 return got;
Chris@11 753 }
Chris@11 754