Advertisement
Agent_Silence

CrescendoMusicEditor

Jun 18th, 2014 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.26 KB | None | 0 0
  1. term.setBackgroundColor(colors.lightGray)
  2. term.clear()
  3. addedKeys = {}
  4. current_octave = 1
  5. current_page = 1
  6. current_instrument = "Piano/Harp"
  7. timestamp = 0.25
  8.  
  9. function instCode(access)
  10.     if access == "Piano/Harp" then
  11.         return colors.green
  12.     elseif access == "Bass Drum" then
  13.         return colors.red
  14.     elseif access == "Clicks/Sticks" then
  15.         return colors.cyan
  16.     elseif access == "Bass Guitar" then
  17.         return colors.purple
  18.     elseif access == "Snare Drum" then
  19.         return colors.orange
  20.     end
  21. end
  22.  
  23. function blockMissing()
  24.     term.setCursorPos(2,13)
  25.     term.setTextColor(colors.red)
  26.     term.setBackgroundColor(colors.lightGray)
  27.     term.write("Iron Noteblck Missing")
  28.     sleep(2)
  29.     term.setTextColor(colors.lightGray)
  30.     term.write("                   ")
  31. end
  32.  
  33. function maxKeys()
  34.     term.setCursorPos(2,13)
  35.     term.setTextColor(colors.red)
  36.     term.setBackgroundColor(colors.lightGray)
  37.     term.write("Exceeded Chord Limit")
  38.     sleep(2)
  39.     term.setTextColor(colors.lightGray)
  40.     term.write("                    ")
  41. end
  42.  
  43. function checkNotes()
  44.     local checkID = 1
  45.     local foundNotes = 0
  46.     for i=1,49 do
  47.         for i,v in pairs(addedKeys) do
  48.             if v.key == checkID then
  49.                 foundNotes = foundNotes + 1
  50.                 if foundNotes == 5 then
  51.                     return false, checkID
  52.                 end
  53.                 checkID = checkID + 1
  54.             end
  55.         end
  56.     end
  57.     return true
  58. end
  59.  
  60. function playNotes()
  61.     local music = nil -- Calls local variable
  62.  
  63.     for i,v in pairs(peripheral.getNames()) do -- Find iron noteblock
  64.         if peripheral.getType(v) == "iron_note" then
  65.             music = peripheral.wrap(v)
  66.         end
  67.     end
  68.  
  69.     local currentPos = 1
  70.     local currentPage = 1
  71.     local marked = true
  72.     if music ~= nil then
  73.         for i=1,9 do
  74.             for i=1,49 do
  75.                 marked = true
  76.                 for i,v in pairs(addedKeys) do
  77.                     if v.pos == currentPos and v.page == currentPage then
  78.                         if v.octave == 1 then
  79.                             music.playNote(numInst(v.instrument),v.key)
  80.                         else
  81.                             music.playNote(numInst(v.instrument),v.key+8)
  82.                         end
  83.                         marked = false
  84.                         end
  85.                 end
  86.                 if marked == false then
  87.                     sleep(timestamp)
  88.                 end
  89.                 currentPos = currentPos + 1
  90.             end
  91.             currentPage = currentPage + 1
  92.         end
  93.     else
  94.         blockMissing()
  95.     end
  96.  end
  97.  
  98. function clearNotes()
  99.     term.setTextColor(colors.black)
  100.     term.setBackgroundColor(colors.white)
  101.     term.setCursorPos(1,1)
  102.     print(string.rep("-", 51 * 12))
  103. end
  104.  
  105. function loadPage()
  106.     clearNotes()
  107.     for i,v in pairs(addedKeys) do
  108.         if v.page == current_page then
  109.             term.setCursorPos(v.pos + 2,v.key)
  110.             term.setBackgroundColor(colors.white)
  111.             term.setTextColor(instCode(v.instrument))
  112.             term.write("o")
  113.         end
  114.     end
  115. end
  116.  
  117. function numInst(key)
  118.     if key == "Piano/Harp" then
  119.         return 0
  120.     elseif key == "Bass Drum" then
  121.         return 1
  122.     elseif key == "Snare Drum" then
  123.         return 2
  124.     elseif key == "Clicks/Sticks" then
  125.         return 3
  126.     elseif key == "Bass Guitar" then
  127.         return 4
  128.     end
  129. end
  130.  
  131. function nextInst()
  132.     if current_instrument == "Piano/Harp" then
  133.         return "Bass Drum"
  134.     elseif current_instrument == "Bass Drum"  then
  135.         return "Clicks/Sticks"
  136.     elseif current_instrument == "Clicks/Sticks" then
  137.         return "Bass Guitar"
  138.     elseif current_instrument == "Bass Guitar" then
  139.         return "Snare Drum"
  140.     elseif current_instrument == "Snare Drum" then
  141.         return "Piano/Harp"
  142.     end
  143. end
  144.  
  145. function noteList(position)
  146.     if position == 1 then
  147.         return "G "
  148.     elseif position == 2 then
  149.         return "G#"
  150.     elseif position == 3 then
  151.         return "A "
  152.     elseif position == 4 then
  153.         return "A#"
  154.     elseif position == 5 then
  155.         return "B "
  156.     elseif position == 6 then
  157.         return "C "
  158.     elseif position == 7 then
  159.         return "C#"
  160.     elseif position == 8 then
  161.         return "D "
  162.     elseif position == 9 then
  163.         return "D#"
  164.     elseif position == 10 then
  165.         return "E "
  166.     elseif position == 11 then
  167.         return "F "
  168.     elseif position == 12 then
  169.         return "F#"
  170.     end
  171. end
  172.  
  173. function spaced()
  174.     local word = {}
  175.     for i=1,#current_instrument do
  176.         table.insert(word, string.sub(current_instrument, i, i))
  177.     end
  178.     if #word < #"Clicks/Sticks" then
  179.         repeat table.insert(word, " ") until #word == #"Clicks/Sticks"
  180.     end
  181.     return table.concat(word)
  182. end
  183.  
  184. function note(pos, key)
  185.     local foundNote = false
  186.     local deleteNote = nil
  187.     for i, v in pairs(addedKeys) do
  188.         if v.key == key and v.pos == pos - 2 and v.page == current_page then
  189.             term.setCursorPos(pos,key)
  190.             term.setBackgroundColor(colors.white)
  191.             term.setTextColor(colors.black)
  192.             term.write("-")
  193.             foundNote = true
  194.             deleteNote = i
  195.         end
  196.     end
  197.     if foundNote == false then
  198.         local template = {
  199.             key = "1",
  200.             pos = "1",
  201.             page = 1,
  202.             instrument = "Piano/Harp",
  203.             octave = 1
  204.         }
  205.         template.key = key
  206.         template.pos = pos - 2
  207.         template.page = current_page
  208.         template.instrument = current_instrument
  209.         template.octave = current_octave
  210.         table.insert(addedKeys,template)
  211.         term.setCursorPos(pos,key)
  212.         term.setBackgroundColor(colors.white)
  213.         term.setTextColor(instCode(current_instrument))
  214.         if template.octave == 1 then
  215.             term.write("o")
  216.         elseif template.octave == 2 then
  217.             term.write("q")
  218.         end
  219.     elseif foundNote == true then
  220.         table.remove(addedKeys, deleteNote)
  221.     end
  222. end
  223.  
  224. function removeNotes()
  225.     for i=1,#addedKeys do
  226.         table.remove(addedKeys)
  227.     end
  228. end
  229.  
  230. function drawGUI()
  231.     for i=1,12 do
  232.         term.setBackgroundColor(colors.gray)
  233.         term.setTextColor(colors.white)
  234.         term.setCursorPos(1,i)
  235.         term.write(noteList(i))
  236.     end
  237.     term.setBackgroundColor(colors.lightGray)
  238.     term.setTextColor(colors.lightGray)
  239.     term.setCursorPos(1,13)
  240.     print(string.rep(" ", 51))
  241.     term.setCursorPos(2,14)
  242.     term.setTextColor(colors.white)
  243.     term.setBackgroundColor(colors.gray)
  244.     term.write("Page : "..current_page)
  245.     term.setCursorPos(2,15)
  246.     if #tostring(timestamp) == 4 then
  247.         term.write("Tempo : "..timestamp)
  248.     elseif #tostring(timestamp) == 3 then
  249.         term.write("Tempo : "..timestamp.."0")
  250.     elseif #tostring(timestamp) == 1 then
  251.         term.write("Tempo : "..timestamp..".00")
  252.     end
  253.     term.setCursorPos(2,16)
  254.     term.write("Octave "..current_octave)
  255.     term.setCursorPos(2,17)
  256.     term.write("Clear Notes")
  257.     term.setCursorPos(2,18)
  258.     term.setTextColor(instCode(current_instrument))
  259.     term.write(spaced())
  260.     term.setTextColor(colors.white)
  261. end
  262.  
  263. function inputGUI()
  264.     term.setBackgroundColor(colors.lightGray)
  265.     term.clear()
  266.     term.setCursorPos(16,7)
  267.     term.setTextColor(colors.lightGray)
  268.     term.setBackgroundColor(colors.black)
  269.     term.write("  File Name Here  ")
  270.     term.setCursorPos(16,7)
  271.     term.setTextColor(colors.white)
  272.     local input = read()
  273.     return input
  274. end
  275.  
  276. function save()
  277.     local filename = inputGUI()
  278.     local file = fs.open(filename, "w")
  279.     file.write("addedKeys = "..textutils.serialize(addedKeys))
  280.     file.close()
  281.     term.setCursorPos(18,8)
  282.     term.write("File Saved")
  283.     sleep(2)
  284.     drawGUI()
  285.     clearNotes()
  286.     loadPage()
  287. end
  288.  
  289. function loadSave()
  290.     local filename = inputGUI()
  291.     if fs.exists(filename) then
  292.         fs.move(filename, "Loading")
  293.         os.loadAPI("Loading")
  294.         addedKeys = Loading.addedKeys
  295.         term.setCursorPos(18,8)
  296.         term.write("File Loaded")
  297.         os.unloadAPI("Loading")
  298.         fs.move("Loading",filename)
  299.         sleep(2)
  300.         drawGUI()
  301.         clearNotes()
  302.         loadPage()
  303.     else
  304.         term.setCursorPos(18,8)
  305.         term.setTextColor(colors.red)
  306.         term.write("File not found")
  307.         sleep(2)
  308.         drawGUI()
  309.         clearNotes()
  310.         loadPage()
  311.     end
  312. end
  313.  
  314. drawGUI()
  315. clearNotes()
  316. loadPage()
  317.  
  318. -- Event Handler Code --
  319. while true do
  320.     loadPage()
  321.     drawGUI()
  322.     event, button, xClick, yClick = os.pullEvent()
  323.     if event == "mouse_click" then
  324.        
  325.          if yClick < 13 and 2 < xClick and button ~= 3 then
  326.             note(xClick, yClick)
  327.         end
  328.              
  329.         if xClick > 1 and xClick < 14 and yClick == 18 then
  330.             current_instrument = nextInst()
  331.         end
  332.        
  333.         if xClick > 1 and xClick < 12 and yClick == 17 then
  334.             removeNotes()
  335.             clearNotes()
  336.             loadPage()
  337.         end
  338.        
  339.         if xClick > 1 and xClick < 10 and yClick == 14 and button == 1 then
  340.             if current_page < 9 then
  341.                  current_page = current_page + 1
  342.                  loadPage()
  343.             end
  344.         elseif xClick > 1 and xClick < 10 and yClick == 14 and button == 2 then
  345.             if 1 < current_page then
  346.                 current_page = current_page - 1
  347.                 loadPage()
  348.             end
  349.         end
  350.  
  351.         if xClick > 1 and xClick < 10 and yClick == 16 then
  352.             if current_octave == 1 then
  353.                 current_octave = 2
  354.             elseif current_octave == 2 then
  355.                 current_octave = 1
  356.             end
  357.         end
  358.  
  359.         if button == 3 then
  360.             playNotes()    
  361.         end
  362.        
  363.     elseif event == "key" or event == "char" then
  364.         if event == "key" then
  365.             if button == keys.q then
  366.                 playNotes()
  367.             end
  368.            
  369.             if button == keys.left then
  370.                 if 1 < current_page then
  371.                     current_page = current_page - 1
  372.                     loadPage()
  373.                 end
  374.             end
  375.        
  376.             if button == keys.right then
  377.                 if current_page < 9 then
  378.                     current_page = current_page + 1
  379.                     loadPage()
  380.                 end
  381.             end
  382.        
  383.             if button == keys.e then
  384.                 save()
  385.             elseif button == keys.e then
  386.                 loadSave()
  387.             end
  388.            
  389.             if button == keys.up then
  390.                 if timestamp < 9.75 then
  391.                     timestamp = timestamp + 0.25
  392.                 end
  393.             elseif button == keys.down then
  394.                 if timestamp > 0.25 then
  395.                     timestamp = timestamp - 0.25
  396.                 end
  397.             end
  398.         elseif event == "char" then
  399.             if button == "q" then
  400.                 playNotes()
  401.             end
  402.            
  403.             if button == "e" then
  404.                 save()
  405.             elseif button == "r" then
  406.                 loadSave()
  407.             end
  408.         end
  409.     end
  410. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement