# Code Module to get Instrument Range



## Big Bob (May 13, 2006)

Hi Guys,

Many scripts that we write require the user to specify the Instrument Range from lowest to highest key. Here is a fairly concise way of doing that, using the new KSP string variable enhancements. The User Function named 'Note2Key' can also be generally useful whenever you need to convert from a MIDI note# (60) to the Scale-Tone/Oct format (C3). Because of the string enhancements, the Tones array only needs to be declared one time. After that, any number of calls to Note2Key can be made with minimal in-line code generation.

In the example given, the low and high notes of the IR are held internally as MIDI note #s in integers named Low_Key and Top_Key. The string equivalents are held in the string variables LowKey and TopKey. You will need the latest NL Editor V0.98 to compile this source into a K2-ready source that you can run.

God Bless,

Bob

*on init* 
``_{ ------------------- Constants ------------------- }_ 
``*declare* *const* On := 1 
``*declare* *const* Off := 0 
``*declare* @SetCap```_{ 'Set Inst Range' button caption }_ 
``SetCap := ' Set Inst Range'```
``*declare* !Tones[12]``_{ Key tones array }_ 
``Tones[0] := 'C' 
``Tones[1] := 'C#' 
``Tones[2] := 'D' 
``Tones[3] := 'D#' 
``Tones[4] := 'E' 
``Tones[5] := 'F' 
``Tones[6] := 'F#' 
``Tones[7] := 'G' 
``Tones[8] := 'G#' 
``Tones[9] := 'A' 
``Tones[10] := 'A#' 
``Tones[11] := 'B' 
``_{ ------------- Low/High Key variables --------------- }_ 
``*declare* Low_Key```_{ Numeric value }_ 
``*declare* @LowKey```_{ String value }_ 
``*declare* @LowCap```_{ Caption }_ 
``Low_Key := 0 
``LowKey := 'C-2' 
``LowCap := ' Low Key: '```
``*declare* Top_Key```_{ Numeric value }_ 
``*declare* @TopKey```_{ String value }_ 
``*declare* @TopCap```_{ Caption }_ 
``Top_Key := 127 
``TopKey := 'G8' 
``TopCap := ' High Key: ' 
``_{ -------------- Control Panel Layout ---------------- }_ 
``*declare* ui_button SetIR``_{ Button to initiate setting }_ 
``set_text($SetIR,SetCap)``_{ of new Instrument Range }_ 
``move_control(SetIR,6,1) 
``SetIR :=Off 
```
``*declare* ui_label IR_Box (1,2) _{ Instrument Range Display Box }_ 
``_read_persistent_var(LowKey)``_{ Need to recover these before }_ 
``_read_persistent_var(TopKey)``_{ trying to update IR display }_ 
``_read_persistent_var(Low_Key) 
``_read_persistent_var(Top_Key)```
``move_control(IR_Box,6,2) 
``ShowIR``_{ Display default or last saved IR }_ 
```
``_{ ------------- General Variables --------------- }_ 
``*declare* Get_Low_Key``_{ Flag denoting which IR limit is to be entered next }_ 
```
``_{ ------- Save/Restore with NKP or NKI ---------- }_ 
``make_persistent(Low_Key) 
``make_persistent(LowKey) 
``make_persistent(Top_Key) 
``make_persistent(TopKey) 
*end on* _{ init }_ 

*on note* 
``*if* (SetIR = On) 
````SetInstRange 
``*end if* 
``_{ Rest of NCB goes here }_ 
*end on* 

*on ui_control*(SetIR) 
_(* Start or Abort a new IR input sequence *)_```
``*if* (SetIR = On)```````_{ Start setup mode for new IR input sequence }_ 
````Get_Low_Key := On```_{ Low/High key entry always starts with Low }_ 
````set_text(SetIR,' Hit lowest Key') _{ Prompt User for Low Key }_ 
``*else* _{ User abort of sequence }_ 
````set_text(SetIR,SetCap)``_{ Normal 'off button' caption }_ 
``*end if* 
*end on* _{ Set_Range Button }_ 

_(* ----------------- User Functions ------------------- *)_ 

*function* SetInstRange 
_(* Allows user to set the instrument range from the keyboard *)_ 
``*if* (Get_Low_Key = On) 
````Low_Key := EVENT_NOTE`````_{ Update numeric value of Low_Key }_ 
````ShowIR````````````````````_{ Update display of Instrument Range }_ 
````set_text(SetIR,' Hit Highest Key')``_{ Prompt user for High Key }_ 
````Get_Low_Key := Off```_{ Assume next note will be the High Key }_`````
``*else*``_{ This note is the High Key }_```````````````````````
````Top_Key := EVENT_NOTE`````_{ Update numeric value of Top_Key }_ 
````ShowIR````````````````````_{ Update display of Instrument Range }_ 
````set_text(SetIR,SetCap)````_{ Normal 'off button' caption }_ 
````SetIR := Off``_{ We're done, switch back from IR entry to play mode }_`````
``*end if* 
*end function* _{ SetInstRange }_ 

*function* ShowIR 
_(* Updates display of IR and LowKey/TopKey from Low_Key/Top_Key *)_ 
``Note2Key(Low_Key,LowKey) 
``Note2Key(Top_Key,TopKey) 
``set_text(IR_Box,LowCap & LowKey) _{ Update display }_ 
``add_text_line(IR_Box,TopCap & TopKey) 
*end function* _{ ShowIR }_ 

*function* Note2Key(midi_note,key_name) 
_(* Converts a MIDI note# to a Scale-tone/Octave format string *)_ 
``key_name := Tones[midi_note mod 12] & (midi_note/12 -2) 
*end function* _{ Note2Key }_


----------



## Thonex (May 13, 2006)

Wow!!! Very cool.

Big Bob.... you've been a busy bee (how about that alliteration)  

These are going to be great tools and modules for upcoming scripts!!!

I òUw   :+Uw   :,Uw   :-Uw   :.Uw   :/Uw   :0Uw   :1Uw   :2Uw   :3Uw   :4Uw   :5Uw   :6Uw   :7Uw


----------



## Big Bob (May 19, 2006)

Hi Andrew,

I think posting a link to this thread in your 'sticky' script thread is one of the things that got lost in the 'crash'. You may want to add it again?

God Bless,

Bob


----------



## Thonex (May 19, 2006)

Re-done


----------

