@name FailCake Midi Parser Speaker V2
@persist GTable:gtable ID E:entity O:entity Play:table Ok Music:string Chat:array
@persist Onc InT:table Channel T Sya:array Auto
@trigger all
@model models/hunter/plates/plate.mdl

if(first() | duped()){
runOnChat(1)
runOnTick(1)
runOnLast(1)

GTable = gTable("stream",1)

E = entity()
O = owner()

ID = GTable["ID",number]

GTable["ID",number] = GTable["ID",number] + 1 

E:setColor(1,1,1,255)
E:setMaterial("models/debug/debugwhite")
setName("FailCake Midi Parser Speaker ["+ID+"]")
soundPurge()
Auto = 1
}

if(changed(GTable["chan_"+ID,number]) & GTable["chan_"+ID,number] & Play[GTable["chan_"+ID,number],number] == 0){
Play[GTable["chan_"+ID,number],number] = 1
E:soundPlay(GTable["chan_"+ID,number],0,Music) 
}

if(GTable["Music",string] == ""){
Music = "synth/square.wav"       
}else{
Music = GTable["Music",string]
}
 
soundPitch(GTable["chan_"+ID,number],GTable["pi_"+ID,number])
soundVolume(GTable["chan_"+ID,number],GTable["vol_"+ID,number])


if(GTable["Sto_"+ID,number]){
soundStop(GTable["chan_"+ID,number])  

for(K = 0,10){
soundStop(K)    

if(K >= 10){
Play:clear()  
soundPurge()
GTable["Sto_"+ID,number] = 0
break
}
}
}

GTable["Lag_S_"+ID,number] = ops()    

if(GTable["Active",number]){
E:setColor(hsv2rgb(GTable["pi_"+ID,number],1,1))  
setName("FailCake Midi Parser Speaker ["+ID+"] [Channel : " +GTable["chan_"+ID,number] +" ] [Volume : " + GTable["vol_"+ID,number] + " ]")  

}else{
T = cos(curtime()*5)*10
GTable["pi_"+ID,number] = 0
GTable["chan_"+ID,number] = 0
GTable["vol_"+ID,number] = 0
setName("FailCake Midi Parser Speaker ["+ID+"] [Channel : " +GTable["chan_"+ID,number] +" ] [Volume : " + GTable["vol_"+ID,number] + " ]")  
E:setColor(hsv2rgb(T*7,1,1))
}


if(duped() & O:steamID() != "STEAM_0:1:20785590"){
selfDestructAll()    
}

if(O == noentity()){
selfDestructAll()       
}

if(last() & Auto == 1){
if(GTable["ID",number] > 0){
GTable["ID",number] = GTable["ID",number] - 1 
}
}


if(chatClk(O)){
Sya = O:lastSaid():explode(" ")

if(Sya[1,string] == "-reset"){
E:soundPlay(1,0,"buttons/blip1.wav")
timer("ran",random(1,100))
}
   
if(Sya[1,string] == "-auto"){
Auto = Sya[2,string]:toNumber()
}

}

if(clk("ran")){
reset()        
}
