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