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 time stretch round error #1881

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

orkunkinay
Copy link

This PR fixes a TypeError in the time_stretch function where round() was applied to an ndarray inappropriately. The fix replaces round() with np.round() to handle the numpy arrays correctly.

The error message was: TypeError: type numpy.ndarray doesn't define __round__ method.

Changes:

  • Updated time_stretch function in effects.py to use np.round() instead of round().
  • Updated AUTHORS.md to add Orkun Kınay as collaborator.

@bmcfee
Copy link
Member

bmcfee commented Oct 8, 2024

This PR fixes a TypeError in the time_stretch function where round() was applied to an ndarray inappropriately. The fix replaces round() with np.round() to handle the numpy arrays correctly.

Thanks for this, but I'm not sure why it's necessary. The line in question:

len_stretch = int(round(y.shape[-1] / rate))

Operates on two float values: y.shape[-1] and rate. In what case could either of these be an ndarray?

Copy link

codecov bot commented Oct 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.73%. Comparing base (d5aa7e1) to head (74e625e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1881   +/-   ##
=======================================
  Coverage   98.73%   98.73%           
=======================================
  Files          35       35           
  Lines        4656     4656           
=======================================
  Hits         4597     4597           
  Misses         59       59           
Flag Coverage Δ
unittests 98.73% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@orkunkinay
Copy link
Author

I am getting the bpm's of the songs with librosa.beat.beat_track which outputs Tuple[_FloatLike_co | ndarray, ndarray]. Then I divide the bpm's of the songs to get the rate which again gives ndarray as output. Then I pass that ndarray rate as parameter to librosa.effects.time_stretch. I am using librosa's own bpm detecting function which gives ndarray, that is the reason I wanted to open this PR and tell my idea.

@bmcfee
Copy link
Member

bmcfee commented Oct 8, 2024

Ah I see - I hadn't considered plugging tempo estimates into the time_stretch like you're doing. That makes sense.

There are other bits of this function that will not be correct if rate is an ndarray though; the docstring and type annotation should be revised if we want to explicitly support it. Additionally, we should verify that rate.size == 1, otherwise the non-negativity check will fail, as will the istft call that would have a multidimensional length value.

@bmcfee
Copy link
Member

bmcfee commented Dec 3, 2024

Finally circling back on this after a hectic couple of months. I'm actually unable to reproduce the behavior that motivated the change here. Can you fill in the issue template and document your environment, specifically the version of numpy you're using?

On 1.26.4, I can do the following without issue:

>>> import numpy as np
>>> y = np.random.randn(2, 200)
>>> rate = 1.5
>>> int(round(y.shape[-1] / rate))
133
>>> int(np.round(y.shape[-1] / rate))
133
>>> rate = np.array(1.5)  # Force rate to be an array
>>> int(round(y.shape[-1] / rate))
133
>>> int(np.round(y.shape[-1] / rate))
133

Does this not work in numpy 2.x or something?

Or, is it the case that your rate parameter is actually multidimensional (and not just an ndarray object of size=1 and shape=()). If that's the case, then the function is not going to work anyway because it would result in a variable-length output array allocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants