source: python/demos/demo_reading_speed.py @ 8147e03

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 8147e03 was 07867cd, checked in by Paul Brossier <piem@piem.org>, 9 years ago

demos/demo_reading_speed.py: add reading speed tests

  • Property mode set to 100644
File size: 2.6 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
8This file depends on audioread and librosa:
9    https://github.com/beetbox/audioread
10    https://github.com/bmcfee/librosa
11
12"""
13
14import numpy as np
15import aubio
16import audioread
17import librosa
18
19def read_file_audioread(filename):
20    # taken from librosa.util.utils
21    def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32):
22        # Invert the scale of the data
23        scale = 1./float(1 << ((8 * n_bytes) - 1))
24        # Construct the format string
25        fmt = '<i{:d}'.format(n_bytes)
26        # Rescale and format the data buffer
27        out = scale * np.frombuffer(buf, fmt).astype(dtype)
28        out = out.reshape(2, -1)
29        return out
30
31    with audioread.audio_open(filename) as f:
32        total_frames = 0
33        for buf in f:
34            samples = convert_buffer_to_float(buf)
35            total_frames += samples.shape[1]
36        return total_frames, f.samplerate
37
38def load_file_librosa(filename):
39    y, sr = librosa.load(filename, sr = None)
40    return len(y), sr
41
42def read_file_aubio(filename):
43    f = aubio.source(filename, hop_size = 1024)
44    total_frames = 0
45    while True:
46        samples, read = f()
47        total_frames += read
48        if read < f.hop_size: break
49    return total_frames, f.samplerate
50
51def load_file_aubio(filename):
52    f = aubio.source(filename, hop_size = 1024)
53    y = np.zeros(f.duration, dtype = aubio.float_type)
54    total_frames = 0
55    while True:
56        samples, read = f()
57        y[total_frames:total_frames + read] = samples[:read]
58        total_frames += read
59        if read < f.hop_size: break
60    assert len(y) == total_frames
61    return total_frames, f.samplerate
62
63def test_speed(function, filename):
64    times = []
65    for i in range(10):
66        start = time.time()
67        total_frames, samplerate = function(filename)
68        elapsed = time.time() - start
69        #print ("{:5f} ".format(elapsed)),
70        times.append(elapsed)
71    #print
72    times = np.array(times)
73    duration_min = int(total_frames/float(samplerate) // 60)
74    str_format = '{:25s} took {:5f} seconds avg (±{:5f}) to run on a ~ {:d} minutes long file'
75    print (str_format.format(function.__name__, times.mean(), times.std(), duration_min ))
76
77if __name__ == '__main__':
78    import sys, time
79    if len(sys.argv) < 2:
80        print ("not enough arguments")
81        sys.exit(1)
82    filename = sys.argv[1]
83
84    functions = [read_file_aubio, load_file_aubio, read_file_audioread, load_file_librosa]
85    for f in functions:
86        test_speed(f, filename)
Note: See TracBrowser for help on using the repository browser.