Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Music array player.
- local component = require("component")
- local event = require("event")
- local note = require("note")
- local computer = require("computer")
- local term = require("term")
- local exit = false
- local net = component.modem
- local gfx = component.gpu
- local screen = gfx.getScreen()
- local mx, my = gfx.getResolution()
- net.open(36969)
- local c_addr = {} -- Address list
- local c_list = {} -- Numbered list
- -- This prevents the need to seek later.
- local highlight = manual --What is highlighted by the keyboard cursor.
- local addressExists = function(address)
- if c_addr[address] == nil then
- return false
- else
- return true
- end
- end
- local hostNote = function(dead, me, client, port, dist, v1, v2, v3, v4)
- --print(dead)
- --print(me)
- --print(client)
- --print(tostring(dist))
- --print(tostring(port))
- --print(v1)
- if port == 36969 then
- if v1 == "musicclient" then --Register process. Check for dupes.
- if addressExists(client) == false then
- c_addr[client] = false -- Add them to playable list as idle.
- table.insert(c_list, client)
- net.send(client, 36969, "musicregister")
- term.clear()
- print("Client " .. client .. " connected.")
- computer.beep(433, 0.5)
- print(" ")
- print("Connected clients: " .. tostring(#c_list))
- for a=1,#c_list do
- print(tostring(a) .. ": " .. c_list[a])
- end
- end
- end
- if addressExists(client) then --Run only for my clients.
- if v1 == "music" then -- Handle status updates from clients.
- if v2 == "idle" then
- c_addr[client] = false
- elseif v2 == "playing" then
- c_addr[client] = true
- end
- end
- end
- end
- end
- local selectClient = function() -- Pick the next available client.
- if #c_list == 0 then return end
- for c=1,#c_list do
- if c_addr[c_tlist[c]] then
- return c_list[c]
- end
- end
- return -- Couldn't find an available client to play a note on.
- end
- local playNetworkNote = function(freq, dur)
- local client = selectClient()
- if client == nil then
- -- Put error here. Probably some ominous message or bad-sounding noise.
- else
- net.send(client, 36969, "music", "play", freq, dur)
- end
- end
- --Text and messages.
- --User can pick the tone display.
- local tones = {
- [0] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"},
- [1] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"},
- [2] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b"},
- [3] = {"1.000", "1.060", "1.125", "1.189", "1.250", "1.333", "1.414", "1.500", "1.587", "1.666", "1.782", "1.778"}
- }
- --Menu Definitions
- --Generic changeable menus.
- local options = {
- manual = {
- val = 1,
- names = {"Manual", "Automatic"},
- x1 = 2,
- y1 = 2,
- x2 = 10,
- y2 = 2
- },
- sample = {val = true, names = {"Sample"}, x1 = 12, y1 = 2, x2 = 18, y2 = 2},
- frequency = {val = false, names = {"Frequency"}, x1 = 20, y1 = 2, x2 = 28, y2 = 2},
- sharp = {val = 1, names = {"Sharp", "Flat"}, x1 = 30, y1 = 2, x2 = 34, y2 = 2},
- quit = {
- func = function() --Exit the application.
- print("d")
- event.ignore("touch", screenClick)
- gfx.fill(0,0,mx,my," ")
- exit = true
- end,
- names = {"Quit"}, x1 = mx-5, y1 = 2, x2 = mx-1, y2 = 2
- },
- save = {
- func = function(filename) --Save your work.
- --Stuff to be done here.
- end,
- names = {"Save"}, x1 = mx-10, y1 = 2, x2 = mx-7, y2 = 2
- },
- load = {
- func = function(filename) --Load something.
- --Stuff to be done here.
- end,
- names = {"Load"}, x1 = mx-15, y1 = 2, x2 = mx-12, y2 = 2
- }
- }
- --Controls
- local clickableButtons = {
- "quit", "save", "load", "manual", "sample", "frequency", "sharp"
- }
- local screenClick = function(touch, addr, x, y, button, user)
- for n=1,#clickableButtons do
- local tap = options[clickableButtons[n]]
- if x >= tap.x1
- and x <= tap.x2
- and y >= tap.y1
- and y <= tap.y2 then
- print("a")
- if tap.func ~= nil then
- print("b")
- tap.func() --Run the clicked button's function.
- end
- if type(tap.val) == boolean then --Switch the menu variable when necessary.
- if tap.val then
- tap.val = false
- else
- tap.val = true
- end
- elseif type(tap.val) == number then
- if tap.val == opt.max then
- tap.val = 0
- else
- tap.val = tap.val + 1
- end
- end
- end
- end
- end
- --Graphics
- local display = {}
- display.buttonTheme = function(active, highlight)
- if active == nil then
- gfx.setForeground(0xFFFFFF) --Named toggle.
- elseif active == true then --On
- gfx.setForeground(0x00FF00)
- else --Off
- gfx.setForeground(0xFF0000)
- end
- if highlight then
- gfx.setBackground(0x0000FF)
- else
- gfx.setBackground(0x000000)
- end
- end
- display.isHighlight = function(label)
- if highlight == label then
- return true
- else
- return false
- end
- end
- --Set up the GUI
- gfx.fill(0, 0, mx, my, " ") -- Clear screen to begin graphical display.
- local cross = {
- {1,1}, {mx,1}, {1,my}, {mx,my}, {1,3}, {mx,3}, {1,7}, {mx,7},
- {11,1}, {11,3}, {19,1}, {19,3}, {29,1}, {29,3}, {35,1}, {35,3}
- }
- local pipe = {
- {11,2}, {19,2}, {29,2}, {35,2}
- }
- display.border = function() -- Draw border pipes
- gfx.setForeground(0xFFFFFF)
- gfx.setBackground(0x000000)
- gfx.fill(1, 1, mx, 1, "-")
- gfx.fill(1, 3, mx, 1, "-")
- gfx.fill(1, 7, mx, 1, "-")
- gfx.fill(1, my, mx, 1, "-")
- gfx.fill(1, 1, 1, my, "|")
- gfx.fill(mx, 1, 1, my, "|")
- for r=1,#cross do
- gfx.set(cross[r][1], cross[r][2], "+")
- end
- for s=1,#pipe do
- gfx.set(pipe[s][1], pipe[s][2], "|")
- end
- end
- display.menuItem = function(name)
- local enabled = nil
- local opt = options[name]
- local title = opt.names[1]
- if #opt.names > 1 then --On/Off toggle
- title = opt.names[opt.val]
- else
- enabled = opt.val
- end
- print("")
- print("")
- display.buttonTheme(enabled, false)
- gfx.set(opt.x1, opt.y1, title)
- end
- display.multiMenu = function(...)
- local g = {...}
- for h=1,#g do
- display.menuItem(g[h])
- end
- end
- display.topMenu = function()
- display.multiMenu("quit", "save", "load")
- end
- display.optionMenu = function() --Change a visual toggle for a menu item.
- display.multiMenu("manual", "sample", "frequency", "sharp")
- end
- --display.noteMenu = function(note, octave) --Can change the displayed note or octave.
- --
- --end
- --First update
- display.border()
- display.optionMenu()
- display.topMenu()
- --local musicserver = event.listen("modem_message", hostNote)
- local clicker = event.listen("touch", screenClick)
- while exit == false do
- os.sleep(0.25)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement