xiRetimer

fit recorded audio to a tempo
git clone https://git.ce9e.org/xiRetimer.git

commit
fd2e0480852bc8a66b6ec52fe8ca56d3215a1b0b
parent
68c8aa6ae4e544e81c2d53160a382f5731571b26
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2010-10-31 00:03
file in out

Diffstat

M rb_expermients/fio.h 73 +++++++++++++++++++++++++++++++++++++++++++++++++------------
M rb_expermients/rtlayer.h 14 +++++++-------

2 files changed, 66 insertions, 21 deletions


diff --git a/rb_expermients/fio.h b/rb_expermients/fio.h

@@ -4,18 +4,21 @@
    4     4 #include <sndfile.h>
    5     5 #include <iostream>
    6     6 #include "../src/marker.h"
   -1     7 #include <rubberband/RubberBandStretcher.h>
    7     8 
    8     9 using namespace std;
    9    10 
   10    11 class fio{
   11    -1 
   -1    12   
   12    13 };
   13    14 
   14    15 int main() {
   15    16   // create marker
   16    17   Marker marker;
   17    18   marker.add(0,0);
   18    -1   marker.add(1,1);
   -1    19   marker.add(0.05,0.1);
   -1    20   marker.add(0.5,0.4);
   -1    21   marker.add(1,0.9);
   19    22 
   20    23   // load files
   21    24 
@@ -34,6 +37,8 @@ int main() {
   34    37     return 1;
   35    38   }
   36    39 
   -1    40   int channels= sfinfo.channels;
   -1    41 
   37    42 //  int outFrames=int(marker.getRatio()*sfinfo.frames);
   38    43   sfinfoOut.channels = sfinfo.channels;
   39    44   sfinfoOut.format = sfinfo.format;
@@ -48,30 +53,70 @@ int main() {
   48    53     << sf_strerror(sndfileOut) << endl;
   49    54     return 1;
   50    55   }
   51    -1 
   52    -1   const int bufferLength=441000;
   -1    56   const int bufferLength=100000;  // important
   53    57   float ptr[bufferLength];
   -1    58   float **ibuf = new float *[channels];
   -1    59   for (size_t i = 0; i < channels; ++i) ibuf[i] = new float[bufferLength/channels];
   54    60 
   55    -1   // cut
   -1    61   // *************************************** 
   -1    62   //  The reading is seperated in sections between markers and in buffer to save ram
   -1    63   //
   56    64   for (int i=0; i<marker.length()-1; ++i) {
   57    -1     int frames=int((marker.getOld(i+1)-marker.getOld(i))*sfinfo.frames*sfinfo.channels); // number of frames to read
   58    -1     int count = 0;
   59    -1     int count2 = 0;
   60    -1     int frames2=-1;
   -1    65     unsigned long frames=int((marker.getOld(i+1)-marker.getOld(i))*sfinfo.frames*channels); // number of frames to read
   -1    66     unsigned int count = 0; // # of read frames in this loop
   -1    67     unsigned long count2 = 0; // # of read frames alltogether
   -1    68     unsigned int frames2=0; // # of frames to read in this loop
   -1    69     float ratio=marker.getRatio(i);
   61    70     while (count2<frames) {
   62    71       if (frames-count2 < bufferLength)
   63    72         frames2=frames-count2;
   64    73       else
   65    74         frames2=bufferLength;
   66    -1       count = sf_read_float(sndfile, ptr, frames2);
   -1    75       count = sf_readf_float(sndfile, ptr, frames2/channels);
   67    76       count2 += count;
   68    -1       // time stretch here
   69    -1       // sf_write_float(sndfileOut, ptr, int(count*ratio));
   70    -1       sf_write_float(sndfileOut, ptr, count*1);
   -1    77       if (count <=0) break;
   -1    78 
   -1    79       // float[] to **float
   -1    80       for (int c=0; c<channels; ++c) for (int j=0; j<bufferLength/channels; ++j) ibuf[c][j]=ptr[j*channels+c];
   -1    81 
   -1    82       float **ibuf2 = new float *[channels];
   -1    83       for (size_t j = 0; j < channels; ++j) ibuf2[j] = new float[int(bufferLength/channels*ratio)];
   -1    84 
   -1    85       RubberBand::RubberBandStretcher ts(sfinfo.samplerate, channels, 0, ratio);
   -1    86       int avail=int(bufferLength/channels*ratio);
   -1    87       ts.setMaxProcessSize(count*10);
   -1    88       ts.study(ibuf, count, true);
   -1    89       int a1=-1;
   -1    90       int a2=0;
   -1    91       while (a1!=a2) {
   -1    92         ts.process(ibuf, ts.getSamplesRequired(), false);
   -1    93         a1=a2;
   -1    94         a2=ts.available();
   -1    95       }
   -1    96       ts.process(ibuf, count, true); // hope two times is enough
   -1    97       ts.retrieve(ibuf2, avail);
   -1    98 
   -1    99       // **float to float[] and cutting
   -1   100       float ptr2[avail*channels];
   -1   101       for (size_t c = 0; c < channels; ++c) {
   -1   102         for (int j = 0; j < avail; ++j) {
   -1   103           float value = ibuf2[c][j];
   -1   104           if (value > 1.f) value = 1.f;
   -1   105           if (value < -1.f) value = -1.f;
   -1   106           ptr2[j*channels+c] = value;
   -1   107         }
   -1   108       }
   -1   109       for (int j=0; j<channels; ++j) delete[] ibuf2[j];
   -1   110       delete[] ibuf2;
   -1   111 
   -1   112       // write to file
   -1   113       sf_write_float(sndfileOut, ptr2, avail*channels);
   71   114     }
   72    -1 
   73   115   }
   74   116 
   -1   117   for (int i=0; i<channels; ++i) delete[] ibuf[i];
   -1   118   delete[] ibuf;
   -1   119 
   75   120   sf_close(sndfile);
   76   121   sf_close(sndfileOut);
   77   122 

diff --git a/rb_expermients/rtlayer.h b/rb_expermients/rtlayer.h

@@ -19,7 +19,7 @@ int main() {
   19    19   RTlayer layer;
   20    20 
   21    21   int samples=10000000;
   22    -1   int sample_rate=44100;
   -1    22   int sample_rate=1024;
   23    23   float* raw=new float[samples];
   24    24 
   25    25   layer.marker.add(0,0);
@@ -27,12 +27,12 @@ int main() {
   27    27   layer.marker.add(1,1);
   28    28 
   29    29   for (int i=0; i<layer.marker.length()-1; i++) {
   30    -1 //    RubberBand::RubberBandStretcher ts(44100, 1);
   31    -1 //    ts.study(raw, raw.length, true);
   32    -1 //    ts.process(raw, raw.length, true);
   33    -1 //    int avail=ts.available();
   34    -1 //    float[] tmpRaw=new tmpRaw[avail];
   35    -1 //    addRaw(ts.recieve(tmpRaw,avail));
   -1    30     RubberBand::RubberBandStretcher ts(44100, 1, DefaultOptions);
   -1    31     ts.study(raw, raw.length, true);
   -1    32     ts.process(raw, raw.length, true);
   -1    33     int avail=ts.available();
   -1    34     float[] tmpRaw=new tmpRaw[avail];
   -1    35     addRaw(ts.recieve(tmpRaw,avail));
   36    36   }
   37    37 
   38    38 }