dualB / Melody

Melody offers a simple an powerful music text notation called MELO.
MIT License
13 stars 2 forks source link

Implement getNote to Melody #2

Open jcmk opened 2 years ago

jcmk commented 2 years ago

I propose adding a method to Melody to return the current note in String format, or, alternatively, a class method to turn getFrequency() back into a String representing the note. Ie. current_note = melody.getNote() // Returns "G" or "C#"

In my project I have an LED light show and servo motors synchronized to my music. Currently my LEDs cycle through a fixed pattern in time with the music. I would like to be able to have a certain pattern associated with each note. I could do this using getFrequency(), but this would obviously be a more cumbersome solution. I can think of other projects where having access to the currently playing note instead of just the frequency could be quite useful, such as a piano trainer that illuminated the keys that correspond to the current note. The user of the library could then trim the string depending on whether they cared about just the note, or also the octave, or whether it's sharp or flat, etc.

dualB commented 2 years ago

Inside the library, I already have the "note index", which is an integer value, positive or negative, where index 0 match the 4A at 440 Hz [..., ,-1 is 4G#, -2 is 4G, +1 is 4A#, +2 is 4B, ...]. I can simply expose the method "getNoteIndex()" : you will have all the freedom to map it to an array of strings, or use it in a switch case, or anything else. What do you think?

jcmk commented 2 years ago

I'll take a look at the source, it doesn't help that I know very little about music theory. I think that could work, though I might add a method which uses integer division and modulus to return the note and octave.

dualB commented 2 years ago

You will need to wait for the next version of Melody to access the method "getNoteIndex()", it will be ready maybe tomorrow.

Here's a a code to get the note (from 0=>A, 1=>A# .... to 11=>G#), and octave : int index = melo.getNoteIndex(); int note = index % 12 ; int octave = (index - note )/12 + 4 ;

dualB commented 2 years ago

You can access early version 1.2 directly in Github - it won't be accessible in Arduino Library Manager before I run more tests on it. https://github.com/dualB/Melody/archive/refs/heads/version-1.2.zip