source: tests/src/tempo/test-tempo.c @ 0056314

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

[tests] use run_on_default_source in tempo

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[4e9101e]1#include <aubio.h>
[f69e3bd]2#include "utils_tests.h"
[4e9101e]3
[88d3d31]4int test_wrong_params(void);
5
[f69e3bd]6int main (int argc, char **argv)
[9547247]7{
[f69e3bd]8  uint_t err = 0;
9  if (argc < 2) {
[88d3d31]10    PRINT_WRN("no arguments, running tests\n");
[f84f6bc]11    err = test_wrong_params();
[075bc57]12    PRINT_MSG("usage: %s <source_path> [samplerate] [win_size] [hop_size]\n",
13        argv[0]);
[f69e3bd]14    return err;
15  }
16  uint_t samplerate = 0;
[77db425]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]);
[f69e3bd]22  uint_t n_frames = 0, read = 0;
[9547247]23
[f69e3bd]24  char_t *source_path = argv[1];
[075bc57]25  aubio_source_t * source = new_aubio_source(source_path, samplerate,
26      hop_size);
[f69e3bd]27  if (!source) { err = 1; goto beach; }
[9547247]28
[f69e3bd]29  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
[9547247]30
[f69e3bd]31  // create some vectors
32  fvec_t * in = new_fvec (hop_size); // input audio buffer
[b136658]33  fvec_t * out = new_fvec (1); // output position
[f69e3bd]34
35  // create tempo object
[075bc57]36  aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size,
37      samplerate);
[9547247]38
[63bc67b]39  if (!o) { err = 1; goto beach_tempo; }
40
[f69e3bd]41  do {
42    // put some fresh data in input vector
43    aubio_source_do(source, in, &read);
[9547247]44    // execute tempo
45    aubio_tempo_do(o,in,out);
46    // do something with the beats
[f69e3bd]47    if (out->data[0] != 0) {
[075bc57]48      PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
49          "with confidence %.2f\n",
[f69e3bd]50          aubio_tempo_get_last_ms(o), aubio_tempo_get_last_s(o),
[075bc57]51          aubio_tempo_get_last(o), aubio_tempo_get_bpm(o),
52          aubio_tempo_get_confidence(o));
[f69e3bd]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
[9547247]63  del_aubio_tempo(o);
[63bc67b]64beach_tempo:
[9547247]65  del_fvec(in);
66  del_fvec(out);
[f69e3bd]67  del_aubio_source(source);
68beach:
[9547247]69  aubio_cleanup();
70
[f69e3bd]71  return err;
[4e9101e]72}
[88d3d31]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("unexisting_method", win_size, hop_size, samplerate))
85    return 1;
86
87  // test hop > win fails
88  if (new_aubio_tempo("default", hop_size, win_size, samplerate))
89    return 1;
90
91  // test null hop_size fails
92  if (new_aubio_tempo("default", win_size, 0, samplerate))
93    return 1;
94
95  // test 1 buf_size fails
96  if (new_aubio_tempo("default", 1, 1, samplerate))
97    return 1;
98
99  // test null samplerate fails
100  if (new_aubio_tempo("default", win_size, hop_size, 0))
101    return 1;
102
103  // test short sizes workaround
104  t = new_aubio_tempo("default", 2048, 2048, 500);
105  if (!t)
106    return 1;
107
108  del_aubio_tempo(t);
109
110  t = new_aubio_tempo("default", win_size, hop_size, samplerate);
111  if (!t)
112    return 1;
113
114  in = new_fvec(hop_size);
115  out = new_fvec(1);
116
[be83390]117  // up to step = (next_power_of_two(5.8 * samplerate / hop_size ) / 4 )
118  for (i = 0; i < 256 + 1; i++)
[88d3d31]119  {
120    aubio_tempo_do(t,in,out);
121    PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
122        "with confidence %.2f, was tatum %d\n",
123        aubio_tempo_get_last_ms(t), aubio_tempo_get_last_s(t),
124        aubio_tempo_get_last(t), aubio_tempo_get_bpm(t),
125        aubio_tempo_get_confidence(t), aubio_tempo_was_tatum(t));
126  }
127
128  del_aubio_tempo(t);
129  del_fvec(in);
130  del_fvec(out);
131
[f84f6bc]132  return run_on_default_source(main);
[88d3d31]133}
Note: See TracBrowser for help on using the repository browser.