annotate audioio/AudioGenerator.cpp @ 41:a4a05344c7d6

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