creaktive / pianolizer

An easy-to-use toolkit for music exploration and visualization, an audio spectrum analyzer helping you turn sounds into piano notes
https://sysd.org/pianolizer
MIT License
96 stars 6 forks source link

Question: How are the note bandwidths in your readme calculated? #3

Open aceamarco opened 2 years ago

aceamarco commented 2 years ago

Hello,

Thank you for making such a detailed README! I've really enjoyed reading through your theory section. However, I'm having trouble intuiting where your values for the 'note bandwidth' column are coming from. I understand where the 'estimated bandwidth' is coming from (if I understand it correctly it is the sample_rate / N. The Wikipedia page you linked with the piano key frequencies does not mention their bandwidths.

creaktive commented 2 years ago

Hi @aceamarco! I'm happy you liked it :)

Very interesting question regarding the bandwidth. Conceptually, the musical note frequencies are the centers of the frequency bands. For instance, the center is 440.0Hz for A4, but if you pick a bandwidth wide enough when analyzing the signal, you'll also detect neighbour notes!

So, for detection of notes, you need a bandwidth around the center frequency that does not overlap with the bandwidth of a neighbour note center frequency. The most intuitive value for the bandwidth is the difference (delta) between the center frequencies of 2 adjacent notes.

But here's where it gets confusing: the difference between A4 & A#4 (next note) is ~26.2Hz; and the difference between A4 & G#4 (previous note) is ~24.7Hz! And of course the difference gets smaller for the lower octaves, and bigger for the higher octaves...

On the other hand, what happens when the band around the central frequency is much narrower? Then the detector loses sensitivity for slightly out-of-tune instruments, since it has lower "tolerance", to speak so. Ultimately, very narrow bandwidth of, say, 0.1Hz, would imply very heavy processing, and it would be only good for detecting high-quality pitchforks, as opposed to real musical instruments ¯_(ツ)_/¯

Anyways, what I am actually using as "note bandwidth" is an approximation chosen in such way that it does not overlap with the next/previous note :)

Hope this helps

creaktive commented 2 years ago

Again, thanks for making me curious :) I made the bandwidth adjustable:

aceamarco commented 2 years ago

Oooh I see @creaktive, thank you for such a thorough explanation, this was really helpful! I hadn't intuited that the piano frequencies aren't evenly spaced on both sides. Would you say that the ideal bandwidth is the smaller of the two distances to a key's neighboring frequencies? For example, with the distances you mentioned for key A4, a bandwidth <= 24.7Hz would be ideal (because is the smaller distance compared to A4 to A#4 of 26.2Hz) since it doesn't overlap with either key.

creaktive commented 2 years ago

I wouldn't dare to say it is ideal, but, generally speaking, lowest possible bandwidth that does not overlap with the neighbour frequencies works really well :) There's one important detail, although: in pianolizer, both frequency & bandwidth values are rounded to the closest value that can be represented for a given sample rate. For instance, for 44100Hz sample rate, the A4 band will have the central frequency of 439.96Hz and the bandwidth of 25.88Hz. Close enough :)