~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to muse/node.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Eric Hedekar, Eric Hedekar, Fabrice Coutadeur
  • Date: 2010-01-26 02:32:14 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100126023214-8ez2g5d26d9p584j
Tags: 1.0.1-0ubuntu1
[ Eric Hedekar ]
* New upstream version (LP: #479688)
* Removed patches that were fixed in upstream source
  -[10_64bit_memcorruption_fix]
  -[10_gcc43_build_fixes]
  -[10_lash_private_api_fix]
  -[10_log2f_aliasing_fix]
  -[10_vamgui_init_fix]
  -[20_fix_const]

[ Fabrice Coutadeur ]
* debian/watch: added watch file
* debian/muse.desktop: deleted deprecated Encoding key, Application category
  and icon extension. This fix several warning with lintian and
  desktop-file-validate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//=========================================================
2
2
//  MusE
3
3
//  Linux Music Editor
4
 
//  $Id: node.cpp,v 1.36.2.5 2006/02/01 23:17:38 spamatica Exp $
 
4
//  $Id: node.cpp,v 1.36.2.25 2009/12/20 05:00:35 terminator356 Exp $
5
5
//
6
6
//  (C) Copyright 2000-2004 Werner Schweer (ws@seh.de)
7
7
//=========================================================
9
9
#include <cmath>
10
10
#include <assert.h>
11
11
#include <sndfile.h>
 
12
#include <stdlib.h>
12
13
 
13
14
#include "node.h"
14
15
#include "globals.h"
 
16
#include "gconfig.h"
15
17
#include "song.h"
16
18
#include "xml.h"
17
19
#include "plugin.h"
21
23
#include "wave.h"
22
24
#include "utils.h"      //debug
23
25
#include "ticksynth.h"  // metronome
 
26
#include "al/dsp.h"
 
27
 
 
28
// Uncomment this (and make sure to set Jack buffer size high like 2048) 
 
29
//  to see process flow messages.
 
30
//#define NODE_DEBUG 
 
31
//#define FIFO_DEBUG 
 
32
 
 
33
// Added by Tim. p3.3.18
 
34
//#define METRONOME_DEBUG 
24
35
 
25
36
//---------------------------------------------------------
26
37
//   isMute
28
39
 
29
40
bool MidiTrack::isMute() const
30
41
      {
31
 
      if (_solo)
 
42
      if (_solo || (_internalSolo && !_mute))
32
43
            return false;
33
 
      if (_soloMode)
 
44
      
 
45
      if (_soloRefCnt)
34
46
            return true;
 
47
      
35
48
      return _mute;
36
49
      }
37
50
 
38
51
bool AudioTrack::isMute() const
39
52
      {
40
 
      if (_solo)
 
53
      if (_solo || (_internalSolo && !_mute))
41
54
            return false;
42
 
      if (_soloMode)
 
55
      
 
56
      if (_soloRefCnt)
43
57
            return true;
 
58
      
44
59
      return _mute;
45
60
      }
46
61
 
47
 
bool AudioOutput::isMute() const
48
 
      {
49
 
      return false;
50
 
      }
51
 
 
52
62
//---------------------------------------------------------
53
63
//   setSolo
54
64
//---------------------------------------------------------
55
65
 
56
66
void MidiTrack::setSolo(bool val)
 
67
{
 
68
      if(_solo != val)
57
69
      {
58
 
      //TODO
59
 
      _solo     = val;
60
 
      _soloMode = _solo;
 
70
        _solo = val;
 
71
        updateSoloStates(false);
61
72
      }
 
73
}
62
74
 
63
75
void AudioTrack::setSolo(bool val)
 
76
{
 
77
      if(_solo != val)
64
78
      {
65
 
      //TODO
66
 
      _solo     = val;
67
 
      _soloMode = val;
68
 
      if (mute())
 
79
        _solo = val;
 
80
        updateSoloStates(false);
 
81
      }
 
82
      
 
83
      if (isMute())
69
84
            resetMeter();
 
85
}
 
86
 
 
87
//---------------------------------------------------------
 
88
//   setInternalSolo
 
89
//---------------------------------------------------------
 
90
 
 
91
void Track::setInternalSolo(unsigned int val)
 
92
{
 
93
  _internalSolo = val;
 
94
}
 
95
 
 
96
//---------------------------------------------------------
 
97
//   clearSoloRefCounts
 
98
//   This is a static member function. Required for outside access.
 
99
//   Clears the internal static reference counts. 
 
100
//---------------------------------------------------------
 
101
 
 
102
void Track::clearSoloRefCounts()
 
103
{
 
104
  _soloRefCnt = 0;
 
105
}
 
106
 
 
107
//---------------------------------------------------------
 
108
//   updateSoloState
 
109
//---------------------------------------------------------
 
110
 
 
111
void Track::updateSoloState()
 
112
{
 
113
    if(_solo)
 
114
      _soloRefCnt++;
 
115
    else
 
116
    if(_soloRefCnt && !_tmpSoloChainNoDec)
 
117
      _soloRefCnt--;
 
118
}
 
119
 
 
120
//---------------------------------------------------------
 
121
//   updateInternalSoloStates
 
122
//---------------------------------------------------------
 
123
 
 
124
void Track::updateInternalSoloStates()
 
125
{
 
126
    if(_tmpSoloChainTrack->solo())
 
127
    {
 
128
      _internalSolo++;
 
129
      _soloRefCnt++;
 
130
    }  
 
131
    else
 
132
    if(!_tmpSoloChainNoDec) 
 
133
    {                           
 
134
      if(_internalSolo)
 
135
        _internalSolo--;
 
136
      if(_soloRefCnt)
 
137
        _soloRefCnt--;
 
138
    }  
 
139
}
 
140
 
 
141
//---------------------------------------------------------
 
142
//   updateInternalSoloStates
 
143
//---------------------------------------------------------
 
144
 
 
145
void MidiTrack::updateInternalSoloStates()
 
146
{
 
147
      if(this == _tmpSoloChainTrack)
 
148
        return;
 
149
      
 
150
      Track::updateInternalSoloStates();
 
151
}
 
152
 
 
153
//---------------------------------------------------------
 
154
//   updateInternalSoloStates
 
155
//---------------------------------------------------------
 
156
 
 
157
void AudioTrack::updateInternalSoloStates()
 
158
{
 
159
      if(this == _tmpSoloChainTrack)
 
160
        return;
 
161
      
 
162
      Track::updateInternalSoloStates();
 
163
      
 
164
      if(_tmpSoloChainDoIns)
 
165
      {
 
166
        if(type() == AUDIO_SOFTSYNTH)
 
167
        {
 
168
          const MidiTrackList* ml = song->midis();
 
169
          for(ciMidiTrack im = ml->begin(); im != ml->end(); ++im)
 
170
          {
 
171
            MidiTrack* mt = *im;
 
172
            if(mt->outPort() >= 0 && mt->outPort() == ((SynthI*)this)->midiPort())
 
173
              mt->updateInternalSoloStates();
 
174
          }
 
175
        }
 
176
        
 
177
        const RouteList* rl = inRoutes();
 
178
        for(ciRoute ir = rl->begin(); ir != rl->end(); ++ir)
 
179
        {
 
180
          if(ir->type == TRACK_ROUTE)
 
181
            ir->track->updateInternalSoloStates();
 
182
        }
70
183
      }
 
184
      else
 
185
      {  
 
186
        const RouteList* rl = outRoutes();
 
187
        for(ciRoute ir = rl->begin(); ir != rl->end(); ++ir)
 
188
        {
 
189
          if(ir->type == TRACK_ROUTE)
 
190
            ir->track->updateInternalSoloStates();
 
191
        }
 
192
      }  
 
193
}
 
194
 
 
195
//---------------------------------------------------------
 
196
//   updateSoloStates
 
197
//---------------------------------------------------------
 
198
 
 
199
void MidiTrack::updateSoloStates(bool noDec)
 
200
{
 
201
  if(noDec && !_solo)
 
202
    return;
 
203
  
 
204
  _tmpSoloChainTrack = this;
 
205
  _tmpSoloChainDoIns = false;
 
206
  _tmpSoloChainNoDec = noDec;
 
207
  updateSoloState();
 
208
  
 
209
  if(outPort() >= 0)
 
210
  {
 
211
    MidiDevice *md = midiPorts[outPort()].device();
 
212
    if(md && md->isSynti())
 
213
      ((SynthI*)md)->updateInternalSoloStates();
 
214
  }
 
215
}
 
216
 
 
217
//---------------------------------------------------------
 
218
//   updateSoloStates
 
219
//---------------------------------------------------------
 
220
 
 
221
void AudioTrack::updateSoloStates(bool noDec)
 
222
{
 
223
  if(noDec && !_solo)
 
224
    return;
 
225
  
 
226
  _tmpSoloChainTrack = this;
 
227
  _tmpSoloChainNoDec = noDec;
 
228
  updateSoloState();
 
229
  
 
230
  _tmpSoloChainDoIns = true;
 
231
  if(type() == AUDIO_SOFTSYNTH)
 
232
  {
 
233
    const MidiTrackList* ml = song->midis();
 
234
    for(ciMidiTrack im = ml->begin(); im != ml->end(); ++im)
 
235
    {
 
236
      MidiTrack* mt = *im;
 
237
      if(mt->outPort() >= 0 && mt->outPort() == ((SynthI*)this)->midiPort())
 
238
        mt->updateInternalSoloStates();
 
239
    }
 
240
  }
 
241
  
 
242
  {
 
243
    const RouteList* rl = inRoutes();
 
244
    for(ciRoute ir = rl->begin(); ir != rl->end(); ++ir)
 
245
    {
 
246
      if(ir->type == TRACK_ROUTE)
 
247
        ir->track->updateInternalSoloStates();
 
248
    }
 
249
  }  
 
250
  _tmpSoloChainDoIns = false;
 
251
  {
 
252
    const RouteList* rl = outRoutes();
 
253
    for(ciRoute ir = rl->begin(); ir != rl->end(); ++ir)
 
254
    {
 
255
      if(ir->type == TRACK_ROUTE)
 
256
        ir->track->updateInternalSoloStates();
 
257
    }
 
258
  }  
 
259
}
71
260
 
72
261
//---------------------------------------------------------
73
262
//   setMute
92
281
//---------------------------------------------------------
93
282
 
94
283
void AudioTrack::copyData(unsigned pos, int dstChannels, unsigned nframes, float** dstBuffer)
95
 
      {
96
 
      int srcChannels = channels();
97
 
      float* buffer[srcChannels];
98
 
      float data[nframes * srcChannels];
99
 
 
100
 
      for (int i = 0; i < srcChannels; ++i)
101
 
            buffer[i] = data + i * nframes;
102
 
 
103
 
      if (!getData(pos, srcChannels, nframes, buffer) || off() || (isMute() && !_prefader)) {
104
 
            for (int i = 0; i < dstChannels; ++i) {
105
 
                  memset(dstBuffer[i], 0, sizeof(float) * nframes);
106
 
                  _meter[i] = 0;
107
 
                  }
108
 
            return;
109
 
            }
110
 
 
111
 
      //---------------------------------------------------
112
 
      // aux sends
113
 
      //---------------------------------------------------
114
 
 
115
 
      if (hasAuxSend()) {
116
 
            AuxList* al = song->auxs();
117
 
            unsigned naux = al->size();
118
 
            for (unsigned i = 0; i < naux; ++i) {
119
 
                  float m = _auxSend[i];
120
 
                  if (m <= 0.0)           // optimize
121
 
                        continue;
122
 
                  AudioAux* a = (AudioAux*)((*al)[i]);
123
 
                  float** dst = a->sendBuffer();
124
 
                  for (int ch = 0; ch < srcChannels; ++ch) {
125
 
                        float* db = dst[ch % a->channels()];
126
 
                        float* sb = buffer[ch];
127
 
                        for (unsigned f = 0; f < nframes; ++f) {
128
 
                              *db++ += (*sb++ * m);   // add to mix
129
 
                              }
130
 
                        }
131
 
                  }
132
 
            }
133
 
 
134
 
      //---------------------------------------------------
135
 
      // apply plugin chain
136
 
      //---------------------------------------------------
137
 
 
138
 
      _efxPipe->apply(srcChannels, nframes, buffer);
139
 
 
140
 
      //---------------------------------------------------
141
 
      //    prefader metering
142
 
      //---------------------------------------------------
143
 
 
144
 
      float meter[srcChannels];
145
 
      if (_prefader) {
146
 
            for (int i = 0; i < srcChannels; ++i) {
147
 
                  float* p = buffer[i];
148
 
                  meter[i] = 0.0;
149
 
                  for (unsigned k = 0; k < nframes; ++k) {
150
 
                        double f = fabs(*p);
151
 
                        if (f > meter[i])
152
 
                              meter[i] = f;
153
 
                        ++p;
154
 
                        }
155
 
                  _meter[i] = lrint(meter[i] * 32767.0);
156
 
                  if (_meter[i] > _peak[i])
157
 
                        _peak[i] = _meter[i];
158
 
                  }
159
 
            }
160
 
      if (isMute()) {
161
 
            for (int i = 0; i < dstChannels; ++i)
162
 
                  memset(dstBuffer[i], 0, sizeof(float) * nframes);
163
 
            return;
164
 
            }
165
 
 
166
 
      //---------------------------------------------------
167
 
      // apply volume
168
 
      //    postfader metering
169
 
      //---------------------------------------------------
170
 
 
171
 
      double vol[2];
172
 
      double _volume = volume();
173
 
      double _pan = pan();
174
 
      vol[0] = _volume * (1.0 - _pan);
175
 
      vol[1] = _volume * (1.0 + _pan);
176
 
 
177
 
      if (srcChannels == dstChannels) {
178
 
            if (_prefader) {
179
 
                  for (int c = 0; c < dstChannels; ++c) {
180
 
                        float* sp = buffer[c];
181
 
                        float* dp = dstBuffer[c];
182
 
                        for (unsigned k = 0; k < nframes; ++k)
183
 
                              *dp++ = (*sp++ * vol[c]);
184
 
                        }
185
 
                  }
186
 
            else {
187
 
                  for (int c = 0; c < dstChannels; ++c) {
188
 
                        meter[c] = 0.0;
189
 
                        float* sp = buffer[c];
190
 
                        float* dp = dstBuffer[c];
191
 
                        //printf("2 dstBuffer[c]=%d\n",long(dstBuffer[c]));
192
 
                        for (unsigned k = 0; k < nframes; ++k) {
193
 
                              float val = *sp++ * vol[c];
194
 
                              *dp++ = val;
195
 
                              double f = fabs(val);
196
 
                              if (f > meter[c])
197
 
                                    meter[c] = f;
198
 
                              }
199
 
                        _meter[c] = lrint(meter[c] * 32767.0);
200
 
                        if (_meter[c] > _peak[c])
201
 
                              _peak[c] = _meter[c];
202
 
                        }
203
 
                  }
204
 
            }
205
 
      else if (srcChannels == 1 && dstChannels == 2) {
206
 
            float* sp = buffer[0];
207
 
            if (_prefader) {
208
 
                  for (int c = 0; c < dstChannels; ++c) {
209
 
                        float* dp = dstBuffer[c];
210
 
                        for (unsigned k = 0; k < nframes; ++k)
211
 
                              *dp++ = (*sp++ * vol[c]);
212
 
                        }
213
 
                  }
214
 
            else {
215
 
                  meter[0] = 0.0;
216
 
                  for (unsigned k = 0; k < nframes; ++k) {
217
 
                        float val = *sp++;
218
 
                        double f = fabs(val) * _volume;
219
 
                        if (f > meter[0])
220
 
                              meter[0] = f;
221
 
                        *(dstBuffer[0] + k) = val * vol[0];
222
 
                        *(dstBuffer[1] + k) = val * vol[1];
223
 
                        }
224
 
                  _meter[0] = lrint(meter[0] * 32767.0);
225
 
                  if (_meter[0] > _peak[0])
226
 
                        _peak[0] = _meter[0];
227
 
                  }
228
 
            }
229
 
      else if (srcChannels == 2 && dstChannels == 1) {
230
 
            float* sp1 = buffer[0];
231
 
            float* sp2 = buffer[1];
232
 
            if (_prefader) {
233
 
                  float* dp = dstBuffer[0];
234
 
                  for (unsigned k = 0; k < nframes; ++k)
235
 
                        *dp++ = (*sp1++ * vol[0] + *sp2++ * vol[1]);
236
 
                  }
237
 
            else {
238
 
                  float* dp = dstBuffer[0];
239
 
                  meter[0] = 0.0;
240
 
                  meter[1] = 0.0;
241
 
                  for (unsigned k = 0; k < nframes; ++k) {
242
 
                        float val1 = *sp1++ * vol[0];
243
 
                        float val2 = *sp2++ * vol[1];
244
 
                        double f1 = fabs(val1);
245
 
                        if (f1 > meter[0])
246
 
                              meter[0] = f1;
247
 
                        double f2 = fabs(val2);
248
 
                        if (f2 > meter[1])
249
 
                              meter[1] = f2;
250
 
                        *dp++ = (val1 + val2);
251
 
                        }
252
 
                  _meter[0] = lrint(meter[0] * 32767.0);
253
 
                  if (_meter[0] > _peak[0])
254
 
                        _peak[0] = _meter[0];
255
 
                  _meter[1] = lrint(meter[1] * 32767.0);
256
 
                  if (_meter[1] > _peak[1])
257
 
                        _peak[1] = _meter[1];
258
 
                  }
259
 
            }
260
 
      }
 
284
{
 
285
  //Changed by T356. 12/12/09. 
 
286
  // Overhaul and streamline to eliminate multiple processing during one process loop. 
 
287
  // Was causing ticking sound with synths + multiple out routes because synths were being processed multiple times.
 
288
  // Make better use of AudioTrack::outBuffers as a post-effect pre-volume cache system for multiple calls here during processing.
 
289
  // Previously only WaveTrack used them. (Changed WaveTrack as well).
 
290
  
 
291
  int srcChannels = channels();
 
292
  
 
293
  //Added by Tim. p3.3.16
 
294
  #ifdef NODE_DEBUG
 
295
  printf("MusE: AudioTrack::copyData name:%s processed:%d\n", name().latin1(), processed());
 
296
  #endif
 
297
  
 
298
  // Special consideration for metronome: It is not part of the track list,
 
299
  //  and it has no in or out routes, yet multiple output tracks may call addData on it !
 
300
  // We can't tell how many output tracks call it, so we can only assume there might be more than one.
 
301
  // Not strictly necessary here because only addData is ever called, but just to be consistent...
 
302
  //bool usedirectbuf = (outRoutes()->size() <= 1) || (type() == AUDIO_OUTPUT);
 
303
  bool usedirectbuf = ((outRoutes()->size() <= 1) || (type() == AUDIO_OUTPUT)) && (this != metronome);
 
304
  
 
305
  int i;
 
306
  
 
307
  float* buffer[srcChannels];
 
308
  //float data[nframes * srcChannels];
 
309
  //for(i = 0; i < srcChannels; ++i)
 
310
  //      buffer[i] = data + i * nframes;
 
311
        
 
312
  // precalculate stereo volume
 
313
  double vol[2];
 
314
  double _volume = volume();
 
315
  double _pan = pan();
 
316
  vol[0] = _volume * (1.0 - _pan);
 
317
  vol[1] = _volume * (1.0 + _pan);
 
318
  float meter[srcChannels];
 
319
 
 
320
  // Have we been here already during this process cycle?
 
321
  if(processed())
 
322
  {
 
323
    // If there is only one (or no) output routes, it's an error - we've been called more than once per process cycle!
 
324
    #ifdef NODE_DEBUG
 
325
    if(usedirectbuf)
 
326
      printf("MusE: AudioTrack::copyData Error! One or no out routes, but already processed! Copying local buffers anyway...\n");
 
327
    #endif
 
328
    
 
329
    // Is there already some data gathered from a previous call during this process cycle?
 
330
    if(_haveData)
 
331
    {
 
332
      // Point the input buffers at our local cached 'pre-volume' buffers. They need processing, so continue on after.
 
333
      for(i = 0; i < srcChannels; ++i)
 
334
        buffer[i] = outBuffers[i];
 
335
    }
 
336
    else
 
337
    {
 
338
      // No data was available from a previous call during this process cycle. Zero the supplied buffers and just return.
 
339
      for(i = 0; i < dstChannels; ++i) 
 
340
      {
 
341
        if(config.useDenormalBias) 
 
342
        {
 
343
          for(unsigned int q = 0; q < nframes; ++q)
 
344
            dstBuffer[i][q] = denormalBias;
 
345
        } 
 
346
        else
 
347
          memset(dstBuffer[i], 0, sizeof(float) * nframes);
 
348
      }
 
349
      return;  
 
350
    }
 
351
  }
 
352
  else 
 
353
  {
 
354
    // First time here during this process cycle. 
 
355
    
 
356
    // Point the input buffers at a temporary stack buffer.
 
357
    float data[nframes * srcChannels];
 
358
    for(i = 0; i < srcChannels; ++i)
 
359
        buffer[i] = data + i * nframes;
 
360
  
 
361
    // getData can use the supplied buffers, or change buffer to point to its own local buffers or Jack buffers etc. 
 
362
    // For ex. if this is an audio input, Jack will set the pointers for us in AudioInput::getData!
 
363
    if(!getData(pos, srcChannels, nframes, buffer) || off() || (isMute() && !_prefader)) 
 
364
    {
 
365
      //Added by Tim. p3.3.16
 
366
      #ifdef NODE_DEBUG
 
367
      printf("MusE: AudioTrack::copyData name:%s dstChannels:%d zeroing buffers\n", name().latin1(), dstChannels);
 
368
      #endif
 
369
      
 
370
      // No data was available. Zero the supplied buffers.
 
371
      unsigned int q;
 
372
      for(i = 0; i < dstChannels; ++i) 
 
373
      {
 
374
        if(config.useDenormalBias) 
 
375
        {
 
376
          for(q = 0; q < nframes; ++q)
 
377
            dstBuffer[i][q] = denormalBias;
 
378
        } 
 
379
        else
 
380
          memset(dstBuffer[i], 0, sizeof(float) * nframes);
 
381
      }  
 
382
      
 
383
      for(i = 0; i < srcChannels; ++i) 
 
384
      {
 
385
        //_meter[i] = 0;
 
386
        _meter[i] = 0.0;
 
387
        
 
388
        /*
 
389
        if(!usedirectbuf)
 
390
        {
 
391
          if(config.useDenormalBias) 
 
392
          {
 
393
            for(q = 0; q < nframes; ++q)
 
394
              outBuffers[i][q] = denormalBias;
 
395
          }
 
396
          else    
 
397
            memset(outBuffers[i], 0, sizeof(float) * nframes);
 
398
        }
 
399
        */
 
400
      }
 
401
      
 
402
      _haveData = false;
 
403
      _processed = true;
 
404
      return;
 
405
    }
 
406
 
 
407
    //---------------------------------------------------
 
408
    // apply plugin chain
 
409
    //---------------------------------------------------
 
410
 
 
411
    _efxPipe->apply(srcChannels, nframes, buffer);
 
412
 
 
413
    //---------------------------------------------------
 
414
    // aux sends
 
415
    //---------------------------------------------------
 
416
 
 
417
    if(hasAuxSend() && !isMute()) 
 
418
    {
 
419
      AuxList* al = song->auxs();
 
420
      unsigned naux = al->size();
 
421
      for(unsigned k = 0; k < naux; ++k) 
 
422
      {
 
423
        float m = _auxSend[k];
 
424
        if(m <= 0.0001)           // optimize
 
425
          continue;
 
426
        AudioAux* a = (AudioAux*)((*al)[k]);
 
427
        float** dst = a->sendBuffer();
 
428
        int auxChannels = a->channels();
 
429
        if((srcChannels ==1 && auxChannels==1) || srcChannels == 2) 
 
430
        {
 
431
          for(int ch = 0; ch < srcChannels; ++ch) 
 
432
          {
 
433
            float* db = dst[ch % a->channels()]; // no matter whether there's one or two dst buffers
 
434
            float* sb = buffer[ch];
 
435
            for(unsigned f = 0; f < nframes; ++f) 
 
436
              *db++ += (*sb++ * m * vol[ch]);   // add to mix
 
437
          }
 
438
        }
 
439
        else if(srcChannels==1 && auxChannels==2)  // copy mono to both channels
 
440
        {  
 
441
          for(int ch = 0; ch < auxChannels; ++ch) 
 
442
          {
 
443
            float* db = dst[ch % a->channels()];
 
444
            float* sb = buffer[0];
 
445
            for(unsigned f = 0; f < nframes; ++f) 
 
446
              *db++ += (*sb++ * m * vol[ch]);   // add to mix
 
447
          }
 
448
        }
 
449
      }
 
450
    }
 
451
 
 
452
    //---------------------------------------------------
 
453
    //    prefader metering
 
454
    //---------------------------------------------------
 
455
 
 
456
    if(_prefader) 
 
457
    {
 
458
      for(i = 0; i < srcChannels; ++i) 
 
459
      {
 
460
        float* p = buffer[i];
 
461
        meter[i] = 0.0;
 
462
        for(unsigned k = 0; k < nframes; ++k) 
 
463
        {
 
464
          double f = fabs(*p);
 
465
          if(f > meter[i])
 
466
            meter[i] = f;
 
467
          ++p;
 
468
        }
 
469
        //_meter[i] = lrint(meter[i] * 32767.0);
 
470
        _meter[i] = meter[i];
 
471
        if(_meter[i] > _peak[i])
 
472
          _peak[i] = _meter[i];
 
473
      }
 
474
    }
 
475
    
 
476
    if(isMute()) 
 
477
    {
 
478
      unsigned int q;
 
479
      for(i = 0; i < dstChannels; ++i)
 
480
      {
 
481
        if(config.useDenormalBias) 
 
482
        {
 
483
          for(q = 0; q < nframes; q++)
 
484
            dstBuffer[i][q] = denormalBias;
 
485
        } 
 
486
        else 
 
487
          memset(dstBuffer[i], 0, sizeof(float) * nframes);
 
488
      }        
 
489
      
 
490
      /*
 
491
      if(!usedirectbuf)
 
492
      {
 
493
        for(i = 0; i < srcChannels; ++i) 
 
494
        {
 
495
          if(config.useDenormalBias) 
 
496
          {
 
497
            for(q = 0; q < nframes; ++q)
 
498
              outBuffers[i][q] = denormalBias;
 
499
          }
 
500
          else    
 
501
            memset(outBuffers[i], 0, sizeof(float) * nframes);
 
502
        }
 
503
      }
 
504
      */  
 
505
      
 
506
      _haveData = false;
 
507
      _processed = true;
 
508
      return;
 
509
    }
 
510
    
 
511
    // If we're using local cached 'pre-volume' buffers, copy the input buffers (as they are right now: post-effect pre-volume) back to them. 
 
512
    if(!usedirectbuf)
 
513
    {
 
514
      for(i = 0; i < srcChannels; ++i)
 
515
        AL::dsp->cpy(outBuffers[i], buffer[i], nframes);
 
516
    }
 
517
    
 
518
    // We have some data! Set to true.
 
519
    _haveData = true;
 
520
  }
 
521
  
 
522
  //---------------------------------------------------
 
523
  // apply volume
 
524
  //    postfader metering
 
525
  //---------------------------------------------------
 
526
 
 
527
  if(srcChannels == dstChannels) 
 
528
  {
 
529
    if(_prefader) 
 
530
    {
 
531
      for(int c = 0; c < dstChannels; ++c) 
 
532
      {
 
533
        float* sp = buffer[c];
 
534
        float* dp = dstBuffer[c];
 
535
        for(unsigned k = 0; k < nframes; ++k)
 
536
          *dp++ = (*sp++ * vol[c]);
 
537
      }
 
538
    }
 
539
    else 
 
540
    {
 
541
      for(int c = 0; c < dstChannels; ++c) 
 
542
      {
 
543
        meter[c] = 0.0;
 
544
        float* sp = buffer[c];
 
545
        float* dp = dstBuffer[c];
 
546
        //printf("2 dstBuffer[c]=%d\n",long(dstBuffer[c]));
 
547
        for(unsigned k = 0; k < nframes; ++k) 
 
548
        {
 
549
          float val = *sp++ * vol[c];
 
550
          *dp++ = val;
 
551
          double f = fabs(val);
 
552
          if(f > meter[c])
 
553
            meter[c] = f;
 
554
        }
 
555
        //_meter[c] = lrint(meter[c] * 32767.0);
 
556
        _meter[c] = meter[c];
 
557
        if(_meter[c] > _peak[c])
 
558
          _peak[c] = _meter[c];
 
559
      }
 
560
    }
 
561
  }
 
562
  else if(srcChannels == 1 && dstChannels == 2) 
 
563
  {
 
564
    float* sp = buffer[0];
 
565
    if(_prefader) 
 
566
    {
 
567
      for(int c = 0; c < dstChannels; ++c) 
 
568
      {
 
569
        float* dp = dstBuffer[c];
 
570
        for(unsigned k = 0; k < nframes; ++k)
 
571
          *dp++ = (*sp++ * vol[c]);
 
572
      }
 
573
    }
 
574
    else 
 
575
    {
 
576
      meter[0] = 0.0;
 
577
      for(unsigned k = 0; k < nframes; ++k) 
 
578
      {
 
579
        float val = *sp++;
 
580
        double f = fabs(val) * _volume;
 
581
        if(f > meter[0])
 
582
          meter[0] = f;
 
583
        *(dstBuffer[0] + k) = val * vol[0];
 
584
        *(dstBuffer[1] + k) = val * vol[1];
 
585
      }
 
586
      //_meter[0] = lrint(meter[0] * 32767.0);
 
587
      _meter[0] = meter[0];
 
588
      if(_meter[0] > _peak[0])
 
589
        _peak[0] = _meter[0];
 
590
    }
 
591
  }
 
592
  else if(srcChannels == 2 && dstChannels == 1) 
 
593
  {
 
594
    float* sp1 = buffer[0];
 
595
    float* sp2 = buffer[1];
 
596
    if(_prefader) 
 
597
    {
 
598
      float* dp = dstBuffer[0];
 
599
      for(unsigned k = 0; k < nframes; ++k)
 
600
        *dp++ = (*sp1++ * vol[0] + *sp2++ * vol[1]);
 
601
    }
 
602
    else 
 
603
    {
 
604
      float* dp = dstBuffer[0];
 
605
      meter[0] = 0.0;
 
606
      meter[1] = 0.0;
 
607
      for(unsigned k = 0; k < nframes; ++k) 
 
608
      {
 
609
        float val1 = *sp1++ * vol[0];
 
610
        float val2 = *sp2++ * vol[1];
 
611
        double f1 = fabs(val1);
 
612
        if(f1 > meter[0])
 
613
          meter[0] = f1;
 
614
        double f2 = fabs(val2);
 
615
        if(f2 > meter[1])
 
616
          meter[1] = f2;
 
617
        *dp++ = (val1 + val2);
 
618
      }
 
619
      //_meter[0] = lrint(meter[0] * 32767.0);
 
620
      _meter[0] = meter[0];
 
621
      if(_meter[0] > _peak[0])
 
622
        _peak[0] = _meter[0];
 
623
      //_meter[1] = lrint(meter[1] * 32767.0);
 
624
      _meter[1] = meter[1];
 
625
      if(_meter[1] > _peak[1])
 
626
        _peak[1] = _meter[1];
 
627
    }
 
628
  }
 
629
        
 
630
  _processed = true;
 
631
}
261
632
 
262
633
//---------------------------------------------------------
263
634
//   addData
264
635
//---------------------------------------------------------
265
636
 
266
637
void AudioTrack::addData(unsigned pos, int dstChannels, unsigned nframes, float** dstBuffer)
267
 
      {
268
 
      if (off())
269
 
            return;
270
 
      int srcChannels = channels();
271
 
      float* buffer[srcChannels];
272
 
      float data[nframes * srcChannels];
273
 
 
274
 
      for (int i = 0; i < srcChannels; ++i)
275
 
            buffer[i] = data + i * nframes;
276
 
      // getData can use the supplied buffers or change buffer to point
277
 
      // to local buffers
278
 
 
279
 
      if (!getData(pos, srcChannels, nframes, buffer)) {
280
 
            for (int i = 0; i < srcChannels; ++i)
281
 
                  _meter[i] = 0;
282
 
            return;
283
 
            }
284
 
 
285
 
      //---------------------------------------------------
286
 
      // aux sends
287
 
      //---------------------------------------------------
288
 
 
289
 
      if (hasAuxSend()) {
290
 
            AuxList* al = song->auxs();
291
 
            unsigned naux = al->size();
292
 
            for (unsigned i = 0; i < naux; ++i) {
293
 
                  float m = _auxSend[i];
294
 
                  if (m <= 0.0)           // optimize
295
 
                        continue;
296
 
                  AudioAux* a = (AudioAux*)((*al)[i]);
297
 
                  float** dst = a->sendBuffer();
298
 
                  for (int ch = 0; ch < srcChannels; ++ch) {
299
 
                        float* db = dst[ch % a->channels()];
300
 
                        float* sb = buffer[ch];
301
 
                        for (unsigned f = 0; f < nframes; ++f) {
302
 
                              *db++ += (*sb++ * m);   // add to mix
303
 
                              }
304
 
                        }
305
 
                  }
306
 
            }
307
 
 
308
 
      //---------------------------------------------------
309
 
      // apply plugin chain
310
 
      //---------------------------------------------------
311
 
 
312
 
      _efxPipe->apply(srcChannels, nframes, buffer);
313
 
 
314
 
      //---------------------------------------------------
315
 
      //    prefader metering
316
 
      //---------------------------------------------------
317
 
 
318
 
      float meter[srcChannels];
319
 
      if (_prefader) {
320
 
            for (int i = 0; i < srcChannels; ++i) {
321
 
                  float* p = buffer[i];
322
 
                  meter[i] = 0.0;
323
 
                  for (unsigned k = 0; k < nframes; ++k) {
324
 
                        double f = fabs(*p);
325
 
                        if (f > meter[i])
326
 
                              meter[i] = f;
327
 
                        ++p;
328
 
                        }
329
 
                  _meter[i] = lrint(meter[i] * 32767.0);
330
 
                  if (_meter[i] > _peak[i])
331
 
                        _peak[i] = _meter[i];
332
 
                  }
333
 
            }
334
 
      if (isMute())
335
 
            return;
336
 
 
337
 
      //---------------------------------------------------
338
 
      // apply volume
339
 
      //    postfader metering
340
 
      //---------------------------------------------------
341
 
 
342
 
      double vol[2];
343
 
      double _volume = volume();
344
 
      double _pan = pan();
345
 
      vol[0] = _volume * (1.0 - _pan);
346
 
      vol[1] = _volume * (1.0 + _pan);
347
 
 
348
 
      if (srcChannels == dstChannels) {
349
 
            if (_prefader) {
350
 
                  for (int c = 0; c < dstChannels; ++c) {
351
 
                        float* sp = buffer[c];
352
 
                        float* dp = dstBuffer[c];
353
 
                        for (unsigned k = 0; k < nframes; ++k)
354
 
                              *dp++ += (*sp++ * vol[c]);
355
 
                        }
356
 
                  }
357
 
            else {
358
 
                  for (int c = 0; c < dstChannels; ++c) {
359
 
                        meter[c] = 0.0;
360
 
                        float* sp = buffer[c];
361
 
                        float* dp = dstBuffer[c];
362
 
                        for (unsigned k = 0; k < nframes; ++k) {
363
 
                              float val = *sp++ * vol[c];
364
 
                              *dp++ += val;
365
 
                              double f = fabs(val);
366
 
                              if (f > meter[c])
367
 
                                    meter[c] = f;
368
 
                              }
369
 
                        _meter[c] = lrint(meter[c] * 32767.0);
370
 
                        if (_meter[c] > _peak[c])
371
 
                              _peak[c] = _meter[c];
372
 
                        }
373
 
                  }
374
 
            }
375
 
      else if (srcChannels == 1 && dstChannels == 2) {
376
 
            if (_prefader) {
377
 
                  for (int c = 0; c < dstChannels; ++c) {
378
 
                        float* dp = dstBuffer[c];
379
 
                        float* sp = buffer[0];
380
 
                        for (unsigned k = 0; k < nframes; ++k)
381
 
                              *dp++ += (*sp++ * vol[c]);
382
 
                        }
383
 
                  }
384
 
            else {
385
 
                  float* sp = buffer[0];
386
 
                  meter[0]  = 0.0;
387
 
                  for (unsigned k = 0; k < nframes; ++k) {
388
 
                        float val = *sp++;
389
 
                        double f = fabs(val) * _volume;
390
 
                        if (f > meter[0])
391
 
                              meter[0] = f;
392
 
                        *(dstBuffer[0] + k) += val * vol[0];
393
 
                        *(dstBuffer[1] + k) += val * vol[1];
394
 
                        }
395
 
                  _meter[0] = lrint(meter[0] * 32767.0);
396
 
                  if (_meter[0] > _peak[0])
397
 
                        _peak[0] = _meter[0];
398
 
                  }
399
 
            }
400
 
      else if (srcChannels == 2 && dstChannels == 1) {
401
 
            float* sp1 = buffer[0];
402
 
            float* sp2 = buffer[1];
403
 
            if (_prefader) {
404
 
                  float* dp = dstBuffer[0];
405
 
                  for (unsigned k = 0; k < nframes; ++k)
406
 
                        *dp++ += (*sp1++ * vol[0] + *sp2++ * vol[1]);
407
 
                  }
408
 
            else {
409
 
                  float* dp = dstBuffer[0];
410
 
                  meter[0] = 0.0;
411
 
                  meter[1] = 0.0;
412
 
                  for (unsigned k = 0; k < nframes; ++k) {
413
 
                        float val1 = *sp1++ * vol[0];
414
 
                        float val2 = *sp2++ * vol[1];
415
 
                        double f1 = fabs(val1);
416
 
                        if (f1 > meter[0])
417
 
                              meter[0] = f1;
418
 
                        double f2 = fabs(val2);
419
 
                        if (f2 > meter[1])
420
 
                              meter[1] = f2;
421
 
                        *dp++ += (val1 + val2);
422
 
                        }
423
 
                  _meter[0] = lrint(meter[0] * 32767.0);
424
 
                  if (_meter[0] > _peak[0])
425
 
                        _peak[0] = _meter[0];
426
 
                  _meter[1] = lrint(meter[1] * 32767.0);
427
 
                  if (_meter[1] > _peak[1])
428
 
                        _peak[1] = _meter[1];
429
 
                  }
430
 
            }
431
 
      }
 
638
{
 
639
  //Changed by T356. 12/12/09. 
 
640
  // Overhaul and streamline to eliminate multiple processing during one process loop.
 
641
  // Was causing ticking sound with synths + multiple out routes because synths were being processed multiple times.
 
642
  // Make better use of AudioTrack::outBuffers as a post-effect pre-volume cache system for multiple calls here during processing.
 
643
  // Previously only WaveTrack used them. (Changed WaveTrack as well).
 
644
  
 
645
  //Added by Tim. p3.3.16
 
646
  #ifdef NODE_DEBUG
 
647
  printf("MusE: AudioTrack::addData name:%s processed:%d\n", name().latin1(), processed());
 
648
  #endif
 
649
  
 
650
  if (off())
 
651
  {
 
652
    _processed = true;
 
653
    return;
 
654
  } 
 
655
        
 
656
  int srcChannels = channels();
 
657
  
 
658
  // Special consideration for metronome: It is not part of the track list,
 
659
  //  and it has no in or out routes, yet multiple output tracks may call addData on it !
 
660
  // We can't tell how many output tracks call it, so we can only assume there might be more than one.
 
661
  //bool usedirectbuf = (outRoutes()->size() <= 1) || (type() == AUDIO_OUTPUT);
 
662
  bool usedirectbuf = ((outRoutes()->size() <= 1) || (type() == AUDIO_OUTPUT)) && (this != metronome);
 
663
  
 
664
  int i;
 
665
  
 
666
  float* buffer[srcChannels];
 
667
  //float data[nframes * srcChannels];
 
668
  //for (i = 0; i < srcChannels; ++i)
 
669
  //      buffer[i] = data + i * nframes;
 
670
  
 
671
  // precalculate stereo volume
 
672
  double vol[2];
 
673
  double _volume = volume();
 
674
  double _pan = pan();
 
675
  vol[0] = _volume * (1.0 - _pan);
 
676
  vol[1] = _volume * (1.0 + _pan);
 
677
  float meter[srcChannels];
 
678
 
 
679
  // Have we been here already during this process cycle?
 
680
  if(processed())
 
681
  {
 
682
    // If there is only one (or no) output routes, it's an error - we've been called more than once per process cycle!
 
683
    #ifdef NODE_DEBUG
 
684
    if(usedirectbuf)
 
685
      printf("MusE: AudioTrack::addData Error! One or no out routes, but already processed! Copying local buffers anyway...\n");
 
686
    #endif
 
687
    
 
688
    // Is there already some data gathered from a previous call during this process cycle?
 
689
    if(_haveData)
 
690
    {
 
691
      // Point the input buffers at our local cached 'pre-volume' buffers. They need processing, so continue on after.
 
692
      for(i = 0; i < srcChannels; ++i)
 
693
        buffer[i] = outBuffers[i];
 
694
    }
 
695
    else
 
696
      // No data was available from a previous call during this process cycle. Nothing to add, just return.
 
697
      return;  
 
698
  }
 
699
  else
 
700
  {
 
701
    // First time here during this process cycle. 
 
702
    
 
703
    // Point the input buffers at a temporary stack buffer.
 
704
    float data[nframes * srcChannels];
 
705
    for(i = 0; i < srcChannels; ++i)
 
706
      buffer[i] = data + i * nframes;
 
707
    
 
708
    // getData can use the supplied buffers, or change buffer to point to its own local buffers or Jack buffers etc. 
 
709
    // For ex. if this is an audio input, Jack will set the pointers for us.
 
710
    if(!getData(pos, srcChannels, nframes, buffer)) 
 
711
    {
 
712
      // No data was available. Nothing to add, but zero our local buffers and the meters.
 
713
      for(i = 0; i < srcChannels; ++i)
 
714
      {
 
715
        // If we're using local buffers, we must zero them so that the next thing requiring them 
 
716
        //  during this process cycle will see zeros.
 
717
        /*
 
718
        if(!usedirectbuf)
 
719
        {
 
720
          if(config.useDenormalBias) 
 
721
          {
 
722
            for(unsigned int q = 0; q < nframes; ++q)
 
723
              outBuffers[i][q] = denormalBias;
 
724
          }  
 
725
          else
 
726
            memset(outBuffers[i], 0, sizeof(float) * nframes);
 
727
        }
 
728
        */  
 
729
          
 
730
        //_meter[i] = 0;
 
731
        _meter[i] = 0.0;
 
732
      }      
 
733
      
 
734
      _haveData = false;
 
735
      _processed = true;
 
736
      return;
 
737
    }
 
738
 
 
739
    //---------------------------------------------------
 
740
    // apply plugin chain
 
741
    //---------------------------------------------------
 
742
 
 
743
    _efxPipe->apply(srcChannels, nframes, buffer);
 
744
 
 
745
    //---------------------------------------------------
 
746
    // aux sends
 
747
    //---------------------------------------------------
 
748
 
 
749
    if(hasAuxSend() && !isMute()) 
 
750
    {
 
751
      AuxList* al = song->auxs();
 
752
      unsigned naux = al->size();
 
753
      for(unsigned k = 0; k < naux; ++k) 
 
754
      {
 
755
        float m = _auxSend[k];
 
756
        if(m <= 0.0001)           // optimize
 
757
          continue;
 
758
        AudioAux* a = (AudioAux*)((*al)[k]);
 
759
        float** dst = a->sendBuffer();
 
760
        int auxChannels = a->channels();
 
761
        if((srcChannels ==1 && auxChannels==1) || srcChannels==2) 
 
762
        {
 
763
          for(int ch = 0; ch < srcChannels; ++ch) 
 
764
          {
 
765
            float* db = dst[ch % a->channels()];
 
766
            float* sb = buffer[ch];
 
767
            for(unsigned f = 0; f < nframes; ++f) 
 
768
              *db++ += (*sb++ * m * vol[ch]);   // add to mix
 
769
          }
 
770
        }
 
771
        else if(srcChannels == 1 && auxChannels == 2) 
 
772
        {
 
773
          for(int ch = 0; ch < auxChannels; ++ch) 
 
774
          {
 
775
            float* db = dst[ch % a->channels()];
 
776
            float* sb = buffer[0];
 
777
            for(unsigned f = 0; f < nframes; ++f) 
 
778
              *db++ += (*sb++ * m * vol[ch]);   // add to mix
 
779
          }
 
780
        }
 
781
      }
 
782
    }
 
783
 
 
784
    //---------------------------------------------------
 
785
    //    prefader metering
 
786
    //---------------------------------------------------
 
787
 
 
788
    if(_prefader) 
 
789
    {
 
790
      for(i = 0; i < srcChannels; ++i) 
 
791
      {
 
792
        float* p = buffer[i];
 
793
        meter[i] = 0.0;
 
794
        for(unsigned k = 0; k < nframes; ++k) 
 
795
        {
 
796
          double f = fabs(*p);
 
797
          if(f > meter[i])
 
798
            meter[i] = f;
 
799
          ++p;
 
800
        }
 
801
        //_meter[i] = lrint(meter[i] * 32767.0);
 
802
        _meter[i] = meter[i];
 
803
        if(_meter[i] > _peak[i])
 
804
          _peak[i] = _meter[i];
 
805
      }
 
806
    }
 
807
    
 
808
    if(isMute())
 
809
    {
 
810
      // If we're using local buffers, we must zero them.
 
811
      /*
 
812
      if(!usedirectbuf)
 
813
      {
 
814
        for(i = 0; i < srcChannels; ++i)
 
815
        {
 
816
          if(config.useDenormalBias) 
 
817
          {
 
818
            for(unsigned int q = 0; q < nframes; ++q)
 
819
              outBuffers[i][q] = denormalBias;
 
820
          }  
 
821
          else
 
822
            memset(outBuffers[i], 0, sizeof(float) * nframes);
 
823
        }    
 
824
      }  
 
825
      */
 
826
      
 
827
      _haveData = false;
 
828
      _processed = true;
 
829
      return;
 
830
    }
 
831
    
 
832
    // If we're using local cached 'pre-volume' buffers, copy the input buffers (as they are right now: post-effect pre-volume) back to them. 
 
833
    if(!usedirectbuf)
 
834
    {
 
835
      for(i = 0; i < srcChannels; ++i)
 
836
        AL::dsp->cpy(outBuffers[i], buffer[i], nframes);
 
837
    }
 
838
    
 
839
    // We have some data! Set to true.
 
840
    _haveData = true;
 
841
  }  
 
842
  
 
843
  //---------------------------------------------------
 
844
  // apply volume
 
845
  //    postfader metering
 
846
  //---------------------------------------------------
 
847
 
 
848
  if(srcChannels == dstChannels) 
 
849
  {
 
850
    if(_prefader) 
 
851
    {
 
852
      for(int c = 0; c < dstChannels; ++c) 
 
853
      {
 
854
        float* sp = buffer[c];
 
855
        float* dp = dstBuffer[c];
 
856
        for(unsigned k = 0; k < nframes; ++k)
 
857
          *dp++ += (*sp++ * vol[c]);
 
858
      }
 
859
    }
 
860
    else 
 
861
    {
 
862
      for(int c = 0; c < dstChannels; ++c) 
 
863
      {
 
864
        meter[c] = 0.0;
 
865
        float* sp = buffer[c];
 
866
        float* dp = dstBuffer[c];
 
867
        for(unsigned k = 0; k < nframes; ++k) 
 
868
        {
 
869
          float val = *sp++ * vol[c];
 
870
          *dp++ += val;
 
871
          double f = fabs(val);
 
872
          if (f > meter[c])
 
873
                meter[c] = f;
 
874
        }
 
875
        //_meter[c] = lrint(meter[c] * 32767.0);
 
876
        _meter[c] = meter[c];
 
877
        if(_meter[c] > _peak[c])
 
878
          _peak[c] = _meter[c];
 
879
      }
 
880
    }
 
881
  }
 
882
  else if(srcChannels == 1 && dstChannels == 2) 
 
883
  {
 
884
    if(_prefader) 
 
885
    {
 
886
      for(int c = 0; c < dstChannels; ++c) 
 
887
      {
 
888
        float* dp = dstBuffer[c];
 
889
        float* sp = buffer[0];
 
890
        for(unsigned k = 0; k < nframes; ++k)
 
891
          *dp++ += (*sp++ * vol[c]);
 
892
      }
 
893
    }
 
894
    else 
 
895
    {
 
896
      float* sp = buffer[0];
 
897
      meter[0]  = 0.0;
 
898
      for(unsigned k = 0; k < nframes; ++k) 
 
899
      {
 
900
        float val = *sp++;
 
901
        double f = fabs(val) * _volume;
 
902
        if(f > meter[0])
 
903
          meter[0] = f;
 
904
        *(dstBuffer[0] + k) += val * vol[0];
 
905
        *(dstBuffer[1] + k) += val * vol[1];
 
906
      }
 
907
      //_meter[0] = lrint(meter[0] * 32767.0);
 
908
      _meter[0] = meter[0];
 
909
      if(_meter[0] > _peak[0])
 
910
        _peak[0] = _meter[0];
 
911
    }
 
912
  }
 
913
  else if(srcChannels == 2 && dstChannels == 1) 
 
914
  {
 
915
    float* sp1 = buffer[0];
 
916
    float* sp2 = buffer[1];
 
917
    if(_prefader) 
 
918
    {
 
919
      float* dp = dstBuffer[0];
 
920
      for(unsigned k = 0; k < nframes; ++k)
 
921
        *dp++ += (*sp1++ * vol[0] + *sp2++ * vol[1]);
 
922
    }
 
923
    else 
 
924
    {
 
925
      float* dp = dstBuffer[0];
 
926
      meter[0] = 0.0;
 
927
      meter[1] = 0.0;
 
928
      for(unsigned k = 0; k < nframes; ++k) 
 
929
      {
 
930
        float val1 = *sp1++ * vol[0];
 
931
        float val2 = *sp2++ * vol[1];
 
932
        double f1 = fabs(val1);
 
933
        if(f1 > meter[0])
 
934
          meter[0] = f1;
 
935
        double f2 = fabs(val2);
 
936
        if(f2 > meter[1])
 
937
          meter[1] = f2;
 
938
        *dp++ += (val1 + val2);
 
939
      }
 
940
      //_meter[0] = lrint(meter[0] * 32767.0);
 
941
      _meter[0] = meter[0];
 
942
      if(_meter[0] > _peak[0])
 
943
        _peak[0] = _meter[0];
 
944
      //_meter[1] = lrint(meter[1] * 32767.0);
 
945
      _meter[1] = meter[1];
 
946
      if(_meter[1] > _peak[1])
 
947
        _peak[1] = _meter[1];
 
948
    }
 
949
  }
 
950
  
 
951
  _processed = true;
 
952
}
432
953
 
433
954
//---------------------------------------------------------
434
955
//   readVolume
462
983
            }
463
984
      }
464
985
 
 
986
// Removed by T356
 
987
// "recfile" tag not saved anymore
 
988
/*
465
989
//---------------------------------------------------------
466
990
//   readRecfile
467
991
//---------------------------------------------------------
512
1036
                  }
513
1037
            }
514
1038
      }
 
1039
*/
515
1040
 
516
1041
//---------------------------------------------------------
517
1042
//   setChannels
519
1044
 
520
1045
void Track::setChannels(int n)
521
1046
      {
522
 
      _channels = n;
 
1047
      if(n > MAX_CHANNELS)
 
1048
        _channels = MAX_CHANNELS;
 
1049
      else  
 
1050
        _channels = n;
523
1051
      for (int i = 0; i < _channels; ++i) {
524
 
            _meter[i] = 0;
525
 
            _peak[i]  = 0;
 
1052
            //_meter[i] = 0;
 
1053
            _meter[i] = 0.0;
 
1054
            //_peak[i]  = 0;
 
1055
            _peak[i]  = 0.0;
526
1056
            }
527
1057
      }
528
1058
 
562
1092
      // use supplied buffers
563
1093
 
564
1094
      RouteList* rl = inRoutes();
 
1095
      
 
1096
      // Added by Tim. p3.3.16
 
1097
      #ifdef NODE_DEBUG
 
1098
      printf("AudioTrack::getData name:%s inRoutes:%d\n", name().latin1(), rl->size());
 
1099
      #endif
 
1100
      
565
1101
      iRoute ir = rl->begin();
566
1102
      if (ir == rl->end())
567
1103
            return false;
 
1104
      
 
1105
      // Added by Tim. p3.3.16
 
1106
      #ifdef NODE_DEBUG
 
1107
      printf("    calling copyData on %s...\n", ir->track->name().latin1());
 
1108
      #endif
 
1109
      
568
1110
      ir->track->copyData(pos, channels, nframes, buffer);
569
1111
      ++ir;
570
1112
      for (; ir != rl->end(); ++ir) {
 
1113
            // Added by Tim. p3.3.16
 
1114
            #ifdef NODE_DEBUG
 
1115
            printf("    calling addData on %s...\n", ir->track->name().latin1());
 
1116
            #endif
 
1117
              
571
1118
            ir->track->addData(pos, channels, nframes, buffer);
572
1119
            }
573
1120
      return true;
583
1130
      if (!checkAudioDevice()) return false;
584
1131
      for (int ch = 0; ch < channels; ++ch) {
585
1132
            void* jackPort = jackPorts[ch];
586
 
            if (jackPort)
 
1133
            if (jackPort) {
587
1134
                  buffer[ch] = audioDevice->getBuffer(jackPort, nframes);
588
 
            else
589
 
                  memset(buffer[ch], 0, nframes * sizeof(float));
 
1135
                  if (config.useDenormalBias) {
 
1136
                      for (unsigned int i=0; i < nframes; i++)
 
1137
                              buffer[ch][i] += denormalBias;
 
1138
                  }
 
1139
            } else {
 
1140
                  if (config.useDenormalBias) {
 
1141
                      for (unsigned int i=0; i < nframes; i++)
 
1142
                              buffer[ch][i] = denormalBias;
 
1143
                      } else {
 
1144
                              memset(buffer[ch], 0, nframes * sizeof(float));
 
1145
                      }
 
1146
                  }
590
1147
            }
591
1148
      return true;
592
1149
      }
617
1174
void Track::resetMeter()
618
1175
      {
619
1176
      for (int i = 0; i < _channels; ++i)
620
 
            _meter[i] = 0;
 
1177
            //_meter[i] = 0;
 
1178
            _meter[i] = 0.0;
621
1179
      }
622
1180
 
623
1181
//---------------------------------------------------------
627
1185
void Track::resetPeaks()
628
1186
      {
629
1187
      for (int i = 0; i < _channels; ++i)
630
 
            _peak[i] = 0;
 
1188
            //_peak[i] = 0;
 
1189
            _peak[i] = 0.0;
 
1190
            _lastActivity = 0;
631
1191
      }
632
1192
 
633
1193
//---------------------------------------------------------
706
1266
      unsigned pos = 0;
707
1267
      float* buffer[_channels];
708
1268
 
709
 
// printf("AudioTrack: record() fifo %p\n", &fifo);
710
 
 
711
 
      if (fifo.get(_channels, segmentSize, buffer, &pos)) {
712
 
            printf("AudioTrack::record(): empty fifo\n");
713
 
            return;
714
 
            }
715
 
      if (_recFile) {
716
 
            _recFile->seek(pos, 0);
717
 
            _recFile->write(_channels, buffer, segmentSize);
718
 
            }
719
 
      else {
720
 
            printf("AudioNode::record(): no recFile\n");
 
1269
      //printf("AudioTrack: record() fifo %p, count=%d\n", &fifo, fifo.getCount());
 
1270
 
 
1271
      while(fifo.getCount()) {
 
1272
 
 
1273
            if (fifo.get(_channels, segmentSize, buffer, &pos)) {
 
1274
                  printf("AudioTrack::record(): empty fifo\n");
 
1275
                  return;
 
1276
                  }
 
1277
            if (_recFile) {
 
1278
                  // Line removed by Tim. p3.3.8 Oct 28, 2009
 
1279
                  //_recFile->seek(pos, 0);
 
1280
                  //
 
1281
                  // Fix for recorded waves being shifted ahead by an amount
 
1282
                  //  equal to start record position.
 
1283
                  //
 
1284
                  // From libsndfile ChangeLog:
 
1285
                  // 2008-05-11  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
1286
                  //    * src/sndfile.c
 
1287
                  //    Allow seeking past end of file during write.
 
1288
                  //    
 
1289
                  // I don't know why this line would even be called, because the FIFOs'
 
1290
                  //  'pos' members operate in absolute frames, which at this point 
 
1291
                  //  would be shifted ahead by the start of the wave part.
 
1292
                  // So if you begin recording a new wave part at bar 4, for example, then
 
1293
                  //  this line is seeking the record file to frame 288000 even before any audio is written!
 
1294
                  // Therefore, just let the write do its thing and progress naturally,
 
1295
                  //  it should work OK since everything was OK before the libsndfile change...
 
1296
                  //
 
1297
                  // Tested: With the line, audio record looping sort of works, albiet with the start offset added to
 
1298
                  //  the wave file. And it overwrites existing audio. (Check transport window 'overwrite' function. Tie in somehow...)
 
1299
                  // With the line, looping does NOT work with libsndfile from around early 2007 (my distro's version until now).
 
1300
                  // Therefore it seems sometime between libsndfile ~2007 and today, libsndfile must have allowed 
 
1301
                  //  "seek (behind) on write", as well as the "seek past end" change of 2008...
 
1302
                  //
 
1303
                  // Ok, so removing that line breaks *possible* record audio 'looping' functionality, revealed with
 
1304
                  //  later libsndfile. 
 
1305
                  // Try this... And while we're at it, honour the punchin/punchout, and loop functions !
 
1306
                  //
 
1307
                  // If punchin is on, or we have looped at least once, use left marker as offset.
 
1308
                  // Note that audio::startRecordPos is reset to (roughly) the left marker pos upon loop !
 
1309
                  // (Not any more! I changed Audio::Process)
 
1310
                  // Since it is possible to start loop recording before the left marker (with punchin off), we must 
 
1311
                  //  use startRecordPos or loopFrame or left marker, depending on punchin and whether we have looped yet.
 
1312
                  unsigned fr;
 
1313
                  if(song->punchin() && (audio->loopCount() == 0))
 
1314
                    fr = song->lPos().frame();
 
1315
                  else  
 
1316
                  if((audio->loopCount() > 0) && (audio->getStartRecordPos().frame() > audio->loopFrame()))
 
1317
                    fr = audio->loopFrame();
 
1318
                  else
 
1319
                    fr = audio->getStartRecordPos().frame();
 
1320
                  // Now seek and write. If we are looping and punchout is on, don't let punchout point interfere with looping point.
 
1321
                  if( (pos >= fr) && (!song->punchout() || (!song->loop() && pos < song->rPos().frame())) )
 
1322
                  {
 
1323
                    pos -= fr;
 
1324
                    // Added by Tim. p3.3.8
 
1325
                    //int position = _recFile->seek(0, SEEK_CUR);
 
1326
                    //printf("AudioTrack::record loopcnt:%d lframe:%d newpos:%d curpos:%d start:%d end:%d\n", audio->loopCount(), audio->loopFrame(), pos, position, audio->getStartRecordPos().frame(), audio->getEndRecordPos().frame());
 
1327
                    
 
1328
                    _recFile->seek(pos, 0);
 
1329
                    _recFile->write(_channels, buffer, segmentSize);
 
1330
                  }
 
1331
                    
 
1332
                  }
 
1333
            else {
 
1334
                  printf("AudioNode::record(): no recFile\n");
 
1335
                  }
721
1336
            }
722
1337
      }
723
1338
 
732
1347
      for (int i = 0; i < channels(); ++i) {
733
1348
            if (jackPorts[i]) {
734
1349
                  buffer[i] = audioDevice->getBuffer(jackPorts[i], nframes);
 
1350
                  if (config.useDenormalBias) {
 
1351
                      for (unsigned int j=0; j < nframes; j++)
 
1352
                              buffer[i][j] += denormalBias;
 
1353
                      }
735
1354
                  }
736
1355
            else
737
1356
                  printf("PANIC: processInit: no buffer from audio driver\n");
746
1365
 
747
1366
void AudioOutput::process(unsigned pos, unsigned offset, unsigned n)
748
1367
      {
 
1368
      //Added by Tim. p3.3.16
 
1369
      #ifdef NODE_DEBUG
 
1370
      printf("MusE: AudioOutput::process name:%s processed:%d\n", name().latin1(), processed());
 
1371
      #endif
 
1372
      
749
1373
      for (int i = 0; i < _channels; ++i) {
750
1374
            buffer1[i] = buffer[i] + offset;
751
1375
      }
760
1384
      {
761
1385
      processInit(n);
762
1386
      for (int i = 0; i < channels(); ++i)
763
 
            memset(buffer[i], 0, n * sizeof(float));
 
1387
          if (config.useDenormalBias) {
 
1388
              for (unsigned int j=0; j < n; j++)
 
1389
                  buffer[i][j] = denormalBias;
 
1390
            } else {
 
1391
                  memset(buffer[i], 0, n * sizeof(float));
 
1392
                  }
764
1393
      }
765
1394
 
766
1395
//---------------------------------------------------------
769
1398
 
770
1399
void AudioOutput::processWrite()
771
1400
      {
772
 
      if (audio->isRecording()) {
 
1401
      if (audio->isRecording() && song->bounceOutput == this) {
773
1402
            if (audio->freewheel()) {
774
 
                  // bounce to Track:
775
1403
                  WaveTrack* track = song->bounceTrack;
776
1404
                  if (track && track->recordFlag() && track->recFile())
777
1405
                        track->recFile()->write(_channels, buffer, _nframes);
778
 
                  // bounce to File:
779
1406
                  if (recordFlag() && recFile())
780
1407
                        _recFile->write(_channels, buffer, _nframes);
781
1408
                  }
782
1409
            else {
783
 
                  // bounce to Track:
784
1410
                  WaveTrack* track = song->bounceTrack;
785
1411
                  if (track && track->recordFlag() && track->recFile())
786
1412
                        track->putFifo(_channels, _nframes, buffer);
787
 
                  // bounce to File:
788
1413
                  if (recordFlag() && recFile())
789
1414
                        putFifo(_channels, _nframes, buffer);
790
1415
                  }
791
1416
            }
792
 
      if (audioClickFlag && song->click()) {
 
1417
      // Changed by Tim. p3.3.18
 
1418
      //if (audioClickFlag && song->click()) {
 
1419
      if (sendMetronome() && audioClickFlag && song->click()) {
 
1420
            
 
1421
            // Added by Tim. p3.3.18
 
1422
            #ifdef METRONOME_DEBUG
 
1423
            printf("MusE: AudioOutput::processWrite Calling metronome->addData frame:%u channels:%d frames:%lu\n", audio->pos().frame(), _channels, _nframes);
 
1424
            #endif
 
1425
    
793
1426
            metronome->addData(audio->pos().frame(), _channels, _nframes, buffer);
794
1427
            }
795
1428
      }
796
 
 
797
1429
//---------------------------------------------------------
798
1430
//   setName
799
1431
//---------------------------------------------------------
844
1476
 
845
1477
bool Fifo::put(int segs, unsigned long samples, float** src, unsigned pos)
846
1478
      {
 
1479
      // Added by Tim. p3.3.17
 
1480
      #ifdef FIFO_DEBUG
 
1481
      printf("FIFO::put segs:%d samples:%lu pos:%u\n", segs, samples, pos);
 
1482
      #endif
 
1483
      
847
1484
      if (muse_atomic_read(&count) == nbuffer) {
848
 
            printf("FIFO %p overrun... %ld\n", this, count);
 
1485
            printf("FIFO %p overrun... %d\n", this, count.counter);
849
1486
            return true;
850
1487
            }
851
1488
      FifoBuffer* b = buffer[widx];
852
1489
      int n         = segs * samples;
853
1490
      if (b->maxSize < n) {
854
1491
            if (b->buffer)
855
 
                 delete[] b->buffer;
856
 
            b->buffer  = new float[n];
 
1492
                 // Changed by Tim. p3.3.15
 
1493
                 //delete[] b->buffer;
 
1494
                 free(b->buffer);
 
1495
            // Changed by Tim. p3.3.15
 
1496
            //b->buffer  = new float[n];
 
1497
            posix_memalign((void**)&(b->buffer), 16, sizeof(float) * n);
857
1498
            b->maxSize = n;
858
1499
            }
859
1500
      b->size = samples;
860
1501
      b->segs = segs;
861
1502
      b->pos  = pos;
862
1503
      for (int i = 0; i < segs; ++i)
863
 
            memcpy(b->buffer + i * samples, src[i], samples * sizeof(float));
 
1504
            //memcpy(b->buffer + i * samples, src[i], samples * sizeof(float));
 
1505
            AL::dsp->cpy(b->buffer + i * samples, src[i], samples);
864
1506
      add();
865
1507
      return false;
866
1508
      }
872
1514
 
873
1515
bool Fifo::get(int segs, unsigned long samples, float** dst, unsigned* pos)
874
1516
      {
 
1517
      // Added by Tim. p3.3.17
 
1518
      #ifdef FIFO_DEBUG
 
1519
      printf("FIFO::get segs:%d samples:%lu\n", segs, samples);
 
1520
      #endif
 
1521
      
875
1522
      if (muse_atomic_read(&count) == 0) {
876
 
            printf("FIFO %p underrun...\n", this);
 
1523
            printf("FIFO %p underrun... %d\n", this,count.counter); //by willyfoobar: added count to output //see Fifo::put()
877
1524
            return true;
878
1525
            }
879
1526
      FifoBuffer* b = buffer[ridx];
886
1533
      return false;
887
1534
      }
888
1535
 
 
1536
int Fifo::getCount()
 
1537
      {
 
1538
      return muse_atomic_read(&count);
 
1539
      }
889
1540
//---------------------------------------------------------
890
1541
//   remove
891
1542
//---------------------------------------------------------
902
1553
 
903
1554
bool Fifo::getWriteBuffer(int segs, unsigned long samples, float** buf, unsigned pos)
904
1555
      {
 
1556
      // Added by Tim. p3.3.17
 
1557
      #ifdef FIFO_DEBUG
 
1558
      printf("Fifo::getWriteBuffer segs:%d samples:%lu pos:%u\n", segs, samples, pos);
 
1559
      #endif
 
1560
      
905
1561
      if (muse_atomic_read(&count) == nbuffer)
906
1562
            return true;
907
1563
      FifoBuffer* b = buffer[widx];
908
1564
      int n = segs * samples;
909
1565
      if (b->maxSize < n) {
910
1566
            if (b->buffer)
911
 
                 delete[] b->buffer;
912
 
            b->buffer = new float[n];
 
1567
                 // Changed by Tim. p3.3.15
 
1568
                 //delete[] b->buffer;
 
1569
                 free(b->buffer);
 
1570
            // Changed by Tim. p3.3.15
 
1571
            //b->buffer = new float[n];
 
1572
            posix_memalign((void**)&(b->buffer), 16, sizeof(float) * n);
913
1573
            b->maxSize = n;
914
1574
            }
915
1575
      for (int i = 0; i < segs; ++i)