@name W64-MDecoder
@inputs
@outputs E:entity
@persist Music:string Loop Tempo Note:string K
@trigger all
@model models/expression 2/cpu_processor_mini.mdl

if (first() | duped()){
    interval(100)
    E = entity()
    K = 0
    soundPurge()
}
    
Music = E["DMusic", string]   
Loop = E["PlayL", number]
Tempo = E["PlayT", number]
Path = E["PlayPath", string]

if(execClk("Stop")){
    stoptimer("Pl")
    K = 0
    E:soundPlay(0,0,Path)   
}

if(execClk("Play")){
    timer("Pl",Tempo)    
}


if(clk("Pl")){
    if(K < Music:length()){
        K+=1
    }else{
        if(Loop){
            K = 0    
        }elseif(Loop == 0){
            E:execute("Stop")
        }
    }
    
    Note = Music[K]
    if(Note == " ") {PV = 0}
    if(Note == "a") {PV = 59} # C
    if(Note == "b") {PV = 60} # C#
    if(Note == "c") {PV = 61} # D
    if(Note == "d") {PV = 62} # D#
    if(Note == "e") {PV = 63} # E
    if(Note == "f") {PV = 64} # F
    if(Note == "g") {PV = 65} # F#
    if(Note == "h") {PV = 66} # G
    if(Note == "i") {PV = 67} # G#
    if(Note == "j") {PV = 68} # A
    if(Note == "k") {PV = 69} # A#
    if(Note == "l") {PV = 70} # B
    if(Note == "m") {PV = 71} # C
    if(Note == "n") {PV = 72} # C#
    if(Note == "o") {PV = 73} # D
    if(Note == "p") {PV = 74} # D#
    if(Note == "q") {PV = 75} # E
    if(Note == "r") {PV = 76} # F
    if(Note == "s") {PV = 77} # F#
    if(Note == "t") {PV = 78} # G
    if(Note == "v") {PV = 79} # G#
    if(Note == "u") {PV = 80} # A
    if(Note == "x") {PV = 81} # A#
    if(Note == "z") {PV = 82} # B
    if(Note == "-") {PV = 83} # C - Home
    if(Note == ".") {PV = 84} # C#
    if(Note == "_") {PV = 85} # D
    if(Note == ",") {PV = 86} # D#
    if(Note == "1") {PV = 87} # E
    if(Note == "3") {PV = 88} # F
    if(Note == "4") {PV = 89} # F#
    if(Note == "5") {PV = 90} # G
    if(Note == "6") {PV = 91} # G#
    if(Note == "7") {PV = 92} # A
    if(Note == "8") {PV = 93} # A#
    if(Note == "9") {PV = 94} # B
    if(Note == "0") {PV = 95} # C
    if(Note == "?") {PV = 96} # C#

    soundPitch(0,2^(PV/12))
    timer("Pl",Tempo)
}
