Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More sonification fixes #418

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions mir_eval/sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ def time_frequency(

# Default value for length
if length is None:
length = int(times[-1, 1] * fs)
length = int(np.max(times) * fs)

last_time_in_secs = float(length) / fs

if time_converted and times.shape[0] != gram.shape[1]:
times = np.vstack((times, [times[-1, 1], last_time_in_secs]))
times = np.vstack((times, [np.max(times), last_time_in_secs]))

if times.shape[0] != gram.shape[1]:
raise ValueError(
Expand Down Expand Up @@ -183,15 +183,14 @@ def time_frequency(
bounds_error=False,
fill_value=(gram[:, 0], gram[:, -1]),
)
signal = interpolator(np.arange(length))
else:
# NOTE: This is a special case where there is only one time interval.
# scipy 1.10 and above handle this case directly with the interp1d above,
# but older scipy's do not. This is a workaround for that.
#
# In the 0.9 release, we can bump the minimum scipy to 1.10 and remove this
interpolator = _const_interpolator(gram[:, 0])

signal = interpolator(np.arange(length))
signal = np.tile(gram[:, 0], (1, length))

for n, frequency in enumerate(frequencies):
# Get a waveform of length samples at this frequency
Expand All @@ -213,17 +212,6 @@ def time_frequency(
return output


def _const_interpolator(value):
"""Return a function that returns `value`
no matter the input.
"""

def __interpolator(x):
return value

return __interpolator


def _fast_synthesize(frequency, n_dec, fs, function, length):
"""Efficiently synthesize a signal.
Generate one cycle, and simulate arbitrary repetitions
Expand Down
22 changes: 22 additions & 0 deletions tests/test_sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ def test_time_frequency(fs):
assert len(signal) == 5 * fs


def test_time_frequency_const():

# Sonify with a single interval to hit the const interpolator
s1 = mir_eval.sonify.time_frequency(
np.ones((1, 1)),
np.array([60]),
np.array([[0, 1]]),
8000,
)
# Sonify with two tiem intervals to hit the regular interpolator
# but the second frequency will have no energy, so it doesn't
# change the resulting signal
s2 = mir_eval.sonify.time_frequency(
np.array([[1, 1], [0, 0]]),
np.array([60, 90]),
np.array([[0, 1], [0, 1]]),
8000,
)

assert np.allclose(s1, s2)


def test_time_frequency_offset():
fs = 8000
# Length is 3 seconds, first interval starts at 5.
Expand Down
Loading