[07867cd] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | Compare the speed of several methods for reading and loading a sound file. |
---|
| 7 | |
---|
[1e4d90f] | 8 | Optionally, this file can make use of the following packages: |
---|
[8fb567c] | 9 | |
---|
| 10 | - audioread https://github.com/beetbox/audioread |
---|
[1e4d90f] | 11 | - scipy https://scipy.org |
---|
[8fb567c] | 12 | - librosa https://github.com/bmcfee/librosa |
---|
| 13 | - pydub https://github.com/jiaaro/pydub |
---|
[07867cd] | 14 | |
---|
[1e4d90f] | 15 | Uncomment the function names below and send us your speed results! |
---|
| 16 | |
---|
[07867cd] | 17 | """ |
---|
| 18 | |
---|
[1e4d90f] | 19 | |
---|
| 20 | test_functions = [ |
---|
| 21 | "read_file_aubio", |
---|
| 22 | "load_file_aubio", |
---|
| 23 | #"load_file_scipy", |
---|
| 24 | #"load_file_scipy_mmap", |
---|
| 25 | #"read_file_audioread", |
---|
| 26 | #"load_file_librosa", |
---|
| 27 | #"read_file_pydub", |
---|
| 28 | #"load_file_pydub", |
---|
| 29 | ] |
---|
| 30 | |
---|
| 31 | |
---|
[07867cd] | 32 | import numpy as np |
---|
| 33 | |
---|
| 34 | def read_file_audioread(filename): |
---|
[1e4d90f] | 35 | import audioread |
---|
[07867cd] | 36 | # taken from librosa.util.utils |
---|
| 37 | def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32): |
---|
| 38 | # Invert the scale of the data |
---|
| 39 | scale = 1./float(1 << ((8 * n_bytes) - 1)) |
---|
| 40 | # Construct the format string |
---|
| 41 | fmt = '<i{:d}'.format(n_bytes) |
---|
| 42 | # Rescale and format the data buffer |
---|
| 43 | out = scale * np.frombuffer(buf, fmt).astype(dtype) |
---|
| 44 | return out |
---|
| 45 | |
---|
| 46 | with audioread.audio_open(filename) as f: |
---|
| 47 | total_frames = 0 |
---|
| 48 | for buf in f: |
---|
| 49 | samples = convert_buffer_to_float(buf) |
---|
[8fb567c] | 50 | samples = samples.reshape(f.channels, -1) |
---|
[07867cd] | 51 | total_frames += samples.shape[1] |
---|
| 52 | return total_frames, f.samplerate |
---|
| 53 | |
---|
| 54 | def load_file_librosa(filename): |
---|
[1e4d90f] | 55 | import librosa |
---|
[07867cd] | 56 | y, sr = librosa.load(filename, sr = None) |
---|
[8fb567c] | 57 | #print y.mean(), y.shape |
---|
| 58 | return len(y), sr |
---|
| 59 | |
---|
| 60 | def load_file_scipy(filename): |
---|
[1e4d90f] | 61 | import scipy.io.wavfile |
---|
[8fb567c] | 62 | sr, y = scipy.io.wavfile.read(filename) |
---|
| 63 | y = y.astype('float32') / 32767 |
---|
| 64 | #print y.mean(), y.shape |
---|
[07867cd] | 65 | return len(y), sr |
---|
| 66 | |
---|
[8fb567c] | 67 | def load_file_scipy_mmap(filename): |
---|
[1e4d90f] | 68 | import scipy.io.wavfile |
---|
[8fb567c] | 69 | sr, y = scipy.io.wavfile.read(filename, mmap = True) |
---|
| 70 | #print y.mean(), y.shape |
---|
| 71 | return len(y), sr |
---|
| 72 | |
---|
| 73 | def read_file_pydub(filename): |
---|
[1e4d90f] | 74 | from pydub import AudioSegment |
---|
[8fb567c] | 75 | song = AudioSegment.from_file(filename) |
---|
| 76 | song.get_array_of_samples() |
---|
| 77 | return song.frame_count(), song.frame_rate |
---|
| 78 | |
---|
| 79 | def load_file_pydub(filename): |
---|
[1e4d90f] | 80 | from pydub import AudioSegment |
---|
[8fb567c] | 81 | song = AudioSegment.from_file(filename) |
---|
| 82 | y = np.asarray(song.get_array_of_samples(), dtype = 'float32') |
---|
| 83 | y = y.reshape(song.channels, -1) / 32767. |
---|
| 84 | return song.frame_count(), song.frame_rate |
---|
| 85 | |
---|
[07867cd] | 86 | def read_file_aubio(filename): |
---|
[1e4d90f] | 87 | import aubio |
---|
[07867cd] | 88 | f = aubio.source(filename, hop_size = 1024) |
---|
| 89 | total_frames = 0 |
---|
| 90 | while True: |
---|
[dee266f] | 91 | _, read = f() |
---|
[07867cd] | 92 | total_frames += read |
---|
| 93 | if read < f.hop_size: break |
---|
| 94 | return total_frames, f.samplerate |
---|
| 95 | |
---|
| 96 | def load_file_aubio(filename): |
---|
[1e4d90f] | 97 | import aubio |
---|
[07867cd] | 98 | f = aubio.source(filename, hop_size = 1024) |
---|
| 99 | y = np.zeros(f.duration, dtype = aubio.float_type) |
---|
| 100 | total_frames = 0 |
---|
| 101 | while True: |
---|
| 102 | samples, read = f() |
---|
| 103 | y[total_frames:total_frames + read] = samples[:read] |
---|
| 104 | total_frames += read |
---|
| 105 | if read < f.hop_size: break |
---|
| 106 | assert len(y) == total_frames |
---|
[8fb567c] | 107 | #print y.mean(), y.shape |
---|
[07867cd] | 108 | return total_frames, f.samplerate |
---|
| 109 | |
---|
| 110 | def test_speed(function, filename): |
---|
| 111 | times = [] |
---|
[84838c3] | 112 | for _ in range(10): |
---|
[07867cd] | 113 | start = time.time() |
---|
[1e4d90f] | 114 | try: |
---|
| 115 | total_frames, samplerate = function(filename) |
---|
| 116 | except ImportError as e: |
---|
| 117 | print ("error: failed importing {:s}".format(e)) |
---|
| 118 | return |
---|
[07867cd] | 119 | elapsed = time.time() - start |
---|
| 120 | #print ("{:5f} ".format(elapsed)), |
---|
| 121 | times.append(elapsed) |
---|
[1e4d90f] | 122 | |
---|
[07867cd] | 123 | #print |
---|
| 124 | times = np.array(times) |
---|
| 125 | duration_min = int(total_frames/float(samplerate) // 60) |
---|
| 126 | str_format = '{:25s} took {:5f} seconds avg (±{:5f}) to run on a ~ {:d} minutes long file' |
---|
| 127 | print (str_format.format(function.__name__, times.mean(), times.std(), duration_min )) |
---|
| 128 | |
---|
| 129 | if __name__ == '__main__': |
---|
| 130 | import sys, time |
---|
| 131 | if len(sys.argv) < 2: |
---|
| 132 | print ("not enough arguments") |
---|
| 133 | sys.exit(1) |
---|
| 134 | filename = sys.argv[1] |
---|
| 135 | |
---|
[1e4d90f] | 136 | for f in test_functions: |
---|
[84838c3] | 137 | # get actual function from globals |
---|
| 138 | test_function = globals()[f] |
---|
| 139 | test_speed(test_function, filename) |
---|