How to set a Slider's text box to display MIDI Note Numbers?

Is there some way to have the range of integers 0~127 automatically displayed as MIDI Note Numbers, i.e. C2, C#2, D2 etc.? There must be, but I can’t seem to locate it…

I’m not sure if there is an automatic way, but take these two functions that I use :slight_smile:

static int getOctaveForNoteNumber (const int noteNumber)
{
     int octave = (noteNumber / 12.0f) - 2;
    int index = ( (24 - noteNumber) % 12) * -1;
    
    if (index < 0)
    {
        octave--;
    }
    
    return octave;
};

static String noteNumberToName (const int noteNumber)
{
     StringArray noteName {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "Bb", "B"};
    
     int octave = getOctaveForNoteNumber (noteNumber);
     int index = ( (24 - noteNumber) % 12) * -1;
     
     if (index < 0)
     {
         index = 12 + index;
     }
    
    String note = noteName [ index ] + (String)octave;
    
    return note;
};
2 Likes

Thanks. I also have some functions like this from older projects. I just assumed that something this basic would already be a part of Slider… :slight_smile:

are you looking for MidiMessage::getMidiNoteName() ?

1 Like

Well, I know about that function. Here’s what I’m looking for:

You set up a Slider to display a range of 0 ~ 127. You move the slider, it shows in the number box 0 ~ 127. So far so good.

Now, you “tell it” to display this as MIDI Note Numbers, and it automagically does it. I guess that “option” doesn’t exist.

So in order to make that happen, do I need to get the value, turn it into a string using something like the code up above, and then do what? I’m not sure what to override, or how to tell the Slider that instead of displaying “60” it displays “C4”.

ah - so you can do it two ways, either on the Slider via textFromValue or with the stringFromInt param in the constructor if you’re using AudioProcessorValueTreeState and the related attachment classes.

If the slider is tied to a parameter, then you can set the valueToText lambda for that parameter to make the translation. In order to allow it to switch between number and note name, you can use [this] with that lambda, and then call a function in the Processor to that can query return the value based on the note number and the state of a flag you’ve set as to whether to return number or note names.

oh wow re MidiMessage::getMidiNoteName()

… wish i would find these things before thinking about my own functions :smiley: this happens way too often

N.B. It works with or without parameter. The Slider has the same textToValue function and vice versa as public member.

2 Likes

Regarding MidiMessage::getMidiNoteName() : I hadn’t realized that was a static function. That is perfect! Thanks.

Now, is there anything that goes the other way, i.e. to turn a MIDI Note Name (String) back into a number, i.e. “C4” -> 60 or 48, or do I have to roll my own?

i wrote my own - seems like there should be a static method in the same place :man_shrugging: