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

Divide by zero in log2 calculation #7

Closed
dewhisna opened this issue Apr 22, 2018 · 2 comments
Closed

Divide by zero in log2 calculation #7

dewhisna opened this issue Apr 22, 2018 · 2 comments

Comments

@dewhisna
Copy link

Regardless of the source audio, I get the following warning:

audio_to_midi_melodia.py:142: RuntimeWarning: divide by zero encountered in log2
  midi = 69 + 12*np.log2(hz_nonneg/440.)

In this code block:

def hz2midi(hz):

    # convert from Hz to midi note
    hz_nonneg = hz.copy()
    hz_nonneg[hz <= 0] = 0
    midi = 69 + 12*np.log2(hz_nonneg/440.)
    midi[midi <= 0] = 0

    # round
    midi = np.round(midi)

    return midi

To silence the warning, I edited that to initialize the other entries to a very small value instead of 0, changing that line above the warning to be:

    hz_nonneg[hz <= 0] = 1.e-15;

I don't know if that's the preferred fix or not, as I haven't fully checked to see how it affects the remaining code, but it seems to work OK to get rid of that warning. And the resulting midi files, on the ones that I checked, were identical with or without the fix.

Otherwise, this script and the Melodia plugin work fantastically! Great job with it!

@justinsalamon
Copy link
Owner

justinsalamon commented Apr 23, 2018

Melodia indicates unvoiced frames with non-positive (negative or zero) values. The hz2midi function sets these to 0 before taking the log, which will return -Inf, and then resets these values to 0 again after taking the log. This is the intended behavior. I could probably achieve the same result more elegantly so as to avoid the warning, but the overall functionality is correct.

Otherwise, this script and the Melodia plugin work fantastically! Great job with it!

Cheers!

@justinsalamon
Copy link
Owner

Updated the conversion code to avoid the warnings 4821cde

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

No branches or pull requests

2 participants