comparison audioio/AudioGenerator.cpp @ 13:89bb89894ad6

* As previous commit
author Chris Cannam
date Fri, 17 Feb 2006 18:11:08 +0000
parents 8dc0ae8fccdd
children 0f7997490bae
comparison
equal deleted inserted replaced
12:29b38a641d43 13:89bb89894ad6
36 { 36 {
37 } 37 }
38 38
39 AudioGenerator::~AudioGenerator() 39 AudioGenerator::~AudioGenerator()
40 { 40 {
41 }
42
43 bool
44 AudioGenerator::canPlay(const Model *model)
45 {
46 if (dynamic_cast<const DenseTimeValueModel *>(model) ||
47 dynamic_cast<const SparseOneDimensionalModel *>(model) ||
48 dynamic_cast<const NoteModel *>(model)) {
49 return true;
50 } else {
51 return false;
52 }
41 } 53 }
42 54
43 bool 55 bool
44 AudioGenerator::addModel(Model *model) 56 AudioGenerator::addModel(Model *model)
45 { 57 {
174 } 186 }
175 187
176 void 188 void
177 AudioGenerator::setTargetChannelCount(size_t targetChannelCount) 189 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
178 { 190 {
191 if (m_targetChannelCount == targetChannelCount) return;
192
193 std::cerr << "AudioGenerator::setTargetChannelCount(" << targetChannelCount << ")" << std::endl;
194
179 QMutexLocker locker(&m_mutex); 195 QMutexLocker locker(&m_mutex);
180 m_targetChannelCount = targetChannelCount; 196 m_targetChannelCount = targetChannelCount;
181 197
182 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) { 198 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
183 if (i->second) i->second->setIdealChannelCount(targetChannelCount); 199 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
249 channelBuffer = new float[totalFrames]; 265 channelBuffer = new float[totalFrames];
250 channelBufSiz = totalFrames; 266 channelBufSiz = totalFrames;
251 } 267 }
252 268
253 size_t got = 0; 269 size_t got = 0;
254 270 size_t prevChannel = 999;
255 for (size_t c = 0; c < m_targetChannelCount && c < dtvm->getChannelCount(); ++c) { 271
256 272 for (size_t c = 0; c < m_targetChannelCount; ++c) {
257 if (startFrame >= fadeIn/2) { 273
258 got = dtvm->getValues 274 size_t sourceChannel = (c % dtvm->getChannelCount());
259 (c, startFrame - fadeIn/2, startFrame + frames + fadeOut/2, 275
260 channelBuffer); 276 // std::cerr << "mixing channel " << c << " from source channel " << sourceChannel << std::endl;
261 } else { 277
262 size_t missing = fadeIn/2 - startFrame; 278 float channelGain = gain;
263 got = dtvm->getValues 279 if (pan != 0.0) {
264 (c, 0, startFrame + frames + fadeOut/2, 280 if (c == 0) {
265 channelBuffer + missing); 281 if (pan > 0.0) channelGain *= 1.0 - pan;
266 } 282 } else {
283 if (pan < 0.0) channelGain *= pan + 1.0;
284 }
285 }
286
287 if (prevChannel != sourceChannel) {
288 if (startFrame >= fadeIn/2) {
289 got = dtvm->getValues
290 (sourceChannel,
291 startFrame - fadeIn/2, startFrame + frames + fadeOut/2,
292 channelBuffer);
293 } else {
294 size_t missing = fadeIn/2 - startFrame;
295 got = dtvm->getValues
296 (sourceChannel,
297 0, startFrame + frames + fadeOut/2,
298 channelBuffer + missing);
299 }
300 }
301 prevChannel = sourceChannel;
267 302
268 for (size_t i = 0; i < fadeIn/2; ++i) { 303 for (size_t i = 0; i < fadeIn/2; ++i) {
269 float *back = buffer[c]; 304 float *back = buffer[c];
270 back -= fadeIn/2; 305 back -= fadeIn/2;
271 back[i] += (gain * channelBuffer[i] * i) / fadeIn; 306 back[i] += (channelGain * channelBuffer[i] * i) / fadeIn;
272 } 307 }
273 308
274 for (size_t i = 0; i < frames + fadeOut/2; ++i) { 309 for (size_t i = 0; i < frames + fadeOut/2; ++i) {
275 float mult = gain; 310 float mult = channelGain;
276 if (i < fadeIn/2) { 311 if (i < fadeIn/2) {
277 mult = (mult * i) / fadeIn; 312 mult = (mult * i) / fadeIn;
278 } 313 }
279 if (i > frames - fadeOut/2) { 314 if (i > frames - fadeOut/2) {
280 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut; 315 mult = (mult * ((frames + fadeOut/2) - i)) / fadeOut;
400 } 435 }
401 436
402 plugin->run(blockTime); 437 plugin->run(blockTime);
403 float **outs = plugin->getAudioOutputBuffers(); 438 float **outs = plugin->getAudioOutputBuffers();
404 439
405 for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) { 440 for (size_t c = 0; c < m_targetChannelCount; ++c) {
406 #ifdef DEBUG_AUDIO_GENERATOR 441 #ifdef DEBUG_AUDIO_GENERATOR
407 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; 442 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
408 #endif 443 #endif
409 444
445 size_t sourceChannel = (c % plugin->getAudioOutputCount());
446
447 float channelGain = gain;
448 if (pan != 0.0) {
449 if (c == 0) {
450 if (pan > 0.0) channelGain *= 1.0 - pan;
451 } else {
452 if (pan < 0.0) channelGain *= pan + 1.0;
453 }
454 }
455
410 for (size_t j = 0; j < m_pluginBlockSize; ++j) { 456 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
411 buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j]; 457 buffer[c][i * m_pluginBlockSize + j] +=
458 channelGain * outs[sourceChannel][j];
412 } 459 }
413 } 460 }
414 } 461 }
415 462
416 return got; 463 return got;
534 } 581 }
535 582
536 plugin->run(blockTime); 583 plugin->run(blockTime);
537 float **outs = plugin->getAudioOutputBuffers(); 584 float **outs = plugin->getAudioOutputBuffers();
538 585
539 for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) { 586 for (size_t c = 0; c < m_targetChannelCount; ++c) {
540 #ifdef DEBUG_AUDIO_GENERATOR 587 #ifdef DEBUG_AUDIO_GENERATOR
541 std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; 588 std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
542 #endif 589 #endif
543 590
591 size_t sourceChannel = (c % plugin->getAudioOutputCount());
592
593 float channelGain = gain;
594 if (pan != 0.0) {
595 if (c == 0) {
596 if (pan > 0.0) channelGain *= 1.0 - pan;
597 } else {
598 if (pan < 0.0) channelGain *= pan + 1.0;
599 }
600 }
601
544 for (size_t j = 0; j < m_pluginBlockSize; ++j) { 602 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
545 buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j]; 603 buffer[c][i * m_pluginBlockSize + j] +=
604 channelGain * outs[sourceChannel][j];
546 } 605 }
547 } 606 }
548 } 607 }
549 608
550 return got; 609 return got;