2 # -*- coding: utf-8 -*-
6 Compare the speed of several methods for reading and loading a sound file.
8 Optionally, this file can make use of the following packages:
10 - audioread https://github.com/beetbox/audioread
11 - scipy https://scipy.org
12 - librosa https://github.com/bmcfee/librosa
13 - pydub https://github.com/jiaaro/pydub
15 Uncomment the function names below and send us your speed results!
24 #"load_file_scipy_mmap",
25 #"read_file_audioread",
34 def read_file_audioread(filename):
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)
46 with audioread.audio_open(filename) as f:
49 samples = convert_buffer_to_float(buf)
50 samples = samples.reshape(f.channels, -1)
51 total_frames += samples.shape[1]
52 return total_frames, f.samplerate
54 def load_file_librosa(filename):
56 y, sr = librosa.load(filename, sr = None)
57 #print y.mean(), y.shape
60 def load_file_scipy(filename):
61 import scipy.io.wavfile
62 sr, y = scipy.io.wavfile.read(filename)
63 y = y.astype('float32') / 32767
64 #print y.mean(), y.shape
67 def load_file_scipy_mmap(filename):
68 import scipy.io.wavfile
69 sr, y = scipy.io.wavfile.read(filename, mmap = True)
70 #print y.mean(), y.shape
73 def read_file_pydub(filename):
74 from pydub import AudioSegment
75 song = AudioSegment.from_file(filename)
76 song.get_array_of_samples()
77 return song.frame_count(), song.frame_rate
79 def load_file_pydub(filename):
80 from pydub import AudioSegment
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
86 def read_file_aubio(filename):
88 f = aubio.source(filename, hop_size = 1024)
93 if read < f.hop_size: break
94 return total_frames, f.samplerate
96 def load_file_aubio(filename):
98 f = aubio.source(filename, hop_size = 1024)
99 y = np.zeros(f.duration, dtype = aubio.float_type)
103 y[total_frames:total_frames + read] = samples[:read]
105 if read < f.hop_size: break
106 assert len(y) == total_frames
107 #print y.mean(), y.shape
108 return total_frames, f.samplerate
110 def test_speed(function, filename):
115 total_frames, samplerate = function(filename)
116 except ImportError as e:
117 print ("error: failed importing {:s}".format(e))
119 elapsed = time.time() - start
120 #print ("{:5f} ".format(elapsed)),
121 times.append(elapsed)
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 ))
129 if __name__ == '__main__':
131 if len(sys.argv) < 2:
132 print ("not enough arguments")
134 filename = sys.argv[1]
136 for f in test_functions:
137 # get actual function from globals
138 test_function = globals()[f]
139 test_speed(test_function, filename)