source: tests/src/onset/test-onset.c @ 30cb440e

feature/crepe_org
Last change on this file since 30cb440e was ee463f9, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] improve test-onset coverage

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[4e9101e]1#include <aubio.h>
[f69e3bd]2#include "utils_tests.h"
[4e9101e]3
[0f5f40b]4int test_wrong_params(void);
5
[f69e3bd]6int main (int argc, char **argv)
[26775a3]7{
[f69e3bd]8  uint_t err = 0;
9  if (argc < 2) {
10    err = 2;
[0f5f40b]11    PRINT_WRN("no arguments, running tests\n");
[1b3edc6]12    err = test_wrong_params();
[f69e3bd]13    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
14    return err;
15  }
16  uint_t samplerate = 0;
[26775a3]17  uint_t win_s = 1024; // window size
[f69e3bd]18  uint_t hop_size = win_s / 4;
19  uint_t n_frames = 0, read = 0;
[0de816c]20  if ( argc >= 3 ) samplerate = atoi(argv[2]);
21  if ( argc >= 4 ) hop_size = atoi(argv[3]);
[f69e3bd]22
23  char_t *source_path = argv[1];
24  aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size);
25  if (!source) { err = 1; goto beach; }
26
27  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
28
[26775a3]29  // create some vectors
[f69e3bd]30  fvec_t * in = new_fvec (hop_size); // input audio buffer
31  fvec_t * out = new_fvec (2); // output position
32
[26775a3]33  // create onset object
[f69e3bd]34  aubio_onset_t * o = new_aubio_onset("default", win_s, hop_size, samplerate);
35
36  do {
37    // put some fresh data in input vector
38    aubio_source_do(source, in, &read);
39    // execute onset
40    aubio_onset_do(o,in,out);
41    // do something with the onsets
42    if (out->data[0] != 0) {
43      PRINT_MSG("onset at %.3fms, %.3fs, frame %d\n",
44          aubio_onset_get_last_ms(o), aubio_onset_get_last_s(o),
45          aubio_onset_get_last(o));
46    }
47    n_frames += read;
48  } while ( read == hop_size );
49
50  PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
51      n_frames * 1. / samplerate,
52      n_frames, samplerate,
53      n_frames / hop_size, source_path);
54
55  // clean up memory
[2e17371]56  del_aubio_source(source);
[f69e3bd]57  del_aubio_onset(o);
58  del_fvec(in);
[26775a3]59  del_fvec(out);
[f69e3bd]60beach:
[26775a3]61  aubio_cleanup();
[4e9101e]62
[f69e3bd]63  return err;
[4e9101e]64}
[0f5f40b]65
66int test_wrong_params(void)
67{
68  uint_t win_size = 1024;
69  uint_t hop_size = win_size / 2;
70  uint_t samplerate = 44100;
71  // hop_size < 1
[ee463f9]72  if (new_aubio_onset("default", 5, 0, samplerate)) return 1;
73
[0f5f40b]74  // buf_size < 2
[ee463f9]75  if (new_aubio_onset("default", 1, 1, samplerate)) return 1;
76
[0f5f40b]77  // buf_size < hop_size
[ee463f9]78  if (new_aubio_onset("default", hop_size, win_size, samplerate)) return 1;
79
[0f5f40b]80  // samplerate < 1
[ee463f9]81  if (new_aubio_onset("default", 1024, 512, 0)) return 1;
[0f5f40b]82
83  // specdesc creation failed
[ee463f9]84  if (new_aubio_onset("abcd", win_size, win_size/2, samplerate)) return 1;
[0f5f40b]85
86  aubio_onset_t *o;
[2cf905f]87
88  // pv creation might fail
89  o = new_aubio_onset("default", 5, 2, samplerate);
90  if (o) del_aubio_onset(o);
91
[0f5f40b]92  o = new_aubio_onset("default", win_size, hop_size, samplerate);
[ee463f9]93  if (!aubio_onset_set_default_parameters(o, "wrong_type")) return 1;
[0f5f40b]94  del_aubio_onset(o);
95
[1b3edc6]96  return run_on_default_source(main);
[0f5f40b]97}
Note: See TracBrowser for help on using the repository browser.