History

Merge branch 'feature/pitchshift' into feature/timestretch

Paul Brossier committed 7 years ago

Commit 7a02ce92 parents 1301f66 439ba7b40 files · +911 −304Browse files

Showing the changes to python (8 of 40 files). Show all

Unified · Split

8 files
  1. python/demos/demo_wav2midi.py +1 −1
  2. python/ext/py-filter.c +2 −1
  3. python/ext/py-sink.c +4 −2
  4. python/ext/py-source.c +13 −1
  5. python/lib/moresetuptools.py +1 −1
  6. python/tests/test_sink.py +16 −3
  7. python/tests/test_source.py +32 −10
  8. python/tests/utils.py +14 −0
python/demos/demo_wav2midi.py+1−1
@@ -63,7 +63,7 @@ while True: · 62 unmodified lines
6363 delta = frames2tick(total_frames) - last_time
6464 if new_note[2] > 0:
6565 track.append(Message('note_off', note=int(new_note[2]),
66− velocity=127, time=0)
66+ velocity=127, time=delta)
6767 )
6868 track.append(Message('note_on',
6969 note=int(new_note[0]),
python/ext/py-filter.c+2−1
@@ -109,7 +109,8 @@ static void · 108 unmodified lines
109109 Py_filter_del (Py_filter * self)
110110 {
111111 Py_XDECREF(self->out);
112− del_aubio_filter (self->o);
112+ if (self->o)
113+ del_aubio_filter (self->o);
113114 Py_TYPE(self)->tp_free ((PyObject *) self);
114115 }
115116
python/ext/py-sink.c+4−2
@@ -150,8 +150,10 @@ Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds) · 149 unmodified lines
150150 static void
151151 Py_sink_del (Py_sink *self, PyObject *unused)
152152 {
153− del_aubio_sink(self->o);
154− free(self->mwrite_data.data);
153+ if (self->o) {
154+ del_aubio_sink(self->o);
155+ free(self->mwrite_data.data);
156+ }
155157 if (self->uri) {
156158 free(self->uri);
157159 }
python/ext/py-source.c+13−1
@@ -436,6 +436,10 @@ Py_source_do(Py_source * self, PyObject * args) · 435 unmodified lines
436436 /* compute _do function */
437437 aubio_source_do (self->o, &(self->c_read_to), &read);
438438
439+ if (PyErr_Occurred() != NULL) {
440+ return NULL;
441+ }
442+
439443 outputs = PyTuple_New(2);
440444 PyTuple_SetItem( outputs, 0, self->read_to );
441445 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
@@ -457,6 +461,10 @@ Py_source_do_multi(Py_source * self, PyObject * args) · 15 unmodified lines
457461 /* compute _do function */
458462 aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
459463
464+ if (PyErr_Occurred() != NULL) {
465+ return NULL;
466+ }
467+
460468 outputs = PyTuple_New(2);
461469 PyTuple_SetItem( outputs, 0, self->mread_to);
462470 PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
@@ -573,7 +581,10 @@ static PyObject* Pyaubio_source_iter_next(Py_source *self) { · 110 unmodified lines
573581 return vec;
574582 } else if (PyLong_AsLong(size) > 0) {
575583 // short read, return a shorter array
576− PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0);
584+ PyObject *vec = PyTuple_GetItem(done, 0);
585+ // take a copy to prevent resizing internal arrays
586+ PyArrayObject *shortread = (PyArrayObject*)PyArray_FROM_OTF(vec,
587+ NPY_NOTYPE, NPY_ARRAY_ENSURECOPY);
577588 PyArray_Dims newdims;
578589 PyObject *reshaped;
579590 newdims.len = PyArray_NDIM(shortread);
@@ -586,6 +597,7 @@ static PyObject* Pyaubio_source_iter_next(Py_source *self) { · 6 unmodified lines
586597 }
587598 reshaped = PyArray_Newshape(shortread, &newdims, NPY_CORDER);
588599 Py_DECREF(shortread);
600+ Py_DECREF(vec);
589601 return reshaped;
590602 } else {
591603 PyErr_SetNone(PyExc_StopIteration);
python/lib/moresetuptools.py+1−1
@@ -68,7 +68,7 @@ def add_local_macros(ext, usedouble = False): · 67 unmodified lines
6868 # define macros (waf puts them in build/src/config.h)
6969 for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
7070 'HAVE_MATH_H', 'HAVE_STRING_H',
71− 'HAVE_C99_VARARGS_MACROS',
71+ 'HAVE_ERRNO_H', 'HAVE_C99_VARARGS_MACROS',
7272 'HAVE_LIMITS_H', 'HAVE_STDARG_H',
7373 'HAVE_MEMCPY_HACKS']:
7474 ext.define_macros += [(define_macro, 1)]
python/tests/test_sink.py+16−3
@@ -3,7 +3,8 @@ · 2 unmodified lines
33 from numpy.testing import TestCase
44 from aubio import fvec, source, sink
55 from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
6−from _tools import parametrize, skipTest, assert_raises
6+from utils import parse_file_samplerate
7+from _tools import parametrize, skipTest, assert_raises, assert_warns
78
89 list_of_sounds = list_all_sounds('sounds')
910 samplerates = [0, 44100, 8000, 32000]
@@ -60,8 +61,14 @@ class Test_aubio_sink(object): · 50 unmodified lines
6061
6162 @parametrize('hop_size, samplerate, path', all_params)
6263 def test_read_and_write(self, hop_size, samplerate, path):
64+ orig_samplerate = parse_file_samplerate(soundfile)
6365 try:
64− f = source(path, samplerate, hop_size)
66+ if orig_samplerate is not None and orig_samplerate < samplerate:
67+ # upsampling should emit a warning
68+ with assert_warns(UserWarning):
69+ f = source(soundfile, samplerate, hop_size)
70+ else:
71+ f = source(soundfile, samplerate, hop_size)
6572 except RuntimeError as e:
6673 err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
6774 skipTest(err_msg.format(str(e), hop_size, samplerate))
@@ -78,8 +85,14 @@ class Test_aubio_sink(object): · 10 unmodified lines
7885
7986 @parametrize('hop_size, samplerate, path', all_params)
8087 def test_read_and_write_multi(self, hop_size, samplerate, path):
88+ orig_samplerate = parse_file_samplerate(soundfile)
8189 try:
82− f = source(path, samplerate, hop_size)
90+ if orig_samplerate is not None and orig_samplerate < samplerate:
91+ # upsampling should emit a warning
92+ with assert_warns(UserWarning):
93+ f = source(soundfile, samplerate, hop_size)
94+ else:
95+ f = source(soundfile, samplerate, hop_size)
8396 except RuntimeError as e:
8497 err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
8598 skipTest(err_msg.format(str(e), hop_size, samplerate))
python/tests/test_source.py+32−10
@@ -3,9 +3,10 @@ · 2 unmodified lines
33
44 from numpy.testing import TestCase, assert_equal
55 from aubio import source
6−from utils import list_all_sounds
6+from utils import list_all_sounds, parse_file_samplerate
77 import unittest
8−from _tools import parametrize, assert_raises, assert_equal, skipTest
8+from _tools import assert_raises, assert_equal, assert_warns
9+from _tools import parametrize, skipTest
910
1011 list_of_sounds = list_all_sounds('sounds')
1112 samplerates = [0, 44100, 8000, 32000]
@@ -23,23 +24,38 @@ no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!" · 11 unmodified lines
2324
2425 _debug = False
2526
26−class Test_aubio_source_test_case(object):
2727
28− @parametrize('filename', list_of_sounds)
29− def test_close_file(self, filename):
28+class Test_aubio_source_test_case(TestCase):
29+
30+ def setUp(self):
31+ if not default_test_sound:
32+ skipTest(no_sounds_msg)
33+
34+ def test_close_file(self):
3035 samplerate = 0 # use native samplerate
3136 hop_size = 256
32− f = source(filename, samplerate, hop_size)
37+ f = source(default_test_sound, samplerate, hop_size)
3338 f.close()
3439
35− @parametrize('filename', list_of_sounds)
36− def test_close_file_twice(self, filename):
40+ def test_close_file_twice(self):
3741 samplerate = 0 # use native samplerate
3842 hop_size = 256
39− f = source(filename, samplerate, hop_size)
43+ f = source(default_test_sound, samplerate, hop_size)
4044 f.close()
4145 f.close()
4246
47+ def test_read_after_close(self):
48+ samplerate = 0 # use native samplerate
49+ hop_size = 256
50+ f = source(default_test_sound, samplerate, hop_size)
51+ read, frames = f()
52+ f.close()
53+ with assert_raises(RuntimeError):
54+ read, frames = f()
55+ with assert_raises(RuntimeError):
56+ read, frames = f.do_multi()
57+
58+
4359 class Test_aubio_source_read(object):
4460
4561 def read_from_source(self, f):
@@ -60,8 +76,14 @@ class Test_aubio_source_read(object): · 14 unmodified lines
6076
6177 @parametrize('hop_size, samplerate, soundfile', all_params)
6278 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
79+ orig_samplerate = parse_file_samplerate(soundfile)
6380 try:
64− f = source(soundfile, samplerate, hop_size)
81+ if orig_samplerate is not None and orig_samplerate < samplerate:
82+ # upsampling should emit a warning
83+ with assert_warns(UserWarning):
84+ f = source(soundfile, samplerate, hop_size)
85+ else:
86+ f = source(soundfile, samplerate, hop_size)
6587 except RuntimeError as e:
6688 err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
6789 skipTest(err_msg.format(hop_size, samplerate, str(e)))
python/tests/utils.py+14−0
@@ -1,6 +1,7 @@
11 #! /usr/bin/env python
22
33 import os
4+import re
45 import glob
56 import numpy as np
67 from tempfile import mkstemp
@@ -77,3 +78,16 @@ def count_files_in_directory(samples_dir): · 70 unmodified lines
7778 if file_path:
7879 total_files += 1
7980 return total_files
81+
82+def parse_file_samplerate(soundfile):
83+ samplerate = None
84+ # parse samplerate
85+ re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
86+ match_samplerate = re_sr.findall(soundfile)
87+ if match_samplerate:
88+ samplerate = int(match_samplerate[0])
89+ else:
90+ import warnings
91+ warnings.warn(UserWarning("could not parse samplerate for {:s}"
92+ .format(soundfile)))
93+ return samplerate

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t