[c3f4173] | 1 | /** Test for phase vocoder in jack |
---|
| 2 | * |
---|
| 3 | * This program should start correctly, when jackd is started or when |
---|
| 4 | * using JACK_START_SERVER=true and reconstruct each audio input channel |
---|
| 5 | * on the corresponding output channel with some strange effects and a |
---|
| 6 | * delay equal to the hop size (hop_s). |
---|
[437fa65] | 7 | * |
---|
| 8 | */ |
---|
| 9 | |
---|
[7b485af] | 10 | #include <stdio.h> |
---|
[c3f4173] | 11 | #include <unistd.h> /* sleep() */ |
---|
[7e20db8b] | 12 | #include <aubio.h> |
---|
[9d6001cb] | 13 | #ifdef HAVE_JACK |
---|
[740f06b] | 14 | #include "jackio.h" |
---|
[9d6001cb] | 15 | #endif /* HAVE_JACK */ |
---|
[437fa65] | 16 | |
---|
[6938a20] | 17 | uint_t testing = 0; // change this to 1 to listen |
---|
[c3f4173] | 18 | |
---|
[6938a20] | 19 | uint_t win_s = 512; // window size |
---|
| 20 | uint_t hop_s = 128; // hop size |
---|
| 21 | uint_t channels = 2; // number of audio channels |
---|
| 22 | uint_t midiin = 4; // number of midi input channels |
---|
| 23 | uint_t midiout = 2; // number of midi output channels |
---|
| 24 | uint_t pos = 0; // frames%dspblocksize for jack loop |
---|
[437fa65] | 25 | |
---|
[aea235c] | 26 | fvec_t * in[2]; |
---|
| 27 | cvec_t * fftgrain[2]; |
---|
| 28 | fvec_t * out[2]; |
---|
[437fa65] | 29 | |
---|
[aea235c] | 30 | aubio_pvoc_t * pv[2]; |
---|
[437fa65] | 31 | |
---|
| 32 | int aubio_process(float **input, float **output, int nframes); |
---|
| 33 | |
---|
[6938a20] | 34 | int main () |
---|
| 35 | { |
---|
| 36 | /* allocate some memory */ |
---|
[aea235c] | 37 | uint_t i; |
---|
[6938a20] | 38 | for (i=0;i<channels;i++) { |
---|
| 39 | in[i] = new_fvec (hop_s); /* input buffer */ |
---|
| 40 | fftgrain[i] = new_cvec (win_s); /* fft norm and phase */ |
---|
| 41 | out[i] = new_fvec (hop_s); /* output buffer */ |
---|
| 42 | /* allocate fft and other memory space */ |
---|
| 43 | pv[i] = new_aubio_pvoc(win_s,hop_s); |
---|
| 44 | } |
---|
[437fa65] | 45 | |
---|
[b511fa9] | 46 | #ifdef HAVE_JACK |
---|
[6938a20] | 47 | aubio_jack_t * jack_setup; |
---|
| 48 | jack_setup = new_aubio_jack(channels, channels, |
---|
| 49 | midiin, midiout, |
---|
| 50 | (aubio_process_func_t)aubio_process); |
---|
| 51 | aubio_jack_activate(jack_setup); |
---|
| 52 | /* stay in main jack loop for 1 seconds only */ |
---|
| 53 | do { |
---|
| 54 | sleep(1); |
---|
| 55 | } while(testing); |
---|
| 56 | aubio_jack_close(jack_setup); |
---|
[7b485af] | 57 | #else |
---|
[6938a20] | 58 | fprintf(stderr, "WARNING: no jack support\n"); |
---|
[8659f2c] | 59 | #endif |
---|
[6938a20] | 60 | |
---|
| 61 | for (i=0;i<channels;i++) { |
---|
| 62 | del_aubio_pvoc(pv[i]); |
---|
| 63 | del_cvec(fftgrain[i]); |
---|
| 64 | del_fvec(in[i]); |
---|
| 65 | del_fvec(out[i]); |
---|
| 66 | } |
---|
| 67 | aubio_cleanup(); |
---|
| 68 | return 0; |
---|
[437fa65] | 69 | } |
---|
| 70 | |
---|
| 71 | int aubio_process(float **input, float **output, int nframes) { |
---|
| 72 | uint_t i; /*channels*/ |
---|
| 73 | uint_t j; /*frames*/ |
---|
[92eea84] | 74 | for (j=0;j<(unsigned)nframes;j++) { |
---|
[437fa65] | 75 | for (i=0;i<channels;i++) { |
---|
| 76 | /* write input to datanew */ |
---|
[aea235c] | 77 | fvec_write_sample(in[i], input[i][j], pos); |
---|
[437fa65] | 78 | /* put synthnew in output */ |
---|
[aea235c] | 79 | output[i][j] = fvec_read_sample(out[i], pos); |
---|
[437fa65] | 80 | } |
---|
| 81 | /*time for fft*/ |
---|
| 82 | if (pos == hop_s-1) { |
---|
| 83 | /* block loop */ |
---|
[6938a20] | 84 | for (i=0;i<channels;i++) { |
---|
| 85 | aubio_pvoc_do (pv[i], in[i], fftgrain[i]); |
---|
| 86 | // zero phases of first channel |
---|
| 87 | for (i=0;i<fftgrain[i]->length;i++) fftgrain[0]->phas[i] = 0.; |
---|
| 88 | // double phases of second channel |
---|
| 89 | for (i=0;i<fftgrain[i]->length;i++) { |
---|
| 90 | fftgrain[1]->phas[i] = |
---|
| 91 | aubio_unwrap2pi (fftgrain[1]->phas[i] * 2.); |
---|
| 92 | } |
---|
| 93 | // copy second channel to third one |
---|
| 94 | aubio_pvoc_rdo(pv[i], fftgrain[i], out[i]); |
---|
| 95 | pos = -1; |
---|
[c3f4173] | 96 | } |
---|
[aea235c] | 97 | } |
---|
[437fa65] | 98 | pos++; |
---|
| 99 | } |
---|
| 100 | return 0; |
---|
| 101 | } |
---|