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

Fix length check in util.frame() #386

Merged
merged 1 commit into from
Jul 8, 2016
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
8 changes: 4 additions & 4 deletions librosa/util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def frame(y, frame_length=2048, hop_length=512):

'''

if len(y) < frame_length:
raise ParameterError('Buffer is too short (n={:d})'
' for frame_length={:d}'.format(len(y), frame_length))

if hop_length < 1:
raise ParameterError('Invalid hop_length: {:d}'.format(hop_length))

Expand All @@ -88,10 +92,6 @@ def frame(y, frame_length=2048, hop_length=512):
# Compute the number of frames that will fit. The end may get truncated.
n_frames = 1 + int((len(y) - frame_length) / hop_length)

if n_frames < 1:
raise ParameterError('Buffer is too short (n={:d})'
' for frame_length={:d}'.format(len(y), frame_length))

# Vertical stride is one sample
# Horizontal stride is `hop_length` samples
y_frames = as_strided(y, shape=(frame_length, n_frames),
Expand Down
11 changes: 10 additions & 1 deletion tests/test_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,20 @@ def test_frame_contiguous():

@raises(librosa.ParameterError)
def test_frame_size():
'''frame: len(y) == 128, frame_length==256, hop_length=128'''
# frame: len(y) == 128, frame_length==256, hop_length=128
y = np.zeros(64)
librosa.util.frame(y, frame_length=256, hop_length=128)


@raises(librosa.ParameterError)
def test_frame_size_difference():
# In response to issue #385
# https://github.com/librosa/librosa/issues/385
# frame: len(y) == 129, frame_length==256, hop_length=128
y = np.zeros(129)
librosa.util.frame(y, frame_length=256, hop_length=128)


@raises(librosa.ParameterError)
def test_stft_bad_window():

Expand Down