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