python/demos/demo_reading_speed.py: disable other packages by default
authorPaul Brossier <piem@piem.org>
Tue, 10 May 2016 20:08:06 +0000 (22:08 +0200)
committerPaul Brossier <piem@piem.org>
Tue, 10 May 2016 20:08:06 +0000 (22:08 +0200)
python/demos/demo_reading_speed.py

index 1158cec..92d9c24 100755 (executable)
@@ -5,24 +5,34 @@
 
 Compare the speed of several methods for reading and loading a sound file.
 
 
 Compare the speed of several methods for reading and loading a sound file.
 
-This file depends on the following packages:
+Optionally, this file can make use of the following packages:
 
     - audioread     https://github.com/beetbox/audioread
 
     - audioread     https://github.com/beetbox/audioread
+    - scipy         https://scipy.org
     - librosa       https://github.com/bmcfee/librosa
     - pydub         https://github.com/jiaaro/pydub
 
     - librosa       https://github.com/bmcfee/librosa
     - pydub         https://github.com/jiaaro/pydub
 
+Uncomment the function names below and send us your speed results!
+
 """
 
 """
 
+
+test_functions = [
+            "read_file_aubio",
+            "load_file_aubio",
+            #"load_file_scipy",
+            #"load_file_scipy_mmap",
+            #"read_file_audioread",
+            #"load_file_librosa",
+            #"read_file_pydub",
+            #"load_file_pydub",
+            ]
+
+
 import numpy as np
 import numpy as np
-import aubio
-"""
-import audioread
-import librosa
-import scipy.io.wavfile
-from pydub import AudioSegment
-"""
 
 def read_file_audioread(filename):
 
 def read_file_audioread(filename):
+    import audioread
     # taken from librosa.util.utils
     def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32):
         # Invert the scale of the data
     # taken from librosa.util.utils
     def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32):
         # Invert the scale of the data
@@ -42,33 +52,39 @@ def read_file_audioread(filename):
         return total_frames, f.samplerate
 
 def load_file_librosa(filename):
         return total_frames, f.samplerate
 
 def load_file_librosa(filename):
+    import librosa
     y, sr = librosa.load(filename, sr = None)
     #print y.mean(), y.shape
     return len(y), sr
 
 def load_file_scipy(filename):
     y, sr = librosa.load(filename, sr = None)
     #print y.mean(), y.shape
     return len(y), sr
 
 def load_file_scipy(filename):
+    import scipy.io.wavfile
     sr, y = scipy.io.wavfile.read(filename)
     y = y.astype('float32') / 32767
     #print y.mean(), y.shape
     return len(y), sr
 
 def load_file_scipy_mmap(filename):
     sr, y = scipy.io.wavfile.read(filename)
     y = y.astype('float32') / 32767
     #print y.mean(), y.shape
     return len(y), sr
 
 def load_file_scipy_mmap(filename):
+    import scipy.io.wavfile
     sr, y = scipy.io.wavfile.read(filename, mmap = True)
     #print y.mean(), y.shape
     return len(y), sr
 
 def read_file_pydub(filename):
     sr, y = scipy.io.wavfile.read(filename, mmap = True)
     #print y.mean(), y.shape
     return len(y), sr
 
 def read_file_pydub(filename):
+    from pydub import AudioSegment
     song = AudioSegment.from_file(filename)
     song.get_array_of_samples()
     return song.frame_count(), song.frame_rate
 
 def load_file_pydub(filename):
     song = AudioSegment.from_file(filename)
     song.get_array_of_samples()
     return song.frame_count(), song.frame_rate
 
 def load_file_pydub(filename):
+    from pydub import AudioSegment
     song = AudioSegment.from_file(filename)
     y = np.asarray(song.get_array_of_samples(), dtype = 'float32')
     y = y.reshape(song.channels, -1) / 32767.
     return song.frame_count(), song.frame_rate
 
 def read_file_aubio(filename):
     song = AudioSegment.from_file(filename)
     y = np.asarray(song.get_array_of_samples(), dtype = 'float32')
     y = y.reshape(song.channels, -1) / 32767.
     return song.frame_count(), song.frame_rate
 
 def read_file_aubio(filename):
+    import aubio
     f = aubio.source(filename, hop_size = 1024)
     total_frames = 0
     while True:
     f = aubio.source(filename, hop_size = 1024)
     total_frames = 0
     while True:
@@ -78,6 +94,7 @@ def read_file_aubio(filename):
     return total_frames, f.samplerate
 
 def load_file_aubio(filename):
     return total_frames, f.samplerate
 
 def load_file_aubio(filename):
+    import aubio
     f = aubio.source(filename, hop_size = 1024)
     y = np.zeros(f.duration, dtype = aubio.float_type)
     total_frames = 0
     f = aubio.source(filename, hop_size = 1024)
     y = np.zeros(f.duration, dtype = aubio.float_type)
     total_frames = 0
@@ -94,10 +111,15 @@ def test_speed(function, filename):
     times = []
     for i in range(10):
         start = time.time()
     times = []
     for i in range(10):
         start = time.time()
-        total_frames, samplerate = function(filename)
+        try:
+            total_frames, samplerate = function(filename)
+        except ImportError as e:
+            print ("error: failed importing {:s}".format(e))
+            return
         elapsed = time.time() - start
         #print ("{:5f} ".format(elapsed)),
         times.append(elapsed)
         elapsed = time.time() - start
         #print ("{:5f} ".format(elapsed)),
         times.append(elapsed)
+
     #print
     times = np.array(times)
     duration_min = int(total_frames/float(samplerate) // 60)
     #print
     times = np.array(times)
     duration_min = int(total_frames/float(samplerate) // 60)
@@ -111,16 +133,5 @@ if __name__ == '__main__':
         sys.exit(1)
     filename = sys.argv[1]
 
         sys.exit(1)
     filename = sys.argv[1]
 
-    functions = [
-            read_file_aubio,
-            load_file_aubio,
-            #load_file_scipy,
-            #load_file_scipy_mmap,
-            #read_file_audioread,
-            #load_file_librosa,
-            #read_file_pydub,
-            #load_file_pydub,
-            ]
-
-    for f in functions:
-        test_speed(f, filename)
+    for f in test_functions:
+        test_speed(eval(f), filename)