summaryrefslogtreecommitdiff
path: root/muse2/muse/audioconvert.cpp
blob: 9ab2f06699b995292d00f2fbb756b31676ce2cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: audioconvert.cpp,v 1.1.1.1 2009/12/28 16:07:33 terminator356 Exp $
//
//  (C) Copyright 1999-2009 Werner Schweer (ws@seh.de)
//
//  Audio converter module created by Tim
//  (C) Copyright 2009-2011 Tim E. Real (terminator356 A T sourceforge D O T net)
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//
//=========================================================

#include <math.h>

#include "wave.h"
#include "globals.h"
#include "audioconvert.h"
#include "eventbase.h"

//#define AUDIOCONVERT_DEBUG
//#define AUDIOCONVERT_DEBUG_PRC

namespace MusECore {

//---------------------------------------------------------
//   AudioConvertMap
//---------------------------------------------------------

void AudioConvertMap::remapEvents(const EventList* /*el*/)  
{

}

iAudioConvertMap AudioConvertMap::addEvent(EventBase* eb)
{
  iAudioConvertMap iacm = getConverter(eb);
  if(iacm == end())
  {
    AudioConverter* cv = 0;
    if(!eb->sndFile().isNull())
      cv = new SRCAudioConverter(eb->sndFile().channels(), SRC_SINC_MEDIUM_QUALITY);
    
    // Use insert with hint for speed.
    return insert(iacm, std::pair<EventBase*, AudioConverter*> (eb, cv));
  }
  else
    // Adopt a policy of returning an already existing item to enforce no-duplicates.
    return iacm;
}

void AudioConvertMap::removeEvent(EventBase* eb)
{
  iAudioConvertMap iacm = find(eb);
  if(iacm != end())
  {
    AudioConverter* cv = iacm->second;
    if(cv)
      delete cv;
    erase(iacm);  
  }    
}

iAudioConvertMap AudioConvertMap::getConverter(EventBase* eb)
{
  return find(eb);
}

//---------------------------------------------------------
//   AudioConverter
//---------------------------------------------------------

AudioConverter::AudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("AudioConverter::AudioConverter this:%p\n", this);
  #endif

  _refCount = 1;
  _sfCurFrame = 0;
}

AudioConverter::~AudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("AudioConverter::~AudioConverter this:%p\n", this);
  #endif
}

AudioConverter* AudioConverter::reference()
{
  _refCount += 1;
  #ifdef AUDIOCONVERT_DEBUG
  printf("AudioConverter::reference this:%p current refcount:%d\n", this, _refCount);
  #endif
  return this;
}

AudioConverter* AudioConverter::release(AudioConverter* cv)
{
  if(!cv)
    return 0;
  cv->_refCount -= 1;
  #ifdef AUDIOCONVERT_DEBUG
  printf("AudioConverter::release converter:%p current refcount:%d\n", cv, cv->_refCount);
  #endif
  if(cv->_refCount <= 0)
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("AudioConverter::release deleting converter:%p\n", cv);
    #endif
    delete cv;
    cv = 0;
  }
  return cv;  
}

off_t AudioConverter::readAudio(MusECore::SndFileR& f, unsigned offset, float** buffer, int channel, int n, bool doSeek, bool overwrite)
{
  if(f.isNull())
    return _sfCurFrame;
  
  // Added by Tim. p3.3.17 DELETETHIS or comment it in again. it's disabled anyway
  //#ifdef AUDIOCONVERT_DEBUG_PRC
  //printf("AudioConverter::process %s audConv:%p sfCurFrame:%ld offset:%u channel:%d fchan:%d n:%d\n", 
  //        f.name().toLatin1(), this, sfCurFrame, offset, channel, f.channels(), n);
  //#endif
  
  off_t frame     = offset;  // _spos is added before the call.
  unsigned fsrate = f.samplerate();
  bool resample   = isValid() && ((unsigned)MusEGlobal::sampleRate != fsrate);  
  
  // No resampling needed?
  if(!resample)
  {
    // Sample rates are the same. Just a regular seek + read, no conversion.
    _sfCurFrame = f.seek(frame, 0);
    return _sfCurFrame + f.read(channel, buffer, n, overwrite);
  }
  
  // Is a 'transport' seek requested? (Not to be requested with every read! Should only be for 'first read' seeks, or positional 'transport' seeks.)
  // Due to the support of sound file references in MusE, seek must ALWAYS be done before read, as before,
  //  except now we alter the seek position if sample rate conversion is being used and remember the seek positions. 
  if(doSeek)
  {
    // Sample rates are different. Seek to a calculated 'sample rate ratio factored' position.
    
    double srcratio = (double)fsrate / (double)MusEGlobal::sampleRate;
    //long inSize = long((double)frames * _src_ratio) + 1     // From MusE-2 file converter. DELETETHIS ???
    off_t newfr = (off_t)floor(((double)frame * srcratio));    // From simplesynth.
  
    _sfCurFrame = f.seek(newfr, 0);
    
    // Added by Tim. p3.3.17 DELETETHIS 3 or comment it in
    //#ifdef AUDIOCONVERT_DEBUG_PRC
    //printf("AudioConverter::process Seek frame:%ld converted to frame:%ld sfCurFrame:%ld\n", frame, newfr, sfCurFrame);
    //#endif
    
    // Reset the converter. Its current state is meaningless now.
    reset();
  }
  else  
  {
    // No seek requested. 
    // Added by Tim. p3.3.17 DELETETHIS 3 or comment it in
    //#ifdef AUDIOCONVERT_DEBUG_PRC
    //printf("AudioConverter::process No 'transport' seek, rates different. Seeking to sfCurFrame:%ld\n", sfCurFrame);
    //#endif
    
    // Sample rates are different. We can't just tell seek to go to an absolute calculated position, 
    //  since the last position can vary - it might not be what the calculated position is. 
    // We must use the last position left by SRC conversion, ie. let the file position progress on its own.
    _sfCurFrame = f.seek(_sfCurFrame, 0);
  }
  
  /* DELETETHIS 5
  int   fchan              = f.channels();
  long  outFrames          = n;  
  long  outSize            = outFrames * fchan;
  float outbuffer[outSize];
  */
  
  // DELETETHIS 4
  //sfCurFrame = process(f, sfCurFrame, offset, &outbuffer[0], channel, n);
//  sfCurFrame = process(f, sfCurFrame, outbuffer, channel, n);
  //sfCurFrame = process(f, sfCurFrame, buffer, channel, n, overwrite);
  _sfCurFrame = process(f, buffer, channel, n, overwrite);
  
  /* DELETETHIS 58 (whoa!)
  float*  poutbuf = &outbuffer[0];
  if(fchan == channel) 
  {
    if(overwrite)
      //for (size_t i = 0; i < rn; ++i) 
      for (int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) = *poutbuf++;
      }
    else
      //for(size_t i = 0; i < rn; ++i) 
      for(int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) += *poutbuf++;
      }
  }
  else if((fchan == 2) && (channel == 1)) 
  {
    // stereo to mono
    if(overwrite)
      //for(size_t i = 0; i < rn; ++i)
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) = poutbuf[i + i] + poutbuf[i + i + 1];
    else  
      //for(size_t i = 0; i < rn; ++i)
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) += poutbuf[i + i] + poutbuf[i + i + 1];
  }
  else if((fchan == 1) && (channel == 2)) 
  {
    // mono to stereo
    if(overwrite)
      //for(size_t i = 0; i < rn; ++i) 
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) = data;
        *(buffer[1]+i) = data;
      }
    else  
      //for(size_t i = 0; i < rn; ++i) 
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) += data;
        *(buffer[1]+i) += data;
      }
  }
  else 
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("AudioConverter::readAudio Channel mismatch: source chans:%d -> dst chans:%d\n", fchan, channel);
    #endif
  }
  */
  
  return _sfCurFrame;
}

//---------------------------------------------------------
//   SRCAudioConverter
//---------------------------------------------------------

SRCAudioConverter::SRCAudioConverter(int channels, int type) : AudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::SRCAudioConverter this:%p channels:%d type:%d\n", this, channels, type);
  #endif

  _type = type;
  _src_state = 0;
  _channels = channels;
  
  int srcerr;
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::SRCaudioConverter Creating samplerate converter type:%d with %d channels\n", _type, _channels);
  #endif
  _src_state = src_new(_type, _channels, &srcerr); 
  if(!_src_state)      
    printf("SRCAudioConverter::SRCaudioConverter Creation of samplerate converter type:%d with %d channels failed:%s\n", _type, _channels, src_strerror(srcerr));
}

SRCAudioConverter::~SRCAudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::~SRCAudioConverter this:%p\n", this);
  #endif
  if(_src_state)
    src_delete(_src_state);
}

void SRCAudioConverter::setChannels(int ch)
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::setChannels this:%p channels:%d\n", this, ch);
  #endif
  if(_src_state)
    src_delete(_src_state);
  _src_state = 0;
  
  _channels = ch;
  int srcerr;
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::setChannels Creating samplerate converter type:%d with %d channels\n", _type, ch);
  #endif  
  _src_state = src_new(_type, ch, &srcerr);  
  if(!_src_state)      
    printf("SRCAudioConverter::setChannels of samplerate converter type:%d with %d channels failed:%s\n", _type, ch, src_strerror(srcerr));
  return;  
}

void SRCAudioConverter::reset()
{
  if(!_src_state)
    return;
  #ifdef AUDIOCONVERT_DEBUG
  printf("SRCAudioConverter::reset this:%p\n", this);
  #endif
  int srcerr = src_reset(_src_state);
  if(srcerr != 0)      
    printf("SRCAudioConverter::reset Converter reset failed: %s\n", src_strerror(srcerr));
  return;  
}

off_t SRCAudioConverter::process(MusECore::SndFileR& f, float** buffer, int channel, int n, bool overwrite)
{
  //return src_process(_src_state, sd); DELETETHIS
  
  if(f.isNull())
    return _sfCurFrame;
  
  // Added by Tim. p3.3.17 DELETETHIS 4
  //#ifdef AUDIOCONVERT_DEBUG_PRC
  //printf("AudioConverter::process %s audConv:%p sfCurFrame:%ld offset:%u channel:%d fchan:%d n:%d\n", 
  //        f.name().toLatin1(), this, sfCurFrame, offset, channel, f.channels(), n);
  //#endif
  
//  off_t frame     = offset;  // _spos is added before the call. DELETETHIS
  unsigned fsrate = f.samplerate();
  //bool resample   = src_state && ((unsigned)MusEGlobal::sampleRate != fsrate);   DELETETHIS 2
//  bool resample   = isValid() && ((unsigned)MusEGlobal::sampleRate != fsrate);  
  
  if((MusEGlobal::sampleRate == 0) || (fsrate == 0))
  {  
    #ifdef AUDIOCONVERT_DEBUG
    printf("SRCAudioConverter::process Error: MusEGlobal::sampleRate or file samplerate is zero!\n");
    #endif
    return _sfCurFrame;
  }  
  
  SRC_DATA srcdata;
  int fchan       = f.channels();
  // Ratio is defined as output sample rate over input samplerate.
  double srcratio = (double)MusEGlobal::sampleRate / (double)fsrate;
  // Extra input compensation.
  long inComp = 1;
  
  long outFrames  = n;  
  //long outSize   = outFrames * channel; DELETETHIS
  long outSize    = outFrames * fchan;
  
  //long inSize = long(outSize * srcratio) + 1                      // From MusE-2 file converter. DELETETHIS3
  //long inSize = (long)floor(((double)outSize / srcratio));        // From simplesynth.
  //long inFrames = (long)floor(((double)outFrames / srcratio));    // From simplesynth.
  long inFrames = (long)ceil(((double)outFrames / srcratio)) + inComp;    // From simplesynth.
  // DELETETHIS
  //long inFrames = (long)floor(double(outFrames * sfinfo.samplerate) / double(MusEGlobal::sampleRate));    // From simplesynth.
  
  long inSize = inFrames * fchan;
  //long inSize = inFrames * channel; DELETETHIS
  
  // Start with buffers at expected sizes. We won't need anything larger than this, but add 4 for good luck.
  float inbuffer[inSize + 4];
  float outbuffer[outSize];
      
  //size_t sfTotalRead  = 0; DELETETHIS
  size_t rn           = 0;
  long totalOutFrames = 0;
  
  srcdata.data_in       = inbuffer;
  srcdata.data_out      = outbuffer;
//  srcdata.data_out      = buffer; DELETETHIS
  
  // Set some kind of limit on the number of attempts to completely fill the output buffer, 
  //  in case something is really screwed up - we don't want to get stuck in a loop here.
  int attempts = 10;
  for(int attempt = 0; attempt < attempts; ++attempt)
  {
    rn = f.readDirect(inbuffer, inFrames);
    //sfTotalRead += rn; DELETETHIS
    
    // convert
    //srcdata.data_in       = inbuffer; DELETETHIS 4
    //srcdata.data_out      = outbuffer;
    //srcdata.data_out      = poutbuf;
    //srcdata.input_frames  = inSize;
    srcdata.input_frames  = rn;
    srcdata.output_frames = outFrames;
    srcdata.end_of_input  = ((long)rn != inFrames);
    srcdata.src_ratio     = srcratio;
  
    //#ifdef AUDIOCONVERT_DEBUG_PRC DELETETHIS or comment it in, or maybe add an additional if (heavyDebugMsg)?
    //printf("AudioConverter::process attempt:%d inFrames:%ld outFrames:%ld rn:%d data in:%p out:%p", 
    //  attempt, inFrames, outFrames, rn, srcdata.data_in, srcdata.data_out);
    //#endif
    
    int srcerr = src_process(_src_state, &srcdata);
    if(srcerr != 0)      
    {
      printf("\nSRCAudioConverter::process SampleRate converter process failed: %s\n", src_strerror(srcerr));
      return _sfCurFrame += rn;
    }
    
    totalOutFrames += srcdata.output_frames_gen;
    
    //#ifdef AUDIOCONVERT_DEBUG_PRC DELETETHIS or comment in or heavyDebugMsg
    //printf(" frames used in:%ld out:%ld totalOutFrames:%ld data in:%p out:%p\n", srcdata.input_frames_used, srcdata.output_frames_gen, totalOutFrames, srcdata.data_in, srcdata.data_out);
    //#endif
    
    #ifdef AUDIOCONVERT_DEBUG
    if(srcdata.output_frames_gen != outFrames)
      printf("SRCAudioConverter::process %s output_frames_gen:%ld != outFrames:%ld inFrames:%ld srcdata.input_frames_used:%ld rn:%d\n", 
        f.name().toLatin1(), srcdata.output_frames_gen, outFrames, inFrames, srcdata.input_frames_used, rn); 
    #endif
    
    // If the number of frames read by the soundfile equals the input frames, go back.
    // Otherwise we have reached the end of the file, so going back is useless since
    //  there shouldn't be any further calls. 
    if((long)rn == inFrames)
    {
      // Go back by the amount of unused frames.
      sf_count_t seekn = inFrames - srcdata.input_frames_used;
      if(seekn != 0)
      {
        #ifdef AUDIOCONVERT_DEBUG_PRC
        printf("SRCAudioConverter::process Seek-back by:%d\n", seekn);
        #endif
        _sfCurFrame = f.seek(-seekn, SEEK_CUR);
      }
      else  
        _sfCurFrame += rn;
      
      if(totalOutFrames == n)
      {
        // We got our desired number of output frames. Stop attempting.
        break;
      }
      else  
      {
        // No point in continuing if on last attempt.
        if(attempt == (attempts - 1))
          break;
          
        #ifdef AUDIOCONVERT_DEBUG
        printf("SRCAudioConverter::process %s attempt:%d totalOutFrames:%ld != n:%d try again\n", f.name().toLatin1(), attempt, totalOutFrames, n);
        #endif
        
        // SRC didn't give us the number of frames we requested. 
        // This can occasionally be radically different from the requested frames, or zero,
        //  even when ample excess input frames are supplied.
        // Move the src output pointer to a new position.
        srcdata.data_out += srcdata.output_frames_gen * channel;
        // Set new number of maximum out frames.
        outFrames -= srcdata.output_frames_gen;
        // Calculate the new number of file input frames required.
        inFrames = (long)ceil(((double)outFrames / srcratio)) + inComp;
        // Keep trying.
        continue;
      }  
    }
    else
    {
      _sfCurFrame += rn;
      #ifdef AUDIOCONVERT_DEBUG
      printf("SRCAudioConverter::process %s rn:%zd != inFrames:%ld output_frames_gen:%ld outFrames:%ld srcdata.input_frames_used:%ld\n", 
        f.name().toLatin1(), rn, inFrames, srcdata.output_frames_gen, outFrames, srcdata.input_frames_used);
      #endif
      
      // We've reached the end of the file. Convert the number of frames read.
      //rn = (double)rn * srcratio + 1;   DELETETHIS 5
      //rn = (long)floor((double)rn * srcratio); 
      //if(rn > (size_t)outFrames)
      //  rn = outFrames;  
      // Stop attempting.
      break;  
    }
  }
  
  // If we still didn't get the desired number of output frames.
  if(totalOutFrames != n)
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("SRCAudioConverter::process %s totalOutFrames:%ld != n:%d\n", f.name().toLatin1(), totalOutFrames, n);
    #endif
          
    // Let's zero the rest of it.
    long b = totalOutFrames * channel;
    long e = n * channel;
    for(long i = b; i < e; ++i)
      outbuffer[i] = 0.0f;
  }
  
  float*  poutbuf = outbuffer;
  if(fchan == channel) 
  {
    if(overwrite)
      for (int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) = *poutbuf++;
      }
    else
      for(int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) += *poutbuf++;
      }
  }
  else if((fchan == 2) && (channel == 1)) 
  {
    // stereo to mono
    if(overwrite)
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) = poutbuf[i + i] + poutbuf[i + i + 1];
    else  
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) += poutbuf[i + i] + poutbuf[i + i + 1];
  }
  else if((fchan == 1) && (channel == 2)) 
  {
    // mono to stereo
    if(overwrite)
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) = data;
        *(buffer[1]+i) = data;
      }
    else  
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) += data;
        *(buffer[1]+i) += data;
      }
  }
  else 
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("SRCAudioConverter::process Channel mismatch: source chans:%d -> dst chans:%d\n", fchan, channel);
    #endif
  }
  
  return _sfCurFrame;
}

#ifdef RUBBERBAND_SUPPORT

//---------------------------------------------------------
//   RubberBandAudioConverter
//---------------------------------------------------------

RubberBandAudioConverter::RubberBandAudioConverter(int channels, int options) : AudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("RubberBandAudioConverter::RubberBandAudioConverter this:%p channels:%d options:%x\n", this, channels, options);
  #endif

  _options = options;
  _rbs = 0;
  _channels = channels;
  
  _rbs = new RubberBandStretcher(MusEGlobal::sampleRate, _channels, _options);  // , initialTimeRatio = 1.0, initialPitchScale = 1.0 DELETETHIS
}

RubberBandAudioConverter::~RubberBandAudioConverter()
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("RubberBandAudioConverter::~RubberBandAudioConverter this:%p\n", this);
  #endif
  if(_rbs)
    delete _rbs;
}

void RubberBandAudioConverter::setChannels(int ch)
{
  #ifdef AUDIOCONVERT_DEBUG
  printf("RubberBandAudioConverter::setChannels this:%p channels:%d\n", this, ch);
  #endif
  if(_rbs)
    delete _rbs;
  _rbs = 0;
  
  _channels = ch;
  _rbs = new RubberBandStretcher(MusEGlobal::sampleRate, _channels, _options);  // , initialTimeRatio = 1.0, initialPitchScale = 1.0
}

void RubberBandAudioConverter::reset()
{
  if(!_rbs)
    return;
  #ifdef AUDIOCONVERT_DEBUG
  printf("RubberBandAudioConverter::reset this:%p\n", this);
  #endif
  _rbs->reset();
  return;  
}

/////////////////////////////////
// TODO: Not finished yet..
////////////////////////////////
// DELETETHIS well then... but maybe we can clean it up anyway? remove the below for example ;)?
off_t RubberBandAudioConverter::process(MusECore::SndFileR& f, float** buffer, int channel, int n, bool overwrite)
{
  //return src_process(_src_state, sd); DELETETHIS
  
  if(f.isNull())
    return _sfCurFrame;
  
  // Added by Tim. p3.3.17 DELETETHIS 4
  //#ifdef AUDIOCONVERT_DEBUG_PRC
  //printf("AudioConverter::process %s audConv:%p sfCurFrame:%ld offset:%u channel:%d fchan:%d n:%d\n", 
  //        f.name().toLatin1(), this, sfCurFrame, offset, channel, f.channels(), n);
  //#endif
  
//  off_t frame     = offset;  // _spos is added before the call. DELETETHIS
  unsigned fsrate = f.samplerate();
  //bool resample   = src_state && ((unsigned)MusEGlobal::sampleRate != fsrate);   DELETETHIS 2
//  bool resample   = isValid() && ((unsigned)MusEGlobal::sampleRate != fsrate);  
  
  if((MusEGlobal::sampleRate == 0) || (fsrate == 0))
  {  
    #ifdef AUDIOCONVERT_DEBUG
    printf("RubberBandAudioConverter::process Error: MusEGlobal::sampleRate or file samplerate is zero!\n");
    #endif
    return _sfCurFrame;
  }  
  
//  SRC_DATA srcdata;
  int fchan       = f.channels();
  // Ratio is defined as output sample rate over input samplerate.
  double srcratio = (double)MusEGlobal::sampleRate / (double)fsrate;
  // Extra input compensation.
  long inComp = 1;
  
  long outFrames  = n;  
  //long outSize   = outFrames * channel; DELETETHIS
  long outSize    = outFrames * fchan;
  
  //long inSize = long(outSize * srcratio) + 1                      // From MusE-2 file converter.  DELETETHIS 3
  //long inSize = (long)floor(((double)outSize / srcratio));        // From simplesynth.
  //long inFrames = (long)floor(((double)outFrames / srcratio));    // From simplesynth.
  long inFrames = (long)ceil(((double)outFrames / srcratio)) + inComp;    // From simplesynth.
  // DELETETHIS
  //long inFrames = (long)floor(double(outFrames * sfinfo.samplerate) / double(MusEGlobal::sampleRate));    // From simplesynth.
  
  long inSize = inFrames * fchan;
  //long inSize = inFrames * channel; DELETETHIS
  
  // Start with buffers at expected sizes. We won't need anything larger than this, but add 4 for good luck.
  float inbuffer[inSize]; // +4
//  float outbuffer[outSize]; DELETETHIS
      
  //float* rbinbuffer[fchan]; DELETETHIS 4
  //float rbindata[inSize];
  //for (int i = 0; i < fchan; ++i)
  //      rbinbuffer[i] = rbindata + i * inFrames;
      
  float* rboutbuffer[fchan];
  float rboutdata[outSize];
  for (int i = 0; i < fchan; ++i)
        rboutbuffer[i] = rboutdata + i * outFrames;
      
  //size_t sfTotalRead  = 0; DELETETHIS
  size_t rn           = 0;
  long totalOutFrames = 0;
  
//  srcdata.data_in       = inbuffer; DELETETHIS 3
  //srcdata.data_out      = outbuffer;
//  srcdata.data_out      = buffer;
  float** data_out       = rboutbuffer;
  
  // For just sample rate conversion, apply same ratio to both time and pitch.
  _rbs->setTimeRatio(srcratio);
  _rbs->setPitchScale(srcratio);
  
  // Set some kind of limit on the number of attempts to completely fill the output buffer, 
  //  in case something is really screwed up - we don't want to get stuck in a loop here.
  int attempts = 10;
  for(int attempt = 0; attempt < attempts; ++attempt)
  {
    size_t sreq = _rbs->getSamplesRequired();
    
    size_t rbinSize = sreq * fchan;
    float* rbinbuffer[fchan];
    float rbindata[rbinSize];
    for(int i = 0; i < fchan; ++i)
      rbinbuffer[i] = rbindata + i * sreq;
    
//    rn = f.readDirect(inbuffer, inFrames); DELETETHIS
    rn = f.readDirect(inbuffer, sreq);
    //sfTotalRead += rn; DELETETHIS
    
    // Must de-interleave soundfile data to feed to rubberband.
    for(size_t i = 0; i < rn; ++i) 
    {
      for(int ch = 0; ch < fchan; ++ch)
        *(rbinbuffer[ch] + i) = *inbuffer++;
    }
           
    _rbs->process(rbinbuffer, rn, (long)rn != inFrames);
    
    // "This function returns -1 if all data has been fully processed and all output read, and the stretch process is now finished." 
    int savail = _rbs->available(); 
    
    
    // convert
    //srcdata.data_in       = inbuffer; DELETETHIS 4
    //srcdata.data_out      = outbuffer;
    //srcdata.data_out      = poutbuf;
    //srcdata.input_frames  = inSize;
    srcdata.input_frames  = rn;
    srcdata.output_frames = outFrames;
    srcdata.end_of_input  = ((long)rn != inFrames);
    srcdata.src_ratio     = srcratio;
  
    //#ifdef AUDIOCONVERT_DEBUG_PRC DELETETHIS or comment in
    //printf("AudioConverter::process attempt:%d inFrames:%ld outFrames:%ld rn:%d data in:%p out:%p", 
    //  attempt, inFrames, outFrames, rn, srcdata.data_in, srcdata.data_out);
    //#endif
    
    int srcerr = src_process(_src_state, &srcdata);
    if(srcerr != 0)      
    {
      printf("\RubberBandAudioConverter::process SampleRate converter process failed: %s\n", src_strerror(srcerr));
      return _sfCurFrame += rn;
    }
    
    totalOutFrames += srcdata.output_frames_gen;
    
    //#ifdef AUDIOCONVERT_DEBUG_PRC DELETETHIS or comment in
    //printf(" frames used in:%ld out:%ld totalOutFrames:%ld data in:%p out:%p\n", srcdata.input_frames_used, srcdata.output_frames_gen, totalOutFrames, srcdata.data_in, srcdata.data_out);
    //#endif
    
    #ifdef AUDIOCONVERT_DEBUG
    if(srcdata.output_frames_gen != outFrames)
      printf("RubberBandAudioConverter::process %s output_frames_gen:%ld != outFrames:%ld inFrames:%ld srcdata.input_frames_used:%ld rn:%d\n", 
        f.name().toLatin1(), srcdata.output_frames_gen, outFrames, inFrames, srcdata.input_frames_used, rn); 
    #endif
    
    // If the number of frames read by the soundfile equals the input frames, go back.
    // Otherwise we have reached the end of the file, so going back is useless since
    //  there shouldn't be any further calls. 
    if((long)rn == inFrames)
    {
      // Go back by the amount of unused frames.
      sf_count_t seekn = inFrames - srcdata.input_frames_used;
      if(seekn != 0)
      {
        #ifdef AUDIOCONVERT_DEBUG_PRC
        printf("RubberBandAudioConverter::process Seek-back by:%d\n", seekn);
        #endif
        _sfCurFrame = f.seek(-seekn, SEEK_CUR);
      }
      else  
        _sfCurFrame += rn;
      
      if(totalOutFrames == n)
      {
        // We got our desired number of output frames. Stop attempting.
        break;
      }
      else  
      {
        // No point in continuing if on last attempt.
        if(attempt == (attempts - 1))
          break;
          
        #ifdef AUDIOCONVERT_DEBUG
        printf("RubberBandAudioConverter::process %s attempt:%d totalOutFrames:%ld != n:%d try again\n", f.name().toLatin1(), attempt, totalOutFrames, n);
        #endif
        
        // We didn't get the number of frames we requested. 
        // This can occasionally be radically different from the requested frames, or zero,
        //  even when ample excess input frames are supplied.
        // Move the src output pointer to a new position.
        srcdata.data_out += srcdata.output_frames_gen * channel;
        // Set new number of maximum out frames.
        outFrames -= srcdata.output_frames_gen;
        // Calculate the new number of file input frames required.
        inFrames = (long)ceil(((double)outFrames / srcratio)) + inComp;
        // Keep trying.
        continue;
      }  
    }
    else
    {
      _sfCurFrame += rn;
      #ifdef AUDIOCONVERT_DEBUG
      printf("RubberBandAudioConverter::process %s rn:%zd != inFrames:%ld output_frames_gen:%ld outFrames:%ld srcdata.input_frames_used:%ld\n", 
        f.name().toLatin1(), rn, inFrames, srcdata.output_frames_gen, outFrames, srcdata.input_frames_used);
      #endif
      
      // We've reached the end of the file. Convert the number of frames read.
      //rn = (double)rn * srcratio + 1;  DELETETHIS 5
      //rn = (long)floor((double)rn * srcratio); 
      //if(rn > (size_t)outFrames)
      //  rn = outFrames;  
      // Stop attempting.
      break;  
    }
  }
  
  // If we still didn't get the desired number of output frames.
  if(totalOutFrames != n)
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("RubberBandAudioConverter::process %s totalOutFrames:%ld != n:%d\n", f.name().toLatin1(), totalOutFrames, n);
    #endif
          
    // Let's zero the rest of it.
    long b = totalOutFrames * channel;
    long e = n * channel;
    for(long i = b; i < e; ++i)
      buffer[i] = 0.0f;
  }
  
  float*  poutbuf = outbuffer;
  if(fchan == channel) 
  {
    if(overwrite)
      for (int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) = *poutbuf++;
      }
    else
      for(int i = 0; i < n; ++i) 
      {
        for(int ch = 0; ch < channel; ++ch)
          *(buffer[ch] + i) += *poutbuf++;
      }
  }
  else if((fchan == 2) && (channel == 1)) 
  {
    // stereo to mono
    if(overwrite)
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) = poutbuf[i + i] + poutbuf[i + i + 1];
    else  
      for(int i = 0; i < n; ++i)
        *(buffer[0] + i) += poutbuf[i + i] + poutbuf[i + i + 1];
  }
  else if((fchan == 1) && (channel == 2)) 
  {
    // mono to stereo
    if(overwrite)
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) = data;
        *(buffer[1]+i) = data;
      }
    else  
      for(int i = 0; i < n; ++i) 
      {
        float data = *poutbuf++;
        *(buffer[0]+i) += data;
        *(buffer[1]+i) += data;
      }
  }
  else 
  {
    #ifdef AUDIOCONVERT_DEBUG
    printf("RubberBandAudioConverter::process Channel mismatch: source chans:%d -> dst chans:%d\n", fchan, channel);
    #endif
  }
  
  return _sfCurFrame;
}

#endif // RUBBERBAND_SUPPORT

} // namespace MusECore