Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local defaultSong = "chobb" --default song, .nbs is automatically appended
- local defaultSide = "top" --side of the note block
- -- Noteblock Studio file (.nbs) player for MiscPeripherals (a ComputerCraft mod)
- -- version 2
- -- 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/
- -- If you use this in anything you create, please show credit where due, and let me know because I'd love to see it used!! =)
- -- Edited by thefofthef for use with redstone
- --Sintax: "<program name>" for redstone control (uses the default song) or "<program name> song (.nbs is automatically appended) noteBlockSide"
- local Mdebug = true
- local rSide = "front" --side of the redstone toggle
- --if Mdebug then print("") end
- local args = {...}
- local auto = false; --toggled true if there aren't arguments for redstone control
- local box --note block
- if args[1] == nil then
- auto=true;
- box=peripheral.wrap(defaultSide)
- elseif type(args[1])=="string" and args[2]==nil then
- auto=false;
- box=peripheral.wrap(defaultSide)
- elseif type(args[1])~="string" and type(args[2])~="string" then
- error("syntax is <play song side> or <play>!")
- elseif type(args[1])=="string" and type(args[2])=="string" then
- auto=false;
- box=peripheral.wrap(args[2])
- end
- function newSong(x)
- if Mdebug then print("loading song") end
- 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
- function readShort(fh) --little endian, fh is open in rb mode
- if Mdebug then print("reading short...") end
- 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;
- if Mdebug then print("read short") end
- return ret;
- end
- function readInt(fh) --little endian, fh is open in rb mode
- local ret = 0;
- if Mdebug then print("reading int...") end
- 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;
- if Mdebug then print("read int") end
- return ret;
- end
- function readString(fh, len) --fh is open in rb mode
- if Mdebug then print("reading string...") end
- 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
- if Mdebug then print("read string") end
- return ret;
- end
- function readHeader()
- if Mdebug then print("reading header...") end
- song.length = readShort(song.fh);
- song.height = readShort(song.fh);
- song.name = 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));
- if Mdebug then print("read header") end
- end
- function readNotes()
- local curtk = 1;
- local tk = -1;
- local layer = -1;
- local inst = 0;
- local note = 33; -- MC is 33 to 57
- if Mdebug then print("reading notes...") end
- 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.965; -- * 0.965 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
- if Mdebug then print("read notes") end
- return true;
- end
- function loadSong(fSong) -- loads a song
- song = newSong(fs.open(fSong..".nbs", "rb"));
- readHeader();
- readNotes();
- song.fh.close();
- action = 'songReady';
- if Mdebug then print("loaded song") end
- end
- function playNotes()
- isPlaying = true;
- for i = 1, #song.music.wait - 1 do
- if song.music.wait[i] ~= 0 then
- os.sleep(song.music.wait[i]);
- end
- if redstone.getInput(rSide) == false and auto == true then break; end
- pcall(box.playNote, song.music.inst[i], song.music.note[i]);
- end
- isPlaying = false;
- end
- if auto==false then
- loadSong(args[1])
- print("Playing "..args[1]..".nbs")
- playNotes()
- else
- loadSong(defaultSong)
- while true do
- if redstone.getInput(rSide) == true then
- playNotes()
- os.sleep(4)
- end
- os.sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment