tests/: move file around to match src/
[aubio.git] / tests / src / spectral / test-phasevoc-jack.c
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).
7  *
8  */
9
10 #include <stdio.h>
11 #include <unistd.h>  /* sleep() */
12 #include <aubio.h>
13 #include "jackio.h"
14
15 uint_t testing  = 0;  /* change this to 1 to listen        */
16
17 uint_t win_s    = 512;/* window size                       */
18 uint_t hop_s    = 128;/* hop size                          */
19 uint_t channels = 2;  /* number of audio channels          */
20 uint_t midiin   = 4;  /* number of midi input channels     */
21 uint_t midiout  = 2;  /* number of midi output channels    */
22 uint_t pos      = 0;  /* frames%dspblocksize for jack loop */
23
24 fvec_t * in[2];
25 cvec_t * fftgrain[2];
26 fvec_t * out[2];
27
28 aubio_pvoc_t * pv[2];
29
30 int aubio_process(float **input, float **output, int nframes);
31
32 int main(){
33         /* allocate some memory */
34   uint_t i;
35     for (i=0;i<channels;i++) {
36         in[i]       = new_fvec (hop_s); /* input buffer       */
37         fftgrain[i] = new_cvec (win_s); /* fft norm and phase */
38         out[i]      = new_fvec (hop_s); /* output buffer      */
39         /* allocate fft and other memory space */
40         pv[i] = new_aubio_pvoc(win_s,hop_s);
41     }
42
43 #ifdef HAVE_JACK
44         aubio_jack_t * jack_setup;
45         jack_setup  = new_aubio_jack(channels, channels, 
46             midiin, midiout,
47             (aubio_process_func_t)aubio_process);
48         aubio_jack_activate(jack_setup);
49         /* stay in main jack loop for 1 seconds only */
50         do {
51           sleep(1);
52         } while(testing);
53         aubio_jack_close(jack_setup);
54 #else
55         fprintf(stderr, "WARNING: no jack support\n");
56 #endif
57         
58     for (i=0;i<channels;i++) {
59         del_aubio_pvoc(pv[i]);
60         del_cvec(fftgrain[i]);
61         del_fvec(in[i]);
62         del_fvec(out[i]);
63     }
64         aubio_cleanup();
65         return 0;
66 }
67
68 int aubio_process(float **input, float **output, int nframes) {
69   uint_t i;       /*channels*/
70   uint_t j;       /*frames*/
71   for (j=0;j<(unsigned)nframes;j++) {
72     for (i=0;i<channels;i++) {
73       /* write input to datanew */
74       fvec_write_sample(in[i], input[i][j], pos);
75       /* put synthnew in output */
76       output[i][j] = fvec_read_sample(out[i], pos);
77     }
78     /*time for fft*/
79     if (pos == hop_s-1) {
80       /* block loop */
81     for (i=0;i<channels;i++) {
82       aubio_pvoc_do (pv[i], in[i], fftgrain[i]);
83       // zero phases of first channel
84       for (i=0;i<fftgrain[i]->length;i++) fftgrain[0]->phas[i] = 0.; 
85       // double phases of second channel
86       for (i=0;i<fftgrain[i]->length;i++) {
87         fftgrain[1]->phas[i] = 
88           aubio_unwrap2pi (fftgrain[1]->phas[i] * 2.); 
89       }
90       // copy second channel to third one
91       aubio_pvoc_rdo(pv[i], fftgrain[i], out[i]);
92       pos = -1;
93     }
94     }
95     pos++;
96   }
97   return 0;
98 }