Data AcQuisition And Real-Time AnalysisScope - Spectrum - Spectrogram - Signal Generator
Software for Windows
Science with your Sound Card!
Contact us about
MIDI Changes Script Computer Keyboard Control
Changes: K!, K#, K$, Ka
A MIDI Changes script can read the ASCII character value of the computer keyboard key that is currently down or that has recently gone down, or read current shift states, or read the state (0 or 1) of a specific key. The value can be used in any expression, such as to set an instrument number or other Changes variable. This allows near-real-time user control of the performance.
The key value or state is a "read-only" variable. It can only appear on the right side of an expression. The general format is K followed by a single symbol or character.
Note that Daqarta dialog hot-keys are not disabled just because they are used in Changes scripts. For example, if the Generator control dialog is open and has the keyboard focus (highlighted from having clicked the mouse on it), then the unshifted 'G' key will toggle the Generator on or off. It will do this even if your script is using that key for another purpose. The best way to avoid unintended operations is to not open main Daqarta control dialogs during DaqMusiq performances that use keyboard control.
However, this Changes keyboard monitor does not respond to keystrokes used for text or value entry.
K! returns the value of a key that is down at the time the command that uses this is executed. For example UA=K! sets User Variable UA to the key value, if any. If no key is down, UA is set to 0.
K# returns the value of the most recent key to go down, even if it has been released by the time the command is executed. The internal value is automatically cleared after reading. If the key is held down while being read repeatedly, the return will alternate between the key value and zero.
Only keys for "printable" alphanumeric or symbol characters (including spaces) can be read by these methods, not shift keys, Tab, Enter, Backspace, function keys, etc. The values are case sensitive, such that 'A' returns 0x41 (65), while 'a' returns 0x61 (97). Similarly, '1' returns 0x31 (49), while the shifted version '!' returns 0x21 (33).
You can see the values of keys via oL=K#, which will update the left Output Display whenever it is run. Insert this command into the Changes script of a voice that is updated often (low Wait value), or activate Changes on an unused voice and use a script of:
That will update the display on every beat.
You can test the character with an IF statement, using the equivalent ASCII value of the character to be tested for. But is it usually more convenient to test for the character directly, by enclosing it in quotes:
[K!="A" oL="A-key down"]
The lowest ASCII value that can be returned is 32 (space) and the highest is 126 (tilde ~), a difference of 94. Most MIDI controls have a range of 0-127, so if you want the keyboard to cover the whole range you can use:
UA=K# [UA>=32 oL=UA*127/94-43]
When K# returns 32 (space), the Left Output Display oL is 32 * 127 / 94 - 43 = 0. When K# returns 126 (tilde), oL is 126 * 127 / 94 - 43 = 127. But note that K# returns 0 there is no key pressed, so the IF statement on the second line only updates the Output Display if UA is 32 or above, meaning a key was pressed.
You could replace the oL with a command to set any desired MIDI parameter, such as instrument number.
Unlike tonal instrument numbers and most other MIDI controls, percussion voices have a limited range of values. The General MIDI percussion sounds are 35-81, a range of 46 values. To use the keyboard to select one of these sounds for percussion voice IA you can use:
UP=K# [UP>=32 IA=UP*46/94+20]
But notice that the key range is more than twice as large as the percussion range, so you can actually use the keyboard to set two different percussion voices. For example, to set IA using key values 33-79 and IB using key values 80-126 you can use:
UP=K# [UP>=33 [UP<=79 IA=UP+2 | IB=UP-45]]
The extended percussion range of 27-87 is available on many synthesizers, including the Windows default Microsoft GS Wavetable Software Synthesizer. To set IA using this range of 60 values, use:
UP=K# [UP>=32 IA=UP*60/94+7]
K$ returns the current (not recent) shift states. The CTRL, ALT, and SHIFT keys are encoded into the bits of the return value such that:
1 = CTRL 2 = ALT 4 = SHIFT
If multiple shift keys are down, the returned value will be the sum of the relevant bit values. For example, 3 means that both CTRL and ALT are currently down.
The format is Ka, where a is an alphanumeric character (case ignored). For example, UA=Kq will set User Variable UA to 1 if the 'q' key is down at the time the command is processed, or else 0 if it is up.
The main use for key states is with IF statements to control execution pathways. For example, you could use [Ka=1 a2=3 | a2=0 ] to set Voice 2 Arpeggio to a value of 3 whenever the 'a' key is down, and 0 when it is up.
Instead of using one key to select one of two values, you can use multiple keys to set multiple values. Following the previous example:
[Ka=1 a2=3] ;IF 'a' down, set a2=3
[Kb=1 a2=0] ;IF 'b' down, set a2=0
Alternatively, you can get a "push-On, push-Off" action from a single key, by using a User Variable and a pair of nested IFs:
[Ka=1 [U1=3 U1=0 | U1=3] ]
(If 'a' is down and U1 = 3, then set it to 0, else set it to 3 if it wasn't already.)
But many commands only need a 0 or 1 value to change state, so controlling them with a key is even simpler. For example, b2=Kb sets Note Bend for Voice 2 when the 'b' key is down, and clears it when it is up.
Note that there is no way to make these keyboard commands truly real-time. Changes scripts must always have a least one Wait command, and for performances that use Tempo Voice this may delay response by a beat. However, for live Pitch Track performances where you don't use Voice Tempo the response latency can be as low as the Trace Update interval.
For fastest response, you can dedicate the Changes script of one Voice Setup just for commands that must be run on every note. Set W=1 at the start of the script, and then include the commands.
You can control any voice from any script, so using one script just for this purpose doesn't compromise control over that voice... you just control it from another script. Since scripts are multitasking they all appear to run at the same time, so this won't slow down other scripts.
Note also that the K read-only Changes script variable operates identically to the read-only macro variable Key?, using the same symbols and characters. In particular, either K# and Key?# will clear or update the same internal "recent key" value after use.
Questions? Comments? Contact us!We respond to ALL inquiries, typically within 24 hrs.
Over 30 Years of Innovative Instrumentation
© Copyright 2007 - 2017 by Interstellar Research
All rights reserved