@name FailCake W64 Music Composer
@inputs Keys
@persist [O E]:entity [Music Sound Gta]:string [Ksy]:table [Say]:array
@persist Record [Rec File]:string Encode PV Tempo Loop TEMP:array DTEMP:string NewTempo [Saves Saves2]:array Load Save K Spam Play
@trigger all  

if(first()){
runOnTick(1)  
runOnChat(1)
runOnFile(1)

O = owner()
E = entity() 
Music = "" 
Sound = "synth/triangle_440.wav"
Tempo = 500 ## default tempo
Play = 0
Spam = 0
K = 0

Ksy["32",string] = " "
Ksy["97",string] = "a"
Ksy["98",string] = "b"
Ksy["99",string] = "c"
Ksy["100",string] = "d"
Ksy["101",string] = "e"
Ksy["102",string] = "f"
Ksy["103",string] = "g"
Ksy["104",string] = "h"
Ksy["105",string] = "i"
Ksy["106",string] = "j"
Ksy["107",string] = "k"
Ksy["108",string] = "l"
Ksy["109",string] = "m"
Ksy["110",string] = "n"
Ksy["111",string] = "o"
Ksy["112",string] = "p"
Ksy["113",string] = "q"
Ksy["114",string] = "r"
Ksy["115",string] = "s"
Ksy["116",string] = "t"
Ksy["118",string] = "v"
Ksy["117",string] = "u"
Ksy["120",string] = "x"
Ksy["122",string] = "z"
Ksy["45",string] = "-"
Ksy["46",string] = "."
Ksy["95",string] = "_"
Ksy["44",string] = ","
Ksy["49",string] = "1"
Ksy["51",string] = "3"
Ksy["52",string] = "4"
Ksy["53",string] = "5"
Ksy["54",string] = "6"
Ksy["55",string] = "7"
Ksy["56",string] = "8"
Ksy["57",string] = "9"
Ksy["48",string] = "0"
Ksy["123",string] = "?"

Ksy["47",string] = "~"
Ksy["36",string] = "$"
}

if(chatClk(O)){
Say = O:lastSaid():explode(" ") 
  
if(Say[1,string] == "-save" & Save == 0 & Load == 0){
File = Say[2,string] 

if(File:length() > 0){  
Save = 1 
}

} 

if(Say[1,string] == "-play" & Save == 0 & Load == 0 & Rec != "" & Encode == 0){
Play = 1   
hint("* Playing Music! *",7)
}

if(Say[1,string] == "-loop"){
Loop = Say[2,string]:toNumber() 
hint("* Loop : "+Loop+" *",7)   
}

if(Say[1,string] == "-load" & Save == 0 & Load == 0 & Encode == 0){
File = Say[2,string]    
if(File:length() > 0){
Load = 1
}
}

if(Say[1,string] == "-tempo" & Save == 0 & Load == 0 & Encode == 0){
Tempo = Say[2,string]:toNumber() 
hint("* Tempo : "+Tempo+" *",7)  
}  

if(Say[1,string] == "-encode" & Save == 0 & Load == 0 & Encode == 0){
File = Say[2,string]    
if(File:length() > 0){
Encode = 1
}
}


if(Say[1,string] == "-record" & Record == 0 & Save == 0 & Load == 0 & Encode == 0){
Record = 1  
Rec = ""
hint("* Recording! *",7)
} 

if(Say[1,string] == "-stoprec" & Record == 1 & Save == 0 & Load == 0 & Encode == 0){
Record = 0 
print(Rec)
soundPurge()
hint("* Stoped Recording! *",7)
}
 
if(Say[1,string] == "-stop" & Play == 1 & Save == 0 & Load == 0){
Play = 0 
soundPurge()
hint("* Stoped Playing! *",7)
}
 
}
if(Play == 0){
if(changed(Keys) & Keys){
Note = Ksy[Keys:toString(),string]  

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#    
if(Note == "~") {PV = 0 NewTempo = Tempo / 4} # Quater note
if(Note == "$") {PV = 0 NewTempo = Tempo / 2} # Half note
  
 
if(Record == 1){   
Rec = Rec + Ksy[Keys:toString(),string]    
}       
    
E:soundPlay(0,0,Sound)

soundPitch(0,2^(PV/12))

}
setName("W64 Music Composer ["+Note+"]")
}


if(clk("stop")){
soundStop(0)    
}


if(Play == 1 & Rec:length() > 1){
timer("Tesd",Tempo) 

if(changed(Play) & Play){
E:soundPlay(0,0,Sound)   
}
   
}

if(clk("Tesd")){
  
if(K <= Rec:length()){    
K+=1  
}else{
if(Loop == 1){
K = 0    
}else{
Play = 0
soundPurge()
hint("* PlayBack Done! *",7)
stoptimer("Tesd")
}

}

Note = Rec[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#    
if(Note == "~") {PV = 0 NewTempo = Tempo / 4} # Quater note
if(Note == "$") {PV = 0 NewTempo = Tempo / 2} # Half note
  
soundPitch(0,2^(PV/12))

if(Play == 1){
    
if(Note == "~" | Note == "$"){    
timer("Tesd",NewTempo)
}else{
timer("Tesd",Tempo)
}

}
 
setName("W64 Music Composer ["+Note+"]")
} 



if(Save == 1){
# Put music into a table.
Saves[1,string] = Rec
Saves[2,string] = Tempo:toString() 

Gta = glonEncode(Saves)
fileWrite(File + ".txt",Gta)
hint("File : "+ File + ".txt Saved!",7)
Save = 0  
reset()  
}

if(Load == 1){
fileLoad(File+".txt")  
Load = 2  
}

if(Load == 2){
if(fileClk()&fileLoaded()){     
Saves2:clear()
Saves2 = glonDecode(fileRead())

if(fileLoaded()){

print(File + ".txt Loaded!")
Load = 0  
Rec = Saves2[1,string]

if(!Saves2[2,string]:toNumber() == 0){
Tempo = Saves2[2,string]:toNumber()
}else{
Tempo = 500
}

}

}
}


if(Encode == 1){
fileLoad(File+".txt")  
Encode = 2  
}

if(Encode == 2){
    
if(fileClk()&fileLoaded()){     
if(fileLoaded()){
TEMP[1,string] = fileRead()
hint(File + ".txt Encoding!",7)
print(TEMP[1,string])
Encode = 3  
}
}
}

if(Encode == 3){
DTEMP = glonEncode(TEMP)
fileWrite(File+".txt",DTEMP) 
Encode = 0
reset()   
}
