annotate audioio/AudioGenerator.cpp @ 22:7f32bb07629a

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