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