Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local patternLength = 16
- local beatLength = 4
- local sp = peripheral.find("speaker")
- local function pitch(note)
- return math.pow(2, (note-12)/12)
- end
- instruments = {
- -- musical
- ["bass"] = "block.note.bass",
- ["bassdrum"] = "block.note.bassdrum",
- ["bell"] = "block.note.bell",
- ["chime"] = "block.note.chime",
- ["flute"] = "block.note.flute",
- ["guitar"] = "block.note.guitar",
- ["hat"] = "block.note.hat",
- ["snare"] = "block.note.snare",
- ["harp"] = "block.note.harp",
- ["xylophone"] = "block.noteblock.xylophone",
- -- sfx
- ["levelup"] ="entity.player.levelup"
- }
- local function playNote(inst, vol, note)
- sp.playSound(instruments[inst], vol, note)
- end
- local notes = {
- "F#", "G ", "G#", "A ", "A#", "B ",
- "C ", "C#", "D ", "D#", "E ", "F "
- }
- local song = {}
- local patterns = {}
- local instruments = {}
- local currentPattern = 0
- local currentPatternData = {}
- local currentChannel = 0
- local currentYOffset = 0
- local currentXOffset = 0
- local pressingShift = false
- local beatsOnScreen = math.floor(36/beatLength)
- local scrW, scrH = term.getSize()
- local function addCharAt(str, char, pos)
- local prev = string.sub(str, 1, pos)
- local post = string.sub(str, pos+1, string.len(str))
- return prev..char..post
- end
- local function replaceCharAt(str, char, pos)
- local prev = string.sub(str, 1, pos)
- local post = string.sub(str, pos+2, string.len(str))
- return prev..char..post
- end
- local function DrawPattern()
- local channel = currentChannel
- local pattern = currentPattern
- local xoffset = currentXOffset
- local yoffset = currentYOffset
- for _y=1, scrH-1 do
- local y = scrH-_y
- local octave = math.floor((_y+yoffset-7)/12)+1
- local n = (_y+yoffset)%12
- local c = xoffset
- term.setCursorPos(1,y+1)
- term.setBackgroundColor(colors.black)
- if (n==1 or n==3 or n==5 or n==8 or n==10) then
- term.setTextColor(colors.blue)
- else
- term.setTextColor(colors.cyan)
- end
- term.write(notes[(n-1)%12+1]..tostring(octave))
- if (n==1 or n==3 or n==5 or n==8 or n==10) then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.lightGray)
- end
- for c=currentXOffset, math.min(patternLength-1, currentXOffset+beatsOnScreen-1) do
- if (c%4 == 0) then
- term.write("|")
- else
- term.write(",")
- end
- term.write(string.rep("_",beatLength-1))
- end
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.write(" ")
- local i = 1
- for c=xoffset, math.min(xoffset+beatsOnScreen-1, patternLength-1) do
- term.setCursorPos(3+i, 1)
- term.write(tonumber(c+1))
- term.write(" ")
- i = i+beatLength
- end
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.black)
- for c=xoffset*beatLength+1, math.min(xoffset+beatsOnScreen-1, patternLength-1)*beatLength+beatLength do
- local n = currentPatternData[c]
- if (n ~= ".") then
- local x, y = musicToPixel(c, n)
- term.setCursorPos(x, y)
- term.write(" ")
- end
- end
- end
- local function DrawScreen()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- DrawPattern()
- end
- local function scrollRight()
- if (currentXOffset < patternLength-beatsOnScreen) then
- currentXOffset = currentXOffset+1
- DrawPattern()
- end
- end
- local function scrollLeft()
- if (currentXOffset > 0) then
- currentXOffset = currentXOffset-1
- DrawPattern()
- end
- end
- local function scrollUp()
- if (currentYOffset < 26-scrH) then
- currentYOffset = currentYOffset+1
- DrawPattern()
- end
- end
- local function scrollDown()
- if (currentYOffset > 0) then
- currentYOffset = currentYOffset-1
- DrawPattern()
- end
- end
- local function loadPattern(str)
- if (str == nil) then
- for i=1, patternLength*beatLength do
- currentPatternData[i] = "."
- end
- end
- end
- function pixelToMusic(x, y)
- local time = x-3+currentXOffset*beatLength
- local note = scrH-y+currentYOffset
- return time, note
- end
- function musicToPixel(time, note)
- local x = time+3-currentXOffset*beatLength
- local y = -note+currentYOffset+scrH
- return x, y
- end
- local function characterToNote(c, chord)
- if (chord) then
- return string.byte(c)-65
- else
- return string.byte(c)-97
- end
- end
- local function noteToCharacter(n, chord)
- if (chord) then
- return string.char(n+65) --Mayuscula
- else
- return string.char(n+97) --minuscula
- end
- end
- local function placeNote(x, y)
- local time, note = pixelToMusic(x,y)
- playNote("harp", 1, pitch(note))
- currentPatternData[time] = note
- DrawPattern()
- end
- loadPattern()
- playNote("levelup", 0.5, 3)
- DrawScreen()
- while true do
- local event, a, b, c = os.pullEvent()
- if (event == "key") then
- local key = a
- if (key == keys.up) then
- scrollUp()
- elseif (key == keys.down) then
- scrollDown()
- elseif (key == keys.left) then
- scrollLeft()
- elseif (key == keys.right) then
- scrollRight()
- elseif (key == keys.leftShift) then
- pressingShift = true
- end
- end
- if (event == "key_up") then
- local key = a
- if (key == keys.leftShift) then
- pressingShift = false
- end
- end
- if (event == "mouse_scroll") then
- local scrollDirection = a
- if (scrollDirection == 1) then
- if (not pressingShift) then
- scrollDown()
- else
- scrollRight()
- end
- else
- if (not pressingShift) then
- scrollUp()
- else
- scrollLeft()
- end
- end
- end
- if (event == "mouse_click") then
- local button = a
- local x = b
- local y = c
- if (x > 3 and x < 4+4*math.min(9, patternLength) and y > 1) then
- placeNote(x,y)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement