question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

make tuning frequency customizable in midi_to_hz() function

See original GitHub issue

Description

Make midi_to_hz() accept a custom tuning frequency. Currently, 440.0 is hard coded.

I think it would be nice to have midi_to_hz() accept a different tuning frequency. That way, it would be easy to generate frequencies for notes following that tuning, etc.

I.e., turn midi_to_hz(notes) to midi_to_hz(notes, A4=440.0).

And change:

return 440.0 * (2.0 ** ((np.asanyarray(notes) - 69.0)/12.0))

to:

return A4 * (2.0 ** ((np.asanyarray(notes) - 69.0)/12.0)).

(I am not sure whether librosa has “agreed upon” a note naming convention for tuning. Is C4 the middle C (261 Hz in equal temperament) in librosa? I think so.)

Code here: https://librosa.github.io/librosa/_modules/librosa/core/time_frequency.html#midi_to_hz

The change would add the the functionality of choosing tuning frequency to other functions that depend on midi_to_hz() as well, such as note_to_hz(): https://librosa.github.io/librosa/_modules/librosa/core/time_frequency.html#note_to_hz

I can write a PR if this seems helpful.

Thanks and BR!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bmcfeecommented, May 17, 2020

Thanks @jlw365 !

* Do you want to wait on adding a `units` parameter of sorts in additional methods as discussed [here](https://github.com/librosa/librosa/issues/977#issuecomment-525722692) then?

Yeah, let’s hold off on that one, since it might require a good amount of integration and auditing to make sure it’s consistently implemented across the package. I think for now, manually converting ref to tuning is totally fine. Anyone that wants to use ref pitches instead can afford the extra line of code to convert. 😁

* Confirming that these would be added to the core [time_frequency module](https://librosa.github.io/librosa/_modules/librosa/core/time_frequency.html), with unit tests in the respective location, and not in general [utils](https://librosa.github.io/librosa/util.html)?

Yup, these would fit in with all the other unit conversions.

* Thoughts on naming them `ref_pitch_to_tuning` and `tuning_to_ref_pitch`?

How about A4_to_tuning (and vice versa)? ref_pitch is a bit wordy, and might not be precise enough for readers to directly grok from skimming the documentation. A440_to_tuning would be good because A440 is more descriptive than “reference pitch”, but bad because if the input was actually 440, we wouldn’t be calling the function at all. A4_to_tuning allows for any frequency corresponding to A4, but I think most readers looking for this kind of function would immediately understand what it is.

I’m open to other suggestions though! Maybe we can go with this, and punt further discussion to code review?

1reaction
juliawilkinscommented, May 17, 2020

Hi @bmcfee, would be happy to take this issue on. Just wanted to confirm a few things based on the above discussion:

  • For this initial PR, based on your recent comment, sounds like you’d like to start by adding the two ref-pitch to tuning conversion util methods described above. Do you want to wait on adding a units parameter of sorts in additional methods as discussed here then?
  • Confirming that these would be added to the core time_frequency module, with unit tests in the respective location, and not in general utils?
  • Thoughts on naming them ref_pitch_to_tuning and tuning_to_ref_pitch?

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Midi/Frequency Conversion - McGill University
Midi/Frequency Conversion. Western music notation and scale systems are based on "octave equivalence". Sounds related by integer multiples of an octave are ...
Read more >
Python audio processing at lightspeed Part 3: pyo
The midiToHz() function takes a MIDI note number, which might be fractional, and converts it into a frequency in Hz. It can also...
Read more >
Programming with midi, and tuning notes to specific frequencies
As supposed by @Brad, here is a solution with the MIDI Tuning Standard: The steps are: Request for tuning change; Map all the...
Read more >
Note names, MIDI numbers and frequencies - W
To convert from any frequency to pitch (i.e. to the nearest note and how far it is out of tune), go to the...
Read more >
MANUAL SONIC PI
The Align button will neaten the code for you to make it look more professional. ... The frequency or pitch of a waveform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found