From 4120fbc9a4fe29d1d7c2e25be6212014dd790086 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Mon, 16 May 2016 03:16:00 +0200 Subject: [PATCH] python/demos: python3 and double precision compatibility --- python/demos/demo_bpm_extract.py | 12 +++++++++--- python/demos/demo_filterbank.py | 2 +- python/demos/demo_filterbank_slaney.py | 2 +- python/demos/demo_filterbank_triangle_bands.py | 4 ++-- python/demos/demo_mel-energy.py | 10 +++++----- python/demos/demo_mfcc.py | 4 ++-- python/demos/demo_onset.py | 6 +++--- python/demos/demo_onset_plot.py | 13 +++++++------ python/demos/demo_pitch.py | 10 +++++----- python/demos/demo_pitch_sinusoid.py | 7 ++++--- python/demos/demo_pysoundcard_record.py | 4 ++-- python/demos/demo_simple_robot_voice.py | 7 ++++--- python/demos/demo_simple_spectral_weighting.py | 9 +++++---- python/demos/demo_sink.py | 14 ++++++++------ python/demos/demo_sink_create_woodblock.py | 10 +++++----- python/demos/demo_sink_multi.py | 17 +++++++++-------- python/demos/demo_slicing.py | 7 ++++--- python/demos/demo_source.py | 12 +++++++----- python/demos/demo_specdesc.py | 18 +++++++++--------- python/demos/demo_spectrogram.py | 16 ++++++++-------- python/demos/demo_tempo.py | 6 +++--- python/demos/demo_tempo_plot.py | 12 ++++++------ python/demos/demo_tss.py | 6 +++--- python/demos/demo_waveform_plot.py | 4 ++-- 24 files changed, 114 insertions(+), 98 deletions(-) diff --git a/python/demos/demo_bpm_extract.py b/python/demos/demo_bpm_extract.py index e11cea13..7935f428 100755 --- a/python/demos/demo_bpm_extract.py +++ b/python/demos/demo_bpm_extract.py @@ -45,12 +45,18 @@ def get_file_bpm(path, params = None): break # Convert to periods and to bpm - bpms = 60./diff(beats) - b = median(bpms) + if len(beats) > 1: + if len(beats) < 4: + print("few beats found in {:s}".format(path)) + bpms = 60./diff(beats) + b = median(bpms) + else: + b = 0 + print("not enough beats found in {:s}".format(path)) return b if __name__ == '__main__': import sys for f in sys.argv[1:]: bpm = get_file_bpm(f) - print "%6s" % ("%.2f" % bpm), f + print("{:6s} {:s}".format("{:2f}".format(bpm), f)) diff --git a/python/demos/demo_filterbank.py b/python/demos/demo_filterbank.py index 69dc383d..54aff573 100755 --- a/python/demos/demo_filterbank.py +++ b/python/demos/demo_filterbank.py @@ -19,7 +19,7 @@ coeffs[4] *= 5. f.set_coeffs(coeffs) -times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters) +times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters) title('Bank of filters built using a simple list of boundaries\nThe middle band has been amplified by 2.') loglog(times.T, f.get_coeffs().T, '.-') xlim([50, samplerate/2]) diff --git a/python/demos/demo_filterbank_slaney.py b/python/demos/demo_filterbank_slaney.py index 0a42505e..f0e041e2 100755 --- a/python/demos/demo_filterbank_slaney.py +++ b/python/demos/demo_filterbank_slaney.py @@ -11,7 +11,7 @@ f.set_mel_coeffs_slaney(samplerate) from pylab import loglog, title, show, xlim, ylim, xlabel, ylabel xlim([0,samplerate / 2]) -times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * 40) +times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * 40) loglog(times.T, f.get_coeffs().T, '.-') title('Mel frequency bands coefficients') xlim([100, 7500]) diff --git a/python/demos/demo_filterbank_triangle_bands.py b/python/demos/demo_filterbank_triangle_bands.py index 7b02e7d9..c65df93d 100755 --- a/python/demos/demo_filterbank_triangle_bands.py +++ b/python/demos/demo_filterbank_triangle_bands.py @@ -16,7 +16,7 @@ f.set_triangle_bands(freqs, samplerate) subplot(211) title('Examples of filterbank built with set_triangle_bands and set_coeffs') -times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters) +times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters) loglog(times.T, f.get_coeffs().T, '.-') xlim([50, samplerate/2]) ylim([1.0e-6, 2.0e-2]) @@ -37,7 +37,7 @@ coeffs[4] *= 5. f.set_coeffs(coeffs) subplot(212) -times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters) +times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters) loglog(times.T, f.get_coeffs().T, '.-') xlim([50, samplerate/2]) ylim([1.0e-6, 2.0e-2]) diff --git a/python/demos/demo_mel-energy.py b/python/demos/demo_mel-energy.py index d433058c..db27f7e2 100755 --- a/python/demos/demo_mel-energy.py +++ b/python/demos/demo_mel-energy.py @@ -5,10 +5,10 @@ from aubio import source, pvoc, filterbank from numpy import vstack, zeros win_s = 512 # fft size -hop_s = win_s / 4 # hop size +hop_s = win_s // 4 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -34,14 +34,14 @@ while True: samples, read = s() fftgrain = pv(samples) new_energies = f(fftgrain) - print '%f' % (total_frames / float(samplerate) ), - print ' '.join(['%f' % b for b in new_energies]) + timestr = '%f' % (total_frames / float(samplerate) ) + print('{:s} {:s}'.format(timestr, ' '.join(['%f' % b for b in new_energies]))) energies = vstack( [energies, new_energies] ) total_frames += read if read < hop_s: break if 1: - print "done computing, now plotting" + print("done computing, now plotting") import matplotlib.pyplot as plt from demo_waveform_plot import get_waveform_plot from demo_waveform_plot import set_xlabels_sample2time diff --git a/python/demos/demo_mfcc.py b/python/demos/demo_mfcc.py index d69ccef3..dfbd7ed0 100755 --- a/python/demos/demo_mfcc.py +++ b/python/demos/demo_mfcc.py @@ -5,13 +5,13 @@ from aubio import source, pvoc, mfcc from numpy import vstack, zeros win_s = 512 # fft size -hop_s = win_s / 4 # hop size +hop_s = win_s // 4 # hop size n_filters = 40 # must be 40 for mfcc n_coeffs = 13 samplerate = 44100 if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] + print("Usage: %s " % sys.argv[0]) sys.exit(1) source_filename = sys.argv[1] diff --git a/python/demos/demo_onset.py b/python/demos/demo_onset.py index 949a6639..43e4aede 100755 --- a/python/demos/demo_onset.py +++ b/python/demos/demo_onset.py @@ -4,10 +4,10 @@ import sys from aubio import source, onset win_s = 512 # fft size -hop_s = win_s / 2 # hop size +hop_s = win_s // 2 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -28,7 +28,7 @@ total_frames = 0 while True: samples, read = s() if o(samples): - print "%f" % o.get_last_s() + print("%f" % o.get_last_s()) onsets.append(o.get_last()) total_frames += read if read < hop_s: break diff --git a/python/demos/demo_onset_plot.py b/python/demos/demo_onset_plot.py index 437baf73..5169cf7c 100755 --- a/python/demos/demo_onset_plot.py +++ b/python/demos/demo_onset_plot.py @@ -5,10 +5,10 @@ from aubio import onset, source from numpy import hstack, zeros win_s = 512 # fft size -hop_s = win_s / 2 # hop size +hop_s = win_s // 2 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -34,10 +34,10 @@ total_frames = 0 while True: samples, read = s() if o(samples): - print "%f" % (o.get_last_s()) + print("%f" % (o.get_last_s())) onsets.append(o.get_last()) # keep some data to plot it later - new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) + new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0) allsamples_max = hstack([allsamples_max, new_maxes]) desc.append(o.get_descriptor()) tdesc.append(o.get_thresholded_descriptor()) @@ -61,9 +61,10 @@ if 1: plt1.xaxis.set_visible(False) plt1.yaxis.set_visible(False) desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ] - desc_plot = [d / max(desc) for d in desc] + desc_max = max(desc) if max(desc) != 0 else 1. + desc_plot = [d / desc_max for d in desc] plt2.plot(desc_times, desc_plot, '-g') - tdesc_plot = [d / max(desc) for d in tdesc] + tdesc_plot = [d / desc_max for d in tdesc] for stamp in onsets: stamp /= float(samplerate) plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r') diff --git a/python/demos/demo_pitch.py b/python/demos/demo_pitch.py index 094eee7a..555a30e6 100755 --- a/python/demos/demo_pitch.py +++ b/python/demos/demo_pitch.py @@ -4,17 +4,17 @@ import sys from aubio import source, pitch if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] downsample = 1 -samplerate = 44100 / downsample +samplerate = 44100 // downsample if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) -win_s = 4096 / downsample # fft size -hop_s = 512 / downsample # hop size +win_s = 4096 // downsample # fft size +hop_s = 512 // downsample # hop size s = source(filename, samplerate, hop_s) samplerate = s.samplerate @@ -36,7 +36,7 @@ while True: #pitch = int(round(pitch)) confidence = pitch_o.get_confidence() #if confidence < 0.8: pitch = 0. - #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence) + print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence)) pitches += [pitch] confidences += [confidence] total_frames += read diff --git a/python/demos/demo_pitch_sinusoid.py b/python/demos/demo_pitch_sinusoid.py index 6c3985c5..629f3273 100755 --- a/python/demos/demo_pitch_sinusoid.py +++ b/python/demos/demo_pitch_sinusoid.py @@ -22,7 +22,7 @@ samplerate = 44100 sin_length = (samplerate * 10) % 512 * 512 freqs = np.zeros(sin_length) -partition = sin_length / 8 +partition = sin_length // 8 pointer = 0 pointer += partition @@ -44,9 +44,10 @@ a = build_sinusoid(sin_length, freqs, samplerate) for method in methods: p = aubio.pitch(method, buf_size, hop_size, samplerate) cands[method] = run_pitch(p, a) - print cands[method] + print(method) + print(cands[method]) -print "done computing" +print("done computing") if 1: import matplotlib.pyplot as plt diff --git a/python/demos/demo_pysoundcard_record.py b/python/demos/demo_pysoundcard_record.py index 2b5c706e..b97a838d 100755 --- a/python/demos/demo_pysoundcard_record.py +++ b/python/demos/demo_pysoundcard_record.py @@ -10,7 +10,6 @@ def record_sink(sink_path): duration = 5 # in seconds s = Stream(blocksize = hop_size, channels = 1) g = sink(sink_path, samplerate = int(s.samplerate)) - print s.channels s.start() total_frames = 0 @@ -22,7 +21,8 @@ def record_sink(sink_path): g(mono_vec, hop_size) total_frames += hop_size except KeyboardInterrupt: - print "stopped after", "%.2f seconds" % (total_frames / s.samplerate) + duration = total_frames / float(s.samplerate) + print("stopped after %.2f seconds" % duration) s.stop() if __name__ == '__main__': diff --git a/python/demos/demo_simple_robot_voice.py b/python/demos/demo_simple_robot_voice.py index 08370db4..670f79f0 100755 --- a/python/demos/demo_simple_robot_voice.py +++ b/python/demos/demo_simple_robot_voice.py @@ -5,7 +5,7 @@ from aubio import source, sink, pvoc if __name__ == '__main__': if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] + print('usage: %s ' % sys.argv[0]) sys.exit(1) samplerate = 44100 f = source(sys.argv[1], samplerate, 256) @@ -13,7 +13,7 @@ if __name__ == '__main__': total_frames, read = 0, 256 win_s = 512 # fft size - hop_s = win_s / 2 # hop size + hop_s = win_s // 2 # hop size pv = pvoc(win_s, hop_s) # phase vocoder while read: @@ -25,4 +25,5 @@ if __name__ == '__main__': g(new_samples, read) # write to output total_frames += read - print "wrote", total_frames, "from", f.uri, "to", g.uri + format_str = "read {:d} samples from {:s}, written to {:s}" + print(format_str.format(total_frames, f.uri, g.uri)) diff --git a/python/demos/demo_simple_spectral_weighting.py b/python/demos/demo_simple_spectral_weighting.py index b84f7d8d..dd4abbe4 100755 --- a/python/demos/demo_simple_spectral_weighting.py +++ b/python/demos/demo_simple_spectral_weighting.py @@ -13,7 +13,7 @@ def hanningz(size): if __name__ == '__main__': if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] + print('usage: %s ' % sys.argv[0]) sys.exit(1) samplerate = 0 if len(sys.argv) > 3: samplerate = int(sys.argv[3]) @@ -22,7 +22,7 @@ if __name__ == '__main__': g = sink(sys.argv[2], samplerate) win_s = 512 # fft size - hop_s = win_s / 2 # hop size + hop_s = win_s // 2 # hop size pv = pvoc(win_s, hop_s) # phase vocoder # spectral weighting vector @@ -30,7 +30,7 @@ if __name__ == '__main__': .8 * hanningz(80)[40:], zeros( 50 ), 1.3 * hanningz(100), - zeros (win_s / 2 + 1 - 40 - 50 - 100), + zeros (win_s // 2 + 1 - 40 - 50 - 100), ] ) if 0: @@ -52,4 +52,5 @@ if __name__ == '__main__': g(new_samples, read) total_frames += read - print "read", total_frames / float(samplerate), "seconds from", f.uri + duration = total_frames / float(samplerate) + print("read {:.3f}s from {:s}".format(duration, f.uri)) diff --git a/python/demos/demo_sink.py b/python/demos/demo_sink.py index 93139ae8..ce2bb1b6 100755 --- a/python/demos/demo_sink.py +++ b/python/demos/demo_sink.py @@ -5,7 +5,7 @@ from aubio import source, sink if __name__ == '__main__': if len(sys.argv) < 3: - print 'usage: %s [samplerate] [hop_size]' % sys.argv[0] + print('usage: %s [samplerate] [hop_size]' % sys.argv[0]) sys.exit(1) if len(sys.argv) > 3: samplerate = int(sys.argv[3]) @@ -22,8 +22,10 @@ if __name__ == '__main__': vec, read = f() g(vec, read) total_frames += read - print "wrote", "%.2fs" % (total_frames / float(samplerate) ), - print "(", total_frames, "frames", "in", - print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", - print "from", f.uri, - print "to", g.uri + outstr = "wrote %.2fs" % (total_frames / float(samplerate)) + outstr += " (%d frames in" % total_frames + outstr += " %d blocks" % (total_frames // f.hop_size) + outstr += " at %dHz)" % f.samplerate + outstr += " from " + f.uri + outstr += " to " + g.uri + print(outstr) diff --git a/python/demos/demo_sink_create_woodblock.py b/python/demos/demo_sink_create_woodblock.py index 745daaef..5a42adcb 100755 --- a/python/demos/demo_sink_create_woodblock.py +++ b/python/demos/demo_sink_create_woodblock.py @@ -2,11 +2,11 @@ import sys from math import pi, e -from aubio import sink +from aubio import sink, float_type from numpy import arange, sin, exp, zeros if len(sys.argv) < 2: - print 'usage: %s [samplerate]' % sys.argv[0] + print('usage: %s [samplerate]' % sys.argv[0]) sys.exit(1) samplerate = 44100 # samplerate in Hz @@ -25,9 +25,9 @@ decay = .5 period = float(samplerate) / pitch # create a sine lookup table tablelen = 1000 -sinetable = arange(tablelen + 1, dtype = 'float32') +sinetable = arange(tablelen + 1, dtype = float_type) sinetable = 0.7 * sin(twopi * sinetable/tablelen) -sinetone = zeros((duration,), dtype = 'float32') +sinetone = zeros((duration,), dtype = float_type) # compute sinetone at floating point period for i in range(duration): @@ -39,7 +39,7 @@ for i in range(duration): sinetone[i] = a + frac * (b -a) # apply some envelope -float_ramp = arange(duration, dtype = 'float32') +float_ramp = arange(duration, dtype = float_type) sinetone *= exp( - e * float_ramp / duration / decay) sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) ) diff --git a/python/demos/demo_sink_multi.py b/python/demos/demo_sink_multi.py index dcf20da7..70eb6360 100755 --- a/python/demos/demo_sink_multi.py +++ b/python/demos/demo_sink_multi.py @@ -5,7 +5,7 @@ from aubio import source, sink if __name__ == '__main__': if len(sys.argv) < 3: - print 'usage: %s [samplerate] [hop_size]' % sys.argv[0] + print('usage: %s [samplerate] [hop_size]' % sys.argv[0]) sys.exit(1) if len(sys.argv) > 3: samplerate = int(sys.argv[3]) @@ -22,10 +22,11 @@ if __name__ == '__main__': vec, read = f.do_multi() g.do_multi(vec, read) total_frames += read - print "wrote", "%.2fs" % (total_frames / float(samplerate) ), - print "(", total_frames, "frames", "in", - print total_frames / f.hop_size, "blocks", - print "of", f.channels, "channels", - print "at", "%dHz" % f.samplerate, ")", - print "from", f.uri, - print "to", g.uri + outstr = "wrote %.2fs" % (total_frames / float(samplerate)) + outstr += " (%d frames in" % total_frames + outstr += " %d blocks" % (total_frames // f.hop_size) + outstr += " of %d channels" % f.channels + outstr += " at %dHz)" % f.samplerate + outstr += " from " + f.uri + outstr += " to " + g.uri + print(outstr) diff --git a/python/demos/demo_slicing.py b/python/demos/demo_slicing.py index a21252ad..d10d0af6 100755 --- a/python/demos/demo_slicing.py +++ b/python/demos/demo_slicing.py @@ -6,7 +6,7 @@ from aubio import source, sink if __name__ == '__main__': if len(sys.argv) < 3: - print 'usage: %s ' % sys.argv[0] + print('usage: %s ' % sys.argv[0]) sys.exit(1) source_file = sys.argv[1] duration = float(sys.argv[2]) @@ -44,7 +44,8 @@ if __name__ == '__main__': total_frames_written += read total_duration = total_frames_written / float(samplerate) slice_n += 1 - print 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals(), - print ' (total duration %(total_duration).2fs)' % locals() + outstr = 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals() + outstr += ' (total duration %(total_duration).2fs)' % locals() + print(outstr) # close source and sink files del f, g diff --git a/python/demos/demo_source.py b/python/demos/demo_source.py index 30318b1d..c60d785f 100755 --- a/python/demos/demo_source.py +++ b/python/demos/demo_source.py @@ -5,7 +5,7 @@ from aubio import source if __name__ == '__main__': if len(sys.argv) < 2: - print 'usage: %s [samplerate] [hop_size]' % sys.argv[0] + print('usage: %s [samplerate] [hop_size]' % sys.argv[0]) sys.exit(1) samplerate = 0 hop_size = 256 @@ -20,7 +20,9 @@ if __name__ == '__main__': vec, read = f() total_frames += read if read < f.hop_size: break - print "read", "%.2fs" % (total_frames / float(samplerate) ), - print "(", total_frames, "frames", "in", - print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", - print "from", f.uri + outstr = "read %.2fs" % (total_frames / float(samplerate)) + outstr += " (%d frames in" % total_frames + outstr += " %d blocks" % (total_frames // f.hop_size) + outstr += " at %dHz)" % f.samplerate + outstr += " from " + f.uri + print(outstr) diff --git a/python/demos/demo_specdesc.py b/python/demos/demo_specdesc.py index b1520293..71e38eae 100755 --- a/python/demos/demo_specdesc.py +++ b/python/demos/demo_specdesc.py @@ -1,14 +1,14 @@ #! /usr/bin/env python import sys +import numpy as np from aubio import fvec, source, pvoc, specdesc -from numpy import hstack win_s = 512 # fft size -hop_s = win_s / 4 # hop size +hop_s = win_s // 4 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -30,7 +30,7 @@ o = {} for method in methods: cands = [] - all_descs[method] = fvec(0) + all_descs[method] = np.array([]) o[method] = specdesc(method, win_s) total_frames = 0 @@ -39,17 +39,17 @@ downsample = 2 while True: samples, read = s() fftgrain = pv(samples) - #print "%f" % ( total_frames / float(samplerate) ), + #outstr = "%f" % ( total_frames / float(samplerate) ) for method in methods: specdesc_val = o[method](fftgrain)[0] - all_descs[method] = hstack ( [all_descs[method], specdesc_val] ) - #print "%f" % specdesc_val, - #print + all_descs[method] = np.append(all_descs[method], specdesc_val) + #outstr += " %f" % specdesc_val + #print(outstr) total_frames += read if read < hop_s: break if 1: - print "done computing, now plotting" + print("done computing, now plotting") import matplotlib.pyplot as plt from demo_waveform_plot import get_waveform_plot from demo_waveform_plot import set_xlabels_sample2time diff --git a/python/demos/demo_spectrogram.py b/python/demos/demo_spectrogram.py index 73e3c938..51be9174 100755 --- a/python/demos/demo_spectrogram.py +++ b/python/demos/demo_spectrogram.py @@ -1,19 +1,19 @@ #! /usr/bin/env python import sys, os.path -from aubio import pvoc, source +from aubio import pvoc, source, float_type from numpy import zeros, log10, vstack import matplotlib.pyplot as plt def get_spectrogram(filename, samplerate = 0): win_s = 512 # fft window size - hop_s = win_s / 2 # hop size - fft_s = win_s / 2 + 1 # spectrum bins + hop_s = win_s // 2 # hop size + fft_s = win_s // 2 + 1 # spectrum bins a = source(filename, samplerate, hop_s) # source file if samplerate == 0: samplerate = a.samplerate pv = pvoc(win_s, hop_s) # phase vocoder - specgram = zeros([0, fft_s], dtype='float32') # numpy array to store spectrogram + specgram = zeros([0, fft_s], dtype=float_type) # numpy array to store spectrogram # analysis while True: @@ -28,8 +28,8 @@ def get_spectrogram(filename, samplerate = 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.) + outstr = "total time: %0.2fs" % total_time + print(outstr + ", samplerate: %.2fkHz" % (samplerate / 1000.)) n_xticks = 10 n_yticks = 10 @@ -65,12 +65,12 @@ def get_spectrogram(filename, samplerate = 0): if __name__ == '__main__': if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] + print("Usage: %s " % sys.argv[0]) else: for soundfile in sys.argv[1:]: fig = get_spectrogram(soundfile) # display graph - fig.show() + plt.show() #outimage = os.path.basename(soundfile) + '.png' #print ("writing: " + outimage) #plt.savefig(outimage) diff --git a/python/demos/demo_tempo.py b/python/demos/demo_tempo.py index 17b959be..51e1ae32 100755 --- a/python/demos/demo_tempo.py +++ b/python/demos/demo_tempo.py @@ -4,10 +4,10 @@ import sys from aubio import tempo, source win_s = 512 # fft size -hop_s = win_s / 2 # hop size +hop_s = win_s // 2 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -33,7 +33,7 @@ while True: is_beat = o(samples) if is_beat: this_beat = int(total_frames - delay + is_beat[0] * hop_s) - print "%f" % (this_beat / float(samplerate)) + print("%f" % (this_beat / float(samplerate))) beats.append(this_beat) total_frames += read if read < hop_s: break diff --git a/python/demos/demo_tempo_plot.py b/python/demos/demo_tempo_plot.py index 043e144f..48a6ccf3 100755 --- a/python/demos/demo_tempo_plot.py +++ b/python/demos/demo_tempo_plot.py @@ -4,10 +4,10 @@ import sys from aubio import tempo, source win_s = 512 # fft size -hop_s = win_s / 2 # hop size +hop_s = win_s // 2 # hop size if len(sys.argv) < 2: - print "Usage: %s [samplerate]" % sys.argv[0] + print("Usage: %s [samplerate]" % sys.argv[0]) sys.exit(1) filename = sys.argv[1] @@ -42,8 +42,8 @@ if len(beats) > 1: from numpy import mean, median, diff import matplotlib.pyplot as plt bpms = 60./ diff(beats) - print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm' - print 'plotting', filename + print('mean period: %.2fbpm, median: %.2fbpm' % (mean(bpms), median(bpms))) + print('plotting %s' % filename) plt1 = plt.axes([0.1, 0.75, 0.8, 0.19]) plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1) plt.rc('lines',linewidth='.8') @@ -75,5 +75,5 @@ if len(beats) > 1: plt.show() else: - print 'mean period:', "%.2f" % 0, 'bpm', 'median', "%.2f" % 0, 'bpm', - print 'nothing to plot, file too short?' + print('mean period: %.2fbpm, median: %.2fbpm' % (0, 0)) + print('plotting %s' % filename) diff --git a/python/demos/demo_tss.py b/python/demos/demo_tss.py index aa9107e0..f8c29aa2 100755 --- a/python/demos/demo_tss.py +++ b/python/demos/demo_tss.py @@ -5,12 +5,12 @@ from aubio import source, sink, pvoc, tss if __name__ == '__main__': if len(sys.argv) < 2: - print 'usage: %s ' % sys.argv[0] + print('usage: %s ' % sys.argv[0]) sys.exit(1) samplerate = 44100 - win_s = 1024 # fft size - hop_s = win_s / 4 # block size + win_s = 1024 # fft size + hop_s = win_s // 4 # block size threshold = 0.5 f = source(sys.argv[1], samplerate, hop_s) diff --git a/python/demos/demo_waveform_plot.py b/python/demos/demo_waveform_plot.py index b4bd42a3..54342239 100755 --- a/python/demos/demo_waveform_plot.py +++ b/python/demos/demo_waveform_plot.py @@ -21,7 +21,7 @@ def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, do while True: samples, read = a() # keep some data to plot it later - new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) + new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0) allsamples_max = hstack([allsamples_max, new_maxes]) total_frames += read if read < hop_s: break @@ -48,7 +48,7 @@ def set_xlabels_sample2time(ax, latest_sample, samplerate): if __name__ == '__main__': import matplotlib.pyplot as plt if len(sys.argv) < 2: - print "Usage: %s " % sys.argv[0] + print("Usage: %s " % sys.argv[0]) else: for soundfile in sys.argv[1:]: get_waveform_plot(soundfile) -- 2.11.0