History

python/aubiomodule.c: add midi/bin/freq conversion

Paul Brossier committed 13 years ago

Commit 6a50b9e1 parent 68d04922 files · +140 −2Browse files

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

Unified · Split

2 files
  1. python/aubiomodule.c +109 −2
  2. python/tests/test_mathutils.py +31 −0
python/aubiomodule.c+109−2
@@ -39,6 +39,108 @@ Py_alpha_norm (PyObject * self, PyObject * args) · 38 unmodified lines
39 return result;39 return result;
40}40}
41 41
42static char Py_bintomidi_doc[] = "convert bin to midi";
43
44static PyObject *
45Py_bintomidi (PyObject * self, PyObject * args)
46{
47 smpl_t input, samplerate, fftsize;
48 smpl_t output;
49
50 if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
51 return NULL;
52 }
53
54 output = aubio_bintomidi (input, samplerate, fftsize);
55
56 return (PyObject *)PyFloat_FromDouble (output);
57}
58
59static char Py_miditobin_doc[] = "convert midi to bin";
60
61static PyObject *
62Py_miditobin (PyObject * self, PyObject * args)
63{
64 smpl_t input, samplerate, fftsize;
65 smpl_t output;
66
67 if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
68 return NULL;
69 }
70
71 output = aubio_miditobin (input, samplerate, fftsize);
72
73 return (PyObject *)PyFloat_FromDouble (output);
74}
75
76static char Py_bintofreq_doc[] = "convert bin to freq";
77
78static PyObject *
79Py_bintofreq (PyObject * self, PyObject * args)
80{
81 smpl_t input, samplerate, fftsize;
82 smpl_t output;
83
84 if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
85 return NULL;
86 }
87
88 output = aubio_bintofreq (input, samplerate, fftsize);
89
90 return (PyObject *)PyFloat_FromDouble (output);
91}
92
93static char Py_freqtobin_doc[] = "convert freq to bin";
94
95static PyObject *
96Py_freqtobin (PyObject * self, PyObject * args)
97{
98 smpl_t input, samplerate, fftsize;
99 smpl_t output;
100
101 if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) {
102 return NULL;
103 }
104
105 output = aubio_freqtobin (input, samplerate, fftsize);
106
107 return (PyObject *)PyFloat_FromDouble (output);
108}
109
110static char Py_freqtomidi_doc[] = "convert freq to midi";
111
112static PyObject *
113Py_freqtomidi (PyObject * self, PyObject * args)
114{
115 smpl_t input;
116 smpl_t output;
117
118 if (!PyArg_ParseTuple (args, "|f", &input)) {
119 return NULL;
120 }
121
122 output = aubio_freqtomidi (input);
123
124 return (PyObject *)PyFloat_FromDouble (output);
125}
126
127static char Py_miditofreq_doc[] = "convert midi to freq";
128
129static PyObject *
130Py_miditofreq (PyObject * self, PyObject * args)
131{
132 smpl_t input;
133 smpl_t output;
134
135 if (!PyArg_ParseTuple (args, "|f", &input)) {
136 return NULL;
137 }
138
139 output = aubio_miditofreq (input);
140
141 return (PyObject *)PyFloat_FromDouble (output);
142}
143
42static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";144static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate";
43 145
44static PyObject *146static PyObject *
@@ -106,9 +208,14 @@ Py_min_removal(PyObject * self, PyObject * args) · 61 unmodified lines
106}208}
107 209
108static PyMethodDef aubio_methods[] = {210static PyMethodDef aubio_methods[] = {
211 {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc},
212 {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc},
213 {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc},
214 {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc},
215 {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc},
216 {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc},
109 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},217 {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc},
110 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS,218 {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc},
111 Py_zero_crossing_rate_doc},
112 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},219 {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
113 {NULL, NULL} /* Sentinel */220 {NULL, NULL} /* Sentinel */
114};221};
python/tests/test_mathutils.py+31−0
@@ -0,0 +1,31 @@
1#! /usr/bin/env python
2
3from numpy.testing import TestCase, run_module_suite
4from numpy.testing import assert_equal, assert_almost_equal
5from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq
6
7class aubio_mathutils(TestCase):
8
9 def test_miditobin(self):
10 a = [ miditobin(a, 44100, 512) for a in range(128) ]
11
12 def test_bintomidi(self):
13 a = [ bintomidi(a, 44100, 512) for a in range(128) ]
14
15 def test_freqtobin(self):
16 a = [ freqtobin(a, 44100, 512) for a in range(128) ]
17
18 def test_bintofreq(self):
19 a = [ bintofreq(a, 44100, 512) for a in range(128) ]
20
21 def test_freqtomidi(self):
22 a = [ freqtomidi(a) for a in range(128) ]
23
24 def test_miditofreq(self):
25 freqs = [ miditofreq(a) for a in range(128) ]
26 midis = [ freqtomidi(a) for a in freqs ]
27 print midis
28
29if __name__ == '__main__':
30 from unittest import main
31 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