source: tests/src/tempo/test-tempo.c @ 7dea72f

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 7dea72f was fd1d4d5, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] improve test-tempo coverage

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include <aubio.h>
2#include "utils_tests.h"
3
4int test_wrong_params(void);
5
6int main (int argc, char **argv)
7{
8  uint_t err = 0;
9  if (argc < 2) {
10    PRINT_WRN("no arguments, running tests\n");
11    err = test_wrong_params();
12    PRINT_MSG("usage: %s <source_path> [samplerate] [win_size] [hop_size]\n",
13        argv[0]);
14    return err;
15  }
16  uint_t samplerate = 0;
17  if ( argc >= 3 ) samplerate = atoi(argv[2]);
18  uint_t win_size = 1024; // window size
19  if ( argc >= 4 ) win_size = atoi(argv[3]);
20  uint_t hop_size = win_size / 4;
21  if ( argc >= 5 ) hop_size = atoi(argv[4]);
22  uint_t n_frames = 0, read = 0;
23
24  char_t *source_path = argv[1];
25  aubio_source_t * source = new_aubio_source(source_path, samplerate,
26      hop_size);
27  if (!source) { err = 1; goto beach; }
28
29  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
30
31  // create some vectors
32  fvec_t * in = new_fvec (hop_size); // input audio buffer
33  fvec_t * out = new_fvec (1); // output position
34
35  // create tempo object
36  aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size,
37      samplerate);
38
39  if (!o) { err = 1; goto beach_tempo; }
40
41  do {
42    // put some fresh data in input vector
43    aubio_source_do(source, in, &read);
44    // execute tempo
45    aubio_tempo_do(o,in,out);
46    // do something with the beats
47    if (out->data[0] != 0) {
48      PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
49          "with confidence %.2f\n",
50          aubio_tempo_get_last_ms(o), aubio_tempo_get_last_s(o),
51          aubio_tempo_get_last(o), aubio_tempo_get_bpm(o),
52          aubio_tempo_get_confidence(o));
53    }
54    n_frames += read;
55  } while ( read == hop_size );
56
57  PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
58      n_frames * 1. / samplerate,
59      n_frames, samplerate,
60      n_frames / hop_size, source_path);
61
62  // clean up memory
63  del_aubio_tempo(o);
64beach_tempo:
65  del_fvec(in);
66  del_fvec(out);
67  del_aubio_source(source);
68beach:
69  aubio_cleanup();
70
71  return err;
72}
73
74int test_wrong_params(void)
75{
76  uint_t win_size = 1024;
77  uint_t hop_size = 256;
78  uint_t samplerate = 44100;
79  aubio_tempo_t *t;
80  fvec_t* in, *out;
81  uint_t i;
82
83  // test wrong method fails
84  if (new_aubio_tempo("undefined", win_size, hop_size, samplerate)) return 1;
85
86  // test hop > win fails
87  if (new_aubio_tempo("default", hop_size, win_size, samplerate)) return 1;
88
89  // test null hop_size fails
90  if (new_aubio_tempo("default", win_size, 0, samplerate)) return 1;
91
92  // test 1 buf_size fails
93  if (new_aubio_tempo("default", 1, 1, samplerate)) return 1;
94
95  // test null samplerate fails
96  if (new_aubio_tempo("default", win_size, hop_size, 0)) return 1;
97
98  // test short sizes workaround
99  t = new_aubio_tempo("default", 2048, 2048, 500);
100  if (!t) return 1;
101
102  del_aubio_tempo(t);
103
104  t = new_aubio_tempo("default", win_size, hop_size, samplerate);
105  if (!t) return 1;
106
107  in = new_fvec(hop_size);
108  out = new_fvec(1);
109
110  // up to step = (next_power_of_two(5.8 * samplerate / hop_size ) / 4 )
111  for (i = 0; i < 256 + 1; i++)
112  {
113    aubio_tempo_do(t,in,out);
114    PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
115        "with confidence %.2f, was tatum %d\n",
116        aubio_tempo_get_last_ms(t), aubio_tempo_get_last_s(t),
117        aubio_tempo_get_last(t), aubio_tempo_get_bpm(t),
118        aubio_tempo_get_confidence(t), aubio_tempo_was_tatum(t));
119  }
120
121  del_aubio_tempo(t);
122  del_fvec(in);
123  del_fvec(out);
124
125  return run_on_default_source(main);
126}
Note: See TracBrowser for help on using the repository browser.