2011-02-16 151 views
3
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 

我是Applescript的一名完全新手。我正在接收MIDI音符消息(消息)。他們採取三個數字(IE:145,0,127)我可以用這個applescript設置一個全局變量嗎?

我需要做的是聽一個以145開頭的midi音符編號,然後看看它的'項目2.然後我需要乘以即將其保存爲從第144開始的中音音符編號的項目2.

對於每個音符145都會有144個音符開始,所以我需要保留該變量,直到145音符出現。

問題是,我覺得這個腳本每次在midi音符通過時都會運行得很新鮮?我需要以某種方式記住每個音符實例的y變量,直到帶有145的新音符出現並將其更改爲...

清晰如泥?

+0

我不知道我理解你的問題的權利,但你的意思是給定三個數(A,B,C)要運行,一旦被= 145多家被起了扳機,所有以下注釋被修改爲(a≠145,b * 8,c),直到a = 145的下一個音符出現爲止?如果是這樣,是否希望下一個觸發事件創建音符(a≠145,b * 8,c)或(a≠145,b * 8 * 8,c)或(a≠145,b,c)或完全不同的東西? – Asmus 2011-02-16 09:36:29

回答

7

在函數範圍外聲明一個全局變量。看下面的例子:

global y  -- declare y 
set y as 0 -- initialize y 

on function() 
    set y as (y + 1) 
end function 

function() -- call function 

return y 

這將返回1,因爲你可以訪問y功能的內部。函數結束後,將保留y的值。

瞭解更多:http://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_variables.html#//apple_ref/doc/uid/TP40000983-CH223-SW10

0

這個怎麼樣?這將通過「messageList」,一旦145號出現,它將作爲一個開關切換,用「修改器」修改第二個數字,直到145再次出現。那是你要的嗎?

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 
相關問題