I created a table of musical notes and corresponding frequencies:
I would like to create a block that reports the musical note that is nearest a specified frequency. One approach would to create a loop and check each frequency in the table until the closest one is identified. Since there are only eight octaves of notes in the table (i.e., 96 items), that might not be an unreasonable approach. However, is there a more elegant strategy?
you could try incorporating some sort of binary search
or you could estimate the index of the frequency in the table. since musical scales are exponential, you’ll probably have to use logarithms to do this.
If you load the audio comp library, there's a note from hz block, which might be what you're looking for. I created a block that can translate a note number to a note name, so here it is.
not really important but whether you say, for example, C# or Db is dependent upon the scale. So the notes in the B major scale would be (getting this from memory i may be incorrect) B C# D# E F# G# A#, and not like B Db D# E F# G# A#, because in the latter D appears twice. In your situation, however, there isn’t really a defined scale so you can just do whatever you want I guess. Which is why I said this isn’t really important.
and one of my favorite little quick projects is to connect it with the play frequency command inside a forever loop to make an auto-tune "Cher" effect for whistling into my headset microphone.
But you need the Math behind that, because you're not getting the frequency from the microphone right?
I'm using this formula for it:
Of formatting the 12th root of 2? $$\sqrt[12]2$$ gives $$\sqrt[12]2$$. I was just too lazy to look it up. I'm embarrassed to say that I've forgotten how to make the 12 smaller than the 2 in LaTeX.