1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy.testing import TestCase, assert_equal |
---|
4 | from numpy import sin, arange, mean, median, isnan, pi |
---|
5 | from aubio import fvec, pitch, freqtomidi, float_type |
---|
6 | |
---|
7 | class aubio_pitch_Good_Values(TestCase): |
---|
8 | |
---|
9 | def skip_test_new_default(self): |
---|
10 | " creating a pitch object without parameters " |
---|
11 | p = pitch() |
---|
12 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], |
---|
13 | ['default', 1024, 512, 44100]) |
---|
14 | |
---|
15 | def test_run_on_silence(self): |
---|
16 | " creating a pitch object with parameters " |
---|
17 | p = pitch('default', 2048, 512, 32000) |
---|
18 | assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], |
---|
19 | ['default', 2048, 512, 32000]) |
---|
20 | |
---|
21 | def test_run_on_zeros(self): |
---|
22 | " running on silence gives 0 " |
---|
23 | p = pitch('default', 2048, 512, 32000) |
---|
24 | f = fvec (512) |
---|
25 | for _ in range(10): assert_equal (p(f), 0.) |
---|
26 | |
---|
27 | def test_run_on_ones(self): |
---|
28 | " running on ones gives 0 " |
---|
29 | p = pitch('default', 2048, 512, 32000) |
---|
30 | f = fvec (512) |
---|
31 | f[:] = 1 |
---|
32 | for _ in range(10): assert_equal (p(f), 0.) |
---|
33 | |
---|
34 | class aubio_pitch_Sinusoid(TestCase): |
---|
35 | |
---|
36 | def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq): |
---|
37 | # create pitch object |
---|
38 | p = pitch(method, buf_size, hop_size, samplerate) |
---|
39 | # duration in seconds |
---|
40 | seconds = .3 |
---|
41 | # duration in samples |
---|
42 | duration = seconds * samplerate |
---|
43 | # increase to the next multiple of hop_size |
---|
44 | duration = duration - duration % hop_size + hop_size; |
---|
45 | # build sinusoid |
---|
46 | sinvec = self.build_sinusoid(duration, freq, samplerate) |
---|
47 | |
---|
48 | self.run_pitch(p, sinvec, freq) |
---|
49 | |
---|
50 | def build_sinusoid(self, length, freq, samplerate): |
---|
51 | return sin( 2. * pi * arange(length).astype(float_type) * freq / samplerate) |
---|
52 | |
---|
53 | def run_pitch(self, p, input_vec, freq): |
---|
54 | pitches, errors = [], [] |
---|
55 | input_blocks = input_vec.reshape((-1, p.hop_size)) |
---|
56 | for new_block in input_blocks: |
---|
57 | pitch = p(new_block)[0] |
---|
58 | pitches.append(pitch) |
---|
59 | errors.append(1. - freqtomidi(pitch) / freqtomidi(freq)) |
---|
60 | assert_equal ( len(input_blocks), len(pitches) ) |
---|
61 | assert_equal ( isnan(pitches), False ) |
---|
62 | # cut the first candidates |
---|
63 | #cut = ( p.buf_size - p.hop_size ) / p.hop_size |
---|
64 | pitches = pitches[2:] |
---|
65 | errors = errors[2:] |
---|
66 | # check that the mean of all relative errors is less than 10% |
---|
67 | #assert abs (mean(errors) ) < 0.1, pitches |
---|
68 | assert abs (median(errors) ) < 0.06, "median of relative errors is bigger than 0.06 (%f)\n found %s\n errors %s" % (mean(errors), pitches, errors) |
---|
69 | #print 'len(pitches), cut:', len(pitches), cut |
---|
70 | #print 'median errors: ', median(errors), 'median pitches: ', median(pitches) |
---|
71 | |
---|
72 | pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" , "specacf" ] |
---|
73 | pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" ] |
---|
74 | |
---|
75 | #freqs = [ 27.5, 55., 110., 220., 440., 880., 1760., 3520. ] |
---|
76 | freqs = [ 110., 220., 440., 880., 1760., 3520. ] |
---|
77 | signal_modes = [] |
---|
78 | for freq in freqs: |
---|
79 | signal_modes += [ |
---|
80 | ( 4096, 2048, 44100, freq ), |
---|
81 | ( 2048, 512, 44100, freq ), |
---|
82 | ( 2048, 1024, 44100, freq ), |
---|
83 | ( 2048, 1024, 32000, freq ), |
---|
84 | ] |
---|
85 | |
---|
86 | freqs = [ ] #55., 110., 220., 440., 880., 1760., 3520. ] |
---|
87 | for freq in freqs: |
---|
88 | signal_modes += [ |
---|
89 | ( 2048, 1024, 22050, freq ), |
---|
90 | ( 1024, 256, 16000, freq ), |
---|
91 | ( 1024, 256, 8000, freq ), |
---|
92 | ( 1024, 512+256, 8000, freq ), |
---|
93 | ] |
---|
94 | |
---|
95 | """ |
---|
96 | signal_modes = [ |
---|
97 | ( 4096, 512, 44100, 2.*882. ), |
---|
98 | ( 4096, 512, 44100, 882. ), |
---|
99 | ( 4096, 512, 44100, 440. ), |
---|
100 | ( 2048, 512, 44100, 440. ), |
---|
101 | ( 2048, 1024, 44100, 440. ), |
---|
102 | ( 2048, 1024, 44100, 440. ), |
---|
103 | ( 2048, 1024, 32000, 440. ), |
---|
104 | ( 2048, 1024, 22050, 440. ), |
---|
105 | ( 1024, 256, 16000, 440. ), |
---|
106 | ( 1024, 256, 8000, 440. ), |
---|
107 | ( 1024, 512+256, 8000, 440. ), |
---|
108 | ] |
---|
109 | """ |
---|
110 | |
---|
111 | def create_test (algo, mode): |
---|
112 | def do_test_pitch(self): |
---|
113 | self.run_pitch_on_sinusoid(algo, mode[0], mode[1], mode[2], mode[3]) |
---|
114 | return do_test_pitch |
---|
115 | |
---|
116 | for algo in pitch_algorithms: |
---|
117 | for mode in signal_modes: |
---|
118 | _test_method = create_test (algo, mode) |
---|
119 | _test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.0f' % ( algo, |
---|
120 | mode[0], mode[1], mode[2], mode[3] ) |
---|
121 | setattr (aubio_pitch_Sinusoid, _test_method.__name__, _test_method) |
---|
122 | |
---|
123 | if __name__ == '__main__': |
---|
124 | from unittest import main |
---|
125 | main() |
---|