shithub: aubio

Download patch

ref: 8fb567c216b107a503a12de4fa9bf3f30fa59a5f
parent: 143682ba94bf859c8bf730650e52af15addb7d00
author: Paul Brossier <piem@piem.org>
date: Tue May 10 17:53:01 EDT 2016

python/demos: remove unused import and variables

--- a/python/demos/demo_filterbank.py
+++ b/python/demos/demo_filterbank.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 
 from aubio import filterbank, fvec
-from pylab import loglog, show, subplot, xlim, ylim, xlabel, ylabel, title
+from pylab import loglog, show, xlim, ylim, xlabel, ylabel, title
 from numpy import vstack, arange
 
 win_s = 2048
--- a/python/demos/demo_filterbank_slaney.py
+++ b/python/demos/demo_filterbank_slaney.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 
 from aubio import filterbank
-from numpy import array, arange, vstack
+from numpy import arange, vstack
 
 win_s = 8192
 samplerate = 16000
--- a/python/demos/demo_keyboard.py
+++ b/python/demos/demo_keyboard.py
@@ -25,7 +25,6 @@
     return xb, xw, 2/3. *scaleb, 1/2. * scalew
 
 def create_keyboard_patches(firstnote, lastnote, ax = None):
-    import numpy as np
     import matplotlib.pyplot as plt
     from matplotlib.path import Path
     import matplotlib.patches as mpatches
--- a/python/demos/demo_mel-energy.py
+++ b/python/demos/demo_mel-energy.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 
 import sys
-from aubio import fvec, source, pvoc, filterbank
+from aubio import source, pvoc, filterbank
 from numpy import vstack, zeros
 
 win_s = 512                 # fft size
--- a/python/demos/demo_mfcc.py
+++ b/python/demos/demo_mfcc.py
@@ -2,7 +2,7 @@
 
 import sys
 from aubio import source, pvoc, mfcc
-from numpy import array, vstack, zeros
+from numpy import vstack, zeros
 
 win_s = 512                 # fft size
 hop_s = win_s / 4           # hop size
--- a/python/demos/demo_onset_plot.py
+++ b/python/demos/demo_onset_plot.py
@@ -2,7 +2,7 @@
 
 import sys
 from aubio import onset, source
-from numpy import array, hstack, zeros
+from numpy import hstack, zeros
 
 win_s = 512                 # fft size
 hop_s = win_s / 2           # hop size
--- a/python/demos/demo_pitch.py
+++ b/python/demos/demo_pitch.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 
 import sys
-from aubio import source, pitch, freqtomidi
+from aubio import source, pitch
 
 if len(sys.argv) < 2:
     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
--- a/python/demos/demo_pitch_sinusoid.py
+++ b/python/demos/demo_pitch_sinusoid.py
@@ -1,6 +1,6 @@
 #! /usr/bin/env python
 
-from numpy import random, sin, arange, ones, zeros
+from numpy import random, sin, arange, zeros
 from math import pi
 from aubio import fvec, pitch
 
@@ -10,7 +10,6 @@
 def run_pitch(p, input_vec):
   f = fvec (p.hop_size)
   cands = []
-  count = 0
   for vec_slice in input_vec.reshape((-1, p.hop_size)):
     f[:] = vec_slice
     cands.append(p(f))
--- a/python/demos/demo_pysoundcard_record.py
+++ b/python/demos/demo_pysoundcard_record.py
@@ -21,9 +21,8 @@
             mono_vec = vec.sum(-1) / float(s.channels[0])
             g(mono_vec, hop_size)
             total_frames += hop_size
-    except KeyboardInterrupt, e:
+    except KeyboardInterrupt:
         print "stopped after", "%.2f seconds" % (total_frames / s.samplerate)
-        pass
     s.stop()
 
 if __name__ == '__main__':
--- a/python/demos/demo_reading_speed.py
+++ b/python/demos/demo_reading_speed.py
@@ -5,16 +5,22 @@
 
 Compare the speed of several methods for reading and loading a sound file.
 
-This file depends on audioread and librosa:
-    https://github.com/beetbox/audioread
-    https://github.com/bmcfee/librosa
+This file depends on the following packages:
 
+    - audioread     https://github.com/beetbox/audioread
+    - librosa       https://github.com/bmcfee/librosa
+    - pydub         https://github.com/jiaaro/pydub
+
 """
 
 import numpy as np
 import aubio
+"""
 import audioread
 import librosa
+import scipy.io.wavfile
+from pydub import AudioSegment
+"""
 
 def read_file_audioread(filename):
     # taken from librosa.util.utils
@@ -25,7 +31,6 @@
         fmt = '<i{:d}'.format(n_bytes)
         # Rescale and format the data buffer
         out = scale * np.frombuffer(buf, fmt).astype(dtype)
-        out = out.reshape(2, -1)
         return out
 
     with audioread.audio_open(filename) as f:
@@ -32,13 +37,37 @@
         total_frames = 0
         for buf in f:
             samples = convert_buffer_to_float(buf)
+            samples = samples.reshape(f.channels, -1)
             total_frames += samples.shape[1]
         return total_frames, f.samplerate
 
 def load_file_librosa(filename):
     y, sr = librosa.load(filename, sr = None)
+    #print y.mean(), y.shape
     return len(y), sr
 
+def load_file_scipy(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):
+    sr, y = scipy.io.wavfile.read(filename, mmap = True)
+    #print y.mean(), y.shape
+    return len(y), sr
+
+def read_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):
+    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):
     f = aubio.source(filename, hop_size = 1024)
     total_frames = 0
@@ -58,6 +87,7 @@
         total_frames += read
         if read < f.hop_size: break
     assert len(y) == total_frames
+    #print y.mean(), y.shape
     return total_frames, f.samplerate
 
 def test_speed(function, filename):
@@ -81,6 +111,16 @@
         sys.exit(1)
     filename = sys.argv[1]
 
-    functions = [read_file_aubio, load_file_aubio, read_file_audioread, load_file_librosa]
+    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)
--- a/python/demos/demo_sink_create_woodblock.py
+++ b/python/demos/demo_sink_create_woodblock.py
@@ -3,7 +3,7 @@
 import sys
 from math import pi, e
 from aubio import sink
-from numpy import arange, resize, sin, exp, zeros
+from numpy import arange, sin, exp, zeros
 
 if len(sys.argv) < 2:
     print 'usage: %s <outputfile> [samplerate]' % sys.argv[0]
--- a/python/demos/demo_tempo_plot.py
+++ b/python/demos/demo_tempo_plot.py
@@ -39,7 +39,7 @@
 
 if len(beats) > 1:
     # do plotting
-    from numpy import array, arange, mean, median, diff
+    from numpy import mean, median, diff
     import matplotlib.pyplot as plt
     bpms = 60./ diff(beats)
     print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm'
--- a/python/demos/demo_tss.py
+++ b/python/demos/demo_tss.py
@@ -44,3 +44,4 @@
     get_spectrogram(sys.argv[2])
     subplot(313)
     get_spectrogram(sys.argv[3])
+    show()
--- a/python/demos/demo_waveform_plot.py
+++ b/python/demos/demo_waveform_plot.py
@@ -1,7 +1,7 @@
 #! /usr/bin/env python
 
 import sys
-from aubio import pvoc, source
+from aubio import source
 from numpy import zeros, hstack
 
 def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, downsample = 2**4):