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

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

[tests] improve test-tempo coverage

  • 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
[fd1d4d5]84  if (new_aubio_tempo("undefined", win_size, hop_size, samplerate)) return 1;
[88d3d31]85
86  // test hop > win fails
[fd1d4d5]87  if (new_aubio_tempo("default", hop_size, win_size, samplerate)) return 1;
[88d3d31]88
89  // test null hop_size fails
[fd1d4d5]90  if (new_aubio_tempo("default", win_size, 0, samplerate)) return 1;
[88d3d31]91
92  // test 1 buf_size fails
[fd1d4d5]93  if (new_aubio_tempo("default", 1, 1, samplerate)) return 1;
[88d3d31]94
95  // test null samplerate fails
[fd1d4d5]96  if (new_aubio_tempo("default", win_size, hop_size, 0)) return 1;
[88d3d31]97
98  // test short sizes workaround
99  t = new_aubio_tempo("default", 2048, 2048, 500);
[fd1d4d5]100  if (!t) return 1;
[88d3d31]101
102  del_aubio_tempo(t);
103
104  t = new_aubio_tempo("default", win_size, hop_size, samplerate);
[fd1d4d5]105  if (!t) return 1;
[88d3d31]106
107  in = new_fvec(hop_size);
108  out = new_fvec(1);
109
[be83390]110  // up to step = (next_power_of_two(5.8 * samplerate / hop_size ) / 4 )
111  for (i = 0; i < 256 + 1; i++)
[88d3d31]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
[f84f6bc]125  return run_on_default_source(main);
[88d3d31]126}
Note: See TracBrowser for help on using the repository browser.