Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Musicator MAIN
- -- Created with <3 by FoxWorn3365
- -- Initial values
- notes = {}
- clickAssociation = {}
- lineAssociation = {"skip", "skip", "skip", "do_max", "si", "la", "sol", "fa", "mi", "re", "do_min"}
- lineAssociationInverse = {["do_max"] = 10, ["si"] = 11, ["la"] = 12, ["sol"] = 13, ["fa"] = 14, ["mi"] = 15, ["re"] = 16, ["do_min"] = 17}
- rowLineAssociation = {}
- projectAssociation = {}
- frequencyManager = {}
- placedNotes = {}
- relativeNotes = {}
- checkForDelete = {["old"] = "main", ["counter"] = 0}
- page = "home"
- project = ""
- current = "main"
- currentNotes = ""
- termx, termy = term.getSize()
- notes["do_min"] = "261.64"
- notes["do_min_long"] = "261.64"
- notes["re"] = "293.68"
- notes["re_long"] = "293.68"
- notes["mi"] = "329.64"
- notes["mi_long"] = "329.64"
- notes["fa"] = "349.24"
- notes["fa_long"] = "349.24"
- notes["sol"] = "392.00"
- notes["sol_long"] = "392.00"
- notes["la"] = "440.00"
- notes["la_long"] = "440.00"
- notes["si"] = "493.92"
- notes["si_long"] = "493.92"
- notes["do_max"] = "523.28"
- notes["do_max_long"] = "523.28"
- -- Functions
- function reset()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- function drawHead(text)
- term.setTextColor(colors.white)
- paintutils.drawLine(1, 1, termx, 1, colors.lightBlue)
- local position = (termx - #text)/2
- term.setCursorPos(position, 1)
- print(text)
- normalize()
- end
- function notesMenu(ll)
- term.setTextColor(colors.white)
- local isColored = false
- local used = 5
- paintutils.drawLine(1, ll, termx, ll, colors.lightGray)
- for i,v in pairs(notes) do
- if isColored == false then
- term.setBackgroundColor(colors.lightGray)
- isColored = true
- else
- term.setBackgroundColor(colors.gray)
- isColored = false
- end
- term.setCursorPos(used, ll)
- term.write(" "..i.." ")
- local endused = used + (#i + 2)
- for i=used, used+(#i + 2) do
- clickAssociation[i] = i
- end
- used = endused
- end
- end
- function normalize()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- end
- function makeLines()
- normalize()
- local line = 7
- local colorized = true
- for i=0, 11 do
- local noteNumberArray = line-6
- if colorized == false then
- colorized = true
- rowLineAssociation[line] = lineAssociation[noteNumberArray]
- else
- paintutils.drawLine(0, line, termx, line, colors.lightGray)
- rowLineAssociation[line] = lineAssociation[noteNumberArray]
- colorized = false
- end
- line = line+1
- end
- paintutils.drawPixel(1, 13, colors.red)
- normalize()
- end
- function loadPlayer(project)
- for i,file in pairs(fs.list(project)) do
- loadNotes(project, file)
- frequencyManager[file] = i
- relativeNotes[file] = placedNotes
- end
- return fs.list(project)
- end
- function drawProjectMenu(ll, project)
- paintutils.drawLine(1, ll, termx, ll, colors.orange)
- term.setTextColor(colors.white)
- term.setCursorPos(2, ll)
- term.write("Project: "..project..".music")
- term.setCursorPos(46, ll)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.white)
- term.write(" PLAY ")
- normalize()
- end
- function createProjectPart(project, part)
- local file = fs.open(project.."/"..part, "w")
- file.write("{}")
- file.flush()
- file.close()
- end
- function drawSheduleMenuFromProject(ll, project, current)
- local start = 1
- paintutils.drawLine(1, ll, termx, ll, colors.blue)
- if fs.isDir(project) == false then
- shell.run("mkdir "..project)
- local file = fs.open(project.."/main", "w")
- file.write("{}")
- file.flush()
- file.close()
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.white)
- term.setCursorPos(start, ll)
- term.write(" main ")
- else
- local used = 1
- for index,value in pairs(fs.list(project)) do
- if value == current then
- term.setBackgroundColor(colors.lightBlue)
- else
- term.setBackgroundColor(colors.blue)
- end
- term.setTextColor(colors.white)
- term.setCursorPos(used, ll)
- term.write(" "..value.." ")
- local newused = used + (#value+2)
- for i=used, newused-1 do
- projectAssociation[i] = value
- end
- used = used + (#value+2)
- end
- end
- local interactionx, interactiony = term.getCursorPos()
- projectAssociation[interactionx] = "new"
- projectAssociation[interactionx+1] = "new"
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.blue)
- print(" +")
- term.setCursorPos(46, ll)
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- term.write(" SAVE ")
- normalize()
- end
- function loadNotes(project, current)
- currentNotes = current
- local file = fs.open(project.."/"..current, "r")
- local lsn = file.readAll()
- file.close()
- placedNotes = textutils.unserialize(lsn)
- if placedNotes == nil or placedNotes == " " then
- placedNotes = {}
- end
- end
- function drawNotes(project, current)
- if currentNotes ~= current then
- currentNotes = current
- loadNotes(project, current)
- end
- for row,note in pairs(placedNotes) do
- local clearnote = string.gsub(note, "_long", "")
- if lineAssociationInverse[clearnote] ~= nil then
- if string.find(note, "long") then
- paintutils.drawPixel(row, lineAssociationInverse[clearnote], colors.green)
- else
- paintutils.drawPixel(row, lineAssociationInverse[clearnote], colors.black)
- end
- end
- end
- end
- -- Visualizer
- while true do
- if page == "home" then
- reset()
- drawHead("Musicator v1.0 - Make your Music!")
- print("\n\nBenvenuto su Musicator, per iniziare inserisci il nome di un progetto gia' esistente oppure di uno nuovo!")
- project = read()
- page = "editor"
- elseif page == "editor" then
- reset()
- drawHead("Musicator v1.0 - Make your Music!")
- drawProjectMenu(2, project)
- drawSheduleMenuFromProject(3, project, current)
- notesMenu(4)
- makeLines()
- drawNotes(project, current)
- normalize()
- local event, button, x, y = os.pullEvent("mouse_click")
- if x > 0 then
- if y == 3 then
- if x > 42 then
- if button ~= 3 then
- local file = fs.open(project.."/"..current, "w")
- file.write(textutils.serialize(placedNotes))
- file.flush()
- file.close()
- else
- local file = fs.open(project.."/"..current, "w")
- file.write(" ")
- file.flush()
- file.close()
- end
- elseif projectAssociation[x] ~= nil then
- if projectAssociation[x] == "new" then
- paintutils.drawLine(1, 3, termx, 3, colors.lightBlue)
- term.setCursorPos(2, 3)
- term.write("New project part: ")
- term.setCursorPos(20, 3)
- local part = read()
- createProjectPart(project, part)
- current = part
- sleep(0.35)
- else
- if checkForDelete["old"] == projectAssociation[x] and checkForDelete["count"] < 3 then
- checkForDelete["count"] = checkForDelete["count"] + 1
- current = projectAssociation[x]
- elseif checkForDelete["old"] == projectAssociation[x] and checkForDelete["count"] >= 3 and projectAssociation[x] ~= "main" then
- shell.run("rm "..project.."/"..projectAssociation[x])
- elseif checkForDelete["old"] ~= projectAssociation[x] then
- checkForDelete["old"] = projectAssociation[x]
- checkForDelete["count"] = 1
- current = projectAssociation[x]
- else
- exit()
- end
- end
- end
- elseif y == 2 and x > 45 then
- page = "player"
- elseif y > 6 then
- if placedNotes[x] == nil then
- if button == 1 then
- placedNotes[x] = rowLineAssociation[y]
- paintutils.drawPixel(x, y, colors.black)
- elseif button == 2 then
- placedNotes[x] = rowLineAssociation[y].."_long"
- paintutils.drawPixel(x, y, colors.green)
- end
- else
- placedNotes[x] = nil
- paintutils.drawPixel(x, y, colors.white)
- end
- end
- end
- elseif page == "player" then
- reset()
- drawHead("Musicator v1.0 - Player")
- speaker = peripheral.wrap("right")
- print("\n\nPer avviare il player premi un tasto qualsiasi\n")
- os.pullEvent()
- tracks = loadPlayer(project)
- term.setCursorPos(7, 7)
- for i=1, 51, 1 do
- ntp = i
- term.setCursorPos(10, 10)
- local longNote = false
- local nextEmpty = false
- print("Playing note number "..i)
- for fi,file in pairs(tracks) do
- if relativeNotes[file][ntp] ~= nil then
- term.setCursorPos(15, 13)
- if relativeNotes[file][ntp+1] == nil then
- nextEmpty = true
- end
- local note = notes[relativeNotes[file][ntp]]
- if string.find(relativeNotes[file][ntp], "long") then
- longNote = true
- end
- print("Playing note: "..note)
- speaker.start(tonumber(frequencyManager[file]), tonumber(note))
- end
- end
- if longNote then
- sleep(0.85)
- else
- sleep(0.4)
- end
- if nextEmpty then
- speaker.shutdown()
- end
- sleep(0.01)
- end
- else
- page = "home"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement