Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function loadAPIS()
- --[[
- Implementing apis
- - TheModerGuy
- --]]
- local function yield()
- os.queueEvent("fake")
- os.pullEvent("fake")
- end
- -- read short integer (16-bit) from file
- local function readShort(file)
- return file.read() + file.read() * 256
- end
- -- read integer (32-bit) from file
- local function readInt(file)
- return file.read() + file.read() * 256 + file.read() * 65536 + file.read() * 16777216
- end
- -- read string from file
- local function readString(file)
- local s = ""
- local len = readInt(file)
- for i = 1, len do
- local c = file.read()
- if not c then
- break
- end
- s = s..string.char(c)
- end
- return s
- end
- -- read nbs file header
- local function readNBSHeader(file)
- local header = {}
- header.lenght = readShort(file)
- header.height = readShort(file)
- header.name = readString(file)
- if header.name == "" then
- header.name = "Untitled"
- end
- header.author = readString(file)
- if header.author == "" then
- header.author = "Unknown"
- end
- header.original_author = readString(file)
- if header.original_author == "" then
- header.original_author = "Unknown"
- end
- header.description = readString(file)
- header.tempo = readShort(file) / 100
- header.autosave = file.read()
- header.autosave_duration = file.read()
- header.time_signature = file.read()
- header.minutes_spent = readInt(file)
- header.left_clicks = readInt(file)
- header.right_clicks = readInt(file)
- header.blocks_added = readInt(file)
- header.blocks_removed = readInt(file)
- header.filename = readString(file)
- return header
- end
- -- jump to the next tick in the file
- local function nextTick(file, tSong)
- local jump = readShort(file)
- for i = 1, jump - 1 do
- table.insert(tSong, {})
- end
- return jump > 0
- end
- -- read the notes in a tick
- local function readTick(file)
- local t = {}
- local jump = readShort(file)
- while jump > 0 do
- local instrument = file.read() + 1
- if instrument > 5 then
- return nil, "Can't convert custom instruments"
- end
- local note = file.read() - 33
- if note < 0 or note > 24 then
- return nil, "Notes must be in Minecraft's 2 octaves"
- end
- if not t[instrument] then
- t[instrument] = {}
- end
- table.insert(t[instrument], note)
- jump = readShort(file)
- end
- return t
- end
- -- API functions
- -- save a converted song to a file
- function nbsSaveSong(tSong, sPath)
- local file = fs.open(sPath, "w")
- if file then
- file.write(textutils.serialize(tSong))
- file.close()
- return true
- end
- return false, "Error opening file "..sPath
- end
- -- load and convert an .nbs file and save it
- function nbsLoad(sPath, bVerbose)
- local file = fs.open(sPath, "rb")
- if file then
- if bVerbose then
- print("Reading header...")
- end
- local tSong = {}
- local header = readNBSHeader(file)
- tSong.name = header.name
- tSong.author = header.author
- tSong.original_author = header.original_author
- tSong.lenght = header.lenght / header.tempo
- tSong.delay = 1 / header.tempo
- if bVerbose then
- print("Reading ticks...")
- end
- while nextTick(file, tSong) do
- local tick, err = readTick(file, tSong)
- if tick then
- table.insert(tSong, tick)
- else
- file.close()
- return nil, err
- end
- yield()
- end
- file.close()
- return tSong
- end
- return nil, "Error opening file "..sPath
- end
- local total = 0;
- local slept = 0;
- local function _round(_i)
- _i = _i * 20;
- if _i >= 0 then
- return math.floor(_i+.5) / 20
- else
- return math.ceil(_i-.5) / 20
- end
- end
- function atsWait(_sleep)
- if _sleep < 0 then return end
- total = total + _sleep;
- _sleep = _round(_sleep);
- slept = slept + _sleep;
- if (slept - total) >= 0.05 then
- _sleep = _sleep - 0.05;
- slept = slept - 0.05;
- elseif (slept - total) < -0.05 then
- _sleep = _sleep + 0.05;
- slept = slept + 0.05;
- end
- sleep(_sleep);
- end
- end
- local args = {...} -- args table
- if args[2]~= nil then
- loop=args[2]
- end
- loadAPIS()
- local played,played2=true,true
- local delay,lenght
- local side,textColor,bgColor,notesPlayed,row=0,1,2,0,0
- local notenames = {"F#3"," G3","G#3"," A3","A#3"," B3",
- " C4","C#4"," D4","D#4"," E4"," F4","F#4"," G4",
- "G#4"," A4","A#4"," B4"," C5","C#5"," D5","D#5",
- " E5"," F5","F#5"}
- local instnames = {"PIANO","BASS DRUM","SNARE","CLICKS",
- "BASS GUITAR"}
- -- ID is the NoteBlock ID to use
- local id=1
- -- Modem stuff
- function getSide()
- for n,side in pairs(rs.getSides()) do
- if peripheral.getType( side )=="modem" then
- return side
- end
- end
- end
- rednet.open(getSide())
- local MODEM = peripheral.wrap(getSide())
- local PERIPHERALS = MODEM.getNamesRemote()
- local maxId = #(MODEM.getNamesRemote())
- local function play(inst,note)
- if inst==1 then
- inst=0
- elseif inst==2 then
- inst=4
- elseif inst==3 then
- inst=1
- elseif inst==4 then
- inst=2
- elseif inst==5 then
- inst=3
- end
- id = id > maxId and 1 or id
- textColor = textColor * (textColor == bgColor and 2 or 1)
- textColor = textColor >= colors.black and 1 or textColor
- MODEM.callRemote(PERIPHERALS[id],"playNote",inst,note)
- local str = instnames[inst+1] .." "..notenames[note+1]
- local str = instnames[inst+1] .." "..notenames[note+1]
- id = id + 1
- notesPlayed = notesPlayed + 1
- end
- local function load(file)
- local tSong = nbsLoad(file)
- delay = tSong.delay
- lenght = tSong.lenght
- realLenght = lenght * (1/delay)
- for a,b in pairs(tSong) do
- if type(b)=="table" then
- for inst,d in pairs(b) do
- for e,note in pairs(d) do
- play(inst,note)
- end
- played2=true
- end
- if played2==true then
- played=true
- played2=false
- end
- atsWait(delay)
- row = row + 1
- if rs.getInput("front") then
- return
- end
- end
- end
- end
- while true do
- id,msg,dist = rednet.receive()
- data = string.gmatch(msg, "%S+")
- if data(1) == "Player" then
- print("Me")
- if data(2)== "Play" then
- InFile = data(3)
- print("Play")
- load(tostring(InFile))
- rednet.broadcast("DJ DONE")
- end
- if data(2)=="Util" then
- print("Util")
- shell.run(tostring(data(3)))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement