Showing the changes to python (11 of 30 files). Show all
Unified · Split
| @@ -22,7 +22,7 @@ def get_file_bpm(path, params=None): · 21 unmodified lines | |||
| 22 | elif params.mode in ['default']: | 22 | elif params.mode in ['default']: |
| 23 | pass | 23 | pass |
| 24 | else: | 24 | else: |
| 25 | print("unknown mode {:s}".format(params.mode)) | 25 | raise ValueError("unknown mode {:s}".format(params.mode)) |
| 26 | # manual settings | 26 | # manual settings |
| 27 | if 'samplerate' in params: | 27 | if 'samplerate' in params: |
| 28 | samplerate = params.samplerate | 28 | samplerate = params.samplerate |
| @@ -69,9 +69,9 @@ if __name__ == '__main__': · 40 unmodified lines | |||
| 69 | parser = argparse.ArgumentParser() | 69 | parser = argparse.ArgumentParser() |
| 70 | parser.add_argument('-m', '--mode', | 70 | parser.add_argument('-m', '--mode', |
| 71 | help="mode [default|fast|super-fast]", | 71 | help="mode [default|fast|super-fast]", |
| 72 | dest="mode") | 72 | dest="mode", default='default') |
| 73 | parser.add_argument('sources', | 73 | parser.add_argument('sources', |
| 74 | nargs='*', | 74 | nargs='+', |
| 75 | help="input_files") | 75 | help="input_files") |
| 76 | args = parser.parse_args() | 76 | args = parser.parse_args() |
| 77 | for f in args.sources: | 77 | for f in args.sources: |
| @@ -237,7 +237,9 @@ Py_bintomidi (PyObject * self, PyObject * args) · 236 unmodified lines | |||
| 237 | smpl_t input, samplerate, fftsize; | 237 | smpl_t input, samplerate, fftsize; |
| 238 | smpl_t output; | 238 | smpl_t output; |
| 239 | 239 | ||
| 240 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { | 240 | if (!PyArg_ParseTuple (args, |
| 241 | "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, | ||
| 242 | &input, &samplerate, &fftsize)) { | ||
| 241 | return NULL; | 243 | return NULL; |
| 242 | } | 244 | } |
| 243 | 245 | ||
| @@ -252,7 +254,9 @@ Py_miditobin (PyObject * self, PyObject * args) · 8 unmodified lines | |||
| 252 | smpl_t input, samplerate, fftsize; | 254 | smpl_t input, samplerate, fftsize; |
| 253 | smpl_t output; | 255 | smpl_t output; |
| 254 | 256 | ||
| 255 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR , &input, &samplerate, &fftsize)) { | 257 | if (!PyArg_ParseTuple (args, |
| 258 | "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, | ||
| 259 | &input, &samplerate, &fftsize)) { | ||
| 256 | return NULL; | 260 | return NULL; |
| 257 | } | 261 | } |
| 258 | 262 | ||
| @@ -267,7 +271,9 @@ Py_bintofreq (PyObject * self, PyObject * args) · 8 unmodified lines | |||
| 267 | smpl_t input, samplerate, fftsize; | 271 | smpl_t input, samplerate, fftsize; |
| 268 | smpl_t output; | 272 | smpl_t output; |
| 269 | 273 | ||
| 270 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { | 274 | if (!PyArg_ParseTuple (args, |
| 275 | "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, | ||
| 276 | &input, &samplerate, &fftsize)) { | ||
| 271 | return NULL; | 277 | return NULL; |
| 272 | } | 278 | } |
| 273 | 279 | ||
| @@ -282,7 +288,9 @@ Py_freqtobin (PyObject * self, PyObject * args) · 8 unmodified lines | |||
| 282 | smpl_t input, samplerate, fftsize; | 288 | smpl_t input, samplerate, fftsize; |
| 283 | smpl_t output; | 289 | smpl_t output; |
| 284 | 290 | ||
| 285 | if (!PyArg_ParseTuple (args, "|" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, &input, &samplerate, &fftsize)) { | 291 | if (!PyArg_ParseTuple (args, |
| 292 | "" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR, | ||
| 293 | &input, &samplerate, &fftsize)) { | ||
| 286 | return NULL; | 294 | return NULL; |
| 287 | } | 295 | } |
| 288 | 296 | ||
| @@ -94,7 +94,7 @@ Py_filterbank_do(Py_filterbank * self, PyObject * args) · 93 unmodified lines | |||
| 94 | 94 | ||
| 95 | if (self->vec.length != self->win_s / 2 + 1) { | 95 | if (self->vec.length != self->win_s / 2 + 1) { |
| 96 | PyErr_Format(PyExc_ValueError, | 96 | PyErr_Format(PyExc_ValueError, |
| 97 | "input cvec has length %d, but fft expects length %d", | 97 | "input cvec has length %d, but filterbank expects length %d", |
| 98 | self->vec.length, self->win_s / 2 + 1); | 98 | self->vec.length, self->win_s / 2 + 1); |
| 99 | return NULL; | 99 | return NULL; |
| 100 | } | 100 | } |
| @@ -139,7 +139,7 @@ Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args) · 38 unmodified lines | |||
| 139 | &(self->freqs), samplerate); | 139 | &(self->freqs), samplerate); |
| 140 | if (err > 0) { | 140 | if (err > 0) { |
| 141 | PyErr_SetString (PyExc_ValueError, | 141 | PyErr_SetString (PyExc_ValueError, |
| 142 | "error when setting filter to A-weighting"); | 142 | "error when running set_triangle_bands"); |
| 143 | return NULL; | 143 | return NULL; |
| 144 | } | 144 | } |
| 145 | Py_RETURN_NONE; | 145 | Py_RETURN_NONE; |
| @@ -158,7 +158,7 @@ Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args) · 12 unmodified lines | |||
| 158 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); | 158 | err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); |
| 159 | if (err > 0) { | 159 | if (err > 0) { |
| 160 | PyErr_SetString (PyExc_ValueError, | 160 | PyErr_SetString (PyExc_ValueError, |
| 161 | "error when setting filter to A-weighting"); | 161 | "error when running set_mel_coeffs_slaney"); |
| 162 | return NULL; | 162 | return NULL; |
| 163 | } | 163 | } |
| 164 | Py_RETURN_NONE; | 164 | Py_RETURN_NONE; |
| @@ -101,6 +101,8 @@ def parser_add_subcommand_notes(subparsers): · 100 unmodified lines | |||
| 101 | help='estimate midi-like notes (monophonic)') | 101 | help='estimate midi-like notes (monophonic)') |
| 102 | subparser.add_input() | 102 | subparser.add_input() |
| 103 | subparser.add_buf_hop_size() | 103 | subparser.add_buf_hop_size() |
| 104 | subparser.add_silence() | ||
| 105 | subparser.add_release_drop() | ||
| 104 | subparser.add_time_format() | 106 | subparser.add_time_format() |
| 105 | subparser.add_verbose_help() | 107 | subparser.add_verbose_help() |
| 106 | subparser.set_defaults(process=process_notes) | 108 | subparser.set_defaults(process=process_notes) |
| @@ -207,6 +209,12 @@ class AubioArgumentParser(argparse.ArgumentParser): · 100 unmodified lines | |||
| 207 | action="store", dest="silence", default=-70, | 209 | action="store", dest="silence", default=-70, |
| 208 | help="silence threshold") | 210 | help="silence threshold") |
| 209 | 211 | ||
| 212 | def add_release_drop(self): | ||
| 213 | self.add_argument("-d", "--release-drop", | ||
| 214 | metavar = "<value>", type=float, | ||
| 215 | action="store", dest="release_drop", default=10, | ||
| 216 | help="release drop threshold") | ||
| 217 | |||
| 210 | def add_minioi(self, default="12ms"): | 218 | def add_minioi(self, default="12ms"): |
| 211 | self.add_argument("-M", "--minioi", | 219 | self.add_argument("-M", "--minioi", |
| 212 | metavar = "<value>", type=str, | 220 | metavar = "<value>", type=str, |
| @@ -247,6 +255,9 @@ class AubioArgumentParser(argparse.ArgumentParser): · 34 unmodified lines | |||
| 247 | metavar = "<slices>", | 255 | metavar = "<slices>", |
| 248 | action = "store", dest = "cut_until_nslices", default = None, | 256 | action = "store", dest = "cut_until_nslices", default = None, |
| 249 | help="how many extra slices should be added at the end of each slice") | 257 | help="how many extra slices should be added at the end of each slice") |
| 258 | self.add_argument("--create-first", | ||
| 259 | action = "store_true", dest = "create_first", default = False, | ||
| 260 | help="always include first slice") | ||
| 250 | 261 | ||
| 251 | # some utilities | 262 | # some utilities |
| 252 | 263 | ||
| @@ -379,6 +390,10 @@ class process_notes(default_process): · 126 unmodified lines | |||
| 379 | def __init__(self, args): | 390 | def __init__(self, args): |
| 380 | self.parse_options(args, self.valid_opts) | 391 | self.parse_options(args, self.valid_opts) |
| 381 | self.notes = aubio.notes(**self.options) | 392 | self.notes = aubio.notes(**self.options) |
| 393 | if args.silence is not None: | ||
| 394 | self.notes.set_silence(args.silence) | ||
| 395 | if args.release_drop is not None: | ||
| 396 | self.notes.set_release_drop(args.release_drop) | ||
| 382 | super(process_notes, self).__init__(args) | 397 | super(process_notes, self).__init__(args) |
| 383 | def __call__(self, block): | 398 | def __call__(self, block): |
| 384 | return self.notes(block) | 399 | return self.notes(block) |
| @@ -499,7 +514,24 @@ class process_cut(process_onset): · 114 unmodified lines | |||
| 499 | 514 | ||
| 500 | def main(): | 515 | def main(): |
| 501 | parser = aubio_parser() | 516 | parser = aubio_parser() |
| 502 | args = parser.parse_args() | 517 | if sys.version_info[0] != 3: |
| 518 | # on py2, create a dummy ArgumentParser to workaround the | ||
| 519 | # optional subcommand issue. See https://bugs.python.org/issue9253 | ||
| 520 | # This ensures that: | ||
| 521 | # - version string is shown when only '-V' is passed | ||
| 522 | # - help is printed if '-V' is passed with any other argument | ||
| 523 | # - any other argument get forwarded to the real parser | ||
| 524 | parser_root = argparse.ArgumentParser(add_help=False) | ||
| 525 | parser_root.add_argument('-V', '--version', help="show version", | ||
| 526 | action="store_true", dest="show_version") | ||
| 527 | args, extras = parser_root.parse_known_args() | ||
| 528 | if args.show_version == False: # no -V, forward to parser | ||
| 529 | args = parser.parse_args(extras, namespace=args) | ||
| 530 | elif len(extras) != 0: # -V with other arguments, print help | ||
| 531 | parser.print_help() | ||
| 532 | sys.exit(1) | ||
| 533 | else: # in py3, we can simply use parser directly | ||
| 534 | args = parser.parse_args() | ||
| 503 | if 'show_version' in args and args.show_version: | 535 | if 'show_version' in args and args.show_version: |
| 504 | sys.stdout.write('aubio version ' + aubio.version + '\n') | 536 | sys.stdout.write('aubio version ' + aubio.version + '\n') |
| 505 | sys.exit(0) | 537 | sys.exit(0) |
| @@ -101,7 +101,7 @@ def _cut_analyze(options): · 100 unmodified lines | |||
| 101 | 101 | ||
| 102 | s = source(source_uri, samplerate, hopsize) | 102 | s = source(source_uri, samplerate, hopsize) |
| 103 | if samplerate == 0: | 103 | if samplerate == 0: |
| 104 | samplerate = s.get_samplerate() | 104 | samplerate = s.samplerate |
| 105 | options.samplerate = samplerate | 105 | options.samplerate = samplerate |
| 106 | 106 | ||
| 107 | if options.beat: | 107 | if options.beat: |
| @@ -150,7 +150,8 @@ def _cut_slice(options, timestamps): · 42 unmodified lines | |||
| 150 | slice_source_at_stamps(options.source_uri, | 150 | slice_source_at_stamps(options.source_uri, |
| 151 | timestamps, timestamps_end = timestamps_end, | 151 | timestamps, timestamps_end = timestamps_end, |
| 152 | output_dir = options.output_directory, | 152 | output_dir = options.output_directory, |
| 153 | samplerate = options.samplerate) | 153 | samplerate = options.samplerate, |
| 154 | create_first = options.create_first) | ||
| 154 | 155 | ||
| 155 | def main(): | 156 | def main(): |
| 156 | parser = aubio_cut_parser() | 157 | parser = aubio_cut_parser() |
| @@ -1,9 +1,11 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | """ utilities to convert midi note number to and from note names """ | 2 | """ utilities to convert midi note number to and from note names """ |
| 3 | 3 | ||
| 4 | __all__ = ['note2midi', 'midi2note', 'freq2note'] | 4 | __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq'] |
| 5 | 5 | ||
| 6 | import sys | 6 | import sys |
| 7 | from ._aubio import freqtomidi, miditofreq | ||
| 8 | |||
| 7 | py3 = sys.version_info[0] == 3 | 9 | py3 = sys.version_info[0] == 3 |
| 8 | if py3: | 10 | if py3: |
| 9 | str_instances = str | 11 | str_instances = str |
| @@ -24,10 +26,11 @@ def note2midi(note): · 14 unmodified lines | |||
| 24 | } | 26 | } |
| 25 | _valid_octaves = range(-1, 10) | 27 | _valid_octaves = range(-1, 10) |
| 26 | if not isinstance(note, str_instances): | 28 | if not isinstance(note, str_instances): |
| 27 | raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)))) | 29 | msg = "a string is required, got {:s} ({:s})" |
| 30 | raise TypeError(msg.format(str(type(note)), repr(note))) | ||
| 28 | if len(note) not in range(2, 5): | 31 | if len(note) not in range(2, 5): |
| 29 | raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \ | 32 | msg = "string of 2 to 4 characters expected, got {:d} ({:s})" |
| 30 | % (len(note), note)) | 33 | raise ValueError(msg.format(len(note), note)) |
| 31 | notename, modifier, octave = [None]*3 | 34 | notename, modifier, octave = [None]*3 |
| 32 | 35 | ||
| 33 | if len(note) == 4: | 36 | if len(note) == 4: |
| @@ -51,7 +54,8 @@ def note2midi(note): · 17 unmodified lines | |||
| 51 | if octave not in _valid_octaves: | 54 | if octave not in _valid_octaves: |
| 52 | raise ValueError("%s is not a valid octave" % octave) | 55 | raise ValueError("%s is not a valid octave" % octave) |
| 53 | 56 | ||
| 54 | midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] | 57 | midi = 12 + octave * 12 + _valid_notenames[notename] \ |
| 58 | + _valid_modifiers[modifier] | ||
| 55 | if midi > 127: | 59 | if midi > 127: |
| 56 | raise ValueError("%s is outside of the range C-2 to G8" % note) | 60 | raise ValueError("%s is outside of the range C-2 to G8" % note) |
| 57 | return midi | 61 | return midi |
| @@ -61,11 +65,36 @@ def midi2note(midi): · 3 unmodified lines | |||
| 61 | if not isinstance(midi, int_instances): | 65 | if not isinstance(midi, int_instances): |
| 62 | raise TypeError("an integer is required, got %s" % midi) | 66 | raise TypeError("an integer is required, got %s" % midi) |
| 63 | if midi not in range(0, 128): | 67 | if midi not in range(0, 128): |
| 64 | raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi) | 68 | msg = "an integer between 0 and 127 is excepted, got {:d}" |
| 65 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] | 69 | raise ValueError(msg.format(midi)) |
| 70 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', | ||
| 71 | 'A', 'A#', 'B'] | ||
| 66 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) | 72 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) |
| 67 | 73 | ||
| 68 | def freq2note(freq): | 74 | def freq2note(freq): |
| 69 | " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " | 75 | " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " |
| 70 | from aubio import freqtomidi | 76 | nearest_note = int(freqtomidi(freq) + .5) |
| 71 | return midi2note(int(freqtomidi(freq))) | 77 | return midi2note(nearest_note) |
| 78 | |||
| 79 | def note2freq(note): | ||
| 80 | """Convert note name to corresponding frequency, in Hz. | ||
| 81 | |||
| 82 | Parameters | ||
| 83 | ---------- | ||
| 84 | note : str | ||
| 85 | input note name | ||
| 86 | |||
| 87 | Returns | ||
| 88 | ------- | ||
| 89 | freq : float [0, 23000[ | ||
| 90 | frequency, in Hz | ||
| 91 | |||
| 92 | Example | ||
| 93 | ------- | ||
| 94 | >>> aubio.note2freq('A4') | ||
| 95 | 440 | ||
| 96 | >>> aubio.note2freq('A3') | ||
| 97 | 220.1 | ||
| 98 | """ | ||
| 99 | midi = note2midi(note) | ||
| 100 | return miditofreq(midi) | ||
| @@ -6,19 +6,22 @@ from aubio import source, sink · 5 unmodified lines | |||
| 6 | _max_timestamp = 1e120 | 6 | _max_timestamp = 1e120 |
| 7 | 7 | ||
| 8 | def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, | 8 | def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, |
| 9 | output_dir=None, samplerate=0, hopsize=256): | 9 | output_dir=None, samplerate=0, hopsize=256, |
| 10 | create_first=False): | ||
| 10 | """ slice a sound file at given timestamps """ | 11 | """ slice a sound file at given timestamps """ |
| 11 | 12 | ||
| 12 | if timestamps is None or len(timestamps) == 0: | 13 | if timestamps is None or len(timestamps) == 0: |
| 13 | raise ValueError("no timestamps given") | 14 | raise ValueError("no timestamps given") |
| 14 | 15 | ||
| 15 | if timestamps[0] != 0: | 16 | if timestamps[0] != 0 and create_first: |
| 16 | timestamps = [0] + timestamps | 17 | timestamps = [0] + timestamps |
| 17 | if timestamps_end is not None: | 18 | if timestamps_end is not None: |
| 18 | timestamps_end = [timestamps[1] - 1] + timestamps_end | 19 | timestamps_end = [timestamps[1] - 1] + timestamps_end |
| 19 | 20 | ||
| 20 | if timestamps_end is not None: | 21 | if timestamps_end is not None: |
| 21 | if len(timestamps_end) != len(timestamps): | 22 | if len(timestamps_end) == len(timestamps) - 1: |
| 23 | timestamps_end = timestamps_end + [_max_timestamp] | ||
| 24 | elif len(timestamps_end) != len(timestamps): | ||
| 22 | raise ValueError("len(timestamps_end) != len(timestamps)") | 25 | raise ValueError("len(timestamps_end) != len(timestamps)") |
| 23 | else: | 26 | else: |
| 24 | timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] | 27 | timestamps_end = [t - 1 for t in timestamps[1:]] + [_max_timestamp] |
| @@ -48,7 +51,7 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 23 unmodified lines | |||
| 48 | # get hopsize new samples from source | 51 | # get hopsize new samples from source |
| 49 | vec, read = _source.do_multi() | 52 | vec, read = _source.do_multi() |
| 50 | # if the total number of frames read will exceed the next region start | 53 | # if the total number of frames read will exceed the next region start |
| 51 | if len(regions) and total_frames + read >= regions[0][0]: | 54 | while len(regions) and total_frames + read >= regions[0][0]: |
| 52 | #print "getting", regions[0], "at", total_frames | 55 | #print "getting", regions[0], "at", total_frames |
| 53 | # get next region | 56 | # get next region |
| 54 | start_stamp, end_stamp = regions.pop(0) | 57 | start_stamp, end_stamp = regions.pop(0) |
| @@ -75,12 +78,15 @@ def slice_source_at_stamps(source_file, timestamps, timestamps_end=None, · 20 unmodified lines | |||
| 75 | if remaining > start: | 78 | if remaining > start: |
| 76 | # write remaining samples from current region | 79 | # write remaining samples from current region |
| 77 | _sink.do_multi(vec[:, start:remaining], remaining - start) | 80 | _sink.do_multi(vec[:, start:remaining], remaining - start) |
| 78 | #print "closing region", "remaining", remaining | 81 | #print("closing region", "remaining", remaining) |
| 79 | # close this file | 82 | # close this file |
| 80 | _sink.close() | 83 | _sink.close() |
| 81 | elif read > start: | 84 | elif read > start: |
| 82 | # write all the samples | 85 | # write all the samples |
| 83 | _sink.do_multi(vec[:, start:read], read - start) | 86 | _sink.do_multi(vec[:, start:read], read - start) |
| 84 | total_frames += read | 87 | total_frames += read |
| 88 | # remove old slices | ||
| 89 | slices = list(filter(lambda s: s['end_stamp'] > total_frames, | ||
| 90 | slices)) | ||
| 85 | if read < hopsize: | 91 | if read < hopsize: |
| 86 | break | 92 | break |
| @@ -19,13 +19,16 @@ class aubio_cmd(TestCase): · 18 unmodified lines | |||
| 19 | class aubio_cmd_utils(TestCase): | 19 | class aubio_cmd_utils(TestCase): |
| 20 | 20 | ||
| 21 | def test_samples2seconds(self): | 21 | def test_samples2seconds(self): |
| 22 | self.assertEqual(aubio.cmd.samples2seconds(3200, 32000), "0.100000\t") | 22 | self.assertEqual(aubio.cmd.samples2seconds(3200, 32000), |
| 23 | "0.100000\t") | ||
| 23 | 24 | ||
| 24 | def test_samples2milliseconds(self): | 25 | def test_samples2milliseconds(self): |
| 25 | self.assertEqual(aubio.cmd.samples2milliseconds(3200, 32000), "100.000000\t") | 26 | self.assertEqual(aubio.cmd.samples2milliseconds(3200, 32000), |
| 27 | "100.000000\t") | ||
| 26 | 28 | ||
| 27 | def test_samples2samples(self): | 29 | def test_samples2samples(self): |
| 28 | self.assertEqual(aubio.cmd.samples2samples(3200, 32000), "3200\t") | 30 | self.assertEqual(aubio.cmd.samples2samples(3200, 32000), |
| 31 | "3200\t") | ||
| 29 | 32 | ||
| 30 | if __name__ == '__main__': | 33 | if __name__ == '__main__': |
| 31 | main() | 34 | main() |
| @@ -3,7 +3,7 @@ · 2 unmodified lines | |||
| 3 | 3 | ||
| 4 | from __future__ import unicode_literals | 4 | from __future__ import unicode_literals |
| 5 | 5 | ||
| 6 | from aubio import note2midi, freq2note | 6 | from aubio import note2midi, freq2note, note2freq, float_type |
| 7 | from nose2.tools import params | 7 | from nose2.tools import params |
| 8 | import unittest | 8 | import unittest |
| 9 | 9 | ||
| @@ -111,10 +111,27 @@ class note2midi_wrong_values(unittest.TestCase): · 101 unmodified lines | |||
| 111 | 111 | ||
| 112 | class freq2note_simple_test(unittest.TestCase): | 112 | class freq2note_simple_test(unittest.TestCase): |
| 113 | 113 | ||
| 114 | def test_freq2note(self): | 114 | def test_freq2note_above(self): |
| 115 | " make sure freq2note(441) == A4 " | 115 | " make sure freq2note(441) == A4 " |
| 116 | self.assertEqual("A4", freq2note(441)) | 116 | self.assertEqual("A4", freq2note(441)) |
| 117 | 117 | ||
| 118 | def test_freq2note_under(self): | ||
| 119 | " make sure freq2note(439) == A4 " | ||
| 120 | self.assertEqual("A4", freq2note(439)) | ||
| 121 | |||
| 122 | class note2freq_simple_test(unittest.TestCase): | ||
| 123 | |||
| 124 | def test_note2freq(self): | ||
| 125 | " make sure note2freq('A3') == 220" | ||
| 126 | self.assertEqual(220, note2freq("A3")) | ||
| 127 | |||
| 128 | def test_note2freq_under(self): | ||
| 129 | " make sure note2freq(A4) == 440" | ||
| 130 | if float_type == 'float32': | ||
| 131 | self.assertEqual(440, note2freq("A4")) | ||
| 132 | else: | ||
| 133 | self.assertLess(abs(note2freq("A4")-440), 1.e-12) | ||
| 134 | |||
| 118 | if __name__ == '__main__': | 135 | if __name__ == '__main__': |
| 119 | import nose2 | 136 | import nose2 |
| 120 | nose2.main() | 137 | nose2.main() |
| @@ -5,6 +5,7 @@ from numpy.testing import TestCase, assert_equal, assert_almost_equal · 4 unmodified lines | |||
| 5 | from aubio import notes | 5 | from aubio import notes |
| 6 | 6 | ||
| 7 | AUBIO_DEFAULT_NOTES_SILENCE = -70. | 7 | AUBIO_DEFAULT_NOTES_SILENCE = -70. |
| 8 | AUBIO_DEFAULT_NOTES_RELEASE_DROP = 10. | ||
| 8 | AUBIO_DEFAULT_NOTES_MINIOI_MS = 30. | 9 | AUBIO_DEFAULT_NOTES_MINIOI_MS = 30. |
| 9 | 10 | ||
| 10 | class aubio_notes_default(TestCase): | 11 | class aubio_notes_default(TestCase): |
| @@ -38,6 +39,19 @@ class aubio_notes_params(TestCase): · 27 unmodified lines | |||
| 38 | self.o.set_silence(val) | 39 | self.o.set_silence(val) |
| 39 | assert_equal (self.o.get_silence(), val) | 40 | assert_equal (self.o.get_silence(), val) |
| 40 | 41 | ||
| 42 | def test_get_release_drop(self): | ||
| 43 | assert_equal (self.o.get_release_drop(), AUBIO_DEFAULT_NOTES_RELEASE_DROP) | ||
| 44 | |||
| 45 | def test_set_release_drop(self): | ||
| 46 | val = 50 | ||
| 47 | self.o.set_release_drop(val) | ||
| 48 | assert_equal (self.o.get_release_drop(), val) | ||
| 49 | |||
| 50 | def test_set_release_drop_wrong(self): | ||
| 51 | val = -10 | ||
| 52 | with self.assertRaises(ValueError): | ||
| 53 | self.o.set_release_drop(val) | ||
| 54 | |||
| 41 | from .utils import list_all_sounds | 55 | from .utils import list_all_sounds |
| 42 | list_of_sounds = list_all_sounds('sounds') | 56 | list_of_sounds = list_all_sounds('sounds') |
| 43 | 57 | ||
| @@ -23,19 +23,27 @@ class aubio_slicing_test_case(TestCase): · 22 unmodified lines | |||
| 23 | 23 | ||
| 24 | def test_slice_start_only_no_zero(self): | 24 | def test_slice_start_only_no_zero(self): |
| 25 | regions_start = [i*1000 for i in range(1, n_slices)] | 25 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 26 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir) | 26 | slice_source_at_stamps(self.source_file, regions_start, |
| 27 | output_dir = self.output_dir, create_first=True) | ||
| 27 | 28 | ||
| 28 | def test_slice_start_beyond_end(self): | 29 | def test_slice_start_beyond_end(self): |
| 29 | regions_start = [i*1000 for i in range(1, n_slices)] | 30 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 30 | regions_start += [count_samples_in_file(self.source_file) + 1000] | 31 | regions_start += [count_samples_in_file(self.source_file) + 1000] |
| 31 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir) | 32 | slice_source_at_stamps(self.source_file, regions_start, |
| 33 | output_dir = self.output_dir, create_first=True) | ||
| 32 | 34 | ||
| 33 | def test_slice_start_every_blocksize(self): | 35 | def test_slice_start_every_blocksize(self): |
| 34 | hopsize = 200 | 36 | hopsize = 200 |
| 35 | regions_start = [i*hopsize for i in range(1, n_slices)] | 37 | regions_start = [i*hopsize for i in range(0, n_slices)] |
| 36 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, | 38 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
| 37 | hopsize = 200) | 39 | hopsize = 200) |
| 38 | 40 | ||
| 41 | def test_slice_start_every_half_blocksize(self): | ||
| 42 | hopsize = 200 | ||
| 43 | regions_start = [i*hopsize//2 for i in range(0, n_slices)] | ||
| 44 | slice_source_at_stamps(self.source_file, regions_start, | ||
| 45 | output_dir = self.output_dir, hopsize = 200) | ||
| 46 | |||
| 39 | def tearDown(self): | 47 | def tearDown(self): |
| 40 | original_samples = count_samples_in_file(self.source_file) | 48 | original_samples = count_samples_in_file(self.source_file) |
| 41 | written_samples = count_samples_in_directory(self.output_dir) | 49 | written_samples = count_samples_in_directory(self.output_dir) |
| @@ -91,6 +99,19 @@ class aubio_slicing_with_ends_test_case(TestCase): · 49 unmodified lines | |||
| 91 | assert_equal(written_samples, expected_samples, | 99 | assert_equal(written_samples, expected_samples, |
| 92 | "number of samples written different from number of original samples") | 100 | "number of samples written different from number of original samples") |
| 93 | 101 | ||
| 102 | def test_slice_start_and_ends_with_missing_end(self): | ||
| 103 | regions_start = [i*1000 for i in range(n_slices)] | ||
| 104 | regions_ends = [r-1 for r in regions_start[1:]] | ||
| 105 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, | ||
| 106 | output_dir = self.output_dir) | ||
| 107 | written_samples = count_samples_in_directory(self.output_dir) | ||
| 108 | original_samples = count_samples_in_file(self.source_file) | ||
| 109 | total_files = count_files_in_directory(self.output_dir) | ||
| 110 | assert_equal(n_slices, total_files, | ||
| 111 | "number of slices created different from expected") | ||
| 112 | assert_equal(written_samples, original_samples, | ||
| 113 | "number of samples written different from number of original samples") | ||
| 114 | |||
| 94 | def tearDown(self): | 115 | def tearDown(self): |
| 95 | shutil.rmtree(self.output_dir) | 116 | shutil.rmtree(self.output_dir) |
| 96 | 117 | ||
| @@ -133,7 +154,7 @@ class aubio_slicing_wrong_ends_test_case(TestCase): · 36 unmodified lines | |||
| 133 | regions_start = [i*1000 for i in range(1, n_slices)] | 154 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 134 | regions_end = None | 155 | regions_end = None |
| 135 | slice_source_at_stamps (self.source_file, regions_start, regions_end, | 156 | slice_source_at_stamps (self.source_file, regions_start, regions_end, |
| 136 | output_dir = self.output_dir) | 157 | output_dir = self.output_dir, create_first=True) |
| 137 | total_files = count_files_in_directory(self.output_dir) | 158 | total_files = count_files_in_directory(self.output_dir) |
| 138 | assert_equal(n_slices, total_files, | 159 | assert_equal(n_slices, total_files, |
| 139 | "number of slices created different from expected") | 160 | "number of slices created different from expected") |