Showing the changes to python (14 of 14 files). Show all
Unified · Split
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | 3 | from aubio import filterbank, fvec |
| 4 | −from pylab import loglog, show, subplot, xlim, ylim, xlabel, ylabel, title | |
| 4 | +from pylab import loglog, show, xlim, ylim, xlabel, ylabel, title | |
| 5 | 5 | from numpy import vstack, arange |
| 6 | 6 | |
| 7 | 7 | win_s = 2048 |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | 3 | from aubio import filterbank |
| 4 | −from numpy import array, arange, vstack | |
| 4 | +from numpy import arange, vstack | |
| 5 | 5 | |
| 6 | 6 | win_s = 8192 |
| 7 | 7 | samplerate = 16000 |
| @@ -25,7 +25,6 @@ def get_keyboard_edges(firstnote = 21, lastnote = 108): · 24 unmodified lines | ||
| 25 | 25 | return xb, xw, 2/3. *scaleb, 1/2. * scalew |
| 26 | 26 | |
| 27 | 27 | def create_keyboard_patches(firstnote, lastnote, ax = None): |
| 28 | − import numpy as np | |
| 29 | 28 | import matplotlib.pyplot as plt |
| 30 | 29 | from matplotlib.path import Path |
| 31 | 30 | import matplotlib.patches as mpatches |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | 3 | import sys |
| 4 | −from aubio import fvec, source, pvoc, filterbank | |
| 4 | +from aubio import source, pvoc, filterbank | |
| 5 | 5 | from numpy import vstack, zeros |
| 6 | 6 | |
| 7 | 7 | win_s = 512 # fft size |
| @@ -2,7 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | |
| 3 | 3 | import sys |
| 4 | 4 | from aubio import source, pvoc, mfcc |
| 5 | −from numpy import array, vstack, zeros | |
| 5 | +from numpy import vstack, zeros | |
| 6 | 6 | |
| 7 | 7 | win_s = 512 # fft size |
| 8 | 8 | hop_s = win_s / 4 # hop size |
| @@ -2,7 +2,7 @@ · 1 unmodified line | ||
| 2 | 2 | |
| 3 | 3 | import sys |
| 4 | 4 | from aubio import onset, source |
| 5 | −from numpy import array, hstack, zeros | |
| 5 | +from numpy import hstack, zeros | |
| 6 | 6 | |
| 7 | 7 | win_s = 512 # fft size |
| 8 | 8 | hop_s = win_s / 2 # hop size |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | 3 | import sys |
| 4 | −from aubio import source, pitch, freqtomidi | |
| 4 | +from aubio import source, pitch | |
| 5 | 5 | |
| 6 | 6 | if len(sys.argv) < 2: |
| 7 | 7 | print "Usage: %s <filename> [samplerate]" % sys.argv[0] |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | −from numpy import random, sin, arange, ones, zeros | |
| 3 | +from numpy import random, sin, arange, zeros | |
| 4 | 4 | from math import pi |
| 5 | 5 | from aubio import fvec, pitch |
| 6 | 6 | |
| @@ -10,7 +10,6 @@ def build_sinusoid(length, freqs, samplerate): · 3 unmodified lines | ||
| 10 | 10 | def run_pitch(p, input_vec): |
| 11 | 11 | f = fvec (p.hop_size) |
| 12 | 12 | cands = [] |
| 13 | − count = 0 | |
| 14 | 13 | for vec_slice in input_vec.reshape((-1, p.hop_size)): |
| 15 | 14 | f[:] = vec_slice |
| 16 | 15 | cands.append(p(f)) |
| @@ -21,9 +21,8 @@ def record_sink(sink_path): · 20 unmodified lines | ||
| 21 | 21 | mono_vec = vec.sum(-1) / float(s.channels[0]) |
| 22 | 22 | g(mono_vec, hop_size) |
| 23 | 23 | total_frames += hop_size |
| 24 | − except KeyboardInterrupt, e: | |
| 24 | + except KeyboardInterrupt: | |
| 25 | 25 | print "stopped after", "%.2f seconds" % (total_frames / s.samplerate) |
| 26 | − pass | |
| 27 | 26 | s.stop() |
| 28 | 27 | |
| 29 | 28 | if __name__ == '__main__': |
| @@ -5,16 +5,22 @@ · 4 unmodified lines | ||
| 5 | 5 | |
| 6 | 6 | Compare the speed of several methods for reading and loading a sound file. |
| 7 | 7 | |
| 8 | −This file depends on audioread and librosa: | |
| 9 | − https://github.com/beetbox/audioread | |
| 10 | − https://github.com/bmcfee/librosa | |
| 8 | +This file depends on the following packages: | |
| 9 | + | |
| 10 | + - audioread https://github.com/beetbox/audioread | |
| 11 | + - librosa https://github.com/bmcfee/librosa | |
| 12 | + - pydub https://github.com/jiaaro/pydub | |
| 11 | 13 | |
| 12 | 14 | """ |
| 13 | 15 | |
| 14 | 16 | import numpy as np |
| 15 | 17 | import aubio |
| 18 | +""" | |
| 16 | 19 | import audioread |
| 17 | 20 | import librosa |
| 21 | +import scipy.io.wavfile | |
| 22 | +from pydub import AudioSegment | |
| 23 | +""" | |
| 18 | 24 | |
| 19 | 25 | def read_file_audioread(filename): |
| 20 | 26 | # taken from librosa.util.utils |
| @@ -25,20 +31,43 @@ def read_file_audioread(filename): · 4 unmodified lines | ||
| 25 | 31 | fmt = '<i{:d}'.format(n_bytes) |
| 26 | 32 | # Rescale and format the data buffer |
| 27 | 33 | out = scale * np.frombuffer(buf, fmt).astype(dtype) |
| 28 | − out = out.reshape(2, -1) | |
| 29 | 34 | return out |
| 30 | 35 | |
| 31 | 36 | with audioread.audio_open(filename) as f: |
| 32 | 37 | total_frames = 0 |
| 33 | 38 | for buf in f: |
| 34 | 39 | samples = convert_buffer_to_float(buf) |
| 40 | + samples = samples.reshape(f.channels, -1) | |
| 35 | 41 | total_frames += samples.shape[1] |
| 36 | 42 | return total_frames, f.samplerate |
| 37 | 43 | |
| 38 | 44 | def load_file_librosa(filename): |
| 39 | 45 | y, sr = librosa.load(filename, sr = None) |
| 46 | + #print y.mean(), y.shape | |
| 47 | + return len(y), sr | |
| 48 | + | |
| 49 | +def load_file_scipy(filename): | |
| 50 | + sr, y = scipy.io.wavfile.read(filename) | |
| 51 | + y = y.astype('float32') / 32767 | |
| 52 | + #print y.mean(), y.shape | |
| 40 | 53 | return len(y), sr |
| 41 | 54 | |
| 55 | +def load_file_scipy_mmap(filename): | |
| 56 | + sr, y = scipy.io.wavfile.read(filename, mmap = True) | |
| 57 | + #print y.mean(), y.shape | |
| 58 | + return len(y), sr | |
| 59 | + | |
| 60 | +def read_file_pydub(filename): | |
| 61 | + song = AudioSegment.from_file(filename) | |
| 62 | + song.get_array_of_samples() | |
| 63 | + return song.frame_count(), song.frame_rate | |
| 64 | + | |
| 65 | +def load_file_pydub(filename): | |
| 66 | + song = AudioSegment.from_file(filename) | |
| 67 | + y = np.asarray(song.get_array_of_samples(), dtype = 'float32') | |
| 68 | + y = y.reshape(song.channels, -1) / 32767. | |
| 69 | + return song.frame_count(), song.frame_rate | |
| 70 | + | |
| 42 | 71 | def read_file_aubio(filename): |
| 43 | 72 | f = aubio.source(filename, hop_size = 1024) |
| 44 | 73 | total_frames = 0 |
| @@ -58,6 +87,7 @@ def load_file_aubio(filename): · 13 unmodified lines | ||
| 58 | 87 | total_frames += read |
| 59 | 88 | if read < f.hop_size: break |
| 60 | 89 | assert len(y) == total_frames |
| 90 | + #print y.mean(), y.shape | |
| 61 | 91 | return total_frames, f.samplerate |
| 62 | 92 | |
| 63 | 93 | def test_speed(function, filename): |
| @@ -81,6 +111,16 @@ if __name__ == '__main__': · 17 unmodified lines | ||
| 81 | 111 | sys.exit(1) |
| 82 | 112 | filename = sys.argv[1] |
| 83 | 113 | |
| 84 | − functions = [read_file_aubio, load_file_aubio, read_file_audioread, load_file_librosa] | |
| 114 | + functions = [ | |
| 115 | + read_file_aubio, | |
| 116 | + load_file_aubio, | |
| 117 | + #load_file_scipy, | |
| 118 | + #load_file_scipy_mmap, | |
| 119 | + #read_file_audioread, | |
| 120 | + #load_file_librosa, | |
| 121 | + #read_file_pydub, | |
| 122 | + #load_file_pydub, | |
| 123 | + ] | |
| 124 | + | |
| 85 | 125 | for f in functions: |
| 86 | 126 | test_speed(f, filename) |
| @@ -3,7 +3,7 @@ · 2 unmodified lines | ||
| 3 | 3 | import sys |
| 4 | 4 | from math import pi, e |
| 5 | 5 | from aubio import sink |
| 6 | −from numpy import arange, resize, sin, exp, zeros | |
| 6 | +from numpy import arange, sin, exp, zeros | |
| 7 | 7 | |
| 8 | 8 | if len(sys.argv) < 2: |
| 9 | 9 | print 'usage: %s <outputfile> [samplerate]' % sys.argv[0] |
| @@ -39,7 +39,7 @@ while True: · 38 unmodified lines | ||
| 39 | 39 | |
| 40 | 40 | if len(beats) > 1: |
| 41 | 41 | # do plotting |
| 42 | − from numpy import array, arange, mean, median, diff | |
| 42 | + from numpy import mean, median, diff | |
| 43 | 43 | import matplotlib.pyplot as plt |
| 44 | 44 | bpms = 60./ diff(beats) |
| 45 | 45 | print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm' |
| @@ -44,3 +44,4 @@ if __name__ == '__main__': · 43 unmodified lines | ||
| 44 | 44 | get_spectrogram(sys.argv[2]) |
| 45 | 45 | subplot(313) |
| 46 | 46 | get_spectrogram(sys.argv[3]) |
| 47 | + show() | |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | 3 | import sys |
| 4 | −from aubio import pvoc, source | |
| 4 | +from aubio import source | |
| 5 | 5 | from numpy import zeros, hstack |
| 6 | 6 | |
| 7 | 7 | def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, downsample = 2**4): |