Skip to content

Commit

Permalink
Add amplitude parameter to sonify.pitch_contour
Browse files Browse the repository at this point in the history
  • Loading branch information
rabitt authored and craffel committed Nov 10, 2017
1 parent 70ad30c commit d00cdd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
21 changes: 17 additions & 4 deletions mir_eval/sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def __interpolator(x):
return output


def pitch_contour(times, frequencies, fs, function=np.sin, length=None,
kind='linear'):
def pitch_contour(times, frequencies, fs, amplitudes=None, function=np.sin,
length=None, kind='linear'):
'''Sonify a pitch contour.
Parameters
Expand All @@ -200,6 +200,10 @@ def pitch_contour(times, frequencies, fs, function=np.sin, length=None,
fs : int
desired sampling rate of the output signal
amplitudes : np.ndarray
amplitude measurments, nonnegative
defaults to ``np.ones((length,))``
function : function
function to use to synthesize notes, should be :math:`2\pi`-periodic
Expand All @@ -208,7 +212,7 @@ def pitch_contour(times, frequencies, fs, function=np.sin, length=None,
defaults to ``max(times)*fs``
kind : str
Interpolation mode for the frequency estimator.
Interpolation mode for the frequency and amplitude values.
See: ``scipy.interpolate.interp1d`` for valid settings.
Returns
Expand All @@ -233,8 +237,17 @@ def pitch_contour(times, frequencies, fs, function=np.sin, length=None,
# Estimate frequency at sample points
f_est = f_interp(np.arange(length))

if amplitudes is None:
a_est = np.ones((length, ))
else:
# build an amplitude interpolator
a_interp = interp1d(
times * fs, amplitudes, kind=kind,
fill_value=0.0, bounds_error=False, copy=False)
a_est = a_interp(np.arange(length))

# Sonify the waveform
return function(np.cumsum(f_est))
return a_est * function(np.cumsum(f_est))


def chroma(chromagram, times, fs, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_sonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_pitch_contour():
noise = scipy.ndimage.gaussian_filter1d(np.random.randn(len(times)),
sigma=256)
freqs = 440.0 * 2.0**(16 * noise)
amps = np.linspace(0, 1, num=5 * fs, endpoint=True)

# negate a bunch of sequences
idx = np.unique(np.random.randint(0, high=len(times), size=32))
Expand All @@ -95,3 +96,9 @@ def test_pitch_contour():
x = mir_eval.sonify.pitch_contour(times + 5.0, freqs, fs, length=fs * 7)
assert len(x) == fs * 7
assert np.allclose(x[:fs * 5], x[0])

# Test with explicit amplitude
x = mir_eval.sonify.pitch_contour(times, freqs, fs, length=fs * 7,
amplitudes=amps)
assert len(x) == fs * 7
assert np.allclose(x[0], 0)

0 comments on commit d00cdd6

Please sign in to comment.