diff --git a/jams/sonify.py b/jams/sonify.py index 11df81b6..7e1b3e42 100644 --- a/jams/sonify.py +++ b/jams/sonify.py @@ -13,12 +13,14 @@ import six import numpy as np import mir_eval.sonify +from mir_eval.util import filter_kwargs from .eval import validate_annotation from .exceptions import NamespaceError __all__ = ['sonify'] + def clicks(annotation, sr=22050, length=None, **kwargs): '''Sonify clicks timings @@ -26,7 +28,8 @@ def clicks(annotation, sr=22050, length=None, **kwargs): interval, _ = annotation.data.to_interval_values() - return mir_eval.sonify.clicks(interval[:, 0], fs=sr, length=length, **kwargs) + return filter_kwargs(mir_eval.sonify.clicks, interval[:, 0], + fs=sr, length=length, **kwargs) def chord(annotation, sr=22050, length=None, **kwargs): @@ -34,7 +37,10 @@ def chord(annotation, sr=22050, length=None, **kwargs): intervals, chords = annotation.data.to_interval_values() - return mir_eval.sonify.chords(chords, intervals, fs=sr, length=length, **kwargs) + return filter_kwargs(mir_eval.sonify.chords, + chords, intervals, + fs=sr, length=length, + **kwargs) def pitch_midi(annotation, sr=22050, length=None, **kwargs): @@ -49,8 +55,21 @@ def pitch_midi(annotation, sr=22050, length=None, **kwargs): for t, n in enumerate(notes): gram[n, t] = 1.0 - return mir_eval.sonify.time_frequency(gram, freqs, intervals[:, 0], - fs=sr, length=length, **kwargs) + # Compress for efficiency + idx = gram.max(axis=1) > 0 + + gram = gram[idx] + freqs = freqs[idx] + + return filter_kwargs(mir_eval.sonify.time_frequency, + gram, freqs, intervals, + fs=sr, length=length, + **kwargs) + + +SONIFY_MAPPING = {'beat.*|segment.*|onset.*': clicks, + 'chord|chord_harte': chord, + 'pitch_midi': pitch_midi} def sonify(annotation, sr=22050, duration=None, **kwargs): @@ -81,9 +100,6 @@ def sonify(annotation, sr=22050, duration=None, **kwargs): If the annotation has an un-sonifiable namespace ''' - SONIFY_MAPPING = {'beat.*|segment.*|onset.*': clicks, - 'chord|chord_harte': chord, - 'pitch_midi': pitch_midi} length = None if duration is not None: length = int(duration * sr)