Advertisement
GravityCube

RadioCasera

Mar 8th, 2018
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. os.loadAPI("gcapi")
  2.  
  3. existsMonitor = false
  4. if peripheral.find("monitor") then
  5.     os.loadAPI("nMon")
  6.     existsMonitor = true
  7. end
  8.  
  9. --peripheral.find() in Tekkit
  10. function findPeripheral(pType)
  11.     pList = {}
  12.     for _,pName in pairs(peripheral.getNames()) do
  13.         if peripheral.getType(pName) == pType then
  14.             table.insert(pList, peripheral.wrap(pName))
  15.         end
  16.     end
  17.     return unpack(pList)
  18. end
  19. peripheral.find = findPeripheral
  20.  
  21. --Split string
  22. function split(self, delimiter)
  23.     result = {};
  24.     for match in (self..delimiter):gmatch("(.-)"..delimiter) do
  25.         table.insert(result, match);
  26.     end
  27.     return result;
  28. end
  29. string.split = split
  30. --------------------------------------------------------
  31. -->                       MUSIC                      <--
  32. --------------------------------------------------------
  33. local pitchs = {[0]=0, [1]=0.53, [2]=0.56, [3]=0.6, [4]=0.63, [5]=0.67, [6]=0.7, [7]=0.75,
  34. [8]=0.8,[9]=0.85,[10]=0.9,[11]=0.95,[12]=1,[13]=1.05,[14]=1.1,[15]=1.2,[16]=1.25,[17]=1.32,[18]=1.4,
  35. [19]=1.5,[20]=1.6,[21]=1.7,[22]=1.8,[23]=1.9,[24]=2}
  36.  
  37. local instruments = {[1]="note.harp",[2]="note.bassattack",[3]="note.bd",[4]="note.snare",[5]="note.hat"}
  38.  
  39. local song = nil
  40.  
  41. local noteblocks = {peripheral.find("music")}
  42.  
  43. function playNote(instrument, pitch, vol)
  44.     if instrument and pitch and vol then
  45.         for _,noteblock in pairs(noteblocks) do
  46.             noteblock.playSound(instrument, pitch, vol)
  47.         end
  48.     end
  49. end
  50.  
  51. --------------------------------------------------------
  52. -->                  Main programs                   <--
  53. --------------------------------------------------------
  54.  
  55. function startSong()
  56.     local delay = song["delay"]
  57.     for i,tick in ipairs(song) do
  58.         if existsMonitor then
  59.             nMon.updateMonitor(i)
  60.         end
  61.         for instrumentID, notePitchs in pairs(tick) do
  62.             for _,pitchID in pairs(notePitchs) do
  63.                 playNote(instruments[instrumentID], pitchs[pitchID], 0.7)
  64.             end
  65.         end
  66.         sleep(delay)
  67.     end
  68. end
  69.  
  70. function clearTerminal()
  71.     term.setCursorPos(1,1)
  72.     term.clear()
  73. end
  74.  
  75. if existsMonitor then
  76.     nMon.setupMonitor()
  77. end
  78.  
  79. userInput = ''
  80. while true do
  81.     clearTerminal()
  82.    
  83.     print("= List of songs =")
  84.     print(" - portal")
  85.     print(" - nyancat")
  86.     print(" - zelda")
  87.     print(" - sas")
  88.     print(" - tetris")
  89.     print(" - banjo")
  90.     print(" - evilmorty")
  91.     print(" - jinglebells")
  92.    
  93.     write("Song name: ")
  94.     if not arepeat then
  95.         userInput = read()
  96.     end
  97.    
  98.     listC = gcapi.split((userInput .. ' '), ' ')
  99.    
  100.     if table.contains(listC , '-r' ) then
  101.         arepeat = true
  102.     end
  103.    
  104.     if userInput then
  105.         filePath = "/songs/" .. string.split(userInput .. " ", " ")[1]
  106.         print()
  107.        
  108.         if filePath and fs.exists(filePath) then
  109.             file = fs.open(filePath, "r")
  110.             song = textutils.unserialize(file.readAll())
  111.             file.close()
  112.             if existsMonitor then
  113.                 nMon.setSong(song)
  114.             end
  115.             startSong()
  116.         else
  117.             print("Song not found")
  118.             sleep(2)
  119.         end
  120.     end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement