source: tests/src/effects/test-timestretch.c @ 9279ba0

feature/cnnfeature/crepefeature/timestretchfix/ffmpeg5
Last change on this file since 9279ba0 was 9279ba0, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[tests] use fabs in test-timestretch to avoid double truncation

  • Property mode set to 100644
File size: 5.1 KB
Line 
1#define AUBIO_UNSTABLE 1
2#include <aubio.h>
3#include "utils_tests.h"
4
5int test_wrong_params(void);
6
7int main (int argc, char **argv)
8{
9  sint_t err = 0;
10
11  if (argc < 3 || argc >= 9) {
12    PRINT_ERR("wrong number of arguments, running tests\n");
13    err = test_wrong_params();
14    PRINT_MSG("usage: %s <input_path> <output_path> <stretch> [transpose] [mode] [hop_size] [samplerate]\n", argv[0]);
15    PRINT_MSG(" with <stretch> a time stretching ratio in the range [0.025, 10.]\n");
16    PRINT_MSG("      [transpose] a number of semi tones in the range [-24, 24]\n");
17    PRINT_MSG("  and [mode] in 'default', 'crispness:0', ..., 'crispness:6'\n");
18    return err;
19  }
20
21#ifdef HAVE_RUBBERBAND
22  uint_t samplerate = 0; // using source samplerate
23  uint_t hop_size = 64;
24  smpl_t transpose = 0.;
25  smpl_t stretch = 1.;
26  uint_t n_frames = 0, read = 0;
27  uint_t eof = 0, source_read = 0;
28
29  char_t *source_path = argv[1];
30  char_t *sink_path = argv[2];
31  char_t *mode = "default";
32
33  if ( argc >= 4 ) stretch = atof(argv[3]);
34  if ( argc >= 5 ) transpose = atof(argv[4]);
35  if ( argc >= 6 ) mode = argv[5];
36  if ( argc >= 7 ) hop_size = atoi(argv[6]);
37  if ( argc >= 8 ) samplerate = atoi(argv[7]);
38
39  uint_t source_hopsize = 2048;
40  aubio_source_t *s = new_aubio_source(source_path, samplerate, source_hopsize);
41  if (!s) { err = 1; goto beach_source; }
42  if (samplerate == 0) samplerate = aubio_source_get_samplerate(s);
43
44  fvec_t *in = new_fvec(source_hopsize);
45  fvec_t *out = new_fvec(hop_size);
46  if (!out || !in) { err = 1; goto beach_fvec; }
47
48  aubio_timestretch_t *ps = new_aubio_timestretch(mode, stretch, hop_size,
49      samplerate);
50  if (!ps) { err = 1; goto beach_timestretch; }
51  //if (samplerate == 0 ) samplerate = aubio_timestretch_get_samplerate(ps);
52
53  aubio_sink_t *o = new_aubio_sink(sink_path, samplerate);
54  if (!o) { err = 1; goto beach_sink; }
55
56  if (transpose != 0) aubio_timestretch_set_transpose(ps, transpose);
57
58  do {
59    //aubio_timestretch_set_stretch(ps, stretch);
60    //aubio_timestretch_set_transpose(ps, transpose);
61
62    while (aubio_timestretch_get_available(ps) < (sint_t)hop_size && !eof) {
63      aubio_source_do(s, in, &source_read);
64      aubio_timestretch_push(ps, in, source_read);
65      if (source_read < in->length) eof = 1;
66    }
67#if 0
68    if (n_frames == hop_size * 200) {
69      PRINT_MSG("sampler: setting stretch gave %d\n",
70          aubio_timestretch_set_stretch(ps, 2.) );
71      PRINT_MSG("sampler: getting stretch gave %f\n",
72          aubio_timestretch_get_stretch(ps) );
73      PRINT_MSG("sampler: setting transpose gave %d\n",
74          aubio_timestretch_set_transpose(ps, 12.) );
75      PRINT_MSG("sampler: getting transpose gave %f\n",
76          aubio_timestretch_get_transpose(ps) );
77    }
78#endif
79    aubio_timestretch_do(ps, out, &read);
80    aubio_sink_do(o, out, read);
81    n_frames += read;
82  } while ( read == hop_size );
83
84  PRINT_MSG("wrote %d frames at %dHz (%d blocks) from %s written to %s\n",
85      n_frames, samplerate, n_frames / hop_size,
86      source_path, sink_path);
87
88  del_aubio_sink(o);
89beach_sink:
90  del_aubio_timestretch(ps);
91beach_timestretch:
92  del_fvec(out);
93  del_fvec(in);
94beach_fvec:
95  del_aubio_source(s);
96beach_source:
97#else
98  err = 0;
99  PRINT_ERR("aubio was not compiled with rubberband\n");
100#endif
101  return err;
102}
103
104int test_wrong_params(void)
105{
106  const char_t *mode = "default";
107  smpl_t stretch = 1.;
108  uint_t hop_size = 256;
109  uint_t samplerate = 44100;
110
111  if (new_aubio_timestretch("ProcessOffline:?:", stretch, hop_size, samplerate)) return 1;
112  if (new_aubio_timestretch("", stretch, hop_size, samplerate)) return 1;
113  if (new_aubio_timestretch(mode,     41., hop_size, samplerate)) return 1;
114  if (new_aubio_timestretch(mode, stretch,        0, samplerate)) return 1;
115  if (new_aubio_timestretch(mode, stretch, hop_size,          0)) return 1;
116
117  aubio_timestretch_t *p = new_aubio_timestretch(mode, stretch, hop_size,
118      samplerate);
119#ifdef HAVE_RUBBERBAND
120  if (!p) return 1;
121
122  if (aubio_timestretch_get_latency(p) == 0) return 1;
123
124  if (aubio_timestretch_get_samplerate(p) != samplerate) return 1;
125
126  aubio_timestretch_reset(p);
127
128  if (aubio_timestretch_get_transpose(p) != 0) return 1;
129  if (aubio_timestretch_set_transpose(p, 2.)) return 1;
130  if (fabs(aubio_timestretch_get_transpose(p) - 2.) >= 1e-6) return 1;
131  if (!aubio_timestretch_set_transpose(p, 200.)) return 1;
132  if (!aubio_timestretch_set_transpose(p, -200.)) return 1;
133  if (aubio_timestretch_set_transpose(p, 0.)) return 1;
134
135  if (aubio_timestretch_get_pitchscale(p) != 1) return 1;
136  if (aubio_timestretch_set_pitchscale(p, 2.)) return 1;
137  if (fabs(aubio_timestretch_get_pitchscale(p) - 2.) >= 1e-6) return 1;
138  if (!aubio_timestretch_set_pitchscale(p, 0.)) return 1;
139  if (!aubio_timestretch_set_pitchscale(p, 6.)) return 1;
140
141  if (aubio_timestretch_get_stretch(p) != stretch) return 1;
142  if (aubio_timestretch_set_stretch(p, 2.)) return 1;
143  if (fabs(aubio_timestretch_get_stretch(p) - 2.) >= 1e-6) return 1;
144  if (!aubio_timestretch_set_stretch(p, 0.)) return 1;
145  if (!aubio_timestretch_set_stretch(p, 41.)) return 1;
146
147  del_aubio_timestretch(p);
148#else
149  if (p) return 1;
150#endif
151
152  return run_on_default_source_and_sink(main);
153}
Note: See TracBrowser for help on using the repository browser.