2 # -*- coding: utf-8 -*-
6 Compare the speed of several methods for reading and loading a sound file.
8 This file depends on the following packages:
10 - audioread https://github.com/beetbox/audioread
11 - librosa https://github.com/bmcfee/librosa
12 - pydub https://github.com/jiaaro/pydub
21 import scipy.io.wavfile
22 from pydub import AudioSegment
25 def read_file_audioread(filename):
26 # taken from librosa.util.utils
27 def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32):
28 # Invert the scale of the data
29 scale = 1./float(1 << ((8 * n_bytes) - 1))
30 # Construct the format string
31 fmt = '<i{:d}'.format(n_bytes)
32 # Rescale and format the data buffer
33 out = scale * np.frombuffer(buf, fmt).astype(dtype)
36 with audioread.audio_open(filename) as f:
39 samples = convert_buffer_to_float(buf)
40 samples = samples.reshape(f.channels, -1)
41 total_frames += samples.shape[1]
42 return total_frames, f.samplerate
44 def load_file_librosa(filename):
45 y, sr = librosa.load(filename, sr = None)
46 #print y.mean(), y.shape
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
55 def load_file_scipy_mmap(filename):
56 sr, y = scipy.io.wavfile.read(filename, mmap = True)
57 #print y.mean(), y.shape
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
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
71 def read_file_aubio(filename):
72 f = aubio.source(filename, hop_size = 1024)
77 if read < f.hop_size: break
78 return total_frames, f.samplerate
80 def load_file_aubio(filename):
81 f = aubio.source(filename, hop_size = 1024)
82 y = np.zeros(f.duration, dtype = aubio.float_type)
86 y[total_frames:total_frames + read] = samples[:read]
88 if read < f.hop_size: break
89 assert len(y) == total_frames
90 #print y.mean(), y.shape
91 return total_frames, f.samplerate
93 def test_speed(function, filename):
97 total_frames, samplerate = function(filename)
98 elapsed = time.time() - start
99 #print ("{:5f} ".format(elapsed)),
100 times.append(elapsed)
102 times = np.array(times)
103 duration_min = int(total_frames/float(samplerate) // 60)
104 str_format = '{:25s} took {:5f} seconds avg (±{:5f}) to run on a ~ {:d} minutes long file'
105 print (str_format.format(function.__name__, times.mean(), times.std(), duration_min ))
107 if __name__ == '__main__':
109 if len(sys.argv) < 2:
110 print ("not enough arguments")
112 filename = sys.argv[1]
118 #load_file_scipy_mmap,
119 #read_file_audioread,
126 test_speed(f, filename)