Skip to content

Commit

Permalink
Don't sonify negative amplitudes in sonify.time_frequency
Browse files Browse the repository at this point in the history
Fixes #246
  • Loading branch information
bmcfee authored and craffel committed Jun 22, 2018
1 parent f7caa32 commit 56b9360
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mir_eval/sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def time_frequency(gram, frequencies, times, fs, function=np.sin, length=None):
gram : np.ndarray
``gram[n, m]`` is the magnitude of ``frequencies[n]``
from ``times[m]`` to ``times[m + 1]``
Non-positive magnitudes are interpreted as silence.
frequencies : np.ndarray
array of size ``gram.shape[0]`` denoting the frequency of
each row of gram
Expand Down Expand Up @@ -125,6 +128,9 @@ def _fast_synthesize(frequency):
# Use a flatiter to simulate a long 1D buffer
return long_signal.flat

# Threshold the tfgram to remove non-positive values
gram = np.maximum(gram, 0)

# Pre-allocate output signal
output = np.zeros(length)
for n, frequency in enumerate(frequencies):
Expand All @@ -138,8 +144,11 @@ def _fast_synthesize(frequency):
# Sum into the aggregate output waveform
output[start:end] += wave[start:end] * gram[n, m]

# Normalize
output /= np.abs(output).max()
# Normalize, but only if there's non-zero values
norm = np.abs(output).max()
if norm >= np.finfo(output.dtype).tiny:
output /= norm

return output


Expand Down
7 changes: 7 additions & 0 deletions tests/test_sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def test_chords():
assert len(signal) == 11*fs


def test_chord_x():
# This test verifies that X sonifies as silence
intervals = np.array([[0, 1]])
signal = mir_eval.sonify.chords(['X'], intervals, 8000)
assert not np.any(signal), signal


def test_pitch_contour():

# Generate some random pitch
Expand Down

0 comments on commit 56b9360

Please sign in to comment.