Advertisement
ComputerMan123

NBStudio - By: TheDacinator

Dec 12th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.66 KB | None | 0 0
  1. --Variables
  2. local tArgs = {...}
  3. local x,y = term.getSize()
  4. local song = {["len"]=10,["tempo"] = 120,["author"] = '',["name"] = ""}
  5. if tArgs[1] then
  6.   local file = fs.open(tArgs[1],"r")
  7.   song = textutils.unserialize(file.readAll())
  8.   file.close()
  9. end
  10. local scroll = 0
  11. local xscroll = 0
  12. local textnote = "right"
  13. local note = peripheral.wrap(textnote)
  14. local screen = 1
  15. local nbs = {}
  16. local notedic = {[0]="e#F","eG","eG#","eA","eA#","eB","eC","eC#","eD","eD#","eE","eF","eF#","1G","1G#","1A","1A#","1B","1C","1C#","1D","1D#","1E","1F","1F#"}
  17. local insdic = {["s"] = "eSound Effect",[0] = "1Harp","2Bass Drum","3Snare","4Hat","5Bass Attack"}
  18. --Functions
  19. local desc = function(inp,type)
  20.   term.setCursorPos(2,1)
  21.   local dic = nil
  22.   if type == 1 then
  23.     dic = notedic
  24.   else
  25.     dic = insdic
  26.   end
  27.   term.setBackgroundColor(colors.gray)
  28.   if string.sub(dic[inp],1,1) == "e" then
  29.     term.setTextColor(colors.red)
  30.   elseif string.sub(dic[inp],1,1) == "1" then
  31.     term.setTextColor(colors.orange)
  32.   elseif string.sub(dic[inp],1,1) == "2" then
  33.     term.setTextColor(colors.magenta)
  34.   elseif string.sub(dic[inp],1,1) == "3" then
  35.     term.setTextColor(colors.lightBlue)
  36.   elseif string.sub(dic[inp],1,1) == "4" then
  37.     term.setTextColor(colors.yellow)
  38.   elseif string.sub(dic[inp],1,1) == "5" then
  39.     term.setTextColor(colors.lime)
  40.   end
  41.   term.write(string.sub(dic[inp],2))
  42. end
  43. local rennum = function(inp)
  44.   if inp < 10 then
  45.     return inp
  46.   else
  47.     return string.char(inp+87)
  48.   end
  49. end
  50. local drawEdit = function()
  51.   term.setBackgroundColor(colors.black)
  52.   term.clear()
  53.   term.setCursorPos(1,1)
  54.   term.setBackgroundColor(colors.gray)
  55.   term.setTextColor(colors.white)
  56.   term.write(string.rep(" ",x))
  57.   term.setCursorPos(math.ceil(x/2),1)
  58.   term.write("^")
  59.   term.setCursorPos(x-3,1)
  60.   term.write("<  >")
  61.   term.setCursorPos(1,y)
  62.   term.write(string.rep(" ",x))
  63.   term.setCursorPos(math.ceil(x/2),y)
  64.   term.write("v")
  65.   term.setCursorPos(x-6,y)
  66.   term.setTextColor(colors.green)
  67.   term.write("Options")
  68.   term.setTextColor(colors.red)
  69.   term.setCursorPos(1,y)
  70.   term.write("Close")
  71. end
  72. local drawNotes = function()
  73.   for i = 2,y-1,1 do
  74.     term.setCursorPos(1,i)
  75.     if song[i-1+scroll] then
  76.       if song[i-1+scroll]["ins"] == "s" then
  77.         term.blit("s","e","9")
  78.       else
  79.         term.blit(tostring(song[i-1+scroll]["ins"]),tostring(song[i-1+scroll]["ins"]+1),"7")
  80.       end
  81.       local a = nil
  82.       for j = 1,x-1,1 do
  83.         if song[i-1+scroll][xscroll+j] then
  84.           if song[i-1+scroll][xscroll+j] > 12 then
  85.             a = "1"
  86.           else
  87.             a = "e"
  88.           end
  89.           term.blit(tostring(rennum(song[i-1+scroll][xscroll+j])),"0",a)
  90.         else
  91.           term.blit("=","7","f")
  92.         end
  93.       end
  94.     else
  95.       term.blit("|","8","7")
  96.     end
  97.   end
  98.   term.setBackgroundColor(colors.black)
  99.   term.setTextColor(colors.red)
  100.   if song["len"]-xscroll <= x and song["len"]-xscroll > 1 then
  101.     for i = 2,y-1 do
  102.       term.setCursorPos(song["len"]-xscroll,i)
  103.       term.write("|")
  104.     end
  105.   end
  106. end
  107. incrementNote = function(inp,scrl,t)
  108.   local maxn = 0
  109.   if t == 1 then
  110.     maxn = 24
  111.   else
  112.     maxn = 4
  113.   end
  114.   if inp == 0 and scrl == -1 then
  115.     if t == 1 then
  116.       return nil
  117.     else
  118.       return "s"
  119.     end
  120.   elseif inp == nil or inp == "s" then
  121.     if scrl == -1 then
  122.       return maxn
  123.     else
  124.       return 0
  125.     end
  126.   elseif inp == maxn and scrl == 1 then
  127.     if t == 1 then
  128.       return nil
  129.     else
  130.       return "s"
  131.     end
  132.   else
  133.     return inp + scrl
  134.   end
  135. end
  136. local popup = function(text)
  137.   local win = nil
  138.   if x > 51 and y > 19 then
  139.     win = window.create(term.current(),(x-51)/2+1,(y-19)/2+1,51,19)
  140.   else
  141.     win = window.create(term.current(),1,1,x,y)
  142.   end
  143.   local winx,winy = win.getSize()
  144.   win.setCursorPos(1,1)
  145.   win.write(string.rep("X",winx))
  146.   win.setCursorPos(1,winy)
  147.   win.write(string.rep("X",winx))
  148.   for i = 2,winy-1,1 do
  149.     win.setCursorPos(1,i)
  150.     win.write("X"..string.rep(" ",winx-2).."X")
  151.   end
  152.   win.setCursorPos((winx-string.len(text))/2+1,3)
  153.   win.write(text)
  154.   win.setCursorPos(3,winy-2)
  155.   local out = read()
  156.   win.setVisible(false)
  157.   return out
  158. end
  159. local playNote = function(nx,ny,ins)
  160.   local n = nil
  161.   if ins then
  162.     n = peripheral.wrap(ins)
  163.   else
  164.     n = note
  165.   end
  166.   if song[ny-1+scroll] and song[ny-1+scroll][nx-1+xscroll] then
  167.     if song[ny-1+scroll]["ins"] == "s" and song[ny-1+scroll]["sound"] then
  168.       n.playSound(song[ny-1+scroll]["sound"],1,song[ny-1+scroll][nx-1+xscroll])
  169.     elseif song[ny-1+scroll]["ins"] ~= "s" then
  170.       n.playNote(song[ny-1]["ins"],song[ny-1+scroll][nx-1+xscroll])
  171.     end
  172.   end
  173. end
  174. local largestnum = function(tbl)
  175.   local out = 0
  176.   for key,value in pairs(tbl) do
  177.     if type(key) == "number" and key > out then
  178.       out = key
  179.     end
  180.   end
  181.   return out
  182. end
  183. local drawOptions = function()
  184.   term.setBackgroundColor(colors.black)
  185.   term.setTextColor(colors.white)
  186.   term.clear()
  187.   term.setCursorPos(1,1)
  188.   term.setTextColor(colors.white)
  189.   term.setCursorPos(1,1)
  190.   term.setTextColor(colors.lightBlue)
  191.   term.write("Tempo: "..song["tempo"].." bpm")
  192.   term.setCursorPos(1,2)
  193.   term.setTextColor(colors.orange)
  194.   term.write("Editing Noteblock: "..textnote)
  195.   term.setCursorPos(1,3)
  196.   term.setTextColor(colors.lime)
  197.   term.write("Song Name: "..song["name"])
  198.   term.setCursorPos(1,4)
  199.   term.setTextColor(colors.blue)
  200.   term.write("Author: "..song["author"])
  201.   term.setCursorPos(1,5)
  202.   term.setTextColor(colors.brown)
  203.   term.write("Edit Playback Noteblocks")
  204.   term.setCursorPos(1,6)
  205.   term.setTextColor(colors.red)
  206.   term.write("Save")
  207.   term.setCursorPos(1,7)
  208.   term.setTextColor(colors.magenta)
  209.   term.write("Load")
  210.   term.setCursorPos(1,8)
  211.   term.setTextColor(colors.cyan)
  212.   term.write("Play")
  213.   term.setCursorPos(1,9)
  214.   term.setTextColor(colors.yellow)
  215.   term.write("Exit")
  216.   term.setCursorPos(1,10)
  217.   term.setTextColor(colors.lightBlue)
  218.   term.write("New")
  219.   term.setCursorPos(1,11)
  220.   term.setTextColor(colors.purple)
  221.   term.write("Stop")
  222. end
  223. local drawNbs = function()
  224.   term.setTextColor(colors.orange)
  225.   term.setBackgroundColor(colors.black)
  226.   term.clear()
  227.   term.setCursorPos(1,1)
  228.   term.write("Playback Noteblocks:")
  229.   term.setCursorPos(1,2)
  230.   term.setTextColor(colors.lightGray)
  231.   for i = 1,#nbs,1 do
  232.     term.write("  "..nbs[i])
  233.     term.setCursorPos(1,i+2)
  234.   end
  235.   term.setTextColor(colors.cyan)
  236.   term.write("Add")
  237.   local cx,cy = term.getCursorPos()
  238.   term.setCursorPos(1,cy+1)
  239.   term.setTextColor(colors.red)
  240.   term.write("Exit")
  241. end
  242. local songplay = false
  243. local playSong = function()
  244.   while true do
  245.     if songplay == true then
  246.       local nbnum = 1
  247.       songplay = false
  248.       for j = 1,song["len"] do
  249.         for i = 1,table.maxn(song) do
  250.           if song[i] and song[i][j] then
  251.             playNote(j+1-xscroll,i+1-scroll,nbs[math.floor(nbnum)])
  252.             nbnum = nbnum + 0.25
  253.             if math.floor(nbnum) > #nbs and math.floor(nbnum) > 1 then
  254.               break
  255.             end
  256.           end
  257.           if songplay then
  258.             break
  259.           end
  260.         end
  261.         nbnum = 1
  262.         sleep(60/song["tempo"])
  263.         if songplay then
  264.           break
  265.         end
  266.       end
  267.     end
  268.     sleep(1)
  269.   end
  270. end
  271. --Code
  272. local code = function()
  273. term.clear()
  274. drawEdit()
  275. drawNotes()
  276. local condition = true
  277. while condition do
  278.   local event,a,b,c,d = os.pullEventRaw()
  279.   if event == "terminate" then
  280.     term.setBackgroundColor(colors.black)
  281.     term.setTextColor(colors.white)
  282.     term.clear()
  283.     term.setCursorPos(1,1)
  284.     break
  285.   end
  286.   if event == "mouse_click" and a == 1 then
  287.     if b >= x-6 and c == y then
  288.       screen = 2
  289.       drawOptions()
  290.       local condition = true
  291.       while condition do
  292.         local event,a,b,c = os.pullEvent("mouse_click")
  293.         if a == 1 then
  294.           if c == 1 then
  295.             song["tempo"] = popup("Enter in the new tempo (bpm).")
  296.             drawOptions()
  297.           elseif c == 2 then
  298.             textnote = popup("Enter in the new noteblock for editing.")
  299.             note = peripheral.wrap(textnote)
  300.             drawOptions()
  301.           elseif c == 3 then
  302.             song["name"] = popup("Enter in the new name.")
  303.             drawOptions()
  304.           elseif c == 4 then
  305.             song["author"] = popup("Enter in the new author.")
  306.             drawOptions()
  307.           elseif c == 5 then
  308.             drawNbs()
  309.             while true do
  310.               local event,a,b,c = os.pullEvent()
  311.               if a == 1 then
  312.                 if c >= 2 and c <= #nbs+1 and string.lower(popup("Delete this noteblock? (Y/N)")) == "y" then
  313.                   table.remove(nbs,c-1)
  314.                   drawNbs()
  315.                 elseif c == #nbs+2 then
  316.                   table.insert(nbs,popup("Enter in the new noteblock peripheral."))
  317.                   drawNbs()
  318.                 elseif c == #nbs+3 then
  319.                   drawOptions()
  320.                   break
  321.                 end
  322.               end
  323.             end
  324.           elseif c == 6 then
  325.             local save = fs.open(popup("Enter in the directory to save."),"w")
  326.             if save then
  327.               save.write(textutils.serialize(song))
  328.             end
  329.             save.close()
  330.             drawOptions()
  331.           elseif c == 7 then
  332.             local a = popup("Enter in the directory to load.","r")
  333.             if fs.exists(a) then
  334.               local load = fs.open(a,"r")
  335.               song = textutils.unserialize(load.readAll())
  336.               load.close()
  337.             end
  338.             drawOptions()
  339.           elseif c == 8 then
  340.             songplay = true
  341.           elseif c == 9 then
  342.             drawEdit()
  343.             drawNotes()
  344.             break
  345.           elseif c == 10 then
  346.             song = {["len"]=10,["tempo"]=120,["name"]="",['author']=''}
  347.           elseif c == 11 then
  348.             songplay = 1
  349.           end
  350.         end
  351.       end
  352.     elseif c == y and largestnum(song) > y-4+scroll then
  353.       scroll = scroll+1
  354.       drawEdit()
  355.       drawNotes()
  356.     elseif b == 1 and c < y and c > 1 then
  357.       if not song[c-1+scroll] then
  358.         song[c-1+scroll] = {}
  359.         song[c-1+scroll]["ins"] = 0
  360.         drawEdit()
  361.         drawNotes()
  362.         desc(song[c-1+scroll]["ins"],2)
  363.       else
  364.         if string.lower(popup("Delete This Track? (Y/N)")) == "y" then
  365.           song[c-1+scroll] = nil
  366.         end
  367.         drawEdit()
  368.         drawNotes()
  369.       end
  370.     elseif c == 1 and b <= x-4 and scroll > 0 then
  371.       scroll = scroll-1
  372.       drawEdit()
  373.       drawNotes()
  374.     elseif c == 1 and b >= x-1 then
  375.       xscroll = xscroll+1
  376.       drawEdit()
  377.       drawNotes()
  378.     elseif c == 1 and b >= x-3 and xscroll ~= 0 then
  379.       xscroll = xscroll-1
  380.       drawEdit()
  381.       drawNotes()
  382.     end
  383.   end
  384.   if event == "mouse_scroll" and b > 1 and b < x and c > 1 and c < y and song[c-1+scroll] then
  385.     song[c-1+scroll][b-1+xscroll] = incrementNote(song[c-1+scroll][b-1+xscroll],a*-1,1)
  386.     drawNotes()
  387.     if song[c-1+scroll][b-1+xscroll] then
  388.       playNote(b,c)
  389.       drawEdit()
  390.       drawNotes()
  391.       desc(song[c-1+scroll][b-1+xscroll],1)
  392.     end
  393.   end
  394.   if event == "mouse_click" and a == 2 and b == 1 and c < y and c > 1 and song[c-1+scroll] and song[c-1+scroll]["ins"] == "s" then
  395.     song[c-1+scroll]["sound"] = popup("Enter in the new sound location")
  396.     drawEdit()
  397.     drawNotes()
  398.     desc(song[c-1+scroll]["ins"],2)
  399.   end
  400.   if event == "mouse_click" and b > 1 and c > 1 and c < y and song[c-1+scroll] then
  401.     if a == 1 then
  402.       song[c-1+scroll][b-1+xscroll] = nil
  403.       drawNotes()
  404.     elseif a == 2 then
  405.       playNote(b,c)
  406.       drawEdit()
  407.       drawNotes()
  408.       if song[c-1+scroll][b-1+xscroll] then
  409.       desc(song[c-1+scroll][b-1+xscroll],1)
  410.       end
  411.     end
  412.   end
  413.   if event == "mouse_scroll" and b == 1 then
  414.     if not song[c-1+scroll] then
  415.       song[c-1+scroll] = {}
  416.     end
  417.     song[c-1+scroll]["ins"] = incrementNote(song[c-1+scroll]["ins"],a*-1,2)
  418.     drawEdit()
  419.     drawNotes()
  420.     desc(song[c-1+scroll]["ins"],2)
  421.   end
  422.   if event == "mouse_click" and a == 3 and c > 1 and c < y and b > 1 then
  423.     song["len"] = b+xscroll
  424.     drawEdit()
  425.     drawNotes()
  426.   end
  427.   if event == "mouse_click" and a == 2 and b == 1 and c > 1 and c < y then
  428.     drawEdit()
  429.     drawNotes()
  430.     desc(song[c-1+scroll]["ins"],2)
  431.   end
  432.   if event == "mouse_click" and a == 1 and b <= 5 and c == y then
  433.     break
  434.   end
  435.   if event == "redstone" then
  436.     songplay = true
  437.     rs.setOutput("back",true)
  438.     sleep(0.2)
  439.     rs.setOutput("back",false)
  440.   end
  441.   sleep(0)
  442. end
  443. term.setBackgroundColor(colors.black)
  444. term.setTextColor(colors.white)
  445. term.clear()
  446. term.setCursorPos(1,1)
  447. end
  448. parallel.waitForAny(code,playSong)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement