Advertisement
Guest User

memusic

a guest
Jan 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.97 KB | None | 0 0
  1. local patternLength = 32
  2. local beatLength = 4
  3. local bpm = 240
  4. local songLength = 1
  5.  
  6. local sp = {}
  7.  
  8. local i = 0
  9. local p = peripheral.wrap("speaker_"..i)
  10. repeat
  11.     sp[i] = p
  12.     i = i+1
  13.     p = peripheral.wrap("speaker_"..i)
  14. until p == nil
  15.  
  16. local nextSpeaker = 0
  17. local speakerCount = table.getn(sp)
  18.  
  19. local instrumentCount = 10
  20. local instruments = {
  21.     -- musical
  22.     {["name"] = "piano", ["file"] = "block.note.harp"},
  23.     {["name"] = "bajo", ["file"] = "block.note.bass"},
  24.     {["name"] = "campana", ["file"] = "block.note.bell"},
  25.     {["name"] = "tintineo", ["file"] = "block.note.chime"},
  26.     {["name"] = "xylofón", ["file"] = "block.note.xylophone"},
  27.     {["name"] = "flauta", ["file"] = "block.note.flute"},
  28.     {["name"] = "guitarra", ["file"] = "block.note.guitar"},
  29.     {["name"] = "bombo", ["file"] = "block.note.basedrum"},
  30.     {["name"] = "platillo", ["file"] = "block.note.hat"},
  31.     {["name"] = "redoblante", ["file"] = "block.note.snare"},
  32.     -- sfx
  33.     {["name"] = "levelup", ["file"] ="entity.player.levelup"}
  34. }
  35.  
  36. local notes = {
  37.     "F#", "G ", "G#", "A ", "A#", "B ",
  38.     "C ", "C#", "D ", "D#", "E ", "F "
  39. }
  40.  
  41. local song = {}
  42. local patterns = {}
  43.  
  44. local currentPattern = 1
  45. local currentSongPattern = 1
  46. local currentChannel = 1
  47. local currentYOffset = 0
  48. local currentXOffset = 0
  49. local cursor = 1
  50. local playing = false
  51. local pressingShift = false
  52.  
  53. local playingTimer = nil
  54. local delayedNoteTimer = nil
  55. local delayedAllChannels = false
  56. local delayTime = 1
  57.  
  58. local menuSongPatternScroll = 1
  59. local menuChannelScroll = 1
  60.  
  61. local beatsOnScreen = 30
  62. local scrW, scrH = term.getSize()
  63.  
  64. -- Data of the current pattern
  65. local function getCurrentNotes(time)
  66.     local notes = patterns[currentPattern].channels[currentChannel].notes[time]
  67.     if (notes == nil) then
  68.         return ""
  69.     else
  70.         return notes
  71.     end
  72. end
  73.  
  74. -- Data of the pattern for the specified channel
  75. local function getChannelNotes(channel, time)
  76.     local notes = patterns[currentPattern].channels[channel].notes[time]
  77.     if (notes == nil) then
  78.         return ""
  79.     else
  80.         return notes
  81.     end
  82. end
  83.  
  84. -- Get an instrument by name
  85. local function getInstrumentByName(name)
  86.     for k, v in pairs(instruments) do
  87.         if (v.name == name) then
  88.             return v
  89.         end
  90.     end
  91.     error("Invalid instrument name")
  92. end
  93.  
  94. -- Note character to note number
  95. local function characterToNote(c)
  96.     local delayed = true
  97.     if (string.byte(c) >= 97) then
  98.         delayed = false
  99.     end
  100.    
  101.     if (delayed) then
  102.         return string.byte(c)-65, delayed
  103.     else
  104.         return string.byte(c)-97, delayed
  105.     end
  106. end
  107.  
  108. -- Note number to note character
  109. local function noteToCharacter(n, delayed)
  110.     if (delayed) then
  111.         return string.char(tonumber(n)+65) --Mayuscula
  112.     else
  113.         return string.char(tonumber(n)+97) --minuscula
  114.     end
  115. end
  116.  
  117. -- Pitch of a note number
  118. local function notePitch(n)
  119.     return math.pow(2, (n-12)/12)
  120. end
  121.  
  122. -- Pitch of a note character
  123. local function charPitch(c)
  124.     return notePitch(characterToNote(c))
  125. end
  126.  
  127. local function playSoundEffect(soundname, vol, pitch)
  128.     sp[nextSpeaker].playSound(getInstrumentByName(soundname).file, vol, pitch)
  129.     nextSpeaker = nextSpeaker+1
  130.     if (nextSpeaker > speakerCount) then
  131.         nextSpeaker = 0
  132.     end
  133. end
  134.  
  135. -- Play a single note number
  136. local function playNote(inst, vol, note)
  137.     sp[nextSpeaker].playSound(instruments[inst].file, vol, note)
  138.     nextSpeaker = nextSpeaker+1
  139.     if (nextSpeaker > speakerCount) then
  140.         nextSpeaker = 0
  141.     end
  142. end
  143.  
  144. -- Play a string of notes
  145. local function playBar(inst, vol, notes, delayed)
  146.     for n=1, string.len(notes) do
  147.         local note = string.sub(notes, n, n)
  148.         if (delayed and string.byte(note) < 97) then -- If its delayed, play only uppercases
  149.             playNote(inst, vol, charPitch(note))
  150.         elseif (not delayed and string.byte(note) >= 97) then -- If not, play only lowercases
  151.             playNote(inst, vol, charPitch(note))
  152.         end
  153.     end
  154. end
  155.  
  156. -- Play the delayed bar
  157. local function playCurrentBarDelayed()
  158.     if (delayedAllChannels) then
  159.         for i=1, instrumentCount do
  160.             playBar(i, 1, getChannelNotes(i, delayTime), true)
  161.         end
  162.     else
  163.         playBar(currentChannel, 1, getCurrentNotes(delayTime), true)
  164.     end
  165. end
  166.  
  167. -- Play the bar at a specified time
  168. local function playSpecificBar(time, allChannels)
  169.     delayTime = time
  170.     if (allChannels) then
  171.         for i=1, instrumentCount do
  172.             playBar(i, 1, getChannelNotes(i, time), false)
  173.         end
  174.     else
  175.         playBar(currentChannel, 1, getCurrentNotes(time), false)
  176.     end
  177.     delayedNoteTimer = os.startTimer(30/bpm)
  178.     delayedAllChannels = allChannels
  179. end
  180.  
  181. -- Play the current channel's bar currently indicated by the cursor
  182. local function playCurrentChannelBar()
  183.     playSpecificBar(cursor, false)
  184. end
  185.  
  186. -- Play the every bar currently indicated by the cursor
  187. local function playCurrentBar()
  188.     playSpecificBar(cursor, true)
  189. end
  190.  
  191. -- Add a character to the middle of a string
  192. local function addCharAt(str, char, pos)
  193.     local prev = string.sub(str, 1, pos)
  194.     local post = string.sub(str, pos+1, string.len(str))
  195.     return prev..char..post
  196. end
  197.  
  198. -- Remove a character from a string
  199. local function removeCharacterAt(str, pos)
  200.     local prev = string.sub(str, 1, pos-1)
  201.     local post = string.sub(str, pos+1, string.len(str))
  202.     return prev..post
  203. end
  204.  
  205. -- Replace a character in the middle of a string
  206. local function replaceCharAt(str, char, pos)
  207.     local prev = string.sub(str, 1, pos)
  208.     local post = string.sub(str, pos+2, string.len(str))
  209.     return prev..char..post
  210. end
  211.  
  212. -- Pixel coordinates to time and note character
  213. local function pixelToMusic(x, y, delayed)
  214.     local time = x-3+currentXOffset
  215.     local note = noteToCharacter(scrH-y+currentYOffset, delayed)
  216.     return time, note
  217. end
  218.  
  219. -- Time and note character to pixel coordinates
  220. local function musicToPixel(time, char)
  221.     local noteNumber, delayed = characterToNote(char)
  222.     local x = time+3-currentXOffset
  223.     local y = -noteNumber+currentYOffset+scrH
  224.     return x, y
  225. end
  226.  
  227. -- StartPlaying
  228. local function playFromCursor()
  229.     playing = true
  230.     playingTimer = os.startTimer(60/bpm)
  231.     playCurrentBar()
  232. end
  233.  
  234. -- StartPlaying
  235. local function playFromStart()
  236.     cursor = 1
  237.     currentXOffset = 0
  238.     playing = true
  239.     playingTimer = os.startTimer(60/bpm)
  240.     playCurrentBar()
  241. end
  242.  
  243. -- Pause
  244. local function stop()
  245.     playing = false
  246.     cursor = 1
  247. end
  248.  
  249. -- Pause
  250. local function pause()
  251.     playing = false
  252. end
  253.  
  254. -- Draw the musical sheet section of the screen
  255. local function DrawPattern()
  256.     local channel = currentChannel
  257.     local pattern = currentPattern
  258.     local xoffset = currentXOffset
  259.     local yoffset = currentYOffset
  260.  
  261.     for _y=1, scrH-2 do
  262.         local y = scrH-_y
  263.         local octave = math.floor((_y+yoffset-7)/12)+1
  264.         local n = (_y+yoffset)%12
  265.         local c = xoffset
  266.         term.setCursorPos(1,y+1)
  267.        
  268.         term.setBackgroundColor(colors.black)
  269.         if (n==1 or n==3 or n==5 or n==8 or n==10) then
  270.             term.setTextColor(colors.blue)
  271.         else
  272.             term.setTextColor(colors.cyan)
  273.         end
  274.         term.write(notes[(n-1)%12+1]..tostring(octave))
  275.        
  276.         local lineBackgroundColor = colors.white
  277.         local lineTextColor = colors.lightGray
  278.         local blackNote = false
  279.         if (n==1 or n==3 or n==5 or n==8 or n==10) then
  280.             lineBackgroundColor = colors.lightGray
  281.             lineTextColor = colors.gray
  282.             blackNote = true
  283.         end
  284.  
  285.         for c=xoffset, math.min(patternLength-1, xoffset+beatsOnScreen-1) do
  286.             term.setBackgroundColor(lineBackgroundColor)
  287.             term.setTextColor(lineTextColor)
  288.  
  289.             -- Draw Cursor Bar
  290.             if (cursor == c+1) then
  291.                 if (playing) then
  292.                     term.setBackgroundColor(colors.red)
  293.                     term.setTextColor(colors.black)
  294.                 else
  295.                     term.setBackgroundColor(colors.yellow)
  296.                     if (blackNote) then
  297.                         term.setTextColor(colors.brown)
  298.                     else
  299.                         term.setTextColor(colors.orange)
  300.                     end
  301.                 end
  302.             end
  303.  
  304.             if (c%(beatLength*4) == 0) then
  305.                 term.write("|")
  306.             elseif (c%beatLength == 0) then
  307.                 term.write(",")
  308.             else
  309.                 term.write("_")
  310.             end
  311.         end
  312.     end
  313.     term.setCursorPos(1,1)
  314.     term.setBackgroundColor(colors.black)
  315.     term.setTextColor(colors.yellow)
  316.     term.write(string.rep(" ", beatLength+3))
  317.     local i = 1+(-xoffset)%beatLength
  318.     local firstBeat = math.floor((xoffset-1)/beatLength)+1
  319.     for c=firstBeat, math.min(firstBeat+beatsOnScreen/beatLength-1, patternLength/beatLength-1) do
  320.         term.setCursorPos(3+i, 2)
  321.         term.write(tonumber(c+1))
  322.         term.write(string.rep(" ", beatLength))
  323.         i = i+beatLength
  324.     end
  325.    
  326.     -- Draw other instruments notes
  327.     for ch=1, instrumentCount do
  328.         if (ch ~= currentChannel) then
  329.             for time=xoffset+1, math.min(xoffset+beatsOnScreen, patternLength) do
  330.                 local notes = getChannelNotes(ch, time)
  331.                 local paintedNotes = {}
  332.                 for n=1, string.len(notes) do
  333.                     local noteCharacter = string.sub(notes, n, n)
  334.                     local delayed = true
  335.                     if (string.byte(noteCharacter) >= 97) then
  336.                         delayed = false
  337.                     end
  338.                     local x, y = musicToPixel(time, noteCharacter)
  339.                     term.setTextColor(colors.lightGray)
  340.                     if (y > 2) then
  341.                         if (paintedNotes[y]) then
  342.                             term.setBackgroundColor(colors.pink)
  343.                             term.setCursorPos(x, y)
  344.                             term.write("x")
  345.                         else
  346.                             if (delayed) then
  347.                                 term.setBackgroundColor(colors.pink)
  348.                                 term.setCursorPos(x, y)
  349.                                 term.write(">")
  350.                                 paintedNotes[y] = true
  351.                             else
  352.                                 term.setBackgroundColor(colors.lightBlue)
  353.                                 term.setCursorPos(x, y)
  354.                                 term.write(" ")
  355.                                 paintedNotes[y] = true
  356.                             end
  357.                         end
  358.                     end
  359.                 end
  360.             end
  361.         end
  362.     end
  363.  
  364.     -- Draw current instruments notes
  365.     for time=xoffset+1, math.min(xoffset+beatsOnScreen, patternLength) do
  366.         local notes = getCurrentNotes(time)
  367.         local paintedNotes = {}
  368.         for n=1, string.len(notes) do
  369.             local noteCharacter = string.sub(notes, n, n)
  370.             local delayed = true
  371.             if (string.byte(noteCharacter) >= 97) then
  372.                 delayed = false
  373.             end
  374.             local x, y = musicToPixel(time, noteCharacter)
  375.             term.setTextColor(colors.black)
  376.             if (y > 2) then
  377.                 if (paintedNotes[y]) then
  378.                     term.setBackgroundColor(colors.purple)
  379.                     term.setCursorPos(x, y)
  380.                     term.write("x")
  381.                 else
  382.                     if (delayed) then
  383.                         term.setBackgroundColor(colors.red)
  384.                         term.setCursorPos(x, y)
  385.                         term.write(">")
  386.                         paintedNotes[y] = true
  387.                     else
  388.                         term.setBackgroundColor(colors.blue)
  389.                         term.setCursorPos(x, y)
  390.                         term.write(" ")
  391.                         paintedNotes[y] = true
  392.                     end
  393.                 end
  394.             end
  395.         end
  396.     end
  397.  
  398.     term.setBackgroundColor(colors.pink)
  399.     term.setTextColor(colors.black)
  400.     term.setCursorPos(1,1)
  401.     term.write(" Parte "..tostring(song.patterns[currentSongPattern]).." ")
  402.  
  403.     term.setBackgroundColor(colors.black)
  404.     term.write(" ")
  405.  
  406.     term.setBackgroundColor(colors.lime)
  407.     term.write(" "..instruments[currentChannel].name.." ")
  408. end
  409.  
  410. -- Draw the control panel
  411. local function DrawPanel()
  412.     local panelWidth = scrW-(beatsOnScreen+3)
  413.     local x = scrW-panelWidth+1
  414.     term.setBackgroundColor(colors.brown)
  415.     for y=1, scrH do
  416.         term.setCursorPos(x,y)
  417.         term.write(string.rep(" ", panelWidth))
  418.     end
  419.  
  420.     term.setBackgroundColor(colors.orange)
  421.     term.setTextColor(colors.black)
  422.  
  423.  
  424.     -- Menues
  425.     term.setCursorPos(x+1, 1)
  426.     term.write("File")
  427.     term.setCursorPos(x+7, 1)
  428.     term.write("Opc.")
  429.  
  430.  
  431.     -- Patterns
  432.     term.setTextColor(colors.black)
  433.     if (menuSongPatternScroll == 1) then
  434.         term.setBackgroundColor(colors.gray)
  435.     else
  436.         term.setBackgroundColor(colors.lightGray)
  437.     end
  438.     term.setCursorPos(x+1, 3)
  439.     term.write("   ^   ")
  440.  
  441.     local sp = menuSongPatternScroll
  442.     for y= 4, 8 do
  443.         if (sp == currentSongPattern) then
  444.                 term.setBackgroundColor(colors.pink)
  445.         else
  446.             term.setBackgroundColor(colors.black)
  447.         end
  448.         term.setCursorPos(x+1, y)
  449.         term.write("       ")
  450.  
  451.         if (sp <= songLength) then
  452.             if (sp == currentSongPattern) then
  453.                 term.setBackgroundColor(colors.pink)
  454.                 term.setTextColor(colors.black)
  455.             else
  456.                 term.setBackgroundColor(colors.black)
  457.                 term.setTextColor(colors.pink)
  458.             end
  459.  
  460.             term.setCursorPos(x+1, y)
  461.             term.write("#"..sp)
  462.  
  463.             term.setCursorPos(x+5, y)
  464.             term.write("P"..sp)
  465.  
  466.             sp = sp+1
  467.         end
  468.     end
  469.  
  470.     term.setTextColor(colors.black)
  471.     if (menuSongPatternScroll >= songLength-4) then
  472.         term.setBackgroundColor(colors.gray)
  473.     else
  474.         term.setBackgroundColor(colors.lightGray)
  475.     end
  476.     term.setCursorPos(x+1, 9)
  477.     term.write("   v   ")
  478.  
  479.      -- Instruments
  480.     term.setTextColor(colors.black)
  481.     if (menuChannelScroll == 1) then
  482.         term.setBackgroundColor(colors.gray)
  483.     else
  484.         term.setBackgroundColor(colors.lightGray)
  485.     end
  486.     term.setCursorPos(x+10, 3)
  487.     term.write("   ^   ")
  488.  
  489.     local ch = menuChannelScroll
  490.     local y = 4
  491.     for y= 4, 8 do
  492.         if (ch == currentChannel) then
  493.             term.setBackgroundColor(colors.lime)
  494.         else
  495.             term.setBackgroundColor(colors.black)
  496.         end
  497.         term.setCursorPos(x+10, y)
  498.         term.write("       ")
  499.  
  500.         term.setCursorPos(x+10, y)
  501.         if (ch == currentChannel) then
  502.             term.setBackgroundColor(colors.lime)
  503.             term.setTextColor(colors.black)
  504.         else
  505.             term.setBackgroundColor(colors.black)
  506.             term.setTextColor(colors.lime)
  507.         end
  508.  
  509.         term.write(string.sub(instruments[ch].name, 1, 7))
  510.         ch = ch+1
  511.     end
  512.  
  513.     term.setTextColor(colors.black)
  514.     if (menuChannelScroll >= instrumentCount-4) then
  515.         term.setBackgroundColor(colors.gray)
  516.     else
  517.         term.setBackgroundColor(colors.lightGray)
  518.     end
  519.     term.setCursorPos(x+10, 9)
  520.     term.write("   v   ")
  521.  
  522.     term.setTextColor(colors.black)
  523.  
  524.  
  525.     -- Play button
  526.     --term.setCursorPos(x+3, 11)
  527.     --if (playing) then
  528.     --  term.setBackgroundColor(colors.red)
  529.     --  term.write("||")
  530.     --else
  531.     --  term.setBackgroundColor(colors.lime)
  532.     --  term.write(" >")
  533.     --end
  534.  
  535.         -- Play from the beggining button
  536.     --term.setCursorPos(x+7, 11)
  537.     --if (playing) then
  538.     --  term.setBackgroundColor(colors.red)
  539.     --  term.write("[]")
  540.     --else
  541.     --  term.setBackgroundColor(colors.lime)
  542.     --  term.write("|>")
  543.     --end
  544.  
  545.     term.setCursorPos(x+2, 11)
  546.     term.setBackgroundColor(colors.brown)
  547.     term.setTextColor(colors.white)
  548.     term.write("Vel: ")
  549.     term.setBackgroundColor(colors.white)
  550.     term.setTextColor(colors.black)
  551.     term.write(" "..bpm.." ")
  552.  
  553.     term.setCursorPos(x+2, 13)
  554.     term.setBackgroundColor(colors.brown)
  555.     term.setTextColor(colors.white)
  556.     term.write("Len: ")
  557.     term.setBackgroundColor(colors.white)
  558.     term.setTextColor(colors.black)
  559.     term.write(" "..patternLength.." ")
  560.  
  561.     term.setCursorPos(x+2, 15)
  562.     term.setBackgroundColor(colors.brown)
  563.     term.setTextColor(colors.white)
  564.     term.write("Beat:")
  565.     term.setBackgroundColor(colors.white)
  566.     term.setTextColor(colors.black)
  567.     term.write(" "..beatLength.." ")
  568. end
  569.  
  570. -- Draw the entire screen
  571. local function DrawScreen()
  572.     term.setBackgroundColor(colors.black)
  573.     term.clear()
  574.     term.setTextColor(colors.white)
  575.     DrawPattern()
  576.     DrawPanel()
  577. end
  578.  
  579. -- GUI Functions
  580. -- Scroll sheet
  581. local function scrollRight() if (currentXOffset < patternLength-beatsOnScreen) then currentXOffset = currentXOffset+1
  582. DrawScreen() end end
  583. local function scrollLeft() if (currentXOffset > 0) then currentXOffset = currentXOffset-1
  584. DrawScreen() end end
  585. local function scrollUp() if (currentYOffset < 27-scrH) then currentYOffset = currentYOffset+1
  586. DrawScreen() end end
  587. local function scrollDown() if (currentYOffset > 0) then currentYOffset = currentYOffset-1
  588. DrawScreen() end end
  589. -- Scroll song patterns
  590. local function nextSongPattern() if (menuSongPatternScroll < songLength-4) then menuSongPatternScroll = menuSongPatternScroll+1
  591. DrawPanel() end end
  592. local function prevSongPattern() if (menuSongPatternScroll > 1) then menuSongPatternScroll = menuSongPatternScroll-1
  593. DrawPanel() end end
  594. -- Scroll instruments
  595. local function nextSongPattern() if (menuChannelScroll < instrumentCount-4) then menuChannelScroll = menuChannelScroll+1
  596. DrawPanel() end end
  597. local function prevSongPattern() if (menuChannelScroll > 1) then menuChannelScroll = menuChannelScroll-1
  598. DrawPanel() end end
  599.  
  600. -- Load pattern from a string, or create a blank one
  601. local function loadPattern(str)
  602.     if (str == nil) then
  603.         if (patterns[currentPattern] == nil) then
  604.             patterns[currentPattern] = {
  605.                 ["channels"] = {},
  606.                 ["length"] = patternLength,
  607.             }
  608.         end
  609.         for i=1, instrumentCount do
  610.             patterns[currentPattern].channels[i] = {
  611.                 ["notes"] = {}
  612.             }
  613.         end
  614.     end
  615. end
  616.  
  617. -- Load song from file, or create a new one
  618. local function loadSong(str)
  619.     if (str == nil) then
  620.         song = {
  621.             ["patterns"] = {1},
  622.             ["instrument"] = {
  623.                 "bass", "bassdrumbell", "bell", "chime", "flute", "guitar", "hat", "snare", "harp", "xylophone"
  624.             }
  625.         }
  626.         currentPattern = 1
  627.         loadPattern()
  628.     end
  629. end
  630.  
  631. -- Place a note at a specific x, y coordinate on the screen
  632. local function placeNote(x, y, delayed)
  633.     local time, note = pixelToMusic(x,y,delayed)
  634.    
  635.     local notes = getCurrentNotes(time)
  636.     local done = false
  637.     -- See if the note is already there, if it is, delete it
  638.     for n=1, string.len(notes) do
  639.         local character = string.sub(notes, n, n)
  640.         if (character == note) then
  641.             notes = removeCharacterAt(notes, n)      
  642.             patterns[currentPattern].channels[currentChannel].notes[time] = notes
  643.             done = true
  644.             break
  645.         end
  646.     end
  647.     -- If there is no note there, add it
  648.     if (not done) then
  649.         notes = notes..note
  650.         patterns[currentPattern].channels[currentChannel].notes[time] = notes
  651.     end
  652.    
  653.     -- Play remaining notes
  654.     playSpecificBar(time, false)
  655.    
  656.     -- Redraw sheet
  657.     DrawScreen()
  658. end
  659.  
  660. -- Start
  661. loadSong()
  662. playSoundEffect("levelup", 0.5, 3)
  663. DrawScreen()
  664.  
  665. -- Update
  666. while true do
  667.     local event, a, b, c = os.pullEvent()
  668.     if (event == "timer") then
  669.         local completed = a
  670.         if (completed == playingTimer) then
  671.             if (playing) then
  672.                 cursor = cursor+1
  673.                 if (cursor > patternLength) then
  674.                     cursor = 1
  675.                     currentXOffset = 1
  676.                 end
  677.                 if (cursor > currentXOffset+beatsOnScreen) then
  678.                     currentXOffset = math.min(currentXOffset+beatsOnScreen, patternLength-beatsOnScreen)
  679.                 end
  680.                 playCurrentBar()
  681.                 DrawScreen()
  682.                 playingTimer = os.startTimer(60/bpm)
  683.             end
  684.         elseif (completed == delayedNoteTimer) then
  685.             playCurrentBarDelayed()
  686.         end
  687.     elseif (event == "key") then
  688.         local key = a
  689.         if (key == keys.up) then
  690.             scrollUp()
  691.         elseif (key == keys.down) then
  692.             scrollDown()
  693.         elseif (key == keys.left) then
  694.             scrollLeft()
  695.         elseif (key == keys.right) then
  696.             scrollRight()
  697.         elseif (key == keys.leftShift) then
  698.             pressingShift = true
  699.         elseif (key == keys.space) then
  700.             if (playing) then
  701.                 pause()
  702.             else
  703.                 playFromCursor()
  704.             end
  705.             DrawScreen()
  706.         elseif (key == keys.enter) then
  707.             if (playing) then
  708.                 stop()
  709.             else
  710.                 playFromStart()
  711.             end
  712.             DrawScreen()
  713.         end
  714.     elseif (event == "key_up") then
  715.         local key = a
  716.         if (key == keys.leftShift) then
  717.             pressingShift = false
  718.         end
  719.     elseif (event == "mouse_scroll") then
  720.         local scrollDirection = a
  721.         if (scrollDirection == 1) then
  722.             if (not pressingShift) then scrollDown()
  723.             else scrollRight() end
  724.         else
  725.             if (not pressingShift) then scrollUp()
  726.             else scrollLeft() end
  727.         end
  728.     elseif (event == "mouse_click") then
  729.         local button = a
  730.         local x = b
  731.         local y = c
  732.         -- Note Painter
  733.         if (x > 3 and x < 4+math.min(beatsOnScreen, patternLength)) then
  734.             if (y > 2) then
  735.                 if (button == 1) then
  736.                     placeNote(x,y,false) -- Left click = normal note
  737.                 else
  738.                     placeNote(x,y,true) -- Right click = delayed note
  739.                 end
  740.             else
  741.                 local time, note = pixelToMusic(x,y)
  742.                 cursor = time
  743.                 playCurrentBar()
  744.                 DrawScreen()
  745.             end
  746.         else
  747.  
  748.             -- Panel
  749.             local panelx = beatsOnScreen+3
  750.             -- Patterns
  751.             if (x >= panelx+1 and x <= panelx+8) then
  752.                 if (y == 3) then
  753.                     prevSongPattern()
  754.                 end
  755.  
  756.                 for i=4, 8 do
  757.                     if (y == i) then
  758.                         local selectedSongPattern = i-4+menuSongPatternScroll
  759.                         if (selectedSongPattern <= songLength) then
  760.                             currentSongPattern = selectedSongPattern
  761.                             DrawScreen()
  762.                         end
  763.                         break
  764.                     end
  765.                 end
  766.  
  767.                 if (y == 9) then
  768.                     nextSongPattern()
  769.                 end
  770.             end
  771.  
  772.             -- Patterns
  773.             if (x >= panelx+10 and x <= panelx+17) then
  774.                 if (y == 3) then
  775.                     prevSongPattern()
  776.                 end
  777.  
  778.                 for i=4, 8 do
  779.                     if (y == i) then
  780.                         local selectedChannel = i-4+menuChannelScroll
  781.                         if (selectedChannel <= instrumentCount) then
  782.                             currentChannel = selectedChannel
  783.                             playNote(currentChannel, 1, 1)
  784.                             DrawScreen()
  785.                         end
  786.                         break
  787.                     end
  788.                 end
  789.  
  790.                 if (y == 9) then
  791.                     nextSongPattern()
  792.                 end
  793.             end
  794.  
  795.             if (y == 11) then -- Velocidad
  796.                 term.setCursorPos(panelx+8, 11)
  797.                 term.setBackgroundColor(colors.yellow)
  798.                 term.setTextColor(colors.black)
  799.                 term.write("     ")
  800.                 term.setCursorPos(panelx+9, 11)
  801.                 pause()
  802.                 local newbpm = tonumber(read())
  803.                 if (newbpm ~= nil) then
  804.                     bpm = math.floor(newbpm)
  805.                 end
  806.                 DrawPanel()
  807.             end
  808.  
  809.             if (y == 13) then -- PatternLength
  810.                 term.setCursorPos(panelx+8, 13)
  811.                 term.setBackgroundColor(colors.yellow)
  812.                 term.setTextColor(colors.black)
  813.                 term.write("     ")
  814.                 term.setCursorPos(panelx+9, 13)
  815.                 pause()
  816.                 local newLength = tonumber(read())
  817.                 if (newLength ~= nil) then
  818.                     patternLength = math.floor(newLength)
  819.                 end
  820.                 DrawScreen()
  821.             end
  822.  
  823.             if (y == 15) then -- Beat
  824.                 term.setCursorPos(panelx+8, 15)
  825.                 term.setBackgroundColor(colors.yellow)
  826.                 term.setTextColor(colors.black)
  827.                 term.write("     ")
  828.                 term.setCursorPos(panelx+9, 15)
  829.                 pause()
  830.                 local newBeat = tonumber(read())
  831.                 if (newBeat ~= nil) then
  832.                     beatLength = math.floor(newBeat)
  833.                 end
  834.                 DrawScreen()
  835.             end
  836.         end
  837.     end
  838. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement