Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -5,24 +5,34 @@ · 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 the following packages: | 8 | Optionally, this file can make use of the following packages: |
| 9 | 9 | ||
| 10 | - audioread https://github.com/beetbox/audioread | 10 | - audioread https://github.com/beetbox/audioread |
| 11 | - scipy https://scipy.org | ||
| 11 | - librosa https://github.com/bmcfee/librosa | 12 | - librosa https://github.com/bmcfee/librosa |
| 12 | - pydub https://github.com/jiaaro/pydub | 13 | - pydub https://github.com/jiaaro/pydub |
| 13 | 14 | ||
| 15 | Uncomment the function names below and send us your speed results! | ||
| 16 | |||
| 14 | """ | 17 | """ |
| 15 | 18 | ||
| 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 | |||
| 16 | import numpy as np | 32 | 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 | 33 | ||
| 25 | def read_file_audioread(filename): | 34 | def read_file_audioread(filename): |
| 35 | import audioread | ||
| 26 | # taken from librosa.util.utils | 36 | # taken from librosa.util.utils |
| 27 | def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32): | 37 | def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32): |
| 28 | # Invert the scale of the data | 38 | # Invert the scale of the data |
| @@ -42,33 +52,39 @@ def read_file_audioread(filename): · 13 unmodified lines | |||
| 42 | return total_frames, f.samplerate | 52 | return total_frames, f.samplerate |
| 43 | 53 | ||
| 44 | def load_file_librosa(filename): | 54 | def load_file_librosa(filename): |
| 55 | import librosa | ||
| 45 | y, sr = librosa.load(filename, sr = None) | 56 | y, sr = librosa.load(filename, sr = None) |
| 46 | #print y.mean(), y.shape | 57 | #print y.mean(), y.shape |
| 47 | return len(y), sr | 58 | return len(y), sr |
| 48 | 59 | ||
| 49 | def load_file_scipy(filename): | 60 | def load_file_scipy(filename): |
| 61 | import scipy.io.wavfile | ||
| 50 | sr, y = scipy.io.wavfile.read(filename) | 62 | sr, y = scipy.io.wavfile.read(filename) |
| 51 | y = y.astype('float32') / 32767 | 63 | y = y.astype('float32') / 32767 |
| 52 | #print y.mean(), y.shape | 64 | #print y.mean(), y.shape |
| 53 | return len(y), sr | 65 | return len(y), sr |
| 54 | 66 | ||
| 55 | def load_file_scipy_mmap(filename): | 67 | def load_file_scipy_mmap(filename): |
| 68 | import scipy.io.wavfile | ||
| 56 | sr, y = scipy.io.wavfile.read(filename, mmap = True) | 69 | sr, y = scipy.io.wavfile.read(filename, mmap = True) |
| 57 | #print y.mean(), y.shape | 70 | #print y.mean(), y.shape |
| 58 | return len(y), sr | 71 | return len(y), sr |
| 59 | 72 | ||
| 60 | def read_file_pydub(filename): | 73 | def read_file_pydub(filename): |
| 74 | from pydub import AudioSegment | ||
| 61 | song = AudioSegment.from_file(filename) | 75 | song = AudioSegment.from_file(filename) |
| 62 | song.get_array_of_samples() | 76 | song.get_array_of_samples() |
| 63 | return song.frame_count(), song.frame_rate | 77 | return song.frame_count(), song.frame_rate |
| 64 | 78 | ||
| 65 | def load_file_pydub(filename): | 79 | def load_file_pydub(filename): |
| 80 | from pydub import AudioSegment | ||
| 66 | song = AudioSegment.from_file(filename) | 81 | song = AudioSegment.from_file(filename) |
| 67 | y = np.asarray(song.get_array_of_samples(), dtype = 'float32') | 82 | y = np.asarray(song.get_array_of_samples(), dtype = 'float32') |
| 68 | y = y.reshape(song.channels, -1) / 32767. | 83 | y = y.reshape(song.channels, -1) / 32767. |
| 69 | return song.frame_count(), song.frame_rate | 84 | return song.frame_count(), song.frame_rate |
| 70 | 85 | ||
| 71 | def read_file_aubio(filename): | 86 | def read_file_aubio(filename): |
| 87 | import aubio | ||
| 72 | f = aubio.source(filename, hop_size = 1024) | 88 | f = aubio.source(filename, hop_size = 1024) |
| 73 | total_frames = 0 | 89 | total_frames = 0 |
| 74 | while True: | 90 | while True: |
| @@ -78,6 +94,7 @@ def read_file_aubio(filename): · 3 unmodified lines | |||
| 78 | return total_frames, f.samplerate | 94 | return total_frames, f.samplerate |
| 79 | 95 | ||
| 80 | def load_file_aubio(filename): | 96 | def load_file_aubio(filename): |
| 97 | import aubio | ||
| 81 | f = aubio.source(filename, hop_size = 1024) | 98 | f = aubio.source(filename, hop_size = 1024) |
| 82 | y = np.zeros(f.duration, dtype = aubio.float_type) | 99 | y = np.zeros(f.duration, dtype = aubio.float_type) |
| 83 | total_frames = 0 | 100 | total_frames = 0 |
| @@ -94,10 +111,15 @@ def test_speed(function, filename): · 10 unmodified lines | |||
| 94 | times = [] | 111 | times = [] |
| 95 | for i in range(10): | 112 | for i in range(10): |
| 96 | start = time.time() | 113 | start = time.time() |
| 97 | total_frames, samplerate = function(filename) | 114 | try: |
| 115 | total_frames, samplerate = function(filename) | ||
| 116 | except ImportError as e: | ||
| 117 | print ("error: failed importing {:s}".format(e)) | ||
| 118 | return | ||
| 98 | elapsed = time.time() - start | 119 | elapsed = time.time() - start |
| 99 | #print ("{:5f} ".format(elapsed)), | 120 | #print ("{:5f} ".format(elapsed)), |
| 100 | times.append(elapsed) | 121 | times.append(elapsed) |
| 122 | |||
| 101 | 123 | ||
| 102 | times = np.array(times) | 124 | times = np.array(times) |
| 103 | duration_min = int(total_frames/float(samplerate) // 60) | 125 | duration_min = int(total_frames/float(samplerate) // 60) |
| @@ -111,16 +133,5 @@ if __name__ == '__main__': · 7 unmodified lines | |||
| 111 | sys.exit(1) | 133 | sys.exit(1) |
| 112 | filename = sys.argv[1] | 134 | filename = sys.argv[1] |
| 113 | 135 | ||
| 114 | functions = [ | 136 | for f in test_functions: |
| 115 | read_file_aubio, | 137 | test_speed(eval(f), filename) |
| 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) | ||