Advertisement
Guest User

music_server.lua

a guest
Apr 17th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.50 KB | None | 0 0
  1. -- Music array player.
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local note = require("note")
  6. local computer = require("computer")
  7. local term = require("term")
  8.  
  9. local exit = false
  10.  
  11. local net = component.modem
  12. local gfx = component.gpu
  13. local screen = gfx.getScreen()
  14. local mx, my = gfx.getResolution()
  15.  
  16. net.open(36969)
  17.  
  18. local c_addr = {} -- Address list
  19. local c_list = {} -- Numbered list
  20. -- This prevents the need to seek later.
  21.  
  22. local highlight = manual --What is highlighted by the keyboard cursor.
  23.  
  24. local addressExists = function(address)
  25.   if c_addr[address] == nil then
  26.     return false
  27.   else
  28.     return true
  29.   end
  30. end
  31.  
  32. local hostNote = function(dead, me, client, port, dist, v1, v2, v3, v4)
  33.   --print(dead)
  34.   --print(me)
  35.   --print(client)
  36.   --print(tostring(dist))
  37.   --print(tostring(port))
  38.   --print(v1)
  39.   if port == 36969 then
  40.     if v1 == "musicclient" then --Register process. Check for dupes.
  41.       if addressExists(client) == false then
  42.         c_addr[client] = false -- Add them to playable list as idle.
  43.         table.insert(c_list, client)
  44.         net.send(client, 36969, "musicregister")
  45.         term.clear()
  46.         print("Client " .. client .. " connected.")
  47.         computer.beep(433, 0.5)
  48.         print(" ")
  49.         print("Connected clients: " .. tostring(#c_list))
  50.         for a=1,#c_list do
  51.           print(tostring(a) .. ": " .. c_list[a])
  52.         end
  53.       end
  54.     end
  55.     if addressExists(client) then --Run only for my clients.
  56.       if v1 == "music" then -- Handle status updates from clients.
  57.         if v2 == "idle" then
  58.           c_addr[client] = false
  59.         elseif v2 == "playing" then
  60.           c_addr[client] = true
  61.         end
  62.       end
  63.     end
  64.   end
  65. end
  66.  
  67. local selectClient = function() -- Pick the next available client.
  68.   if #c_list == 0 then return end
  69.   for c=1,#c_list do
  70.     if c_addr[c_tlist[c]] then
  71.       return c_list[c]
  72.     end
  73.   end
  74.   return -- Couldn't find an available client to play a note on.
  75. end
  76.  
  77. local playNetworkNote = function(freq, dur)
  78.   local client = selectClient()
  79.   if client == nil then
  80.     -- Put error here. Probably some ominous message or bad-sounding noise.
  81.   else
  82.     net.send(client, 36969, "music", "play", freq, dur)
  83.   end
  84. end
  85.  
  86. --Text and messages.
  87.  
  88. --User can pick the tone display.
  89. local tones = {
  90.   [0] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"},
  91.   [1] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"},
  92.   [2] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b"},
  93.   [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"}
  94. }
  95.  
  96. --Menu Definitions
  97.  
  98. --Generic changeable menus.
  99. local options = {
  100.   manual = {
  101.     val = 1,
  102.     names = {"Manual", "Automatic"},
  103.     x1 = 2,
  104.     y1 = 2,
  105.     x2 = 10,
  106.     y2 = 2
  107.   },
  108.   sample = {val = true, names = {"Sample"}, x1 = 12, y1 = 2, x2 = 18, y2 = 2},
  109.   frequency = {val = false, names = {"Frequency"}, x1 = 20, y1 = 2, x2 = 28, y2 = 2},
  110.   sharp = {val = 1, names = {"Sharp", "Flat"}, x1 = 30, y1 = 2, x2 = 34, y2 = 2},
  111.   quit = {
  112.     func = function() --Exit the application.
  113.       print("d")
  114.       event.ignore("touch", screenClick)
  115.       gfx.fill(0,0,mx,my," ")
  116.       exit = true
  117.     end,
  118.     names = {"Quit"}, x1 = mx-5, y1 = 2, x2 = mx-1, y2 = 2
  119.   },
  120.   save = {
  121.     func = function(filename) --Save your work.
  122.       --Stuff to be done here.
  123.     end,
  124.     names = {"Save"}, x1 = mx-10, y1 = 2, x2 = mx-7, y2 = 2
  125.   },
  126.   load = {
  127.     func = function(filename) --Load something.
  128.       --Stuff to be done here.
  129.     end,
  130.     names = {"Load"}, x1 = mx-15, y1 = 2, x2 = mx-12, y2 = 2
  131.   }
  132. }
  133.  
  134. --Controls
  135.  
  136. local clickableButtons = {
  137.   "quit", "save", "load", "manual", "sample", "frequency", "sharp"
  138. }
  139.  
  140. local screenClick = function(touch, addr, x, y, button, user)
  141.   for n=1,#clickableButtons do
  142.     local tap = options[clickableButtons[n]]
  143.     if x >= tap.x1
  144.     and x <= tap.x2
  145.     and y >= tap.y1
  146.     and y <= tap.y2 then
  147.       print("a")
  148.       if tap.func ~= nil then
  149.         print("b")
  150.         tap.func() --Run the clicked button's function.
  151.       end
  152.       if type(tap.val) == boolean then --Switch the menu variable when necessary.
  153.         if tap.val then
  154.           tap.val = false
  155.         else
  156.           tap.val = true
  157.         end
  158.       elseif type(tap.val) == number then
  159.         if tap.val == opt.max then
  160.           tap.val = 0
  161.         else
  162.           tap.val = tap.val + 1
  163.         end
  164.       end
  165.     end
  166.   end
  167. end
  168.  
  169. --Graphics
  170.  
  171. local display = {}
  172.  
  173. display.buttonTheme = function(active, highlight)
  174.   if active == nil then
  175.     gfx.setForeground(0xFFFFFF) --Named toggle.
  176.   elseif active == true then --On
  177.     gfx.setForeground(0x00FF00)
  178.   else --Off
  179.     gfx.setForeground(0xFF0000)
  180.   end
  181.   if highlight then
  182.     gfx.setBackground(0x0000FF)
  183.   else
  184.     gfx.setBackground(0x000000)
  185.   end
  186. end
  187.  
  188. display.isHighlight = function(label)
  189.   if highlight == label then
  190.     return true
  191.   else
  192.     return false
  193.   end
  194. end
  195.  
  196. --Set up the GUI
  197. gfx.fill(0, 0, mx, my, " ") -- Clear screen to begin graphical display.
  198.  
  199. local cross = {
  200.   {1,1}, {mx,1}, {1,my}, {mx,my}, {1,3}, {mx,3}, {1,7}, {mx,7},
  201.   {11,1}, {11,3}, {19,1}, {19,3}, {29,1}, {29,3}, {35,1}, {35,3}
  202. }
  203. local pipe = {
  204.   {11,2}, {19,2}, {29,2}, {35,2}
  205. }
  206. display.border = function() -- Draw border pipes
  207.   gfx.setForeground(0xFFFFFF)
  208.   gfx.setBackground(0x000000)
  209.   gfx.fill(1, 1, mx, 1, "-")
  210.   gfx.fill(1, 3, mx, 1, "-")
  211.   gfx.fill(1, 7, mx, 1, "-")
  212.   gfx.fill(1, my, mx, 1, "-")
  213.   gfx.fill(1, 1, 1, my, "|")
  214.   gfx.fill(mx, 1, 1, my, "|")
  215.   for r=1,#cross do
  216.     gfx.set(cross[r][1], cross[r][2], "+")
  217.   end
  218.   for s=1,#pipe do
  219.     gfx.set(pipe[s][1], pipe[s][2], "|")
  220.   end
  221. end
  222.  
  223. display.menuItem = function(name)
  224.   local enabled = nil
  225.   local opt = options[name]
  226.   local title = opt.names[1]
  227.   if #opt.names > 1 then --On/Off toggle
  228.     title = opt.names[opt.val]
  229.   else
  230.     enabled = opt.val
  231.   end
  232.   print("")
  233.   print("")
  234.   display.buttonTheme(enabled, false)
  235.   gfx.set(opt.x1, opt.y1, title)
  236. end
  237.  
  238. display.multiMenu = function(...)
  239.   local g = {...}
  240.   for h=1,#g do
  241.     display.menuItem(g[h])
  242.   end
  243. end
  244.  
  245. display.topMenu = function()
  246.   display.multiMenu("quit", "save", "load")
  247. end
  248.  
  249. display.optionMenu = function() --Change a visual toggle for a menu item.
  250.   display.multiMenu("manual", "sample", "frequency", "sharp")
  251. end
  252.  
  253. --display.noteMenu = function(note, octave) --Can change the displayed note or octave.
  254. --  
  255. --end
  256.  
  257. --First update
  258. display.border()
  259. display.optionMenu()
  260. display.topMenu()
  261.  
  262. --local musicserver = event.listen("modem_message", hostNote)
  263. local clicker = event.listen("touch", screenClick)
  264.  
  265. while exit == false do
  266.   os.sleep(0.25)
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement