From: Paul Brossier Date: Sun, 10 Feb 2013 01:47:37 +0000 (-0500) Subject: moved tests to subdirectory X-Git-Tag: 0.4.0-beta1~335^2~20 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=26adcfb10b34d9673d5c6bbec5d97c925982bf53;p=aubio.git moved tests to subdirectory --- diff --git a/python/demo_beats_and_tempo.py b/python/demo_beats_and_tempo.py deleted file mode 100755 index 2b33bb19..00000000 --- a/python/demo_beats_and_tempo.py +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import tempo, source - -win_s = 512 # fft size -hop_s = win_s / 2 # hop size -samplerate = 44100 - -if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] - sys.exit(1) - -filename = sys.argv[1] -beats = [] - -s = source(filename, samplerate, hop_s) -t = tempo("default", win_s, hop_s) - -block_read = 0 -while True: - samples, read = s() - isbeat = t(samples) - if isbeat: - thisbeat = (block_read * hop_s + isbeat[0]) / samplerate - print "%.4f" % thisbeat - beats.append (thisbeat) - block_read += 1 - if read < hop_s: break - -periods = [60./(b - a) for a,b in zip(beats[:-1],beats[1:])] - -from numpy import mean, median -print 'mean period:', mean(periods), 'bpm' -print 'median period:', median(periods), 'bpm' - -from pylab import plot, show -plot(beats[1:], periods) -show() diff --git a/python/demo_onset_sinusoid.py b/python/demo_onset_sinusoid.py deleted file mode 100755 index 97f9231d..00000000 --- a/python/demo_onset_sinusoid.py +++ /dev/null @@ -1,84 +0,0 @@ -#! /usr/bin/env python - -from numpy import random, sin, arange, ones, zeros -from math import pi -from aubio import fvec, onset - -def build_sinusoid(length, freqs, samplerate): - return sin( 2. * pi * arange(length) * freqs / samplerate) - -def run_onset(p, input_vec): - f = fvec (p.hop_size) - cands = [] - count = 0 - for vec_slice in input_vec.reshape((-1, p.hop_size)): - f[:] = vec_slice - cands.append(o(f)) - return cands - -methods = ['default', - 'energy', - 'complex', - 'phase', - 'specdiff', - 'kl', - 'mkl', - 'specflux', - 'centroid', - 'spread', - 'skewness', - 'kurtosis', - 'slope', - 'decrease', - 'rolloff', - ] - -cands = {} -buf_size = 2048 -hop_size = 512 -samplerate = 44100 -sin_length = (samplerate * 10) % 512 * 512 -freqs = zeros(sin_length) - -partition = sin_length / 8 -pointer = 0 - -pointer += partition -freqs[pointer: pointer + partition] = 440 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 740 - -pointer += partition -freqs[ pointer : pointer + partition ] = 1480 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) - -a = build_sinusoid(sin_length, freqs, samplerate) - -for method in methods: - o = onset(method, buf_size, hop_size, samplerate) - cands[method] = run_onset(o, a) - -print "done computing" - -if 1: - from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot - subplot (211) - legend(methods+['ground truth'], 'upper right') - xlabel('time (s)') - ylabel('amplitude') - ramp = arange(0, sin_length).astype('float') / samplerate - plot(ramp, a, ':') - subplot (212) - ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate - for method in methods: - plot(ramp, cands[method],'.-') - legend(methods, 'upper right') - xlabel('time (s)') - ylabel('spectral descriptor value') - show() - diff --git a/python/demo_pitch_sinusoid.py b/python/demo_pitch_sinusoid.py deleted file mode 100755 index 6407e586..00000000 --- a/python/demo_pitch_sinusoid.py +++ /dev/null @@ -1,68 +0,0 @@ -#! /usr/bin/env python - -from numpy import random, sin, arange, ones, zeros -from math import pi -from aubio import fvec, pitch - -def build_sinusoid(length, freqs, samplerate): - return sin( 2. * pi * arange(length) * freqs / samplerate) - -def run_pitch(p, input_vec): - f = fvec (p.hop_size) - cands = [] - count = 0 - for vec_slice in input_vec.reshape((-1, p.hop_size)): - f[:] = vec_slice - cands.append(p(f)) - return cands - -methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft'] - -cands = {} -buf_size = 2048 -hop_size = 512 -samplerate = 44100 -sin_length = (samplerate * 10) % 512 * 512 -freqs = zeros(sin_length) - -partition = sin_length / 8 -pointer = 0 - -pointer += partition -freqs[pointer: pointer + partition] = 440 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 740 - -pointer += partition -freqs[ pointer : pointer + partition ] = 1480 - -pointer += partition -pointer += partition -freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) - -a = build_sinusoid(sin_length, freqs, samplerate) - -for method in methods: - p = pitch(method, buf_size, hop_size, samplerate) - cands[method] = run_pitch(p, a) - -print "done computing" - -if 1: - from pylab import plot, show, xlabel, ylabel, legend, ylim - ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate - for method in methods: - plot(ramp, cands[method],'.-') - - # plot ground truth - ramp = arange(0, sin_length).astype('float') / samplerate - plot(ramp, freqs, ':') - - legend(methods+['ground truth'], 'upper right') - xlabel('time (s)') - ylabel('frequency (Hz)') - ylim([0,2000]) - show() - diff --git a/python/demo_simple_robot_voice.py b/python/demo_simple_robot_voice.py deleted file mode 100755 index 84c9c6ec..00000000 --- a/python/demo_simple_robot_voice.py +++ /dev/null @@ -1,29 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import source, sink, pvoc - -if __name__ == '__main__': - if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] - sys.exit(1) - samplerate = 44100 - f = source(sys.argv[1], samplerate, 256) - g = sink(sys.argv[2], samplerate) - total_frames, read = 0, 256 - - win_s = 512 # fft size - hop_s = win_s / 2 # hop size - pv = pvoc(win_s, hop_s) # phase vocoder - - while read: - samples, read = f() - spectrum = pv(samples) # compute spectrum - spectrum.phas[:] = 0. # zero phase - new_samples = pv.rdo(spectrum) # compute modified samples - g(new_samples, read) # write to output - total_frames += read - - print "wrote", total_frames, "from", f.uri, "to", g.uri - - diff --git a/python/demo_sink.py b/python/demo_sink.py deleted file mode 100755 index 702838c5..00000000 --- a/python/demo_sink.py +++ /dev/null @@ -1,17 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import source, sink - -if __name__ == '__main__': - if len(sys.argv) < 3: - print 'usage: %s ' % sys.argv[0] - sys.exit(1) - f = source(sys.argv[1], 8000, 256) - g = sink(sys.argv[2], 8000) - total_frames, read = 0, 256 - while read: - vec, read = f() - g(vec, read) - total_frames += read - print "read", total_frames / float(f.samplerate), "seconds from", f.uri diff --git a/python/demo_source.py b/python/demo_source.py deleted file mode 100755 index 65925fc0..00000000 --- a/python/demo_source.py +++ /dev/null @@ -1,15 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import source - -if __name__ == '__main__': - if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] - sys.exit(1) - f = source(sys.argv[1], 8000, 256) - total_frames, read = 0, 256 - while read: - vec, read = f() - total_frames += read - print "read", total_frames / float(f.samplerate), "seconds from", f.uri diff --git a/python/demo_spectrogram.py b/python/demo_spectrogram.py deleted file mode 100755 index 16bd69ef..00000000 --- a/python/demo_spectrogram.py +++ /dev/null @@ -1,63 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import pvoc, source -from numpy import array, arange, zeros, shape, log10, vstack -from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks - -def get_spectrogram(filename): - samplerate = 44100 - win_s = 512 # fft window size - hop_s = win_s / 2 # hop size - fft_s = win_s / 2 + 1 # spectrum bins - - a = source(filename, samplerate, hop_s) # source file - pv = pvoc(win_s, hop_s) # phase vocoder - specgram = zeros([0, fft_s], dtype='float32') # numpy array to store spectrogram - - # analysis - while True: - samples, read = a() # read file - specgram = vstack((specgram,pv(samples).norm)) # store new norm vector - if read < a.hop_size: break - - # plotting - imshow(log10(specgram.T + .001), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r) - axis([0, len(specgram), 0, len(specgram[0])]) - # show axes in Hz and seconds - time_step = hop_s / float(samplerate) - total_time = len(specgram) * time_step - print "total time: %0.2fs" % total_time, - print ", samplerate: %.2fkHz" % (samplerate / 1000.) - n_xticks = 10 - n_yticks = 10 - - def get_rounded_ticks( top_pos, step, n_ticks ): - top_label = top_pos * step - # get the first label - ticks_first_label = top_pos * step / n_ticks - # round to the closest .1 - ticks_first_label = round ( ticks_first_label * 10. ) / 10. - # compute all labels from the first rounded one - ticks_labels = [ ticks_first_label * n for n in range(n_ticks) ] + [ top_label ] - # get the corresponding positions - ticks_positions = [ ticks_labels[n] / step for n in range(n_ticks) ] + [ top_pos ] - # convert to string - ticks_labels = [ "%.1f" % x for x in ticks_labels ] - # return position, label tuple to use with x/yticks - return ticks_positions, ticks_labels - - # apply to the axis - xticks( *get_rounded_ticks ( len(specgram), time_step, n_xticks ) ) - yticks( *get_rounded_ticks ( len(specgram[0]), (samplerate / 2. / 1000.) / len(specgram[0]), n_yticks ) ) - ylabel('Frequency (kHz)') - xlabel('Time (s)') - -if __name__ == '__main__': - if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] - else: - for soundfile in sys.argv[1:]: - get_spectrogram(soundfile) - # display graph - show() diff --git a/python/demo_tss.py b/python/demo_tss.py deleted file mode 100755 index 0d670ce1..00000000 --- a/python/demo_tss.py +++ /dev/null @@ -1,47 +0,0 @@ -#! /usr/bin/env python - -import sys -from aubio import source, sink, pvoc, tss - -if __name__ == '__main__': - if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] - sys.exit(1) - - samplerate = 44100 - win_s = 512 # fft size - hop_s = win_s / 2 # block size - threshold = 0.26 - - f = source(sys.argv[1], samplerate, hop_s) - g = sink(sys.argv[2], samplerate) - h = sink(sys.argv[3], samplerate) - - pv = pvoc(win_s, hop_s) # phase vocoder - pw = pvoc(win_s, hop_s) # another phase vocoder - t = tss(win_s, hop_s) # transient steady state separation - - t.set_threshold(threshold) - - read = hop_s - - while read: - samples, read = f() # read file - spec = pv(samples) # compute spectrum - trans_spec, stead_spec = t(spec) # transient steady-state separation - transients = pv.rdo(trans_spec) # overlap-add synthesis of transients - steadstate = pw.rdo(stead_spec) # overlap-add synthesis of steady states - g(transients, read) # write transients to output - h(steadstate, read) # write steady states to output - - del f, g, h # finish writing the files now - - from demo_spectrogram import get_spectrogram - from pylab import subplot, show - subplot(311) - get_spectrogram(sys.argv[1]) - subplot(312) - get_spectrogram(sys.argv[2]) - subplot(313) - get_spectrogram(sys.argv[3]) - show() diff --git a/python/demos/demo_beats_and_tempo.py b/python/demos/demo_beats_and_tempo.py new file mode 100755 index 00000000..2b33bb19 --- /dev/null +++ b/python/demos/demo_beats_and_tempo.py @@ -0,0 +1,39 @@ +#! /usr/bin/env python + +import sys +from aubio import tempo, source + +win_s = 512 # fft size +hop_s = win_s / 2 # hop size +samplerate = 44100 + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + +filename = sys.argv[1] +beats = [] + +s = source(filename, samplerate, hop_s) +t = tempo("default", win_s, hop_s) + +block_read = 0 +while True: + samples, read = s() + isbeat = t(samples) + if isbeat: + thisbeat = (block_read * hop_s + isbeat[0]) / samplerate + print "%.4f" % thisbeat + beats.append (thisbeat) + block_read += 1 + if read < hop_s: break + +periods = [60./(b - a) for a,b in zip(beats[:-1],beats[1:])] + +from numpy import mean, median +print 'mean period:', mean(periods), 'bpm' +print 'median period:', median(periods), 'bpm' + +from pylab import plot, show +plot(beats[1:], periods) +show() diff --git a/python/demos/demo_onset_sinusoid.py b/python/demos/demo_onset_sinusoid.py new file mode 100755 index 00000000..97f9231d --- /dev/null +++ b/python/demos/demo_onset_sinusoid.py @@ -0,0 +1,84 @@ +#! /usr/bin/env python + +from numpy import random, sin, arange, ones, zeros +from math import pi +from aubio import fvec, onset + +def build_sinusoid(length, freqs, samplerate): + return sin( 2. * pi * arange(length) * freqs / samplerate) + +def run_onset(p, input_vec): + f = fvec (p.hop_size) + cands = [] + count = 0 + for vec_slice in input_vec.reshape((-1, p.hop_size)): + f[:] = vec_slice + cands.append(o(f)) + return cands + +methods = ['default', + 'energy', + 'complex', + 'phase', + 'specdiff', + 'kl', + 'mkl', + 'specflux', + 'centroid', + 'spread', + 'skewness', + 'kurtosis', + 'slope', + 'decrease', + 'rolloff', + ] + +cands = {} +buf_size = 2048 +hop_size = 512 +samplerate = 44100 +sin_length = (samplerate * 10) % 512 * 512 +freqs = zeros(sin_length) + +partition = sin_length / 8 +pointer = 0 + +pointer += partition +freqs[pointer: pointer + partition] = 440 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 740 + +pointer += partition +freqs[ pointer : pointer + partition ] = 1480 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) + +a = build_sinusoid(sin_length, freqs, samplerate) + +for method in methods: + o = onset(method, buf_size, hop_size, samplerate) + cands[method] = run_onset(o, a) + +print "done computing" + +if 1: + from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot + subplot (211) + legend(methods+['ground truth'], 'upper right') + xlabel('time (s)') + ylabel('amplitude') + ramp = arange(0, sin_length).astype('float') / samplerate + plot(ramp, a, ':') + subplot (212) + ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate + for method in methods: + plot(ramp, cands[method],'.-') + legend(methods, 'upper right') + xlabel('time (s)') + ylabel('spectral descriptor value') + show() + diff --git a/python/demos/demo_pitch_sinusoid.py b/python/demos/demo_pitch_sinusoid.py new file mode 100755 index 00000000..6407e586 --- /dev/null +++ b/python/demos/demo_pitch_sinusoid.py @@ -0,0 +1,68 @@ +#! /usr/bin/env python + +from numpy import random, sin, arange, ones, zeros +from math import pi +from aubio import fvec, pitch + +def build_sinusoid(length, freqs, samplerate): + return sin( 2. * pi * arange(length) * freqs / samplerate) + +def run_pitch(p, input_vec): + f = fvec (p.hop_size) + cands = [] + count = 0 + for vec_slice in input_vec.reshape((-1, p.hop_size)): + f[:] = vec_slice + cands.append(p(f)) + return cands + +methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft'] + +cands = {} +buf_size = 2048 +hop_size = 512 +samplerate = 44100 +sin_length = (samplerate * 10) % 512 * 512 +freqs = zeros(sin_length) + +partition = sin_length / 8 +pointer = 0 + +pointer += partition +freqs[pointer: pointer + partition] = 440 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 740 + +pointer += partition +freqs[ pointer : pointer + partition ] = 1480 + +pointer += partition +pointer += partition +freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) + +a = build_sinusoid(sin_length, freqs, samplerate) + +for method in methods: + p = pitch(method, buf_size, hop_size, samplerate) + cands[method] = run_pitch(p, a) + +print "done computing" + +if 1: + from pylab import plot, show, xlabel, ylabel, legend, ylim + ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate + for method in methods: + plot(ramp, cands[method],'.-') + + # plot ground truth + ramp = arange(0, sin_length).astype('float') / samplerate + plot(ramp, freqs, ':') + + legend(methods+['ground truth'], 'upper right') + xlabel('time (s)') + ylabel('frequency (Hz)') + ylim([0,2000]) + show() + diff --git a/python/demos/demo_simple_robot_voice.py b/python/demos/demo_simple_robot_voice.py new file mode 100755 index 00000000..84c9c6ec --- /dev/null +++ b/python/demos/demo_simple_robot_voice.py @@ -0,0 +1,29 @@ +#! /usr/bin/env python + +import sys +from aubio import source, sink, pvoc + +if __name__ == '__main__': + if len(sys.argv) < 2: + print 'usage: %s ' % sys.argv[0] + sys.exit(1) + samplerate = 44100 + f = source(sys.argv[1], samplerate, 256) + g = sink(sys.argv[2], samplerate) + total_frames, read = 0, 256 + + win_s = 512 # fft size + hop_s = win_s / 2 # hop size + pv = pvoc(win_s, hop_s) # phase vocoder + + while read: + samples, read = f() + spectrum = pv(samples) # compute spectrum + spectrum.phas[:] = 0. # zero phase + new_samples = pv.rdo(spectrum) # compute modified samples + g(new_samples, read) # write to output + total_frames += read + + print "wrote", total_frames, "from", f.uri, "to", g.uri + + diff --git a/python/demos/demo_sink.py b/python/demos/demo_sink.py new file mode 100755 index 00000000..702838c5 --- /dev/null +++ b/python/demos/demo_sink.py @@ -0,0 +1,17 @@ +#! /usr/bin/env python + +import sys +from aubio import source, sink + +if __name__ == '__main__': + if len(sys.argv) < 3: + print 'usage: %s ' % sys.argv[0] + sys.exit(1) + f = source(sys.argv[1], 8000, 256) + g = sink(sys.argv[2], 8000) + total_frames, read = 0, 256 + while read: + vec, read = f() + g(vec, read) + total_frames += read + print "read", total_frames / float(f.samplerate), "seconds from", f.uri diff --git a/python/demos/demo_source.py b/python/demos/demo_source.py new file mode 100755 index 00000000..65925fc0 --- /dev/null +++ b/python/demos/demo_source.py @@ -0,0 +1,15 @@ +#! /usr/bin/env python + +import sys +from aubio import source + +if __name__ == '__main__': + if len(sys.argv) < 2: + print 'usage: %s ' % sys.argv[0] + sys.exit(1) + f = source(sys.argv[1], 8000, 256) + total_frames, read = 0, 256 + while read: + vec, read = f() + total_frames += read + print "read", total_frames / float(f.samplerate), "seconds from", f.uri diff --git a/python/demos/demo_spectrogram.py b/python/demos/demo_spectrogram.py new file mode 100755 index 00000000..16bd69ef --- /dev/null +++ b/python/demos/demo_spectrogram.py @@ -0,0 +1,63 @@ +#! /usr/bin/env python + +import sys +from aubio import pvoc, source +from numpy import array, arange, zeros, shape, log10, vstack +from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks + +def get_spectrogram(filename): + samplerate = 44100 + win_s = 512 # fft window size + hop_s = win_s / 2 # hop size + fft_s = win_s / 2 + 1 # spectrum bins + + a = source(filename, samplerate, hop_s) # source file + pv = pvoc(win_s, hop_s) # phase vocoder + specgram = zeros([0, fft_s], dtype='float32') # numpy array to store spectrogram + + # analysis + while True: + samples, read = a() # read file + specgram = vstack((specgram,pv(samples).norm)) # store new norm vector + if read < a.hop_size: break + + # plotting + imshow(log10(specgram.T + .001), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r) + axis([0, len(specgram), 0, len(specgram[0])]) + # show axes in Hz and seconds + time_step = hop_s / float(samplerate) + total_time = len(specgram) * time_step + print "total time: %0.2fs" % total_time, + print ", samplerate: %.2fkHz" % (samplerate / 1000.) + n_xticks = 10 + n_yticks = 10 + + def get_rounded_ticks( top_pos, step, n_ticks ): + top_label = top_pos * step + # get the first label + ticks_first_label = top_pos * step / n_ticks + # round to the closest .1 + ticks_first_label = round ( ticks_first_label * 10. ) / 10. + # compute all labels from the first rounded one + ticks_labels = [ ticks_first_label * n for n in range(n_ticks) ] + [ top_label ] + # get the corresponding positions + ticks_positions = [ ticks_labels[n] / step for n in range(n_ticks) ] + [ top_pos ] + # convert to string + ticks_labels = [ "%.1f" % x for x in ticks_labels ] + # return position, label tuple to use with x/yticks + return ticks_positions, ticks_labels + + # apply to the axis + xticks( *get_rounded_ticks ( len(specgram), time_step, n_xticks ) ) + yticks( *get_rounded_ticks ( len(specgram[0]), (samplerate / 2. / 1000.) / len(specgram[0]), n_yticks ) ) + ylabel('Frequency (kHz)') + xlabel('Time (s)') + +if __name__ == '__main__': + if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + else: + for soundfile in sys.argv[1:]: + get_spectrogram(soundfile) + # display graph + show() diff --git a/python/demos/demo_tss.py b/python/demos/demo_tss.py new file mode 100755 index 00000000..0d670ce1 --- /dev/null +++ b/python/demos/demo_tss.py @@ -0,0 +1,47 @@ +#! /usr/bin/env python + +import sys +from aubio import source, sink, pvoc, tss + +if __name__ == '__main__': + if len(sys.argv) < 2: + print 'usage: %s ' % sys.argv[0] + sys.exit(1) + + samplerate = 44100 + win_s = 512 # fft size + hop_s = win_s / 2 # block size + threshold = 0.26 + + f = source(sys.argv[1], samplerate, hop_s) + g = sink(sys.argv[2], samplerate) + h = sink(sys.argv[3], samplerate) + + pv = pvoc(win_s, hop_s) # phase vocoder + pw = pvoc(win_s, hop_s) # another phase vocoder + t = tss(win_s, hop_s) # transient steady state separation + + t.set_threshold(threshold) + + read = hop_s + + while read: + samples, read = f() # read file + spec = pv(samples) # compute spectrum + trans_spec, stead_spec = t(spec) # transient steady-state separation + transients = pv.rdo(trans_spec) # overlap-add synthesis of transients + steadstate = pw.rdo(stead_spec) # overlap-add synthesis of steady states + g(transients, read) # write transients to output + h(steadstate, read) # write steady states to output + + del f, g, h # finish writing the files now + + from demo_spectrogram import get_spectrogram + from pylab import subplot, show + subplot(311) + get_spectrogram(sys.argv[1]) + subplot(312) + get_spectrogram(sys.argv[2]) + subplot(313) + get_spectrogram(sys.argv[3]) + show() diff --git a/python/test_aubio.py b/python/test_aubio.py deleted file mode 100755 index 951ade98..00000000 --- a/python/test_aubio.py +++ /dev/null @@ -1,14 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite - -class aubiomodule_test_case(TestCase): - - def test_import(self): - """ try importing aubio """ - import aubio - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_cvec.py b/python/test_cvec.py deleted file mode 100755 index 86916464..00000000 --- a/python/test_cvec.py +++ /dev/null @@ -1,50 +0,0 @@ -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -from aubio import cvec -from numpy import array, shape, pi - -class aubio_cvec_test_case(TestCase): - - def test_vector_created_with_zeroes(self): - a = cvec(10) - a - shape(a.norm) - shape(a.phas) - a.norm[0] - assert_equal(a.norm, 0.) - assert_equal(a.phas, 0.) - - def test_vector_assign_element(self): - a = cvec() - a.norm[0] = 1 - assert_equal(a.norm[0], 1) - a.phas[0] = 1 - assert_equal(a.phas[0], 1) - - def test_vector_assign_element_end(self): - a = cvec() - a.norm[-1] = 1 - assert_equal(a.norm[-1], 1) - assert_equal(a.norm[len(a.norm)-1], 1) - a.phas[-1] = 1 - assert_equal(a.phas[-1], 1) - assert_equal(a.phas[len(a.phas)-1], 1) - - def test_assign_cvec_norm_slice(self): - spec = cvec(1024) - spec.norm[40:100] = 100 - assert_equal (spec.norm[0:40], 0) - assert_equal (spec.norm[40:100], 100) - assert_equal (spec.norm[100:-1], 0) - assert_equal (spec.phas, 0) - - def test_assign_cvec_phas_slice(self): - spec = cvec(1024) - spec.phas[39:-1] = -pi - assert_equal (spec.phas[0:39], 0) - assert_equal (spec.phas[39:-1], -pi) - assert_equal (spec.norm, 0) - -if __name__ == '__main__': - from unittest import main - main() diff --git a/python/test_fft.py b/python/test_fft.py deleted file mode 100755 index ccb6e4cf..00000000 --- a/python/test_fft.py +++ /dev/null @@ -1,113 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -# WARNING: numpy also has an fft object -from aubio import fvec, fft, cvec -from numpy import array, shape -from math import pi - -class aubio_fft_test_case(TestCase): - - def test_members(self): - f = fft() - assert_equal (f.win_s, 1024) - - def test_output_dimensions(self): - """ check the dimensions of output """ - win_s = 1024 - timegrain = fvec(win_s) - f = fft(win_s) - fftgrain = f (timegrain) - assert_equal (fftgrain.norm, 0) - assert_equal (shape(fftgrain.norm), (win_s/2+1,)) - assert_equal (fftgrain.phas, 0) - assert_equal (shape(fftgrain.phas), (win_s/2+1,)) - - def test_zeros(self): - """ check the transform of zeros """ - win_s = 512 - timegrain = fvec(win_s) - f = fft(win_s) - fftgrain = f(timegrain) - assert_equal ( fftgrain.norm == 0, True ) - assert_equal ( fftgrain.phas == 0, True ) - - def test_impulse(self): - """ check the transform of one impulse at a random place """ - from random import random - from math import floor - win_s = 256 - i = floor(random()*win_s) - impulse = pi * random() - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[i] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) - assert_equal ( fftgrain.phas <= pi, True) - assert_equal ( fftgrain.phas >= -pi, True) - - def test_impulse_negative(self): - """ check the transform of one impulse at a random place """ - from random import random - from math import floor - win_s = 256 - i = 0 - impulse = -10. - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[i] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) - if impulse < 0: - # phase can be pi or -pi, as it is not unwrapped - assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) - assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) - assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) - else: - assert_equal ( fftgrain.phas[1:-1] == 0, True) - assert_equal ( fftgrain.phas[0] == 0, True) - assert_equal ( fftgrain.phas[-1] == 0, True) - # now check the resynthesis - synthgrain = f.rdo ( fftgrain ) - #self.plot_this ( fftgrain.phas.T ) - assert_equal ( fftgrain.phas <= pi, True) - assert_equal ( fftgrain.phas >= -pi, True) - #self.plot_this ( synthgrain - timegrain ) - assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) - - def test_impulse_at_zero(self): - """ check the transform of one impulse at a index 0 """ - win_s = 1024 - impulse = pi - f = fft(win_s) - timegrain = fvec(win_s) - timegrain[0] = impulse - fftgrain = f ( timegrain ) - #self.plot_this ( fftgrain.phas ) - assert_equal ( fftgrain.phas[0], 0) - # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura) - assert_almost_equal ( fftgrain.phas[1], 0) - assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) - - def test_rdo_before_do(self): - """ check running fft.rdo before fft.do works """ - win_s = 1024 - impulse = pi - f = fft(win_s) - fftgrain = cvec(win_s) - t = f.rdo( fftgrain ) - assert_equal ( t, 0 ) - - def plot_this(self, this): - from pylab import plot, show - plot ( this ) - show () - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_filter.py b/python/test_filter.py deleted file mode 100755 index 14a880d0..00000000 --- a/python/test_filter.py +++ /dev/null @@ -1,68 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, assert_equal, assert_almost_equal -from aubio import fvec, digital_filter -from numpy import array - -def array_from_text_file(filename, dtype = 'float'): - return array([line.split() for line in open(filename).readlines()], - dtype = dtype) - -class aubio_filter_test_case(TestCase): - - def test_members(self): - f = digital_filter() - assert_equal (f.order, 7) - f = digital_filter(5) - assert_equal (f.order, 5) - f(fvec()) - - def test_cweighting_error(self): - f = digital_filter (2) - self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) - f = digital_filter (8) - self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) - f = digital_filter (5) - self.assertRaises ( ValueError, f.set_c_weighting, 4000 ) - f = digital_filter (5) - self.assertRaises ( ValueError, f.set_c_weighting, 193000 ) - f = digital_filter (7) - self.assertRaises ( ValueError, f.set_a_weighting, 193000 ) - f = digital_filter (5) - self.assertRaises ( ValueError, f.set_a_weighting, 192000 ) - - def test_c_weighting(self): - expected = array_from_text_file('c_weighting_test_simple.expected') - f = digital_filter(5) - f.set_c_weighting(44100) - v = fvec(32) - v[12] = .5 - u = f(v) - assert_almost_equal (expected[1], u) - - def test_a_weighting(self): - expected = array_from_text_file('a_weighting_test_simple.expected') - f = digital_filter(7) - f.set_a_weighting(44100) - v = fvec(32) - v[12] = .5 - u = f(v) - assert_almost_equal (expected[1], u) - - def test_a_weighting_parted(self): - expected = array_from_text_file('a_weighting_test_simple.expected') - f = digital_filter(7) - f.set_a_weighting(44100) - v = fvec(16) - v[12] = .5 - u = f(v) - assert_almost_equal (expected[1][:16], u) - # one more time - v = fvec(16) - u = f(v) - assert_almost_equal (expected[1][16:], u) - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_filterbank.py b/python/test_filterbank.py deleted file mode 100755 index c00a5685..00000000 --- a/python/test_filterbank.py +++ /dev/null @@ -1,23 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -from numpy import random -from aubio import cvec, filterbank - -class aubio_filterbank_test_case(TestCase): - - def test_members(self): - f = filterbank(40, 512) - assert_equal ([f.n_filters, f.win_s], [40, 512]) - - def test_set_coeffs(self): - f = filterbank(40, 512) - r = random.random([40, 512 / 2 + 1]).astype('float32') - f.set_coeffs(r) - assert_equal (r, f.get_coeffs()) - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_filterbank_mel.py b/python/test_filterbank_mel.py deleted file mode 100755 index 532ca0b3..00000000 --- a/python/test_filterbank_mel.py +++ /dev/null @@ -1,51 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -from numpy import array, shape -from aubio import cvec, filterbank - -class aubio_filterbank_mel_test_case(TestCase): - - def test_slaney(self): - f = filterbank(40, 512) - f.set_mel_coeffs_slaney(16000) - a = f.get_coeffs() - assert_equal(shape (a), (40, 512/2 + 1) ) - - def test_other_slaney(self): - f = filterbank(40, 512*2) - f.set_mel_coeffs_slaney(44100) - a = f.get_coeffs() - #print "sum is", sum(sum(a)) - for win_s in [256, 512, 1024, 2048, 4096]: - f = filterbank(40, win_s) - f.set_mel_coeffs_slaney(320000) - a = f.get_coeffs() - #print "sum is", sum(sum(a)) - - def test_triangle_freqs_zeros(self): - f = filterbank(9, 1024) - freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] - freqs = array(freq_list, dtype = 'float32') - f.set_triangle_bands(freqs, 48000) - f.get_coeffs().T - assert_equal ( f(cvec(1024)), 0) - - def test_triangle_freqs_ones(self): - f = filterbank(9, 1024) - freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] - freqs = array(freq_list, dtype = 'float32') - f.set_triangle_bands(freqs, 48000) - f.get_coeffs().T - spec = cvec(1024) - spec.norm[:] = 1 - assert_almost_equal ( f(spec), - [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, - 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) - -if __name__ == '__main__': - from unittest import main - main() - - diff --git a/python/test_fvec.py b/python/test_fvec.py deleted file mode 100755 index 643c6f93..00000000 --- a/python/test_fvec.py +++ /dev/null @@ -1,135 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal -from numpy import array, shape - -class aubio_fvec_test_case(TestCase): - - def test_vector_created_with_zeroes(self): - a = fvec(10) - a - shape(a) - a[0] - #del a - assert_equal(array(a), 0.) - - def test_vector_create_with_list(self): - a = fvec([0,1,2,3]) - assert_equal (range(4), a) - - def test_vector_assign_element(self): - a = fvec() - a[0] = 1 - assert_equal(a[0], 1) - - def test_vector_assign_element_end(self): - a = fvec() - a[-1] = 1 - assert_equal(a[-1], 1) - assert_equal(a[len(a)-1], 1) - - def test_vector(self): - a = fvec() - a, len(a) #a.length - a[0] - array(a) - a = fvec(10) - a = fvec(1) - a.T - array(a).T - a = range(len(a)) - - def test_wrong_values(self): - self.assertRaises (ValueError, fvec, -10) - - a = fvec(2) - self.assertRaises (IndexError, a.__getitem__, 3) - self.assertRaises (IndexError, a.__getitem__, 2) - - def test_alpha_norm_of_fvec(self): - a = fvec(2) - self.assertEquals (alpha_norm(a, 1), 0) - a[0] = 1 - self.assertEquals (alpha_norm(a, 1), 0.5) - a[1] = 1 - self.assertEquals (alpha_norm(a, 1), 1) - a = array([0, 1], dtype='float32') - from math import sqrt - assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) - - def test_alpha_norm_of_none(self): - self.assertRaises (ValueError, alpha_norm, None, 1) - - def test_alpha_norm_of_array_of_float32(self): - # check scalar fails - a = array(1, dtype = 'float32') - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 2d array fails - a = array([[2],[4]], dtype = 'float32') - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 1d array - a = array(range(10), dtype = 'float32') - self.assertEquals (alpha_norm(a, 1), 4.5) - - def test_alpha_norm_of_array_of_int(self): - a = array(1, dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - a = array([[[1,2],[3,4]]], dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - a = array(range(10), dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - - def test_alpha_norm_of_array_of_string (self): - a = "hello" - self.assertRaises (ValueError, alpha_norm, a, 1) - - def test_zero_crossing_rate(self): - a = array([0,1,-1], dtype='float32') - assert_almost_equal (zero_crossing_rate(a), 1./3. ) - a = array([0.]*100, dtype='float32') - self.assertEquals (zero_crossing_rate(a), 0 ) - a = array([-1.]*100, dtype='float32') - self.assertEquals (zero_crossing_rate(a), 0 ) - a = array([1.]*100, dtype='float32') - self.assertEquals (zero_crossing_rate(a), 0 ) - - def test_alpha_norm_of_array_of_float64(self): - # check scalar fail - a = array(1, dtype = 'float64') - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 3d array fail - a = array([[[1,2],[3,4]]], dtype = 'float64') - self.assertRaises (ValueError, alpha_norm, a, 1) - # check float64 1d array fail - a = array(range(10), dtype = 'float64') - self.assertRaises (ValueError, alpha_norm, a, 1) - # check float64 2d array fail - a = array([range(10), range(10)], dtype = 'float64') - self.assertRaises (ValueError, alpha_norm, a, 1) - - def test_fvec_min_removal_of_array(self): - a = array([20,1,19], dtype='float32') - b = min_removal(a) - assert_equal (array(b), [19, 0, 18]) - assert_equal (b, [19, 0, 18]) - assert_equal (a, b) - a[0] = 0 - assert_equal (a, b) - - def test_fvec_min_removal_of_array_float64(self): - a = array([20,1,19], dtype='float64') - self.assertRaises (ValueError, min_removal, a) - - def test_fvec_min_removal_of_fvec(self): - a = fvec(3) - a = array([20, 1, 19], dtype = 'float32') - b = min_removal(a) - assert_equal (array(b), [19, 0, 18]) - assert_equal (b, [19, 0, 18]) - assert_equal (a, b) - -if __name__ == '__main__': - from unittest import main - main() diff --git a/python/test_onset.py b/python/test_onset.py deleted file mode 100755 index c6642d51..00000000 --- a/python/test_onset.py +++ /dev/null @@ -1,20 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -# WARNING: numpy also has an fft object -from aubio import onset, cvec -from numpy import array, shape, arange, zeros, log -from math import pi - -class aubio_onset(TestCase): - - def test_members(self): - o = onset() - assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate], - [1024,512,'default',44100]) - - -if __name__ == '__main__': - from unittest import main - main() diff --git a/python/test_peakpicker.py b/python/test_peakpicker.py deleted file mode 100755 index 08313d12..00000000 --- a/python/test_peakpicker.py +++ /dev/null @@ -1,115 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, assert_equal, assert_almost_equal -from aubio import peakpicker, fvec - -class aubio_peakpicker(TestCase): - - def test_members(self): - o = peakpicker() - - def test_peakpicker_zeroes(self): - o = peakpicker() - assert_equal(o.get_thresholded_input(), 0.) - - def test_peakpick_set_threshold(self): - o = peakpicker() - new_threshold = threshold - o.set_threshold(new_threshold) - assert_almost_equal(new_threshold, o.get_threshold()) - - def test_peakpicker_get_threshold(self): - o = peakpicker() - new_threshold = o.get_threshold() - o.set_threshold(new_threshold) - assert_equal(new_threshold, o.get_threshold()) - -buf_size = 1024 -slice_size = 5 -delay = 1 -threshold = .9 - -class aubio_peakpicker_peaks(TestCase): - - def setUp(self): - self.o = peakpicker() - self.o.set_threshold (threshold) - self.vec = fvec(buf_size) - - def test_peakpicker_impulse(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. - self.peaks = [a] - - def test_peakpicker_ramp_up(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. / 4. * 1. - vec[a+1] = 1000. / 4. * 2. - vec[a+2] = 1000. / 4. * 3. - vec[a+3] = 1000. - self.peaks = [a+1] - - def test_peakpicker_ramp_down(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. - vec[a+1] = 1000. / 4. * 3. - vec[a+2] = 1000. / 4. * 2. - vec[a+3] = 1000. / 4. * 1. - self.peaks = [a] - - def test_peakpicker_plateau(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. / 2 - vec[a+1] = 1000. - vec[a+2] = 1000. - vec[a+3] = 1000. - vec[a+4] = 1000. / 2 - self.peaks = [a+1] - - def test_peakpicker_consecutive_peaks(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. / 2 - vec[a+1] = 1000. - vec[a+3] = 1000. - vec[a+4] = 1000. / 2 - self.peaks = [a] - - def test_peakpicker_distant_peaks(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. - vec[a+7] = 1000. - self.peaks = [a, a+7] - - def test_peakpicker_very_distant_peaks(self): - vec = self.vec; o = self.o - a = 345 - vec[a] = 1000. - vec[a+67] = 1000. - self.peaks = [a, a+67] - - def tearDown(self): - fpeaks = [] - for index in range(0,buf_size-slice_size): - sliced = self.vec[index:index+slice_size] - findex = self.o(sliced) - if findex: - # we found a peak - fpeak = index - findex - delay - #print self.peaks, index, '-', findex, '-', delay, '=', fpeak - if not round(index - findex - delay) in self.peaks: - self.fail('missing peak ' + str(fpeak)) - fpeaks.append(fpeak) - if len(fpeaks) != len(self.peaks): - self.fail('some peaks of ' + str(self.peaks) + 'were not found, got only ' + str(fpeaks)) - #print - #print fpeaks, self.peaks - -if __name__ == '__main__': - from unittest import main - main() diff --git a/python/test_phasevoc.py b/python/test_phasevoc.py deleted file mode 100755 index 5dd97866..00000000 --- a/python/test_phasevoc.py +++ /dev/null @@ -1,65 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal -from aubio import fvec, cvec, pvoc -from numpy import array, shape - -class aubio_pvoc_test_case(TestCase): - - def test_members(self): - f = pvoc() - assert_equal ([f.win_s, f.hop_s], [1024, 512]) - f = pvoc(2048, 128) - assert_equal ([f.win_s, f.hop_s], [2048, 128]) - - def test_zeros(self): - win_s, hop_s = 1024, 256 - f = pvoc (win_s, hop_s) - t = fvec (hop_s) - for time in range( 4 * win_s / hop_s ): - s = f(t) - r = f.rdo(s) - assert_equal ( array(t), 0) - assert_equal ( s.norm, 0) - assert_equal ( s.phas, 0) - assert_equal ( r, 0) - - def test_steps_two_channels(self): - """ check the resynthesis of steps is correct """ - f = pvoc(1024, 512) - t1 = fvec(512) - t2 = fvec(512) - # positive step in first channel - t1[100:200] = .1 - # positive step in second channel - t1[20:50] = -.1 - s1 = f(t1) - r1 = f.rdo(s1) - s2 = f(t2) - r2 = f.rdo(s2) - #self.plot_this ( s1.norm.T ) - assert_almost_equal ( t1, r2, decimal = 6 ) - - def test_steps_three_random_channels(self): - from random import random - f = pvoc(64, 16) - t0 = fvec(16) - t1 = fvec(16) - for i in xrange(16): - t1[i] = random() * 2. - 1. - t2 = f.rdo(f(t1)) - t2 = f.rdo(f(t0)) - t2 = f.rdo(f(t0)) - t2 = f.rdo(f(t0)) - assert_almost_equal( t1, t2, decimal = 6 ) - - def plot_this( self, this ): - from pylab import semilogy, show - semilogy ( this ) - show () - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_pitch.py b/python/test_pitch.py deleted file mode 100755 index a0dd3a59..00000000 --- a/python/test_pitch.py +++ /dev/null @@ -1,101 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase -from numpy.testing import assert_equal, assert_almost_equal -from numpy import random, sin, arange, mean, median -from math import pi -from aubio import fvec, pitch - -class aubio_mathutils_test_case(TestCase): - - def test_members(self): - p = pitch() - assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], - ['default', 1024, 512, 44100]) - - def test_members_not_default(self): - p = pitch('mcomb', 2048, 512, 32000) - assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], - ['mcomb', 2048, 512, 32000]) - - def test_run_on_zeros(self): - p = pitch('mcomb', 2048, 512, 32000) - f = fvec (512) - assert_equal ( p(f), 0. ) - - def test_run_on_ones(self): - p = pitch('mcomb', 2048, 512, 32000) - f = fvec (512) - f[:] = 1 - assert( p(f) != 0. ) - - def test_run_default_on_sinusoid(self): - method = 'default' - buf_size = 2048 - hop_size = 512 - samplerate = 32000 - freq = 450. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def test_run_schmitt_on_sinusoid(self): - method = 'schmitt' - buf_size = 4096 - hop_size = 512 - samplerate = 44100 - freq = 800. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def test_run_mcomb_on_sinusoid(self): - method = 'mcomb' - buf_size = 2048 - hop_size = 512 - samplerate = 44100 - freq = 10000. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def test_run_fcomb_on_sinusoid(self): - method = 'fcomb' - buf_size = 2048 - hop_size = 512 - samplerate = 32000 - freq = 440. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def test_run_yin_on_sinusoid(self): - method = 'yin' - buf_size = 4096 - hop_size = 512 - samplerate = 32000 - freq = 880. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def test_run_yinfft_on_sinusoid(self): - method = 'yinfft' - buf_size = 2048 - hop_size = 512 - samplerate = 32000 - freq = 640. - self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) - - def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq): - p = pitch(method, buf_size, hop_size, samplerate) - sinvec = self.build_sinusoid(hop_size * 100, freq, samplerate) - self.run_pitch(p, sinvec, freq) - - def build_sinusoid(self, length, freq, samplerate): - return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate) - - def run_pitch(self, p, input_vec, freq): - count = 0 - pitches, errors = [], [] - for vec_slice in input_vec.reshape((-1, p.hop_size)): - pitch = p(vec_slice) - pitches.append(pitch) - errors.append(1. - pitch / freq) - # check that the mean of all relative errors is less than 10% - assert_almost_equal (mean(errors), 0., decimal = 2) - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_source.py b/python/test_source.py deleted file mode 100755 index ae3b79bb..00000000 --- a/python/test_source.py +++ /dev/null @@ -1,27 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, assert_equal, assert_almost_equal -from aubio import fvec, source -from numpy import array - -path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav" - -class aubio_filter_test_case(TestCase): - - def test_members(self): - f = source(path) - print dir(f) - - def test_read(self): - f = source(path) - total_frames = 0 - while True: - vec, read = f() - total_frames += read - if read < f.hop_size: break - print "read", total_frames / float(f.samplerate), " seconds from", path - -if __name__ == '__main__': - from unittest import main - main() - diff --git a/python/test_specdesc.py b/python/test_specdesc.py deleted file mode 100755 index 675858d2..00000000 --- a/python/test_specdesc.py +++ /dev/null @@ -1,238 +0,0 @@ -#! /usr/bin/env python - -from numpy.testing import TestCase, assert_equal, assert_almost_equal -from numpy import random, arange, log, zeros -from aubio import specdesc, cvec -from math import pi - -methods = ["default", - "energy", - "hfc", - "complex", - "phase", - "specdiff", - "kl", - "mkl", - "specflux", - "centroid", - "spread", - "skewness", - "kurtosis", - "slope", - "decrease", - "rolloff"] -buf_size = 2048 - -class aubio_specdesc(TestCase): - - def test_members(self): - o = specdesc() - - for method in methods: - o = specdesc(method, buf_size) - assert_equal ([o.buf_size, o.method], [buf_size, method]) - - spec = cvec(buf_size) - spec.norm[0] = 1 - spec.norm[1] = 1./2. - #print "%20s" % method, str(o(spec)) - o(spec) - spec.norm = random.random_sample((len(spec.norm),)).astype('float32') - spec.phas = random.random_sample((len(spec.phas),)).astype('float32') - #print "%20s" % method, str(o(spec)) - assert (o(spec) != 0.) - - def test_hfc(self): - o = specdesc("hfc", buf_size) - spec = cvec(buf_size) - # hfc of zeros is zero - assert_equal (o(spec), 0.) - # hfc of ones is sum of all bin numbers - spec.norm[:] = 1 - expected = sum(range(buf_size/2 + 2)) - assert_equal (o(spec), expected) - # changing phase doesn't change anything - spec.phas[:] = 1 - assert_equal (o(spec), sum(range(buf_size/2 + 2))) - - def test_phase(self): - o = specdesc("phase", buf_size) - spec = cvec(buf_size) - # phase of zeros is zero - assert_equal (o(spec), 0.) - spec.phas = random.random_sample((len(spec.phas),)).astype('float32') - # phase of random is not zero - spec.norm[:] = 1 - assert (o(spec) != 0.) - - def test_specdiff(self): - o = specdesc("phase", buf_size) - spec = cvec(buf_size) - # specdiff of zeros is zero - assert_equal (o(spec), 0.) - spec.phas = random.random_sample((len(spec.phas),)).astype('float32') - # phase of random is not zero - spec.norm[:] = 1 - assert (o(spec) != 0.) - - def test_hfc(self): - o = specdesc("hfc") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - assert_equal (a, c.norm) - assert_equal ( sum(a*(a+1)), o(c)) - - def test_complex(self): - o = specdesc("complex") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - assert_equal (a, c.norm) - # the previous run was on zeros, so previous frames are still 0 - # so we have sqrt ( abs ( r2 ^ 2) ) == r2 - assert_equal ( sum(a), o(c)) - # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0 - assert_equal ( 0, o(c)) - - def test_kl(self): - o = specdesc("kl") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - assert_almost_equal( sum(a * log(1.+ a/1.e-10 ) ) / o(c), 1., decimal=6) - - def test_mkl(self): - o = specdesc("mkl") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - assert_almost_equal( sum(log(1.+ a/1.e-10 ) ) / o(c), 1, decimal=6) - - def test_specflux(self): - o = specdesc("specflux") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - assert_equal( sum(a), o(c)) - assert_equal( 0, o(c)) - c.norm = zeros(c.length, dtype='float32') - assert_equal( 0, o(c)) - - def test_centroid(self): - o = specdesc("centroid") - c = cvec() - # make sure centroid of zeros is zero - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - centroid = sum(a*a) / sum(a) - assert_almost_equal (centroid, o(c), decimal = 2) - - c.norm = a * .5 - assert_almost_equal (centroid, o(c), decimal = 2) - - def test_spread(self): - o = specdesc("spread") - c = cvec(2048) - ramp = arange(c.length, dtype='float32') - assert_equal( 0., o(c)) - - a = ramp - c.norm = a - centroid = sum(a*a) / sum(a) - spread = sum( a * pow(ramp - centroid, 2.) ) / sum(a) - assert_almost_equal (o(c), spread, decimal = 1) - - def test_skewness(self): - o = specdesc("skewness") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - centroid = sum(a*a) / sum(a) - spread = sum( (a - centroid)**2 *a) / sum(a) - skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5 - assert_almost_equal (skewness, o(c), decimal = 2) - - c.norm = a * 3 - assert_almost_equal (skewness, o(c), decimal = 2) - - def test_kurtosis(self): - o = specdesc("kurtosis") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length, dtype='float32') - c.norm = a - centroid = sum(a*a) / sum(a) - spread = sum( (a - centroid)**2 *a) / sum(a) - kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2 - assert_almost_equal (kurtosis, o(c), decimal = 2) - - def test_slope(self): - o = specdesc("slope") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length * 2, 0, -2, dtype='float32') - k = arange(c.length, dtype='float32') - c.norm = a - num = len(a) * sum(k*a) - sum(k)*sum(a) - den = (len(a) * sum(k**2) - sum(k)**2) - slope = num/den/sum(a) - assert_almost_equal (slope, o(c), decimal = 5) - - a = arange(0, c.length * 2, +2, dtype='float32') - c.norm = a - num = len(a) * sum(k*a) - sum(k)*sum(a) - den = (len(a) * sum(k**2) - sum(k)**2) - slope = num/den/sum(a) - assert_almost_equal (slope, o(c), decimal = 5) - - a = arange(0, c.length * 2, +2, dtype='float32') - c.norm = a * 2 - assert_almost_equal (slope, o(c), decimal = 5) - - def test_decrease(self): - o = specdesc("decrease") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length * 2, 0, -2, dtype='float32') - k = arange(c.length, dtype='float32') - c.norm = a - decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) - assert_almost_equal (decrease, o(c), decimal = 5) - - a = arange(0, c.length * 2, +2, dtype='float32') - c.norm = a - decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) - assert_almost_equal (decrease, o(c), decimal = 5) - - a = arange(0, c.length * 2, +2, dtype='float32') - c.norm = a * 2 - decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) - assert_almost_equal (decrease, o(c), decimal = 5) - - def test_rolloff(self): - o = specdesc("rolloff") - c = cvec() - assert_equal( 0., o(c)) - a = arange(c.length * 2, 0, -2, dtype='float32') - k = arange(c.length, dtype='float32') - c.norm = a - cumsum = .95*sum(a*a) - i = 0; rollsum = 0 - while rollsum < cumsum: - rollsum += a[i]*a[i] - i+=1 - rolloff = i - assert_equal (rolloff, o(c)) - - -if __name__ == '__main__': - from unittest import main - main() diff --git a/python/tests/test_aubio.py b/python/tests/test_aubio.py new file mode 100755 index 00000000..951ade98 --- /dev/null +++ b/python/tests/test_aubio.py @@ -0,0 +1,14 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite + +class aubiomodule_test_case(TestCase): + + def test_import(self): + """ try importing aubio """ + import aubio + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_cvec.py b/python/tests/test_cvec.py new file mode 100755 index 00000000..86916464 --- /dev/null +++ b/python/tests/test_cvec.py @@ -0,0 +1,50 @@ +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from aubio import cvec +from numpy import array, shape, pi + +class aubio_cvec_test_case(TestCase): + + def test_vector_created_with_zeroes(self): + a = cvec(10) + a + shape(a.norm) + shape(a.phas) + a.norm[0] + assert_equal(a.norm, 0.) + assert_equal(a.phas, 0.) + + def test_vector_assign_element(self): + a = cvec() + a.norm[0] = 1 + assert_equal(a.norm[0], 1) + a.phas[0] = 1 + assert_equal(a.phas[0], 1) + + def test_vector_assign_element_end(self): + a = cvec() + a.norm[-1] = 1 + assert_equal(a.norm[-1], 1) + assert_equal(a.norm[len(a.norm)-1], 1) + a.phas[-1] = 1 + assert_equal(a.phas[-1], 1) + assert_equal(a.phas[len(a.phas)-1], 1) + + def test_assign_cvec_norm_slice(self): + spec = cvec(1024) + spec.norm[40:100] = 100 + assert_equal (spec.norm[0:40], 0) + assert_equal (spec.norm[40:100], 100) + assert_equal (spec.norm[100:-1], 0) + assert_equal (spec.phas, 0) + + def test_assign_cvec_phas_slice(self): + spec = cvec(1024) + spec.phas[39:-1] = -pi + assert_equal (spec.phas[0:39], 0) + assert_equal (spec.phas[39:-1], -pi) + assert_equal (spec.norm, 0) + +if __name__ == '__main__': + from unittest import main + main() diff --git a/python/tests/test_fft.py b/python/tests/test_fft.py new file mode 100755 index 00000000..ccb6e4cf --- /dev/null +++ b/python/tests/test_fft.py @@ -0,0 +1,113 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +# WARNING: numpy also has an fft object +from aubio import fvec, fft, cvec +from numpy import array, shape +from math import pi + +class aubio_fft_test_case(TestCase): + + def test_members(self): + f = fft() + assert_equal (f.win_s, 1024) + + def test_output_dimensions(self): + """ check the dimensions of output """ + win_s = 1024 + timegrain = fvec(win_s) + f = fft(win_s) + fftgrain = f (timegrain) + assert_equal (fftgrain.norm, 0) + assert_equal (shape(fftgrain.norm), (win_s/2+1,)) + assert_equal (fftgrain.phas, 0) + assert_equal (shape(fftgrain.phas), (win_s/2+1,)) + + def test_zeros(self): + """ check the transform of zeros """ + win_s = 512 + timegrain = fvec(win_s) + f = fft(win_s) + fftgrain = f(timegrain) + assert_equal ( fftgrain.norm == 0, True ) + assert_equal ( fftgrain.phas == 0, True ) + + def test_impulse(self): + """ check the transform of one impulse at a random place """ + from random import random + from math import floor + win_s = 256 + i = floor(random()*win_s) + impulse = pi * random() + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_almost_equal ( fftgrain.norm, impulse, decimal = 6 ) + assert_equal ( fftgrain.phas <= pi, True) + assert_equal ( fftgrain.phas >= -pi, True) + + def test_impulse_negative(self): + """ check the transform of one impulse at a random place """ + from random import random + from math import floor + win_s = 256 + i = 0 + impulse = -10. + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[i] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_almost_equal ( fftgrain.norm, abs(impulse), decimal = 6 ) + if impulse < 0: + # phase can be pi or -pi, as it is not unwrapped + assert_almost_equal ( abs(fftgrain.phas[1:-1]) , pi, decimal = 6 ) + assert_almost_equal ( fftgrain.phas[0], pi, decimal = 6) + assert_almost_equal ( fftgrain.phas[-1], pi, decimal = 6) + else: + assert_equal ( fftgrain.phas[1:-1] == 0, True) + assert_equal ( fftgrain.phas[0] == 0, True) + assert_equal ( fftgrain.phas[-1] == 0, True) + # now check the resynthesis + synthgrain = f.rdo ( fftgrain ) + #self.plot_this ( fftgrain.phas.T ) + assert_equal ( fftgrain.phas <= pi, True) + assert_equal ( fftgrain.phas >= -pi, True) + #self.plot_this ( synthgrain - timegrain ) + assert_almost_equal ( synthgrain, timegrain, decimal = 6 ) + + def test_impulse_at_zero(self): + """ check the transform of one impulse at a index 0 """ + win_s = 1024 + impulse = pi + f = fft(win_s) + timegrain = fvec(win_s) + timegrain[0] = impulse + fftgrain = f ( timegrain ) + #self.plot_this ( fftgrain.phas ) + assert_equal ( fftgrain.phas[0], 0) + # could be 0 or -0 depending on fft implementation (0 for fftw3, -0 for ooura) + assert_almost_equal ( fftgrain.phas[1], 0) + assert_almost_equal ( fftgrain.norm[0], impulse, decimal = 6 ) + + def test_rdo_before_do(self): + """ check running fft.rdo before fft.do works """ + win_s = 1024 + impulse = pi + f = fft(win_s) + fftgrain = cvec(win_s) + t = f.rdo( fftgrain ) + assert_equal ( t, 0 ) + + def plot_this(self, this): + from pylab import plot, show + plot ( this ) + show () + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_filter.py b/python/tests/test_filter.py new file mode 100755 index 00000000..14a880d0 --- /dev/null +++ b/python/tests/test_filter.py @@ -0,0 +1,68 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, assert_equal, assert_almost_equal +from aubio import fvec, digital_filter +from numpy import array + +def array_from_text_file(filename, dtype = 'float'): + return array([line.split() for line in open(filename).readlines()], + dtype = dtype) + +class aubio_filter_test_case(TestCase): + + def test_members(self): + f = digital_filter() + assert_equal (f.order, 7) + f = digital_filter(5) + assert_equal (f.order, 5) + f(fvec()) + + def test_cweighting_error(self): + f = digital_filter (2) + self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) + f = digital_filter (8) + self.assertRaises ( ValueError, f.set_c_weighting, 44100 ) + f = digital_filter (5) + self.assertRaises ( ValueError, f.set_c_weighting, 4000 ) + f = digital_filter (5) + self.assertRaises ( ValueError, f.set_c_weighting, 193000 ) + f = digital_filter (7) + self.assertRaises ( ValueError, f.set_a_weighting, 193000 ) + f = digital_filter (5) + self.assertRaises ( ValueError, f.set_a_weighting, 192000 ) + + def test_c_weighting(self): + expected = array_from_text_file('c_weighting_test_simple.expected') + f = digital_filter(5) + f.set_c_weighting(44100) + v = fvec(32) + v[12] = .5 + u = f(v) + assert_almost_equal (expected[1], u) + + def test_a_weighting(self): + expected = array_from_text_file('a_weighting_test_simple.expected') + f = digital_filter(7) + f.set_a_weighting(44100) + v = fvec(32) + v[12] = .5 + u = f(v) + assert_almost_equal (expected[1], u) + + def test_a_weighting_parted(self): + expected = array_from_text_file('a_weighting_test_simple.expected') + f = digital_filter(7) + f.set_a_weighting(44100) + v = fvec(16) + v[12] = .5 + u = f(v) + assert_almost_equal (expected[1][:16], u) + # one more time + v = fvec(16) + u = f(v) + assert_almost_equal (expected[1][16:], u) + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_filterbank.py b/python/tests/test_filterbank.py new file mode 100755 index 00000000..c00a5685 --- /dev/null +++ b/python/tests/test_filterbank.py @@ -0,0 +1,23 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from numpy import random +from aubio import cvec, filterbank + +class aubio_filterbank_test_case(TestCase): + + def test_members(self): + f = filterbank(40, 512) + assert_equal ([f.n_filters, f.win_s], [40, 512]) + + def test_set_coeffs(self): + f = filterbank(40, 512) + r = random.random([40, 512 / 2 + 1]).astype('float32') + f.set_coeffs(r) + assert_equal (r, f.get_coeffs()) + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_filterbank_mel.py b/python/tests/test_filterbank_mel.py new file mode 100755 index 00000000..532ca0b3 --- /dev/null +++ b/python/tests/test_filterbank_mel.py @@ -0,0 +1,51 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from numpy import array, shape +from aubio import cvec, filterbank + +class aubio_filterbank_mel_test_case(TestCase): + + def test_slaney(self): + f = filterbank(40, 512) + f.set_mel_coeffs_slaney(16000) + a = f.get_coeffs() + assert_equal(shape (a), (40, 512/2 + 1) ) + + def test_other_slaney(self): + f = filterbank(40, 512*2) + f.set_mel_coeffs_slaney(44100) + a = f.get_coeffs() + #print "sum is", sum(sum(a)) + for win_s in [256, 512, 1024, 2048, 4096]: + f = filterbank(40, win_s) + f.set_mel_coeffs_slaney(320000) + a = f.get_coeffs() + #print "sum is", sum(sum(a)) + + def test_triangle_freqs_zeros(self): + f = filterbank(9, 1024) + freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] + freqs = array(freq_list, dtype = 'float32') + f.set_triangle_bands(freqs, 48000) + f.get_coeffs().T + assert_equal ( f(cvec(1024)), 0) + + def test_triangle_freqs_ones(self): + f = filterbank(9, 1024) + freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] + freqs = array(freq_list, dtype = 'float32') + f.set_triangle_bands(freqs, 48000) + f.get_coeffs().T + spec = cvec(1024) + spec.norm[:] = 1 + assert_almost_equal ( f(spec), + [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, + 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) + +if __name__ == '__main__': + from unittest import main + main() + + diff --git a/python/tests/test_fvec.py b/python/tests/test_fvec.py new file mode 100755 index 00000000..643c6f93 --- /dev/null +++ b/python/tests/test_fvec.py @@ -0,0 +1,135 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal +from numpy import array, shape + +class aubio_fvec_test_case(TestCase): + + def test_vector_created_with_zeroes(self): + a = fvec(10) + a + shape(a) + a[0] + #del a + assert_equal(array(a), 0.) + + def test_vector_create_with_list(self): + a = fvec([0,1,2,3]) + assert_equal (range(4), a) + + def test_vector_assign_element(self): + a = fvec() + a[0] = 1 + assert_equal(a[0], 1) + + def test_vector_assign_element_end(self): + a = fvec() + a[-1] = 1 + assert_equal(a[-1], 1) + assert_equal(a[len(a)-1], 1) + + def test_vector(self): + a = fvec() + a, len(a) #a.length + a[0] + array(a) + a = fvec(10) + a = fvec(1) + a.T + array(a).T + a = range(len(a)) + + def test_wrong_values(self): + self.assertRaises (ValueError, fvec, -10) + + a = fvec(2) + self.assertRaises (IndexError, a.__getitem__, 3) + self.assertRaises (IndexError, a.__getitem__, 2) + + def test_alpha_norm_of_fvec(self): + a = fvec(2) + self.assertEquals (alpha_norm(a, 1), 0) + a[0] = 1 + self.assertEquals (alpha_norm(a, 1), 0.5) + a[1] = 1 + self.assertEquals (alpha_norm(a, 1), 1) + a = array([0, 1], dtype='float32') + from math import sqrt + assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) + + def test_alpha_norm_of_none(self): + self.assertRaises (ValueError, alpha_norm, None, 1) + + def test_alpha_norm_of_array_of_float32(self): + # check scalar fails + a = array(1, dtype = 'float32') + self.assertRaises (ValueError, alpha_norm, a, 1) + # check 2d array fails + a = array([[2],[4]], dtype = 'float32') + self.assertRaises (ValueError, alpha_norm, a, 1) + # check 1d array + a = array(range(10), dtype = 'float32') + self.assertEquals (alpha_norm(a, 1), 4.5) + + def test_alpha_norm_of_array_of_int(self): + a = array(1, dtype = 'int') + self.assertRaises (ValueError, alpha_norm, a, 1) + a = array([[[1,2],[3,4]]], dtype = 'int') + self.assertRaises (ValueError, alpha_norm, a, 1) + a = array(range(10), dtype = 'int') + self.assertRaises (ValueError, alpha_norm, a, 1) + + def test_alpha_norm_of_array_of_string (self): + a = "hello" + self.assertRaises (ValueError, alpha_norm, a, 1) + + def test_zero_crossing_rate(self): + a = array([0,1,-1], dtype='float32') + assert_almost_equal (zero_crossing_rate(a), 1./3. ) + a = array([0.]*100, dtype='float32') + self.assertEquals (zero_crossing_rate(a), 0 ) + a = array([-1.]*100, dtype='float32') + self.assertEquals (zero_crossing_rate(a), 0 ) + a = array([1.]*100, dtype='float32') + self.assertEquals (zero_crossing_rate(a), 0 ) + + def test_alpha_norm_of_array_of_float64(self): + # check scalar fail + a = array(1, dtype = 'float64') + self.assertRaises (ValueError, alpha_norm, a, 1) + # check 3d array fail + a = array([[[1,2],[3,4]]], dtype = 'float64') + self.assertRaises (ValueError, alpha_norm, a, 1) + # check float64 1d array fail + a = array(range(10), dtype = 'float64') + self.assertRaises (ValueError, alpha_norm, a, 1) + # check float64 2d array fail + a = array([range(10), range(10)], dtype = 'float64') + self.assertRaises (ValueError, alpha_norm, a, 1) + + def test_fvec_min_removal_of_array(self): + a = array([20,1,19], dtype='float32') + b = min_removal(a) + assert_equal (array(b), [19, 0, 18]) + assert_equal (b, [19, 0, 18]) + assert_equal (a, b) + a[0] = 0 + assert_equal (a, b) + + def test_fvec_min_removal_of_array_float64(self): + a = array([20,1,19], dtype='float64') + self.assertRaises (ValueError, min_removal, a) + + def test_fvec_min_removal_of_fvec(self): + a = fvec(3) + a = array([20, 1, 19], dtype = 'float32') + b = min_removal(a) + assert_equal (array(b), [19, 0, 18]) + assert_equal (b, [19, 0, 18]) + assert_equal (a, b) + +if __name__ == '__main__': + from unittest import main + main() diff --git a/python/tests/test_onset.py b/python/tests/test_onset.py new file mode 100755 index 00000000..c6642d51 --- /dev/null +++ b/python/tests/test_onset.py @@ -0,0 +1,20 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +# WARNING: numpy also has an fft object +from aubio import onset, cvec +from numpy import array, shape, arange, zeros, log +from math import pi + +class aubio_onset(TestCase): + + def test_members(self): + o = onset() + assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate], + [1024,512,'default',44100]) + + +if __name__ == '__main__': + from unittest import main + main() diff --git a/python/tests/test_peakpicker.py b/python/tests/test_peakpicker.py new file mode 100755 index 00000000..08313d12 --- /dev/null +++ b/python/tests/test_peakpicker.py @@ -0,0 +1,115 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, assert_equal, assert_almost_equal +from aubio import peakpicker, fvec + +class aubio_peakpicker(TestCase): + + def test_members(self): + o = peakpicker() + + def test_peakpicker_zeroes(self): + o = peakpicker() + assert_equal(o.get_thresholded_input(), 0.) + + def test_peakpick_set_threshold(self): + o = peakpicker() + new_threshold = threshold + o.set_threshold(new_threshold) + assert_almost_equal(new_threshold, o.get_threshold()) + + def test_peakpicker_get_threshold(self): + o = peakpicker() + new_threshold = o.get_threshold() + o.set_threshold(new_threshold) + assert_equal(new_threshold, o.get_threshold()) + +buf_size = 1024 +slice_size = 5 +delay = 1 +threshold = .9 + +class aubio_peakpicker_peaks(TestCase): + + def setUp(self): + self.o = peakpicker() + self.o.set_threshold (threshold) + self.vec = fvec(buf_size) + + def test_peakpicker_impulse(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. + self.peaks = [a] + + def test_peakpicker_ramp_up(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. / 4. * 1. + vec[a+1] = 1000. / 4. * 2. + vec[a+2] = 1000. / 4. * 3. + vec[a+3] = 1000. + self.peaks = [a+1] + + def test_peakpicker_ramp_down(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. + vec[a+1] = 1000. / 4. * 3. + vec[a+2] = 1000. / 4. * 2. + vec[a+3] = 1000. / 4. * 1. + self.peaks = [a] + + def test_peakpicker_plateau(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. / 2 + vec[a+1] = 1000. + vec[a+2] = 1000. + vec[a+3] = 1000. + vec[a+4] = 1000. / 2 + self.peaks = [a+1] + + def test_peakpicker_consecutive_peaks(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. / 2 + vec[a+1] = 1000. + vec[a+3] = 1000. + vec[a+4] = 1000. / 2 + self.peaks = [a] + + def test_peakpicker_distant_peaks(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. + vec[a+7] = 1000. + self.peaks = [a, a+7] + + def test_peakpicker_very_distant_peaks(self): + vec = self.vec; o = self.o + a = 345 + vec[a] = 1000. + vec[a+67] = 1000. + self.peaks = [a, a+67] + + def tearDown(self): + fpeaks = [] + for index in range(0,buf_size-slice_size): + sliced = self.vec[index:index+slice_size] + findex = self.o(sliced) + if findex: + # we found a peak + fpeak = index - findex - delay + #print self.peaks, index, '-', findex, '-', delay, '=', fpeak + if not round(index - findex - delay) in self.peaks: + self.fail('missing peak ' + str(fpeak)) + fpeaks.append(fpeak) + if len(fpeaks) != len(self.peaks): + self.fail('some peaks of ' + str(self.peaks) + 'were not found, got only ' + str(fpeaks)) + #print + #print fpeaks, self.peaks + +if __name__ == '__main__': + from unittest import main + main() diff --git a/python/tests/test_phasevoc.py b/python/tests/test_phasevoc.py new file mode 100755 index 00000000..5dd97866 --- /dev/null +++ b/python/tests/test_phasevoc.py @@ -0,0 +1,65 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from aubio import fvec, cvec, pvoc +from numpy import array, shape + +class aubio_pvoc_test_case(TestCase): + + def test_members(self): + f = pvoc() + assert_equal ([f.win_s, f.hop_s], [1024, 512]) + f = pvoc(2048, 128) + assert_equal ([f.win_s, f.hop_s], [2048, 128]) + + def test_zeros(self): + win_s, hop_s = 1024, 256 + f = pvoc (win_s, hop_s) + t = fvec (hop_s) + for time in range( 4 * win_s / hop_s ): + s = f(t) + r = f.rdo(s) + assert_equal ( array(t), 0) + assert_equal ( s.norm, 0) + assert_equal ( s.phas, 0) + assert_equal ( r, 0) + + def test_steps_two_channels(self): + """ check the resynthesis of steps is correct """ + f = pvoc(1024, 512) + t1 = fvec(512) + t2 = fvec(512) + # positive step in first channel + t1[100:200] = .1 + # positive step in second channel + t1[20:50] = -.1 + s1 = f(t1) + r1 = f.rdo(s1) + s2 = f(t2) + r2 = f.rdo(s2) + #self.plot_this ( s1.norm.T ) + assert_almost_equal ( t1, r2, decimal = 6 ) + + def test_steps_three_random_channels(self): + from random import random + f = pvoc(64, 16) + t0 = fvec(16) + t1 = fvec(16) + for i in xrange(16): + t1[i] = random() * 2. - 1. + t2 = f.rdo(f(t1)) + t2 = f.rdo(f(t0)) + t2 = f.rdo(f(t0)) + t2 = f.rdo(f(t0)) + assert_almost_equal( t1, t2, decimal = 6 ) + + def plot_this( self, this ): + from pylab import semilogy, show + semilogy ( this ) + show () + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_pitch.py b/python/tests/test_pitch.py new file mode 100755 index 00000000..a0dd3a59 --- /dev/null +++ b/python/tests/test_pitch.py @@ -0,0 +1,101 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase +from numpy.testing import assert_equal, assert_almost_equal +from numpy import random, sin, arange, mean, median +from math import pi +from aubio import fvec, pitch + +class aubio_mathutils_test_case(TestCase): + + def test_members(self): + p = pitch() + assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], + ['default', 1024, 512, 44100]) + + def test_members_not_default(self): + p = pitch('mcomb', 2048, 512, 32000) + assert_equal ( [p.method, p.buf_size, p.hop_size, p.samplerate], + ['mcomb', 2048, 512, 32000]) + + def test_run_on_zeros(self): + p = pitch('mcomb', 2048, 512, 32000) + f = fvec (512) + assert_equal ( p(f), 0. ) + + def test_run_on_ones(self): + p = pitch('mcomb', 2048, 512, 32000) + f = fvec (512) + f[:] = 1 + assert( p(f) != 0. ) + + def test_run_default_on_sinusoid(self): + method = 'default' + buf_size = 2048 + hop_size = 512 + samplerate = 32000 + freq = 450. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def test_run_schmitt_on_sinusoid(self): + method = 'schmitt' + buf_size = 4096 + hop_size = 512 + samplerate = 44100 + freq = 800. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def test_run_mcomb_on_sinusoid(self): + method = 'mcomb' + buf_size = 2048 + hop_size = 512 + samplerate = 44100 + freq = 10000. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def test_run_fcomb_on_sinusoid(self): + method = 'fcomb' + buf_size = 2048 + hop_size = 512 + samplerate = 32000 + freq = 440. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def test_run_yin_on_sinusoid(self): + method = 'yin' + buf_size = 4096 + hop_size = 512 + samplerate = 32000 + freq = 880. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def test_run_yinfft_on_sinusoid(self): + method = 'yinfft' + buf_size = 2048 + hop_size = 512 + samplerate = 32000 + freq = 640. + self.run_pitch_on_sinusoid(method, buf_size, hop_size, samplerate, freq) + + def run_pitch_on_sinusoid(self, method, buf_size, hop_size, samplerate, freq): + p = pitch(method, buf_size, hop_size, samplerate) + sinvec = self.build_sinusoid(hop_size * 100, freq, samplerate) + self.run_pitch(p, sinvec, freq) + + def build_sinusoid(self, length, freq, samplerate): + return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate) + + def run_pitch(self, p, input_vec, freq): + count = 0 + pitches, errors = [], [] + for vec_slice in input_vec.reshape((-1, p.hop_size)): + pitch = p(vec_slice) + pitches.append(pitch) + errors.append(1. - pitch / freq) + # check that the mean of all relative errors is less than 10% + assert_almost_equal (mean(errors), 0., decimal = 2) + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_source.py b/python/tests/test_source.py new file mode 100755 index 00000000..ae3b79bb --- /dev/null +++ b/python/tests/test_source.py @@ -0,0 +1,27 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, assert_equal, assert_almost_equal +from aubio import fvec, source +from numpy import array + +path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav" + +class aubio_filter_test_case(TestCase): + + def test_members(self): + f = source(path) + print dir(f) + + def test_read(self): + f = source(path) + total_frames = 0 + while True: + vec, read = f() + total_frames += read + if read < f.hop_size: break + print "read", total_frames / float(f.samplerate), " seconds from", path + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_specdesc.py b/python/tests/test_specdesc.py new file mode 100755 index 00000000..675858d2 --- /dev/null +++ b/python/tests/test_specdesc.py @@ -0,0 +1,238 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, assert_equal, assert_almost_equal +from numpy import random, arange, log, zeros +from aubio import specdesc, cvec +from math import pi + +methods = ["default", + "energy", + "hfc", + "complex", + "phase", + "specdiff", + "kl", + "mkl", + "specflux", + "centroid", + "spread", + "skewness", + "kurtosis", + "slope", + "decrease", + "rolloff"] +buf_size = 2048 + +class aubio_specdesc(TestCase): + + def test_members(self): + o = specdesc() + + for method in methods: + o = specdesc(method, buf_size) + assert_equal ([o.buf_size, o.method], [buf_size, method]) + + spec = cvec(buf_size) + spec.norm[0] = 1 + spec.norm[1] = 1./2. + #print "%20s" % method, str(o(spec)) + o(spec) + spec.norm = random.random_sample((len(spec.norm),)).astype('float32') + spec.phas = random.random_sample((len(spec.phas),)).astype('float32') + #print "%20s" % method, str(o(spec)) + assert (o(spec) != 0.) + + def test_hfc(self): + o = specdesc("hfc", buf_size) + spec = cvec(buf_size) + # hfc of zeros is zero + assert_equal (o(spec), 0.) + # hfc of ones is sum of all bin numbers + spec.norm[:] = 1 + expected = sum(range(buf_size/2 + 2)) + assert_equal (o(spec), expected) + # changing phase doesn't change anything + spec.phas[:] = 1 + assert_equal (o(spec), sum(range(buf_size/2 + 2))) + + def test_phase(self): + o = specdesc("phase", buf_size) + spec = cvec(buf_size) + # phase of zeros is zero + assert_equal (o(spec), 0.) + spec.phas = random.random_sample((len(spec.phas),)).astype('float32') + # phase of random is not zero + spec.norm[:] = 1 + assert (o(spec) != 0.) + + def test_specdiff(self): + o = specdesc("phase", buf_size) + spec = cvec(buf_size) + # specdiff of zeros is zero + assert_equal (o(spec), 0.) + spec.phas = random.random_sample((len(spec.phas),)).astype('float32') + # phase of random is not zero + spec.norm[:] = 1 + assert (o(spec) != 0.) + + def test_hfc(self): + o = specdesc("hfc") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + assert_equal (a, c.norm) + assert_equal ( sum(a*(a+1)), o(c)) + + def test_complex(self): + o = specdesc("complex") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + assert_equal (a, c.norm) + # the previous run was on zeros, so previous frames are still 0 + # so we have sqrt ( abs ( r2 ^ 2) ) == r2 + assert_equal ( sum(a), o(c)) + # second time. c.norm = a, so, r1 = r2, and the euclidian distance is 0 + assert_equal ( 0, o(c)) + + def test_kl(self): + o = specdesc("kl") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + assert_almost_equal( sum(a * log(1.+ a/1.e-10 ) ) / o(c), 1., decimal=6) + + def test_mkl(self): + o = specdesc("mkl") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + assert_almost_equal( sum(log(1.+ a/1.e-10 ) ) / o(c), 1, decimal=6) + + def test_specflux(self): + o = specdesc("specflux") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + assert_equal( sum(a), o(c)) + assert_equal( 0, o(c)) + c.norm = zeros(c.length, dtype='float32') + assert_equal( 0, o(c)) + + def test_centroid(self): + o = specdesc("centroid") + c = cvec() + # make sure centroid of zeros is zero + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + centroid = sum(a*a) / sum(a) + assert_almost_equal (centroid, o(c), decimal = 2) + + c.norm = a * .5 + assert_almost_equal (centroid, o(c), decimal = 2) + + def test_spread(self): + o = specdesc("spread") + c = cvec(2048) + ramp = arange(c.length, dtype='float32') + assert_equal( 0., o(c)) + + a = ramp + c.norm = a + centroid = sum(a*a) / sum(a) + spread = sum( a * pow(ramp - centroid, 2.) ) / sum(a) + assert_almost_equal (o(c), spread, decimal = 1) + + def test_skewness(self): + o = specdesc("skewness") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + centroid = sum(a*a) / sum(a) + spread = sum( (a - centroid)**2 *a) / sum(a) + skewness = sum( (a - centroid)**3 *a) / sum(a) / spread **1.5 + assert_almost_equal (skewness, o(c), decimal = 2) + + c.norm = a * 3 + assert_almost_equal (skewness, o(c), decimal = 2) + + def test_kurtosis(self): + o = specdesc("kurtosis") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length, dtype='float32') + c.norm = a + centroid = sum(a*a) / sum(a) + spread = sum( (a - centroid)**2 *a) / sum(a) + kurtosis = sum( (a - centroid)**4 *a) / sum(a) / spread **2 + assert_almost_equal (kurtosis, o(c), decimal = 2) + + def test_slope(self): + o = specdesc("slope") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length * 2, 0, -2, dtype='float32') + k = arange(c.length, dtype='float32') + c.norm = a + num = len(a) * sum(k*a) - sum(k)*sum(a) + den = (len(a) * sum(k**2) - sum(k)**2) + slope = num/den/sum(a) + assert_almost_equal (slope, o(c), decimal = 5) + + a = arange(0, c.length * 2, +2, dtype='float32') + c.norm = a + num = len(a) * sum(k*a) - sum(k)*sum(a) + den = (len(a) * sum(k**2) - sum(k)**2) + slope = num/den/sum(a) + assert_almost_equal (slope, o(c), decimal = 5) + + a = arange(0, c.length * 2, +2, dtype='float32') + c.norm = a * 2 + assert_almost_equal (slope, o(c), decimal = 5) + + def test_decrease(self): + o = specdesc("decrease") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length * 2, 0, -2, dtype='float32') + k = arange(c.length, dtype='float32') + c.norm = a + decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) + assert_almost_equal (decrease, o(c), decimal = 5) + + a = arange(0, c.length * 2, +2, dtype='float32') + c.norm = a + decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) + assert_almost_equal (decrease, o(c), decimal = 5) + + a = arange(0, c.length * 2, +2, dtype='float32') + c.norm = a * 2 + decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) + assert_almost_equal (decrease, o(c), decimal = 5) + + def test_rolloff(self): + o = specdesc("rolloff") + c = cvec() + assert_equal( 0., o(c)) + a = arange(c.length * 2, 0, -2, dtype='float32') + k = arange(c.length, dtype='float32') + c.norm = a + cumsum = .95*sum(a*a) + i = 0; rollsum = 0 + while rollsum < cumsum: + rollsum += a[i]*a[i] + i+=1 + rolloff = i + assert_equal (rolloff, o(c)) + + +if __name__ == '__main__': + from unittest import main + main()