Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Reworked by Yoshi_E
- -- Gui Rework: Yoshi_E
- -- Noteblock Studio file (.nbs) player for MiscPeripherals (a ComputerCraft mod)
- -- You are free to copy, alter, and distribute this under the following conditions:
- -- 1) You leave these comments and credits attached
- -- 2) You do not make money off of this
- -- Program by James0x57 - http://www.youtube.com/watch?v=ro4EK8smULQ
- -- Noteblock Studio by _Davve_ - http://www.minecraftforum.net/topic/136749-minecraft-note-block-studio-150000-downloads/
- -- MiscPeripherals by RichardG867 - http://www.computercraft.info/forums2/index.php?/topic/4587-cc1481mc146-miscperipherals-23/
- -- ComputerCraft by Dan - http://www.computercraft.info/
- local computer_Dirt=1
- local computer_Sand=2
- local computer_Stone=3
- local computer_Glas=4
- local computer_Wood=5
- local args = {...};
- local menu = {};
- local box = nil;
- local song = nil;
- local action = 'mainList';
- local maxtoshow = 16;
- local selected = 1;
- local startat = 1;
- local background = 1;
- local text = 32768;
- local playing = false;
- local nextone = false;
- local start = false;
- local g=1
- local loop="NL"
- local lastsong = {}
- rednet.open("top")
- -- FLIB API
- function exists(path)
- local file = assert(io.open(path, "r"))
- if file ~= nil then
- file:close()
- return true
- end
- return false
- end
- function getTable(path)
- if exists(path) then
- local file = io.open(path, "r")
- local lines = {}
- local i = 1
- local line = file:read("*l")
- while line ~= nil do
- lines[i] = line
- line = file:read("*l")
- i = i + 1
- end
- file:close()
- return lines
- end
- return {}
- end
- function getLine(path, n)
- if exists(path) then
- local lines = getTable(path)
- return lines[n]
- end
- return ""
- end
- function getText(path)
- if exists(path) then
- local file = assert(io.open(path, "r"))
- return file:read("*a")
- end
- return ""
- end
- function fappend(path, text)
- local file = assert(io.open(path, "a"))
- file:write(text.."\n")
- file:close()
- end
- function fwrite(path, text)
- local file = assert(io.open(path, "w"))
- file:write(text)
- file:close()
- end
- function fwriteAtStart(path, text)
- local _text = getText(path)
- fwrite(path, text.."\n".._text)
- end
- function fwriteFromTable(path, t)
- local text = ""
- for _, line in pairs(t) do
- text = text..line.."\n"
- end
- fwrite(path, text)
- end
- function fappendFromTable(path, t)
- local text = ""
- for _, line in pairs(t) do
- text = text..line.."\n"
- end
- fappend(path, text)
- end
- function fwriteAtStartFromTable(path, t)
- local text = ""
- for _, line in pairs(t) do
- text = text..line.."\n"
- end
- fwriteAtStart(path, text)
- end
- function replaceLine(path, n, text)
- local lines = getTable(path)
- lines[n] = text
- fwriteFromTable(path, lines)
- end
- function getName(path)
- if exists(path) then
- local lastSlashPos = 1
- for i = 1, path:len() do
- if path:sub(i, i) == "/" then
- lastSlashPos = i
- end
- end
- return path:sub(lastSlashPos + 1)
- end
- return ""
- end
- function getPath(path)
- if exists(path) then
- local lastSlashPos = 1
- for i = 1, path:len() do
- if path:sub(i, i) == "/" then
- lastSlashPos = i
- end
- end
- return path:sub(1, lastSlashPos)
- end
- return ""
- end
- function fremove(path)
- os.remove(path)
- end
- term.setBackgroundColour(1)
- function selectedplay()
- song = newSong(fs.open(args[1] .. "/" .. menu[selected], "rb"));
- readHeader();
- --showInfo();
- readNotes();
- song.fh.close();
- --playNotes()
- end
- function headline()
- lastsong = getTable("lastsong")
- --if lastsong[2]~= "NL" then
- selected=tonumber(lastsong[1])
- term.setTextColour(1)
- loop=(lastsong[2])
- if lastsong[3]=="play" then
- playing=true
- else
- playing=false
- end
- --end
- term.clear()
- while true do
- for i=3,19 do
- term.setCursorPos(1,i)
- print(" ")
- end
- term.setBackgroundColour(background)
- term.setTextColour(text)
- term.setCursorPos(1,1)
- term.clearLine()
- if playing ==false then
- print(" << Play >> "..loop)
- term.setCursorPos(1,2)
- term.setBackgroundColour(256)
- term.setTextColour(colours.white)
- term.clearLine()
- if songname~=nil then
- print(" Playing: "..menu[selected])
- else
- print(" Playing: Unknow")
- end
- else
- start=true
- selectedplay()
- print(" << Stop >> "..loop)
- term.setCursorPos(1,2)
- term.setBackgroundColour(256)
- term.setTextColour(colours.white)
- term.clearLine()
- if songname~=nil then
- print(" Playing: "..menu[selected])
- else
- print(" Playing: Unknow")
- end
- end
- term.setBackgroundColour(1)
- term.setTextColour(text)
- --Song List
- term.setCursorPos(1,3)
- for i = g, #menu do
- if g + maxtoshow <= i then break end
- print(i .. ") " .. menu[i]);
- end
- sleep(0.01) -- Debug, if 0 it dobble checks: if event ==...
- event, param1, param2, param3 = os.pullEvent()
- if event == "mouse_click" and param3 == 1 then if param2 == 6 or param2 == 7 or param2 == 8 or param2 == 9 then if playing==false then playing=true else playing=false end end end
- if event == "mouse_click" and param3 == 1 then if param2 == 2 or param2 == 3 then if selected~=1 then selected=selected-1 end nextone=true selectedplay() end end
- if event == "mouse_click" and param3 == 1 then if param2 == 12 or param2 == 13 then if selected<#menu then selected=selected+1 end nextone=true selectedplay() end end
- if event == "mouse_click" and param3 == 1 then if param2 == 16 or param2 == 17 then if loop=="LA" then loop="NL" else if loop=="L1" then loop="LA" else loop="NL" if loop=="NL" then loop="L1" else loop="NL" end end end end end
- if event == "mouse_scroll" and param1 == 1 then if g<=#menu-16 then g=g+1 end end
- if event == "mouse_scroll" and param1 == -1 then if g>1 then g=g-1 end end
- if event == "mouse_click" and param3 == 3 then selected=g end
- if event == "mouse_click" and param3 == 4 then selected=g+1 end
- if event == "mouse_click" and param3 == 5 then selected=g+2 end
- if event == "mouse_click" and param3 == 6 then selected=g+3 end
- if event == "mouse_click" and param3 == 7 then selected=g+4 end
- if event == "mouse_click" and param3 == 8 then selected=g+5 end
- if event == "mouse_click" and param3 == 9 then selected=g+6 end
- if event == "mouse_click" and param3 == 10 then selected=g+7 end
- if event == "mouse_click" and param3 == 11 then selected=g+8 end
- if event == "mouse_click" and param3 == 12 then selected=g+9 end
- if event == "mouse_click" and param3 == 13 then selected=g+10 end
- if event == "mouse_click" and param3 == 14 then selected=g+11 end
- if event == "mouse_click" and param3 == 15 then selected=g+12 end
- if event == "mouse_click" and param3 == 16 then selected=g+13 end
- if event == "mouse_click" and param3 == 17 then selected=g+14 end
- if event == "mouse_click" and param3 == 18 then selected=g+15 end
- end
- end
- for _,side in ipairs({"left"; "right"; "back"; "bottom"; "top"; "front"}) do
- if peripheral.getType(side) == "music" then
- box = peripheral.wrap(side);
- break;
- end
- end
- if turtle ~= nil then
- maxtoshow = 7;
- end
- function newSong(x)
- return {
- fh = x;
- length = 0;
- height = 0;
- name = "";
- author = "";
- originalAuthor = "";
- description = "";
- tempo = 10.00; --tks per second
- autoSaving = 0;
- autoSaveDur = 0;
- timeSig = 4;
- minSpent = 0;
- leftClicks = 0;
- rightClicks = 0;
- blocksAdded = 0;
- blocksRemoved = 0;
- midi = "";
- music = { wait={}; inst={}; note={}; };
- };
- end
- if args[1] == nil or fs.isDir(args[1]) == false then
- args[1] = "songs";
- if fs.isDir(args[1]) == false then
- fs.makeDir("songs");
- end
- end
- for _, file in ipairs(fs.list(args[1])) do
- if fs.isDir(file) == false and string.find(file, ".nbs", -4, true) ~= nil then -- if file and ends in ".nbs"
- menu[#menu+1] = file;
- end
- end
- function readInt(fh) --little endian, fh is open in rb mode
- local ret = 0;
- local x = fh.read();
- if x == nil then return nil; end
- ret = x;
- x = fh.read();
- if x == nil then return nil; end
- ret = (x * 0x100) + ret;
- x = fh.read();
- if x == nil then return nil; end
- ret = (x * 0x10000) + ret;
- x = fh.read();
- if x == nil then return nil; end
- ret = (x * 0x1000000) + ret;
- return ret;
- end
- function readShort(fh) --little endian, fh is open in rb mode
- local ret = 0;
- local x = fh.read();
- if x == nil then return nil; end
- ret = x;
- x = fh.read();
- if x == nil then return nil; end
- ret = (x * 0x100) + ret;
- return ret;
- end
- function readString(fh, len) --fh is open in rb mode
- local ret = "";
- local x = 0;
- for i = 1, len do
- x = fh.read();
- if x == nil then return nil; end
- ret = ret .. string.char(x);
- end
- return ret;
- end
- function readHeader()
- song.length = readShort(song.fh);
- song.height = readShort(song.fh);
- songname = readString(song.fh, readInt(song.fh));
- song.author = readString(song.fh, readInt(song.fh));
- song.originalAuthor = readString(song.fh, readInt(song.fh));
- song.description = readString(song.fh, readInt(song.fh));
- song.tempo = 1.000 / ( readShort(song.fh) / 100.00 );
- song.autoSaving = song.fh.read();
- song.autoSaveDur = song.fh.read();
- song.timeSig = song.fh.read();
- song.minSpent = readInt(song.fh);
- song.leftClicks = readInt(song.fh);
- song.rightClicks = readInt(song.fh);
- song.blocksAdded = readInt(song.fh);
- song.blocksRemoved = readInt(song.fh);
- song.midi = readString(song.fh, readInt(song.fh));
- end
- function readNotes()
- local curtk = 1;
- local tk = -1;
- local layer = -1;
- local inst = 0;
- local note = 33; -- MC is 33 to 57
- while true do
- tk = readShort(song.fh);
- if tk == nil then return false; end
- if tk == 0 then break; end
- while true do
- song.music.wait[curtk] = (tk * song.tempo) * 0.975; -- * 0.975 to speed it up a bit because lua slow
- layer = readShort(song.fh); --can't do anything with this info (yet?)
- if layer == nil then return false; end
- if layer == 0 then break; end
- song.music.inst[curtk]=song.fh.read();
- if song.music.inst[curtk] == 0 then
- song.music.inst[curtk] = 0;
- elseif song.music.inst[curtk] == 2 then
- song.music.inst[curtk] = 1;
- elseif song.music.inst[curtk] == 3 then
- song.music.inst[curtk] = 2;
- elseif song.music.inst[curtk] == 4 then
- song.music.inst[curtk] = 3;
- elseif song.music.inst[curtk] == 1 then
- song.music.inst[curtk] = 4;
- end
- song.music.note[curtk]=song.fh.read()-33;
- tk = 0;
- curtk = curtk + 1;
- end
- end
- return true;
- end
- function playNotes()
- while true do
- if start==false then
- while start==false do
- sleep(1)
- end
- end
- for i = 1, #song.music.wait - 1 do
- if playing==false then
- while playing==false do
- sleep(1)
- end
- end
- if song.music.wait[i]~=nil then
- if song.music.wait[i]>=0 and song.music.wait[i]<=100000 then
- if song.music.wait[i] ~= 0 then
- sleep(song.music.wait[i]);
- end
- if song.music.inst[i]==0 then rednet.send(computer_Dirt,song.music.note[i]) end
- if song.music.inst[i]==1 then rednet.send(computer_Sand,song.music.note[i]) end
- if song.music.inst[i]==2 then rednet.send(computer_Stone,song.music.note[i]) end
- if song.music.inst[i]==3 then rednet.send(computer_Glas,song.music.note[i]) end
- if song.music.inst[i]==4 then rednet.send(computer_Wood,song.music.note[i]) end
- --pcall(box.playNote, song.music.inst[i], song.music.note[i]);
- end
- end
- end
- playing=false
- if loop=="L1" then
- sleep(1)
- selectedplay()
- playing=true
- end
- if loop=="LA" then
- if selected==#menu then
- selected=1
- sleep(1)
- selectedplay()
- playing=true
- else
- selected=selected+1
- sleep(1)
- selectedplay()
- playing=true
- end
- end
- end
- end
- --parallel.waitForAll(start, headline)
- parallel.waitForAll(playNotes, headline)
- --headline()
Add Comment
Please, Sign In to add comment