Display note value in a text box

Post and discuss Lemur Modules.
Post Reply
oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19
Display note value in a text box

Post by oldgearguy » 04 Feb 2014 10:23

There's probably lots of ways to do this, but I wanted a function that would allow me to pass in a MIDI note number (0-127) and have the textual representation displayed. While I was doing it, I also added the ability to offset where '0' is. I used the standard C-2 to G8 range for 0-127, but if you pass in an non-zero offset you can shift that to C0 -> G10 or any other range needed for your app.

The invocation would be something like: setattribute(NoteVal, 'content', txtNote(midi_val, 0)); where NoteVal is a Text object and midi_val is some 0-127 value.

The code is:

Code: Select all

decl tVal, trNotes, trSharps, i, posn;

// two separate arrays to represent C, C#, D, D#, E, F, F#, G, G#, A, A#, B
trNotes = {0x43, 0x43, 0x44, 0x44, 0x45, 0x46, 0x46, 0x47, 0x47, 0x41, 0x41, 0x42};
trSharps = {0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x23, 0x00, 0x23, 0x00, 0x23, 0x00};

i = 0;

// offset = 0 is a base way to go from C-2 to G8
// +12 for starting at C-1, +21 for starting at A-1, +24 for starting at C0, +36 for starting at C1
if (offset < 0) return ('ERR');

posn = midiVal + offset;

tVal[i++] = trNotes[posn % 12];
tVal[i]   = trSharps[posn % 12];
if (tVal[i] != 0)                   // if we have a sharp symbol, move to next space
   i++;
if (posn < 24)                    // 24 is c0 w. no offset.  0x2D is a '-'
   tVal[i++] = 0x2D;
if (posn > 143) {                // 144 is when you need a tens place.  0x31 is a '1'
   tVal[i++] = 0x31;
   posn = posn - 144 + 24;  // 24 is for the un-offset original 2 octaves
}
tVal[i] = 0x30 + abs(2 - floor(posn / 12));

return(arraytostring(tVal));

nick_liine
Liine Staff
Posts:340
Joined:01 Oct 2010 11:06

Re: Display note value in a text box

Post by nick_liine » 06 Feb 2014 11:58

FYI, an upcoming Lemur update will have support for string concatenation. This might help in generating the proper note+octave (for ex: F#3) before setting a 'labels' attribute.

Softcore
Regular
Posts:1639
Joined:04 Nov 2012 08:34

Re: Display note value in a text box

Post by Softcore » 06 Feb 2014 13:17

nick_liine wrote:FYI, an upcoming Lemur update will have support for string concatenation. This might help in generating the proper note+octave (for ex: F#3) before setting a 'labels' attribute.
Niiiiiiiiiiiiice! This will simplify a lot of scripts!!!

Post Reply