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 | |
---|
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 |
---|
13 | |
---|
14 | """ |
---|
15 | |
---|
16 | import numpy as np |
---|
17 | import aubio |
---|
18 | """ |
---|
19 | import audioread |
---|
20 | import librosa |
---|
21 | import scipy.io.wavfile |
---|
22 | from pydub import AudioSegment |
---|
23 | """ |
---|
24 | |
---|
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) |
---|
34 | return out |
---|
35 | |
---|
36 | with audioread.audio_open(filename) as f: |
---|
37 | total_frames = 0 |
---|
38 | for buf in 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 |
---|
43 | |
---|
44 | def load_file_librosa(filename): |
---|
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 |
---|
53 | return len(y), sr |
---|
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 | |
---|
71 | def read_file_aubio(filename): |
---|
72 | f = aubio.source(filename, hop_size = 1024) |
---|
73 | total_frames = 0 |
---|
74 | while True: |
---|
75 | samples, read = f() |
---|
76 | total_frames += read |
---|
77 | if read < f.hop_size: break |
---|
78 | return total_frames, f.samplerate |
---|
79 | |
---|
80 | def load_file_aubio(filename): |
---|
81 | f = aubio.source(filename, hop_size = 1024) |
---|
82 | y = np.zeros(f.duration, dtype = aubio.float_type) |
---|
83 | total_frames = 0 |
---|
84 | while True: |
---|
85 | samples, read = f() |
---|
86 | y[total_frames:total_frames + read] = samples[:read] |
---|
87 | total_frames += 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 |
---|
92 | |
---|
93 | def test_speed(function, filename): |
---|
94 | times = [] |
---|
95 | for i in range(10): |
---|
96 | start = time.time() |
---|
97 | total_frames, samplerate = function(filename) |
---|
98 | elapsed = time.time() - start |
---|
99 | #print ("{:5f} ".format(elapsed)), |
---|
100 | times.append(elapsed) |
---|
101 | #print |
---|
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 )) |
---|
106 | |
---|
107 | if __name__ == '__main__': |
---|
108 | import sys, time |
---|
109 | if len(sys.argv) < 2: |
---|
110 | print ("not enough arguments") |
---|
111 | sys.exit(1) |
---|
112 | filename = sys.argv[1] |
---|
113 | |
---|
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 | |
---|
125 | for f in functions: |
---|
126 | test_speed(f, filename) |
---|