Advertisement
pedrosgali

OCStepSequencer

Jun 2nd, 2014
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.50 KB | None | 0 0
  1. local computer = require("computer")
  2. local fs = require("filesystem")
  3. local serial = require("serialization")
  4. local term = require("term")
  5. local event = require("event")
  6. local component = require("component")
  7. local gpu = component.gpu
  8. local note = component.note_block
  9.  
  10. a = {...}
  11. filename = "c/songs/"..a[1]
  12. timeSig = tonumber(a[2])
  13. detail = tonumber(a[3])
  14.  
  15. currentBar = 1
  16. bpm = 120
  17.  
  18. if not fs.isDirectory("c/songs") then fs.makeDirectory("c/songs") end
  19. if timeSig == nil then timeSig = 4 end
  20. if bpm == nil then bpm = 120 end
  21. if detail == nil then detail = 4 end
  22.  
  23. if detail == nil then detail = 4 end
  24.  
  25. function newScore()
  26.     score = nil
  27.     score = {}
  28.     score.bpm = bpm
  29.     barCount = 0
  30. end
  31.  
  32. function loadScore(filepath)
  33.     file = io.open(filepath, "r")
  34.     score = serial.unserialize(file:read("*all"))
  35.     file:close()
  36.     barCount = score.barCount
  37.     bpm = score.bpm
  38. end
  39.  
  40. function saveScore()
  41.     score.barCount = barCount
  42.     score.bpm = bpm
  43.     file = io.open(filename, "w")
  44.     file:write(serial.serialize(score))
  45.     file:close()
  46. end
  47.  
  48. function fixTempo(beats)
  49.     tempo = 60 / beats
  50.     tempo = tempo / detail
  51.     gpu.setBackground(0x000000)
  52.     gpu.setForeground(0xFFFFFF)
  53.     gpu.set(1, y, currentBar.."/"..barCount.."            "..bpm.." BPM")
  54. end
  55.  
  56. function addBar(beats)
  57.     barCount = barCount + 1
  58.     score[barCount] = {}
  59.     for i = 1, beats * detail do
  60.         score[barCount][i] = "rest"
  61.     end
  62. end
  63.  
  64. function playNote(note)
  65.     note.trigger(note)
  66. end
  67.  
  68. function noteCaller()
  69.     octCount = 0
  70.     noteCount = 0
  71.     noteString = "A1BC2D3EF4G5"
  72.     notePos = 0
  73.     noteTab = {}
  74.     for i = 1, 25 do
  75.         notePos = notePos + 1
  76.         noteCount = noteCount + 1
  77.         noteVal = string.sub(noteString, notePos, notePos)
  78.         if noteVal == "1" then
  79.             noteVal = "A#"
  80.         elseif noteVal == "2" then
  81.             noteVal = "C#"
  82.         elseif noteVal == "3" then
  83.             noteVal = "D#"
  84.         elseif noteVal == "4" then
  85.             noteVal = "F#"
  86.         elseif noteVal == "5" then
  87.             noteVal = "G#"
  88.         end
  89.         label = noteVal..octCount
  90.         noteTab[noteCount] = {}
  91.         noteTab[noteCount]["label"] = label
  92.         noteTab[noteCount]["note"] = i
  93.         if noteVal == "G#" then
  94.             notePos = 0
  95.             octCount = octCount + 1
  96.         end
  97.     end
  98. end
  99.  
  100. function addButton(label, bType, xPos, yPos, w, h, col1, col2, retVal, retVal2)
  101.     if buttonTab == nil then
  102.         buttonTab = {}
  103.         buttonCount = 0
  104.     end
  105.     buttonCount = buttonCount + 1
  106.     buttonTab[buttonCount] = {
  107.         label = label,
  108.         bType = bType,
  109.         xPos = xPos,
  110.         yPos = yPos,
  111.         width = w,
  112.         height = h,
  113.         colour = col1,
  114.         pCol = col1,
  115.         tCol = col2,
  116.         retVal = retVal,
  117.         retVal2 = retVal2,
  118.         active = false,
  119.         }
  120. end
  121.  
  122. function initKeys()
  123.     x, y = gpu.getResolution()
  124.     bWidth = 4
  125.     yStart = y - 15
  126.     for i = 1, #noteTab do
  127.         if string.sub(noteTab[i]["label"], 2, 2) == "#" then
  128.             col = 0x000000
  129.             tCol = 0xFFFFFF
  130.             pCol = 0xFF0000
  131.             bCol = 0x0000FF
  132.             pTCol = 0x000000
  133.         else
  134.             col = 0xFFFFFF
  135.             tCol = 0x000000
  136.             pCol = 0xFF6666
  137.             bCol = 0x6666FF
  138.             pTCol = 0x000000
  139.         end
  140.         addButton(noteTab[i]["label"], "note", 1, yStart - i, 10, 1, col, tCol, noteTab[i]["note"])
  141.         for j = 1, timeSig * detail do
  142.             if j % detail == 1 then
  143.                 addButton("[<>]", "pad", 11 + (bWidth), yStart - i, 3, 1, bCol, pTCol, j, noteTab[i]["note"])
  144.             else
  145.                 addButton("[<>]", "pad", 11 + (bWidth), yStart - i, 3, 1, pCol, pTCol, j, noteTab[i]["note"])
  146.             end
  147.             if j == timeSig * detail then
  148.                 bWidth = 4
  149.             else
  150.                 bWidth = bWidth + 4
  151.             end
  152.         end
  153.     end
  154. end
  155.  
  156. function drawScreen(bar)
  157.     gpu.setBackground(0x000000)
  158.     term.clear()
  159.     for i = 1, #buttonTab do
  160.         gpu.setBackground(buttonTab[i]["colour"])
  161.         gpu.setForeground(buttonTab[i]["tCol"])
  162.         if buttonTab[i]["bType"] ~= "pad" then
  163.             gpu.fill(buttonTab[i]["xPos"], buttonTab[i]["yPos"], buttonTab[i]["width"], buttonTab[i]["height"], " ")
  164.             offset = math.floor((buttonTab[i]["width"] - #buttonTab[i]["label"]) / 2)
  165.             if buttonTab[i]["height"] > 1 then
  166.                 yOffset = math.floor((buttonTab[i]["height"] /2))
  167.                 gpu.set(buttonTab[i]["xPos"] + offset, buttonTab[i]["yPos"] + yOffset, buttonTab[i]["label"])
  168.             else
  169.                 gpu.set(buttonTab[i]["xPos"] + offset, buttonTab[i]["yPos"], buttonTab[i]["label"])
  170.             end
  171.         else
  172.             if score[bar][buttonTab[i]["retVal"]] == buttonTab[i]["retVal2"] then
  173.                 buttonTab[i]["label"] = "[><]"
  174.                 buttonTab[i]["active"] = true
  175.                 buttonTab[i]["colour"] = 0x66FF66
  176.             else
  177.                 buttonTab[i]["label"] = "[<>]"
  178.                 buttonTab[i]["active"] = true
  179.                 buttonTab[i]["colour"] = buttonTab[i]["pCol"]
  180.             end
  181.             gpu.setBackground(buttonTab[i]["colour"])
  182.             gpu.set(buttonTab[i]["xPos"], buttonTab[i]["yPos"], buttonTab[i]["label"])
  183.         end
  184.     end
  185.     gpu.setBackground(0x000000)
  186.     gpu.setForeground(0xFFFFFF)
  187.     gpu.set(1, y, currentBar.."/"..barCount.."            "..bpm.." BPM")
  188. end
  189.  
  190. function play()
  191.     for j = 1, #score do
  192.         for k = 1, #score[j] do
  193.             if score[j][k] ~= "rest" then
  194.                 note.trigger(score[j][k])
  195.             end
  196.             ev = event.pull(tempo, _, ev)
  197.             if ev == "touch" then
  198.                 return true
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. function run()
  205.     ev, sa, p1, p2, p3, p4 = event.pull(.1, _, ev, sa, p1, p2, p3, p4)
  206.     if ev == "key_down" then
  207.         running = false
  208.         return
  209.     elseif ev == "touch" or ev =="drag" then
  210.         for i = 1, #buttonTab do
  211.             if p1 >= buttonTab[i]["xPos"] and p1 <= buttonTab[i]["xPos"] + buttonTab[i]["width"] then
  212.                 if p2 >= buttonTab[i]["yPos"] and p2 <= buttonTab[i]["yPos"] + (buttonTab[i]["height"] - 1) then
  213.                     if buttonTab[i]["bType"] == "note" then
  214.                         gpu.setBackground(0x66FF66)
  215.                         gpu.fill(buttonTab[i]["xPos"], buttonTab[i]["yPos"], buttonTab[i]["width"], buttonTab[i]["height"], " ")
  216.                         note.trigger(buttonTab[i]["retVal"])
  217.                         gpu.setBackground(buttonTab[i]["colour"])
  218.                         gpu.setForeground(buttonTab[i]["tCol"])
  219.                         gpu.fill(buttonTab[i]["xPos"], buttonTab[i]["yPos"], buttonTab[i]["width"], buttonTab[i]["height"], " ")
  220.                         offset = math.floor((buttonTab[i]["width"] - #buttonTab[i]["label"]) / 2)
  221.                         gpu.set(buttonTab[i]["xPos"] + offset, buttonTab[i]["yPos"], buttonTab[i]["label"])
  222.                         return
  223.                     elseif buttonTab[i]["bType"] == "pad" then
  224.                         buttonTab[i]["active"] = not buttonTab[i]["active"]
  225.                         if buttonTab[i]["active"] then
  226.                             buttonTab[i]["colour"] = 0x66FF66
  227.                             buttonTab[i]["label"] = "[><]"
  228.                             score[currentBar][buttonTab[i]["retVal"]] = buttonTab[i]["retVal2"]
  229.                             note.trigger(buttonTab[i]["retVal2"])
  230.                         else
  231.                             buttonTab[i]["colour"] = buttonTab[i]["pCol"]
  232.                             buttonTab[i]["label"] = "[<>]"
  233.                             score[currentBar][buttonTab[i]["retVal"]] = "rest"
  234.                         end
  235.                         gpu.setBackground(buttonTab[i]["colour"])
  236.                         gpu.setForeground(buttonTab[i]["tCol"])
  237.                         gpu.set(buttonTab[i]["xPos"], buttonTab[i]["yPos"], buttonTab[i]["label"])
  238.                     elseif buttonTab[i]["bType"] == "save" then
  239.                         saveScore()
  240.                     elseif buttonTab[i]["bType"] == "loop" then
  241.                         if buttonTab[i]["active"] then
  242.                             loopBool = false
  243.                             buttonTab[i]["active"] = false
  244.                         else
  245.                             loopBool = true
  246.                             buttonTab[i]["active"] = true
  247.                         end
  248.                     elseif buttonTab[i]["bType"] == "play" then
  249.                         if loopBool then
  250.                             while true do
  251.                                 if play() then return end
  252.                             end
  253.                         else
  254.                             play()
  255.                         end
  256.                     elseif buttonTab[i]["bType"] == "<<" then
  257.                         if currentBar > 1 then currentBar = currentBar - 1 end
  258.                         drawScreen(currentBar)
  259.                     elseif buttonTab[i]["bType"] == ">>" then
  260.                         if currentBar < barCount then
  261.                             currentBar = currentBar + 1
  262.                             drawScreen(currentBar)
  263.                         else
  264.                             addBar(timeSig)
  265.                             currentBar = currentBar + 1
  266.                             drawScreen(currentBar)
  267.                         end
  268.                     elseif buttonTab[i]["bType"] == "^" then
  269.                         bpm = bpm + 1
  270.                         fixTempo(bpm)
  271.                     elseif buttonTab[i]["bType"] == "v" then
  272.                         bpm = bpm - 1
  273.                         fixTempo(bpm)
  274.                     end
  275.                 end
  276.             end
  277.         end
  278.     end
  279. end
  280.  
  281. noteCaller()
  282. initKeys()
  283. if fs.exists(filename) then
  284.     loadScore(filename)
  285. else
  286.     newScore()
  287. end
  288. addBar(timeSig)
  289. currentBar = 1
  290. fixTempo(bpm)
  291. addButton("Save", "save", 1, 1, 10, 3, 0xFFFFFF, 0x000000)
  292. addButton("Loop", "loop", 21, 1, 10, 3, 0x00FFFF, 0x000000)
  293. addButton("Play", "play", 31, 1, 10, 3, 0x66FF66, 0x000000)
  294. addButton("<<", "<<", 41, 1, 10, 3, 0x6666FF, 0x000000)
  295. addButton(">>", ">>", 51, 1, 10, 3, 0x6666FF, 0x000000)
  296. addButton("^", "^", 61, 1, 10, 3, 0x66FF66, 0x000000)
  297. addButton("v", "v", 71, 1, 10, 3, 0x66FF66, 0x000000)
  298. drawScreen(currentBar)
  299. running = true
  300. while running do
  301.     run()
  302. end
  303. gpu.setBackground(0x000000)
  304. gpu.setForeground(0xFFFFFF)
  305. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement