Advertisement
Guest User

radio

a guest
May 28th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. os.startTimer(5)
  2. f = fs.open("setup","r")
  3. setup = textutils.unserialize(f.readAll())
  4. os.loadAPI("play")
  5. local radios = {}
  6. local function isRadio(msg)
  7.   if type(msg) ~= "table" then
  8.     return false
  9.   end
  10.   if msg.sType ~= "radio" then
  11.     return false
  12.   end
  13.   return true
  14. end
  15.  
  16. print("Scanning for online radio stations...")
  17. while true do
  18.   local tEvent = {os.pullEvent()}
  19.   if isRadio(tEvent[3]) then
  20.     radios[tEvent[3].sChannel] = tEvent[3].sChannel
  21.   end
  22.   if tEvent[1] == "timer" then
  23.     break
  24.   end
  25. end
  26. i = 0
  27. for k,v in pairs(radios) do
  28.   i = i + 1
  29.   print(i..": "..v)
  30. end
  31. write("Connect to: ")
  32. local chan = read()
  33.  
  34. function updateTime()
  35.   if cont.cur ~= ocur then
  36.     ges = cont.all
  37.     cur = cont.cur
  38.     allm = math.floor(ges/60)
  39.     alls = ges%60
  40.     curm = math.floor(cur/60)
  41.     curs = cur%60
  42.     text = string.format("%2i:%2i",curm,curs):gsub(" ","0").." / "..string.format("%2i:%2i",allm,alls):gsub(" ","0").." - "..cont.name
  43.     x,y = term.getCursorPos()
  44.     term.setCursorPos(1,y)
  45.     term.clearLine()
  46.     term.write(text)
  47.     ocur = cur
  48.   end
  49. end
  50. ocur = -1
  51. block = peripheral.find("iron_note")
  52. while true do
  53.   e = {os.pullEvent("bitnet_message")}
  54.   msg = e[3]
  55.   if msg.sType == "radio" and msg.sChannel == chan then
  56.     cont = msg.tMessage
  57.     updateTime()
  58.     for i,v in pairs(cont.notes) do
  59.       if v and v.pitch then
  60.         if (v.inst == 0 or v.inst == 2 or v.inst == 3) then
  61.           play.play(1,1,v.inst,v.pitch,setup, true)
  62.         elseif (v.inst == 1) then
  63.           play.play(1,3,v.inst,v.pitch,setup, true)
  64.         elseif (v.inst == 4) then
  65.           play.play(1,7,v.inst,v.pitch,setup, true)
  66.         else
  67.           play.play(1,5,v.inst,v.pitch,setup, true)
  68.         end
  69.       end
  70.       --pcall(function() block.playNote(v.inst,v.pitch) end)
  71.     end
  72.   end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement