Skip to content

Commit 74b24f3

Browse files
authored
More sonification fixes (#418)
* fixing const-interpolator again * adding tests and formatting
1 parent 9458174 commit 74b24f3

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

mir_eval/sonify.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ def time_frequency(
116116

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

121121
last_time_in_secs = float(length) / fs
122122

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

126126
if times.shape[0] != gram.shape[1]:
127127
raise ValueError(
@@ -183,15 +183,14 @@ def time_frequency(
183183
bounds_error=False,
184184
fill_value=(gram[:, 0], gram[:, -1]),
185185
)
186+
signal = interpolator(np.arange(length))
186187
else:
187188
# NOTE: This is a special case where there is only one time interval.
188189
# scipy 1.10 and above handle this case directly with the interp1d above,
189190
# but older scipy's do not. This is a workaround for that.
190191
#
191192
# In the 0.9 release, we can bump the minimum scipy to 1.10 and remove this
192-
interpolator = _const_interpolator(gram[:, 0])
193-
194-
signal = interpolator(np.arange(length))
193+
signal = np.tile(gram[:, 0], (1, length))
195194

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

215214

216-
def _const_interpolator(value):
217-
"""Return a function that returns `value`
218-
no matter the input.
219-
"""
220-
221-
def __interpolator(x):
222-
return value
223-
224-
return __interpolator
225-
226-
227215
def _fast_synthesize(frequency, n_dec, fs, function, length):
228216
"""Efficiently synthesize a signal.
229217
Generate one cycle, and simulate arbitrary repetitions

tests/test_sonify.py

+22
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ def test_time_frequency(fs):
5050
assert len(signal) == 5 * fs
5151

5252

53+
def test_time_frequency_const():
54+
55+
# Sonify with a single interval to hit the const interpolator
56+
s1 = mir_eval.sonify.time_frequency(
57+
np.ones((1, 1)),
58+
np.array([60]),
59+
np.array([[0, 1]]),
60+
8000,
61+
)
62+
# Sonify with two tiem intervals to hit the regular interpolator
63+
# but the second frequency will have no energy, so it doesn't
64+
# change the resulting signal
65+
s2 = mir_eval.sonify.time_frequency(
66+
np.array([[1, 1], [0, 0]]),
67+
np.array([60, 90]),
68+
np.array([[0, 1], [0, 1]]),
69+
8000,
70+
)
71+
72+
assert np.allclose(s1, s2)
73+
74+
5375
def test_time_frequency_offset():
5476
fs = 8000
5577
# Length is 3 seconds, first interval starts at 5.

0 commit comments

Comments
 (0)