1 | #! /usr/bin/python |
---|
2 | |
---|
3 | from numpy.testing import TestCase |
---|
4 | from numpy.testing import assert_equal, assert_almost_equal |
---|
5 | from numpy import random, sin, arange, mean, median |
---|
6 | from math import pi |
---|
7 | from aubio import fvec, pitch |
---|
8 | |
---|
9 | class aubio_mathutils_test_case(TestCase): |
---|
10 | |
---|
11 | def test_members(self): |
---|
12 | p = pitch() |
---|
13 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], |
---|
14 | ['default', 1024, 512, 44100]) |
---|
15 | |
---|
16 | def test_members_not_default(self): |
---|
17 | p = pitch('mcomb', 2048, 512, 32000) |
---|
18 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], |
---|
19 | ['mcomb', 2048, 512, 32000]) |
---|
20 | |
---|
21 | def test_run_on_zeros(self): |
---|
22 | p = pitch('mcomb', 2048, 512, 32000) |
---|
23 | f = fvec (512) |
---|
24 | assert_equal ( p(f), 0. ) |
---|
25 | |
---|
26 | def test_run_on_ones(self): |
---|
27 | p = pitch('mcomb', 2048, 512, 32000) |
---|
28 | f = fvec (512) |
---|
29 | f[:] = 1 |
---|
30 | assert( p(f) != 0. ) |
---|
31 | |
---|
32 | def test_run_default_on_sinusoid(self): |
---|
33 | method = 'default' |
---|
34 | buf_size = 2048 |
---|
35 | hop_size = 512 |
---|
36 | samplerate = 32000 |
---|
37 | freq = 450. |
---|
38 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
39 | |
---|
40 | def test_run_schmitt_on_sinusoid(self): |
---|
41 | method = 'schmitt' |
---|
42 | buf_size = 4096 |
---|
43 | hop_size = 512 |
---|
44 | samplerate = 44100 |
---|
45 | freq = 800. |
---|
46 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
47 | |
---|
48 | def test_run_mcomb_on_sinusoid(self): |
---|
49 | method = 'mcomb' |
---|
50 | buf_size = 2048 |
---|
51 | hop_size = 512 |
---|
52 | samplerate = 44100 |
---|
53 | freq = 10000. |
---|
54 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
55 | |
---|
56 | def test_run_fcomb_on_sinusoid(self): |
---|
57 | method = 'fcomb' |
---|
58 | buf_size = 2048 |
---|
59 | hop_size = 512 |
---|
60 | samplerate = 32000 |
---|
61 | freq = 440. |
---|
62 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
63 | |
---|
64 | def test_run_yin_on_sinusoid(self): |
---|
65 | method = 'yin' |
---|
66 | buf_size = 4096 |
---|
67 | hop_size = 512 |
---|
68 | samplerate = 32000 |
---|
69 | freq = 880. |
---|
70 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
71 | |
---|
72 | def test_run_yinfft_on_sinusoid(self): |
---|
73 | method = 'yinfft' |
---|
74 | buf_size = 2048 |
---|
75 | hop_size = 512 |
---|
76 | samplerate = 32000 |
---|
77 | freq = 640. |
---|
78 | self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) |
---|
79 | |
---|
80 | def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq): |
---|
81 | p = pitch(method, buf_size, hop_size, samplerate) |
---|
82 | sinvec = self.build_sinusoid(hop_size * 100, freq, samplerate) |
---|
83 | self.run_pitch(p, sinvec, freq) |
---|
84 | |
---|
85 | def build_sinusoid(self, length, freq, samplerate): |
---|
86 | return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate) |
---|
87 | |
---|
88 | def run_pitch(self, p, input_vec, freq): |
---|
89 | count = 0 |
---|
90 | pitches, errors = [], [] |
---|
91 | for vec_slice in input_vec.reshape((-1, p.hop_size)): |
---|
92 | pitch = p(vec_slice) |
---|
93 | pitches.append(pitch) |
---|
94 | errors.append(1. - pitch / freq) |
---|
95 | # check that the mean of all relative errors is less than 10% |
---|
96 | assert_almost_equal (mean(errors), 0., decimal = 2) |
---|
97 | |
---|
98 | if __name__ == '__main__': |
---|
99 | from unittest import main |
---|
100 | main() |
---|
101 | |
---|