source: python/demos/demo_reading_speed.py @ 9682d9d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since 9682d9d was dee266f, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/demos/demo_reading_speed.py: avoid unused variable

  • Property mode set to 100755
File size: 4.1 KB
RevLine 
[07867cd]1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5
6Compare the speed of several methods for reading and loading a sound file.
7
[1e4d90f]8Optionally, 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]15Uncomment the function names below and send us your speed results!
16
[07867cd]17"""
18
[1e4d90f]19
20test_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]32import numpy as np
33
34def 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
54def 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
60def 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]67def 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
73def 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
79def 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]86def 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
96def 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
110def 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
129if __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)
Note: See TracBrowser for help on using the repository browser.