From eecd4f657a896e6037e4796629ec3bfbb1530ccb Mon Sep 17 00:00:00 2001 From: Vincent Lostanlen Date: Fri, 16 Aug 2019 12:56:28 -0400 Subject: [PATCH] write transform function --- birdvoxpaint/birdvoxpaint.py | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 birdvoxpaint/birdvoxpaint.py diff --git a/birdvoxpaint/birdvoxpaint.py b/birdvoxpaint/birdvoxpaint.py new file mode 100644 index 0000000..83ced63 --- /dev/null +++ b/birdvoxpaint/birdvoxpaint.py @@ -0,0 +1,76 @@ +import librosa +from librosa.util.exceptions.ParameterError +import numpy as np + + +def transform(filename=None, y=None, sr=22050, + n_fft=256, hop_length=32, frame_length=256, fmin=1000, fmax=10000, + indices=[average_energy], segment_duration=10, + verbose=False, n_jobs=-1): + + if n_jobs=-1: + n_jobs = joblib.cpu_count() + + if filename is not None: + if y is not None: + raise ParameterError( + 'Either y or filename must be equal to None') + file_duration = librosa.get_duration(filename=filename) + orig_sr = librosa.get_samplerate(filename) + block_length = segment_duration * orig_sr * n_jobs + y_blocks = librosa.stream(filename, block_length=block_length, + frame_length=frame_length, hop_length=hop_length) + if sr is None: + sr = orig_sr + else: + if (y is None) or (sr is None): + raise ParameterError( + 'At least one of (y, sr) or filename must be provided') + librosa.util.valid_audio(y, mono=True) + block_length = segment_duration * sr * n_jobs + file_duration = librosa.get_duration(y=y, sr=sr) + y_blocks = librosa.util.frame(y, + frame_length=block_length, hop_length=block_length) + + if fmin < 0: + raise ParameterError("fmin={} must be nonnegative".format(fmin)) + + if fmax > (sr/2): + raise ParameterError( + "fmax={} must be smaller than sample rate sr={}".format(fmax, sr)) + + n_indices = len(indices) + n_blocks = int(np.ceil(file_duration / block_duration)) + fft_frequencies = librosa.fft_frequencies(sr=sr, n_fft=n_fft) + bin_start = np.where(fft_frequencies>=fmin)[0][0] + bin_stop = np.where(fft_frequencies