Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- minecraft@univac2:/srv/minecraft/forge$ cat world/computer/0/usr/playsound.lua
- args = {...}
- if #args < 2 then
- print("usage: playsound side soundfile")
- return
- end
- local side = args[1]
- if peripheral.getType(side) ~= "speaker" then
- print("no speaker connected at side "..side)
- return
- end
- local fileName = shell.resolve(args[2])
- if not fs.exists(fileName) then
- print("file "..fileName.." does not exist")
- return
- end
- notes = "cCdDefFgGaAb"
- length = 4
- tempo = 125
- octave = 0
- instrument = "flute"
- function playString(str)
- if string.len(str) < 1 then return end
- for i=1,string.len(str) do
- chr = string.match(str,".",i)
- if chr == "<" then
- octave = octave - 1
- elseif chr == ">" then
- octave = octave + 1
- elseif chr == "l" then
- i = i + 1
- length = string.match(str,"(%d+)",i)
- elseif chr == "t" then
- i = i + 1
- tempo = string.match(str,"(%d+)",i)
- elseif string.find(notes,chr) then
- local wait = 60 / tempo / (length/4)
- local note = string.find(notes,chr) + 5 + octave * 12
- print(chr.." "..note)
- peripheral.call(side,"playNote",instrument,1,note)
- os.sleep(wait)
- end
- end
- end
- local f = fs.open(fileName, "r")
- while true do
- local str = f.readLine()
- if not str then break end
- print(str)
- playString(str)
- end
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment