on runme(message)
if (item 1 of message = 145) then
set x to item 2 of message
else if (item 1 of message = 144) then
set y to item 2 of message
end if
if (item 1 of message = 145) then
return message
else
set y to x * 8
return {item 1 of message, y, item 3 of message}
end if
end runme
I'm a complete newbie to Applescript. I am receiving MIDI note messages (message). They take the form of three numbers (IE: 145, 0, 127)
What I need to do is listen for a midi note number starting with 145, and then look at its' item 2. I then need to multiply that by 8 and save that as the item 2 of a midi note number starting with 144.
There will be several notes starting with 144 for every note with 145. So I need to keep that variable until a 145 note comes along.
The problem is that I think this script runs fresh every time a midi note passes through it? I need to somehow remember the y variable for every instance of note until a new note with 145 comes along and changes it...
clear as mud?
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.
You can declare global—that is, nonlocal—variables by declaring them outside of any function definition. It's usually best to put all global declarations near the beginning of the program, before the first function. A variable is recognized only from the point it is declared, to the end of the file.
Global ScopeGlobal variables can be accessed from anywhere in a JavaScript program. Variables declared with var , let and const are quite similar when declared outside a block.
There are two ways to declare a variable globally: Declare a variable outside the functions. Assign value to a variable inside a function without declaring it using “var” keyword.
Declare a global variable outside the function scope. See the example below:
global y -- declare y
set y as 0 -- initialize y
on function ()
set y as (y + 1)
end function
function() -- call function
return y
This will return 1
since you can access y
inside of the function. After the end of the function, the value of y
will be preserved.
Read more: http://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_variables.html#//apple_ref/doc/uid/TP40000983-CH223-SW10
How about this? This will go through the "messageList" and once the number 145 turns up, it will work as an on toggle to modify the second number with the "modifier" until 145 turns up again. Is that what you want?
global detectedKey
set detectedKey to false
global modifier
set modifier to "1"
global message
set messageList to {"144,4,127", "145,5,127", "144,1,127", "144,2,127", "145,4,127", "144,1,127", "144,2,127"}
repeat with incomingMessage in messageList
display dialog " incoming: " & incomingMessage & "\n outgoing :" & process(incomingMessage) & "\n modifier: " & modifier
end repeat
on process(incomingMessage)
set a to item 1 of seperate(incomingMessage)
set b to item 2 of seperate(incomingMessage)
set c to item 3 of seperate(incomingMessage)
if detectedKey is true then
set outgoingMessage to "144" & "," & b * modifier & "," & c
if a is equal to "145" then
set detectedKey to false
set modifier to "1"
set outgoingMessage to "144" & "," & b * modifier & "," & c
end if
else if detectedKey is false then
if a is equal to "145" then
set detectedKey to true
set modifier to b
set outgoingMessage to "144" & "," & b * modifier & "," & c
else if a is equal to "144" then
set outgoingMessage to a & "," & b & "," & c
end if
end if
return outgoingMessage
end process
on seperate(message)
set oldDelimiters to text item delimiters
set AppleScript's text item delimiters to {","}
return text items of message
set AppleScript's text item delimiters to oldDelimiters
end seperate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With