65
void FxChannel::doProcessing( sampleFrame * _buf )
67
FxMixer * fxm = engine::fxMixer();
68
const fpp_t fpp = engine::mixer()->framesPerPeriod();
70
// <tobydox> ignore the passed _buf
71
// <tobydox> always use m_buffer
72
// <tobydox> this is just an auxilliary buffer if doProcessing()
73
// needs one for processing while running
74
// <tobydox> particularly important for playHandles, so Instruments
75
// can operate on this buffer the whole time
76
// <tobydox> this improves cache hit rate
79
// SMF: OK, due to the fact, that the data from the audio-tracks has been
80
// written into our buffer already, all which needs to be done at this
81
// stage is to process inter-channel sends. I really don't like the idea
82
// of using threads for this -- it just doesn't make any sense and wastes
83
// cpu-cylces... so I just go through every child of this channel and
84
// call the acc. doProcessing() directly.
86
if( m_muteModel.value() == false )
88
// OK, we are not muted, so we go recursively through all the channels
89
// which send to us (our children)...
90
foreach( fx_ch_t senderIndex, m_receives )
92
FxChannel * sender = fxm->effectChannel( senderIndex );
94
// wait for the sender job - either it's just been queued yet,
95
// then ThreadableJob::process() will process it now within this
96
// thread - otherwise it has been is is being processed by another
97
// thread and we just have to wait for it to finish
98
while( sender->state() != ThreadableJob::Done )
103
// get the send level...
105
fxm->channelSendModel( senderIndex, m_channelIndex )->value();
107
// mix it's output with this one's output
108
sampleFrame * ch_buf = sender->m_buffer;
109
const float v = sender->m_volumeModel.value() * amt;
110
for( f_cnt_t f = 0; f < fpp; ++f )
112
_buf[f][0] += ch_buf[f][0] * v;
113
_buf[f][1] += ch_buf[f][1] * v;
118
const float v = m_volumeModel.value();
120
m_fxChain.startRunning();
121
m_stillRunning = m_fxChain.processAudioBuffer( _buf, fpp, true );
122
m_peakLeft = qMax( m_peakLeft, engine::mixer()->peakValueLeft( _buf, fpp ) * v );
123
m_peakRight = qMax( m_peakRight, engine::mixer()->peakValueRight( _buf, fpp ) * v );
128
62
FxMixer::FxMixer() :
129
63
JournallingObject(),
133
// create master channel
66
for( int i = 0; i < NumFxChannels+1; ++i )
68
m_fxChannels[i] = new FxChannel( this );
139
77
FxMixer::~FxMixer()
141
for( int i = 0; i < m_fxChannels.size(); ++i )
79
for( int i = 0; i < NumFxChannels+1; ++i )
143
for( int j = 0; j < m_fxChannels[i]->m_sendAmount.size(); ++j)
145
delete m_fxChannels[i]->m_sendAmount[j];
147
81
delete m_fxChannels[i];
153
int FxMixer::createChannel()
155
const int index = m_fxChannels.size();
156
// create new channel
157
m_fxChannels.push_back( new FxChannel( index, this ) );
159
// reset channel state
160
clearChannel( index );
166
void FxMixer::deleteChannel(int index)
168
m_fxChannels[index]->m_lock.lock();
170
// go through every instrument and adjust for the channel index change
171
TrackContainer::TrackList tracks;
172
tracks += engine::getSong()->tracks();
173
tracks += engine::getBBTrackContainer()->tracks();
175
foreach( track* t, tracks )
177
if( t->type() == track::InstrumentTrack )
179
InstrumentTrack* inst = dynamic_cast<InstrumentTrack *>( t );
180
int val = inst->effectChannelModel()->value(0);
183
// we are deleting this track's fx send
185
inst->effectChannelModel()->setValue(0);
187
else if( val > index )
189
// subtract 1 to make up for the missing channel
190
inst->effectChannelModel()->setValue(val-1);
195
// delete all of this channel's sends and receives
196
for(int i=0; i<m_fxChannels[index]->m_sends.size(); ++i)
198
deleteChannelSend(index, m_fxChannels[index]->m_sends[i]);
200
for(int i=0; i<m_fxChannels[index]->m_receives.size(); ++i)
202
deleteChannelSend(m_fxChannels[index]->m_receives[i], index);
205
for(int i=0; i<m_fxChannels.size(); ++i)
207
// for every send/receive, adjust for the channel index change
208
for(int j=0; j<m_fxChannels[i]->m_sends.size(); ++j)
210
if( m_fxChannels[i]->m_sends[j] > index )
212
// subtract 1 to make up for the missing channel
213
--m_fxChannels[i]->m_sends[j];
216
for(int j=0; j<m_fxChannels[i]->m_receives.size(); ++j)
218
if( m_fxChannels[i]->m_receives[j] > index )
220
// subtract 1 to make up for the missing channel
221
--m_fxChannels[i]->m_receives[j];
227
// actually delete the channel
228
delete m_fxChannels[index];
229
m_fxChannels.remove(index);
234
void FxMixer::moveChannelLeft(int index)
236
// can't move master or first channel
237
if( index <= 1 || index >= m_fxChannels.size() )
243
int a = index - 1, b = index;
245
// go through every instrument and adjust for the channel index change
246
QVector<track *> songTrackList = engine::getSong()->tracks();
247
QVector<track *> bbTrackList = engine::getBBTrackContainer()->tracks();
249
QVector<track *> trackLists[] = {songTrackList, bbTrackList};
250
for(int tl=0; tl<2; ++tl)
252
QVector<track *> trackList = trackLists[tl];
253
for(int i=0; i<trackList.size(); ++i)
255
if( trackList[i]->type() == track::InstrumentTrack )
257
InstrumentTrack * inst = (InstrumentTrack *) trackList[i];
258
int val = inst->effectChannelModel()->value(0);
261
inst->effectChannelModel()->setValue(b);
265
inst->effectChannelModel()->setValue(a);
272
for(int i=0; i<m_fxChannels.size(); ++i)
274
// for every send/receive, adjust for the channel index change
275
for(int j=0; j<m_fxChannels[i]->m_sends.size(); ++j)
277
if( m_fxChannels[i]->m_sends[j] == a )
279
m_fxChannels[i]->m_sends[j] = b;
281
else if( m_fxChannels[i]->m_sends[j] == b )
283
m_fxChannels[i]->m_sends[j] = a;
286
for(int j=0; j<m_fxChannels[i]->m_receives.size(); ++j)
288
if( m_fxChannels[i]->m_receives[j] == a )
290
m_fxChannels[i]->m_receives[j] = b;
292
else if( m_fxChannels[i]->m_receives[j] == b )
294
m_fxChannels[i]->m_receives[j] = a;
299
// actually do the swap
300
FxChannel * tmpChannel = m_fxChannels[a];
301
m_fxChannels[a] = m_fxChannels[b];
302
m_fxChannels[b] = tmpChannel;
307
void FxMixer::moveChannelRight(int index)
309
moveChannelLeft(index+1);
314
void FxMixer::createChannelSend(fx_ch_t fromChannel, fx_ch_t toChannel,
317
// find the existing connection
318
FxChannel * from = m_fxChannels[fromChannel];
319
for(int i=0; i<from->m_sends.size(); ++i){
320
if( from->m_sends[i] == toChannel )
322
// simply adjust the amount
323
from->m_sendAmount[i]->setValue(amount);
328
// connection does not exist. create a new one
330
// add to from's sends
331
from->m_sends.push_back(toChannel);
332
from->m_sendAmount.push_back(new FloatModel(amount, 0, 1, 0.001, NULL,
333
tr("Amount to send")));
335
// add to to's receives
336
m_fxChannels[toChannel]->m_receives.push_back(fromChannel);
342
// delete the connection made by createChannelSend
343
void FxMixer::deleteChannelSend(fx_ch_t fromChannel, fx_ch_t toChannel)
346
FxChannel * from = m_fxChannels[fromChannel];
347
FxChannel * to = m_fxChannels[toChannel];
349
// find and delete the send entry
350
for(int i=0; i<from->m_sends.size(); ++i) {
351
if( from->m_sends[i] == toChannel )
354
delete from->m_sendAmount[i];
355
from->m_sendAmount.remove(i);
356
from->m_sends.remove(i);
361
// find and delete the receive entry
362
for(int i=0; i<to->m_receives.size(); ++i)
364
if( to->m_receives[i] == fromChannel )
367
to->m_receives.remove(i);
374
bool FxMixer::isInfiniteLoop(fx_ch_t sendFrom, fx_ch_t sendTo) {
375
// can't send master to anything
376
if( sendFrom == 0 ) return true;
378
// can't send channel to itself
379
if( sendFrom == sendTo ) return true;
381
// follow sendTo's outputs recursively looking for something that sends
383
for(int i=0; i<m_fxChannels[sendTo]->m_sends.size(); ++i)
385
if( isInfiniteLoop( sendFrom, m_fxChannels[sendTo]->m_sends[i] ) )
395
// how much does fromChannel send its output to the input of toChannel?
396
FloatModel * FxMixer::channelSendModel(fx_ch_t fromChannel, fx_ch_t toChannel)
398
FxChannel * from = m_fxChannels[fromChannel];
399
for(int i=0; i<from->m_sends.size(); ++i){
400
if( from->m_sends[i] == toChannel )
401
return from->m_sendAmount[i];
408
88
void FxMixer::mixToChannel( const sampleFrame * _buf, fx_ch_t _ch )
410
// SMF: it seems like here the track-channels are mixed in... but from where
411
// is this called and when and why...?!?
413
// OK, found it (git grep is your friend...): This is the next part,
414
// where there is a mix between push and pull model inside the core, as
415
// the audio-tracks *push* their data into the fx-channels hopefully just
416
// before the Mixer-Channels are processed... Sorry to say this: but this
417
// took me senseless hours to find out and is silly, too...
419
90
if( m_fxChannels[_ch]->m_muteModel.value() == false )
421
92
m_fxChannels[_ch]->m_lock.lock();
422
MixHelpers::add( m_fxChannels[_ch]->m_buffer, _buf, engine::mixer()->framesPerPeriod() );
93
sampleFrame * buf = m_fxChannels[_ch]->m_buffer;
94
for( f_cnt_t f = 0; f < engine::mixer()->framesPerPeriod(); ++f )
96
buf[f][0] += _buf[f][0];
97
buf[f][1] += _buf[f][1];
99
m_fxChannels[_ch]->m_used = true;
423
100
m_fxChannels[_ch]->m_lock.unlock();
438
void FxMixer::addChannelLeaf( int _ch, sampleFrame * _buf )
440
FxChannel * thisCh = m_fxChannels[_ch];
442
// if we're muted or this channel is seen already, discount it
443
if( thisCh->m_queued )
448
foreach( const int senderIndex, thisCh->m_receives )
450
addChannelLeaf( senderIndex, _buf );
453
// add this channel to job list
454
thisCh->m_queued = true;
455
MixerWorkerThread::addJob( thisCh );
460
159
void FxMixer::masterMix( sampleFrame * _buf )
462
161
const int fpp = engine::mixer()->framesPerPeriod();
464
// recursively loop through channel dependency chain
465
// and add all channels to job list that have no dependencies
466
// when the channel completes it will check its parent to see if it needs
468
MixerWorkerThread::resetJobQueue( MixerWorkerThread::JobQueue::Dynamic );
469
addChannelLeaf( 0, _buf );
470
while( m_fxChannels[0]->state() != ThreadableJob::Done )
472
MixerWorkerThread::startAndWaitForJobs();
474
//m_fxChannels[0]->doProcessing( NULL );
162
memcpy( _buf, m_fxChannels[0]->m_buffer, sizeof( sampleFrame ) * fpp );
164
for( int i = 1; i < NumFxChannels+1; ++i )
166
if( m_fxChannels[i]->m_used )
168
sampleFrame * ch_buf = m_fxChannels[i]->m_buffer;
169
const float v = m_fxChannels[i]->m_volumeModel.value();
170
for( f_cnt_t f = 0; f < fpp; ++f )
172
_buf[f][0] += ch_buf[f][0] * v;
173
_buf[f][1] += ch_buf[f][1] * v;
175
engine::mixer()->clearAudioBuffer( ch_buf,
176
engine::mixer()->framesPerPeriod() );
177
m_fxChannels[i]->m_used = false;
181
processChannel( 0, _buf );
183
if( m_fxChannels[0]->m_muteModel.value() )
185
engine::mixer()->clearAudioBuffer( _buf,
186
engine::mixer()->framesPerPeriod() );
476
190
const float v = m_fxChannels[0]->m_volumeModel.value();
477
MixHelpers::addMultiplied( _buf, m_fxChannels[0]->m_buffer, v, fpp );
191
for( f_cnt_t f = 0; f < engine::mixer()->framesPerPeriod(); ++f )
479
197
m_fxChannels[0]->m_peakLeft *= engine::mixer()->masterGain();
480
198
m_fxChannels[0]->m_peakRight *= engine::mixer()->masterGain();
482
// clear all channel buffers and
483
// reset channel process state
484
for( int i = 0; i < numChannels(); ++i)
486
engine::mixer()->clearAudioBuffer( m_fxChannels[i]->m_buffer, engine::mixer()->framesPerPeriod() );
487
m_fxChannels[i]->reset();
488
m_fxChannels[i]->m_queued = false;
495
204
void FxMixer::clear()
497
while( m_fxChannels.size() > 1 )
507
void FxMixer::clearChannel(fx_ch_t index)
509
FxChannel * ch = m_fxChannels[index];
510
ch->m_fxChain.clear();
511
ch->m_volumeModel.setValue( 1.0f );
512
ch->m_muteModel.setValue( false );
513
ch->m_name = ( index == 0 ) ? tr( "Master" ) : tr( "FX %1" ).arg( index );
514
ch->m_volumeModel.setDisplayName(ch->m_name );
516
// send only to master
519
// delete existing sends
520
for( int i=0; i<ch->m_sends.size(); ++i)
522
deleteChannelSend(index, ch->m_sends[i]);
525
// add send to master
526
createChannelSend(index, 0);
530
for( int i=0; i<ch->m_receives.size(); ++i)
532
deleteChannelSend(ch->m_receives[i], index);
206
for( int i = 0; i <= NumFxChannels; ++i )
208
m_fxChannels[i]->m_fxChain.clear();
209
m_fxChannels[i]->m_volumeModel.setValue( 1.0f );
210
m_fxChannels[i]->m_muteModel.setValue( false );
211
m_fxChannels[i]->m_name = ( i == 0 ) ?
212
tr( "Master" ) : tr( "FX %1" ).arg( i );
213
m_fxChannels[i]->m_volumeModel.setDisplayName(
214
m_fxChannels[i]->m_name );
537
222
void FxMixer::saveSettings( QDomDocument & _doc, QDomElement & _this )
539
for( int i = 0; i < m_fxChannels.size(); ++i )
224
for( int i = 0; i <= NumFxChannels; ++i )
541
FxChannel * ch = m_fxChannels[i];
543
226
QDomElement fxch = _doc.createElement( QString( "fxchannel" ) );
544
227
_this.appendChild( fxch );
546
ch->m_fxChain.saveState( _doc, fxch );
547
ch->m_volumeModel.saveSettings( _doc, fxch, "volume" );
548
ch->m_muteModel.saveSettings( _doc, fxch, "muted" );
228
m_fxChannels[i]->m_fxChain.saveState( _doc, fxch );
229
m_fxChannels[i]->m_volumeModel.saveSettings( _doc, fxch,
231
m_fxChannels[i]->m_muteModel.saveSettings( _doc, fxch,
549
233
fxch.setAttribute( "num", i );
550
fxch.setAttribute( "name", ch->m_name );
552
// add the channel sends
553
for( int si = 0; si < ch->m_sends.size(); ++si )
555
QDomElement sendsDom = _doc.createElement( QString( "send" ) );
556
fxch.appendChild( sendsDom );
558
sendsDom.setAttribute( "channel", ch->m_sends[si] );
559
ch->m_sendAmount[si]->saveSettings( _doc, sendsDom, "amount");
564
// make sure we have at least num channels
565
void FxMixer::allocateChannelsTo(int num)
567
while( num > m_fxChannels.size() - 1 )
571
// delete the default send to master
572
deleteChannelSend(m_fxChannels.size()-1, 0);
234
fxch.setAttribute( "name", m_fxChannels[i]->m_name );
577
241
void FxMixer::loadSettings( const QDomElement & _this )
580
244
QDomNode node = _this.firstChild();
581
bool thereIsASend = false;
583
while( ! node.isNull() )
245
for( int i = 0; i <= NumFxChannels; ++i )
585
247
QDomElement fxch = node.toElement();
587
// index of the channel we are about to load
588
248
int num = fxch.attribute( "num" ).toInt();
590
// allocate enough channels
591
allocateChannelsTo( num );
249
m_fxChannels[num]->m_fxChain.restoreState(
250
fxch.firstChildElement(
251
m_fxChannels[num]->m_fxChain.nodeName() ) );
593
252
m_fxChannels[num]->m_volumeModel.loadSettings( fxch, "volume" );
594
253
m_fxChannels[num]->m_muteModel.loadSettings( fxch, "muted" );
595
254
m_fxChannels[num]->m_name = fxch.attribute( "name" );
597
m_fxChannels[num]->m_fxChain.restoreState( fxch.firstChildElement(
598
m_fxChannels[num]->m_fxChain.nodeName() ) );
601
QDomNodeList chData = fxch.childNodes();
602
for( unsigned int i=0; i<chData.length(); ++i )
604
QDomElement chDataItem = chData.at(i).toElement();
605
if( chDataItem.nodeName() == QString( "send" ) )
608
int sendTo = chDataItem.attribute( "channel" ).toInt();
609
allocateChannelsTo( sendTo) ;
610
float amount = chDataItem.attribute( "amount" ).toFloat();
611
createChannelSend( num, sendTo, amount );
617
255
node = node.nextSibling();
620
// check for old format. 65 fx channels and no explicit sends.
621
if( ! thereIsASend && m_fxChannels.size() == 65 ) {
622
// create a send from every channel into master
623
for( int i=1; i<m_fxChannels.size(); ++i )
625
createChannelSend(i, 0);
629
258
emit dataChanged();