annotate audioio/AudioGenerator.cpp @ 13:89bb89894ad6

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