History

python/ext/py-source.c: add seek, thanks @davebrent for the heads up

Paul Brossier committed 12 years ago

Commit 11b49d71 parent 24931d52 files · +46 −6Browse files

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

Unified · Split

2 files
  1. python/ext/py-source.c +26 −0
  2. python/tests/test_source.py +20 −6
python/ext/py-source.c+26−0
@@ -69,6 +69,11 @@ static char Py_source_close_doc[] = "" · 68 unmodified lines
69"\n"69"\n"
70"Close this source now.";70"Close this source now.";
71 71
72static char Py_source_seek_doc[] = ""
73"x.seek(position)\n"
74"\n"
75"Seek to resampled frame position.";
76
72static PyObject *77static PyObject *
73Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)78Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
74{79{
@@ -238,6 +243,25 @@ Pyaubio_source_close (Py_source *self, PyObject *unused) · 163 unmodified lines
238 Py_RETURN_NONE;243 Py_RETURN_NONE;
239}244}
240 245
246static PyObject *
247Pyaubio_source_seek (Py_source *self, PyObject *args)
248{
249 uint_t err = 0;
250
251 uint_t position;
252 if (!PyArg_ParseTuple (args, "I", &position)) {
253 return NULL;
254 }
255
256 err = aubio_source_seek(self->o, position);
257 if (err != 0) {
258 PyErr_SetString (PyExc_ValueError,
259 "error when seeking in source");
260 return NULL;
261 }
262 Py_RETURN_NONE;
263}
264
241static PyMethodDef Py_source_methods[] = {265static PyMethodDef Py_source_methods[] = {
242 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,266 {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
243 METH_NOARGS, Py_source_get_samplerate_doc},267 METH_NOARGS, Py_source_get_samplerate_doc},
@@ -249,6 +273,8 @@ static PyMethodDef Py_source_methods[] = { · 5 unmodified lines
249 METH_NOARGS, Py_source_do_multi_doc},273 METH_NOARGS, Py_source_do_multi_doc},
250 {"close", (PyCFunction) Pyaubio_source_close,274 {"close", (PyCFunction) Pyaubio_source_close,
251 METH_NOARGS, Py_source_close_doc},275 METH_NOARGS, Py_source_close_doc},
276 {"seek", (PyCFunction) Pyaubio_source_seek,
277 METH_VARARGS, Py_source_seek_doc},
252 {NULL} /* sentinel */278 {NULL} /* sentinel */
253};279};
254 280
python/tests/test_source.py+20−6
@@ -32,7 +32,7 @@ class aubio_source_test_case(aubio_source_test_case_base): · 31 unmodified lines
32 32
33class aubio_source_read_test_case(aubio_source_test_case_base):33class aubio_source_read_test_case(aubio_source_test_case_base):
34 34
35 def read_from_sink(self, f):35 def read_from_source(self, f):
36 total_frames = 036 total_frames = 0
37 while True:37 while True:
38 vec, read = f()38 vec, read = f()
@@ -42,25 +42,26 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 3 unmodified lines
42 print "(", total_frames, "frames", "in",42 print "(", total_frames, "frames", "in",
43 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",43 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
44 print "from", f.uri44 print "from", f.uri
45 return total_frames
45 46
46 def test_samplerate_hopsize(self):47 def test_samplerate_hopsize(self):
47 for p in list_of_sounds:48 for p in list_of_sounds:
48 for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):49 for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
49 f = source(p, samplerate, hop_size)50 f = source(p, samplerate, hop_size)
50 assert f.samplerate != 051 assert f.samplerate != 0
51 self.read_from_sink(f)52 self.read_from_source(f)
52 53
53 def test_samplerate_none(self):54 def test_samplerate_none(self):
54 for p in list_of_sounds:55 for p in list_of_sounds:
55 f = source(p)56 f = source(p)
56 assert f.samplerate != 057 assert f.samplerate != 0
57 self.read_from_sink(f)58 self.read_from_source(f)
58 59
59 def test_samplerate_0(self):60 def test_samplerate_0(self):
60 for p in list_of_sounds:61 for p in list_of_sounds:
61 f = source(p, 0)62 f = source(p, 0)
62 assert f.samplerate != 063 assert f.samplerate != 0
63 self.read_from_sink(f)64 self.read_from_source(f)
64 65
65 def test_wrong_samplerate(self):66 def test_wrong_samplerate(self):
66 for p in list_of_sounds:67 for p in list_of_sounds:
@@ -85,11 +86,23 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 18 unmodified lines
85 f = source(p, 0, 0)86 f = source(p, 0, 0)
86 assert f.samplerate != 087 assert f.samplerate != 0
87 assert f.hop_size != 088 assert f.hop_size != 0
88 self.read_from_sink(f)89 self.read_from_source(f)
90
91 def test_seek_to_half(self):
92 from random import randint
93 for p in list_of_sounds:
94 f = source(p, 0, 0)
95 assert f.samplerate != 0
96 assert f.hop_size != 0
97 a = self.read_from_source(f)
98 c = randint(0, a)
99 f.seek(c)
100 b = self.read_from_source(f)
101 assert a == b + c
89 102
90class aubio_source_readmulti_test_case(aubio_source_read_test_case):103class aubio_source_readmulti_test_case(aubio_source_read_test_case):
91 104
92 def read_from_sink(self, f):105 def read_from_source(self, f):
93 total_frames = 0106 total_frames = 0
94 while True:107 while True:
95 vec, read = f.do_multi()108 vec, read = f.do_multi()
@@ -100,6 +113,7 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 4 unmodified lines
100 print f.channels, "channels and",113 print f.channels, "channels and",
101 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",114 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
102 print "from", f.uri115 print "from", f.uri
116 return total_frames
103 117
104if __name__ == '__main__':118if __name__ == '__main__':
105 from unittest import main119 from unittest import main

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