source: tests/src/io/test-source.c

Last change on this file was 6200052, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[tests] try reading after close in test-source.c

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[301b807]1#include <aubio.h>
[248da64]2#include "utils_tests.h"
[301b807]3
[4435ea3c]4int test_wrong_params(void);
5
6int main(int argc, char **argv)
[248da64]7{
8  uint_t err = 0;
9  if (argc < 2) {
[64d534d]10    PRINT_ERR("not enough arguments, running tests\n");
[4435ea3c]11    err = test_wrong_params();
[f50e534]12    PRINT_MSG("read a wave file as a mono vector\n");
13    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
14    PRINT_MSG("examples:\n");
15    PRINT_MSG(" - read file.wav at original samplerate\n");
16    PRINT_MSG("       %s file.wav\n", argv[0]);
17    PRINT_MSG(" - read file.wav at 32000Hz\n");
18    PRINT_MSG("       %s file.aif 32000\n", argv[0]);
19    PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
20    PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
[248da64]21    return err;
22  }
[301b807]23
[f50e534]24  uint_t samplerate = 0;
[248da64]25  uint_t hop_size = 256;
26  uint_t n_frames = 0, read = 0;
[f9204ac]27  if ( argc >= 3 ) samplerate = atoi(argv[2]);
28  if ( argc >= 4 ) hop_size = atoi(argv[3]);
[248da64]29
30  char_t *source_path = argv[1];
31
[447c673]32  aubio_source_t* s =
33    new_aubio_source(source_path, samplerate, hop_size);
34  fvec_t *vec = new_fvec(hop_size);
[4435ea3c]35  if (!s || !vec) { err = 1; goto beach; }
[301b807]36
[37ca459]37  uint_t n_frames_expected = aubio_source_get_duration(s);
38
[2e01060]39  samplerate = aubio_source_get_samplerate(s);
[7606d23]40
[248da64]41  do {
[301b807]42    aubio_source_do(s, vec, &read);
[4865e4b]43    fvec_print (vec);
[248da64]44    n_frames += read;
45  } while ( read == hop_size );
[301b807]46
[37ca459]47  PRINT_MSG("read %d frames (expected %d) at %dHz (%d blocks) from %s\n",
48            n_frames, n_frames_expected, samplerate, n_frames / hop_size,
49            source_path);
[f50e534]50
[c82a034]51  // close the file (optional)
52  aubio_source_close(s);
53
[7606d23]54beach:
[4435ea3c]55  if (vec)
56    del_fvec(vec);
57  if (s)
58    del_aubio_source(s);
[248da64]59  return err;
[301b807]60}
[4435ea3c]61
62int test_wrong_params(void)
63{
64  char_t *uri = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
65  uint_t samplerate = 44100;
66  uint_t hop_size = 512;
67  uint_t channels, read = 0;
68  fvec_t *vec;
69  fmat_t *mat;
70  aubio_source_t *s;
71
72  if (new_aubio_source(0,    samplerate, hop_size)) return 1;
[5c849af]73  if (new_aubio_source("\0", samplerate, hop_size)) return 1;
[4435ea3c]74  if (new_aubio_source(uri,          -1, hop_size)) return 1;
75  if (new_aubio_source(uri,           0,        0)) return 1;
76
77  s = new_aubio_source(uri, samplerate, hop_size);
78  if (!s) return 1;
79  channels = aubio_source_get_channels(s);
80
81  // vector to read downmixed samples
82  vec = new_fvec(hop_size);
83  // matrix to read individual channels
84  mat = new_fmat(channels, hop_size);
85
86  if (aubio_source_get_samplerate(s) != samplerate) return 1;
87
88  // read first hop_size frames
89  aubio_source_do(s, vec, &read);
90  if (read != hop_size) return 1;
91
[6f22ed5]92  // read again in undersized vector
93  del_fvec(vec);
94  vec = new_fvec(hop_size - 1);
95  aubio_source_do(s, vec, &read);
96  if (read != hop_size - 1) return 1;
97
98  // read again in oversized vector
99  del_fvec(vec);
100  vec = new_fvec(hop_size + 1);
101  aubio_source_do(s, vec, &read);
102  if (read != hop_size) return 1;
103
[4435ea3c]104  // seek to 0
105  if(aubio_source_seek(s, 0)) return 1;
106
107  // read again as multiple channels
108  aubio_source_do_multi(s, mat, &read);
109  if (read != hop_size) return 1;
110
[6f22ed5]111  // read again as multiple channels in an undersized matrix
112  del_fmat(mat);
113  mat = new_fmat(channels - 1, hop_size);
114  aubio_source_do_multi(s, mat, &read);
115  if (read != hop_size) return 1;
116
117  // read again as multiple channels in an undersized matrix
118  del_fmat(mat);
119  mat = new_fmat(channels, hop_size - 1);
120  aubio_source_do_multi(s, mat, &read);
121  if (read != hop_size - 1) return 1;
122
123  // read again as multiple channels in an oversized matrix
124  del_fmat(mat);
125  mat = new_fmat(channels + 1, hop_size);
126  aubio_source_do_multi(s, mat, &read);
127  if (read != hop_size) return 1;
128
129  // read again as multiple channels in an oversized matrix
130  del_fmat(mat);
131  mat = new_fmat(channels, hop_size + 1);
132  aubio_source_do_multi(s, mat, &read);
133  if (read != hop_size) return 1;
134
[4435ea3c]135  // close the file (optional)
136  aubio_source_close(s);
137  // test closing the file a second time
138  aubio_source_close(s);
139
[6200052]140  // reading after close fails
141  del_fvec(vec);
142  vec = new_fvec(hop_size);
143  aubio_source_do(s, vec, &read);
144  del_fmat(mat);
145  mat = new_fmat(channels, hop_size);
146  aubio_source_do_multi(s, mat, &read);
147
[4435ea3c]148  del_aubio_source(s);
149  del_fmat(mat);
150  del_fvec(vec);
151
[19b839f]152  // shouldn't crash on null
153  del_aubio_source(NULL);
154
[4435ea3c]155  return run_on_default_source(main);
156}
Note: See TracBrowser for help on using the repository browser.