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 (mean(errors) ) < 0.1, "error is bigger than 0.1 (%f)" % mean(errors) |
---|
72 | #print 'len(pitches), cut:', len(pitches), cut |
---|
73 | #print 'mean errors: ', mean(errors), 'mean pitches: ', mean(pitches) |
---|
74 | |
---|
75 | pitch_algorithms = [ "default", "yinfft", "yin", "schmitt", "mcomb", "fcomb" , "specacf" ] |
---|
76 | |
---|
77 | signal_modes = [ |
---|
78 | ( 4096, 512, 44100, 2.*882. ), |
---|
79 | ( 4096, 512, 44100, 882. ), |
---|
80 | ( 4096, 512, 44100, 440. ), |
---|
81 | ( 2048, 512, 44100, 440. ), |
---|
82 | ( 2048, 1024, 44100, 440. ), |
---|
83 | ( 2048, 1024, 44100, 440. ), |
---|
84 | ( 2048, 1024, 32000, 440. ), |
---|
85 | ( 2048, 1024, 22050, 440. ), |
---|
86 | ( 1024, 256, 16000, 440. ), |
---|
87 | ( 1024, 256, 8000, 440. ), |
---|
88 | ( 1024, 512+256, 8000, 440. ), |
---|
89 | ] |
---|
90 | |
---|
91 | def create_test (algo, mode): |
---|
92 | def do_test_pitch(self): |
---|
93 | self.run_pitch_on_sinusoid(algo, mode[0], mode[1], mode[2], mode[3]) |
---|
94 | return do_test_pitch |
---|
95 | |
---|
96 | for algo in pitch_algorithms: |
---|
97 | for mode in signal_modes: |
---|
98 | test_method = create_test (algo, mode) |
---|
99 | test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.2f' % ( algo, |
---|
100 | mode[0], mode[1], mode[2], mode[3] ) |
---|
101 | setattr (aubio_pitch_Sinusoid, test_method.__name__, test_method) |
---|
102 | |
---|
103 | if __name__ == '__main__': |
---|
104 | from unittest import main |
---|
105 | main() |
---|