Advertisement
Guest User

musicv2

a guest
Apr 19th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.56 KB | None | 0 0
  1. devMode = false
  2.  
  3.  
  4. operations = {}
  5. operations[1] = 'setTempo'
  6. operations[2] = 'setInstrument'
  7. operations[3] = 'playNote'
  8. operations[4] = 'hold'
  9. operations[5] = 'beginChunk'
  10. operations[6] = 'goToLine'
  11. operations[7] = 'runChunk'
  12. operations[8] = 'endChunk'
  13. --operations[9] = 'setVar'
  14. --operations[10] = 'changeVar'
  15. --operations[11] = 'ifVar'
  16. --operations[12] = 'ifVarEnd'
  17.  
  18. --[[ getInfs
  19. function getInfs(infc,infnum)
  20.      if devMode == true then
  21.          print('trying to extract '.. infnum ..' values from ['.. infc ..']')
  22.         end
  23.      ret = {}
  24.      ret[1] = false
  25.      reqlen = infnum * 2 - 1
  26.      sepps = {}
  27.      seppAmnt = infnum - 1
  28.      seppNum = 0
  29.      if string.len(infc) > reqlen - 1 then
  30.          for i = 1, string.len(infc) do
  31.              if string.sub(infc,i,i) == ',' then
  32.                  if i > seppNum + 1 and i < string.len(infc) and i > 1 then
  33.                      sepps[seppNum + 1] = i
  34.                      seppNum = seppNum + 1
  35.                     end
  36.                 end
  37.             end
  38.          if seppNum == seppAmnt then
  39.              for i = 1, seppAmnt do
  40.                  if i == 1 then
  41.                      begp = 1
  42.                      endp = sepps[1] - 1
  43.                     elseif i == seppAmnt then
  44.                      begp = sepps[seppAmnt] + 1
  45.                      endp = -1
  46.                     else
  47.                      begp = sepps[seppAmnt] + 1
  48.                      endp = sepps[seppAmnt + 1] - 1
  49.                     end
  50.                  ret[i] = string.sub(infc,begp,endp)
  51.                 end
  52.             end
  53.         end
  54.      return(ret)
  55.     end
  56. --]]
  57.  
  58. function ifVar(line,linenum) --not finished
  59.      
  60.     end
  61.  
  62. function changeVar(line,linenum) --not finished
  63.      
  64.     end
  65.  
  66. function setVar(line,linenum) --not finished
  67.      if string.len(line) > 8 then
  68.          inf = string.sub(line,8,-2)
  69.          --sepps = getInfs(inf,2)
  70.          if string.len(inf) > 2 then
  71.              if string.sub(inf,1,1) == ',' or string.sub(inf,-1,-1) == ',' then
  72.                  repErr('Information Provided Is Not Valid',linenum)
  73.                 else
  74.                  
  75.                 end
  76.             else
  77.              repErr('Not Enough Information Provided',linenum)
  78.             end
  79.         else
  80.          repErr('No Information Provided',linenum)
  81.         end
  82.     end
  83.  
  84. function goToLine(line,linenum)
  85.      if string.len(line) > 10 then
  86.          linen = string.sub(line,10,-2)
  87.          linen = convNumber(linen,false)
  88.          if linen > 0 and linen < #flines + 1 then
  89.              linenumt = linen
  90.              went = true
  91.             else
  92.              repErr('Line Does Not Exist',linenum)
  93.             end
  94.         else
  95.          repErr('No Information Provided',linenum)
  96.         end
  97.     end
  98.  
  99. function runChunk(line,linenum)
  100.      if string.len(line) > 10 then
  101.          name = string.sub(line,10,-2)
  102.          exists = false
  103.          for i = 1, #chunkLbls do
  104.              if chunkLbls[i] == name then
  105.                  exists = i
  106.                 end
  107.             end
  108.          if exists == false then
  109.              repErr('Chunk Does not Exist',linenum)
  110.             else
  111.              cline = linenum
  112.              rline = chunkBegs[exists]
  113.              repeat
  114.                  line = flines[rline]
  115.                  es = 0
  116.                  got = false
  117.                  for i = 1, string.len(line) do
  118.                      if string.sub(line,i,i) == ' ' then
  119.                         else
  120.                          if got == false then
  121.                              es = i
  122.                              got = true
  123.                             end
  124.                         end
  125.                     end
  126.                  line = string.sub(line,es,-1)
  127.                  runLine(line,rline)
  128.                  rline = rline + 1
  129.                 until rline == chunkEnds[exists] + 1
  130.             end
  131.         else
  132.          repErr('No Information Provided',linenum)
  133.         end
  134.     end
  135.  
  136. function beginChunk(line,linenum)
  137.      if string.len(line) > 12 then
  138.          name = string.sub(line,12,-2)
  139.          exists = false
  140.          for i = 1, #chunkLbls do
  141.              if chunkLbls[i] == name then
  142.                  exists = true
  143.                 end
  144.             end
  145.          if exists == true then
  146.              repErr('Chunk Label Already Taken',linenum)
  147.             else
  148.              chunkNum = #chunkLbls + 1
  149.              chunkBegs[chunkNum] = linenum + 1
  150.              ends = false
  151.              for i = linenum, #flines do
  152.                  linec = flines[i]
  153.                  es = 0
  154.                  got = false
  155.                  for i = 1, string.len(linec) do
  156.                      if string.sub(linec,i,i) == ' ' then
  157.                         else
  158.                          if got == false then
  159.                              es = i
  160.                              got = true
  161.                             end
  162.                         end
  163.                     end
  164.                  linec = string.sub(linec,es,-1)
  165.                  if linec == 'endChunk('.. name ..')' then
  166.                      ends = i
  167.                     end
  168.                 end
  169.              if ends == false then
  170.                  repErr('Chunk Does Not End',linenum)
  171.                 else
  172.                  chunkEnds[chunkNum] = ends - 1
  173.                  chunkLbls[chunkNum] = name
  174.                  goToLine('goToLine('.. ends + 1 ..')')
  175.                 end
  176.             end
  177.         else
  178.          repErr('No Information Provided',linenum)
  179.         end
  180.     end
  181.  
  182. function repErr(message,linenum)
  183.      if term.isColor() then
  184.          term.setTextColor(colors.red)
  185.         else
  186.          term.setTextColor(colors.black)
  187.         end
  188.      print('Error on line: '.. linenum ..' - '.. message)
  189.      term.setTextColor(colors.black)
  190.      kill = true
  191.     end
  192.  
  193. function checkOp(operation,line)
  194.      ret = false
  195.      leng = string.len(operation)
  196.      if string.len(line) > leng then
  197.          if string.sub(line,1,leng) == operation then
  198.              if string.sub(line,leng + 1,leng + 1) == '(' then
  199.                  if string.sub(line,-1,-1) == ')' then
  200.                      ret = true
  201.                     end
  202.                 end
  203.             end
  204.         end
  205.      return(ret)
  206.     end
  207.  
  208. function runLine(line,linenum)
  209.      roperation = 'none'
  210.      for oper = 1, #operations do
  211.          if checkOp(operations[oper],line) == true then
  212.              roperation = operations[oper]
  213.             end
  214.         end
  215.      if string.sub(line,1,2) == '--' then
  216.         else
  217.          if roperation == 'none' then
  218.              repErr('That Is Not A Valid Operation',linenum)
  219.             else
  220.              if devMode == true then
  221.                  print('running '.. roperation ..' on line '.. linenum)
  222.                 end
  223.              if roperation == 'setTempo' then
  224.                  setTempo(line,linenum)
  225.                 elseif roperation == 'setInstrument' then
  226.                  setInstrument(line,linenum)
  227.                 elseif roperation == 'playNote' then
  228.                  playNote(line,linenum)
  229.                 elseif roperation == 'hold' then
  230.                  hold(line,linenum)
  231.                 elseif roperation == 'beginChunk' then
  232.                  beginChunk(line,linenum)
  233.                 elseif roperation == 'goToLine' then
  234.                  goToLine(line,linenum)
  235.                 elseif roperation == 'runChunk' then
  236.                  runChunk(line,linenum)
  237.                 elseif roperation == 'setVar' then
  238.                  setVar(line,linenum)
  239.                 elseif roperation == 'changeVar' then
  240.                  changeVar(line,linenum)
  241.                 elseif roperation == 'ifVar' then
  242.                  ifVar(line,linenum)
  243.                 end
  244.             end
  245.         end
  246.     end
  247.  
  248. function convNumber(num,dec)
  249.      nnum = 0
  250.      nums = {}
  251.      nums[1] = '0'
  252.      nums[2] = '1'
  253.      nums[3] = '2'
  254.      nums[4] = '3'
  255.      nums[5] = '4'
  256.      nums[6] = '5'
  257.      nums[7] = '6'
  258.      nums[8] = '7'
  259.      nums[9] = '8'
  260.      nums[10] = '9'
  261.      gnums = {}
  262.      gl = 0
  263.      for i = 1, string.len(num) do
  264.          for cnum = 1, #nums do
  265.              if string.sub(num,i,i) == nums[cnum] then
  266.                  anum = cnum - 1
  267.                  gnums[gl + 1] = anum
  268.                  gl = gl + 1
  269.                 end
  270.             end
  271.         end
  272.      place = 1
  273.      for i = gl, 1, -1 do
  274.          nnum = nnum + gnums[i] * place
  275.          place = place * 10
  276.         end
  277.      if dec == true then
  278.          nnum = '.'.. nnum
  279.          nnum = tonumber(nnum)
  280.         end
  281.      return(nnum)
  282.     end
  283.  
  284. function setTempo(line,linenum)
  285.      if string.len(line) > 10 then
  286.          number = string.sub(line,10,-2)
  287.          number = convNumber(number,true)
  288.          tempo = number
  289.         else
  290.          repErr('No Information Provided',linenum)
  291.         end
  292.     end
  293.  
  294. function setInstrument(line,linenum)
  295.      if string.len(line) > 15 then
  296.          str = string.sub(line,15,-2)
  297.          if str == 'piano' then
  298.              instrument = 0
  299.             elseif str == 'base' then
  300.              instrument = 1
  301.             elseif str == 'snare' then
  302.              instrument = 2
  303.             elseif str == 'base2' then
  304.              instrument = 3
  305.             elseif str == 'snare2' then
  306.              instrument = 4
  307.             else
  308.              repErr('Instrument Is Not Valid',linenum)
  309.             end
  310.         else
  311.          repErr('No Information Provided',linenum)
  312.         end
  313.     end
  314.  
  315. function playNote(line,linenum)
  316.      if string.len(line) > 10 then
  317.          number = string.sub(line,10,-2)
  318.          number = convNumber(number,false)
  319.          if number > -1 and number < 25 then
  320.              for i = 1, notenum do
  321.                  peripheral.call(notes[i],'playNote',instrument,number)
  322.                 end
  323.              if devMode == true then
  324.                  print('Playing note '.. number ..' on instrument '.. instrument)
  325.                 end
  326.              playedNote = true
  327.              if tempo == 0 then
  328.                 else
  329.                  sleep(tempo)
  330.                 end
  331.             else
  332.              repErr('Got['.. number ..'] Note Must Be A Number 0-24',linenum)
  333.             end
  334.         else
  335.          repErr('No Information Provided',linenum)
  336.         end
  337.     end
  338.  
  339. function hold(line,linenum)
  340.      if string.len(line) > 6 then
  341.          number = string.sub(line,6,-2)
  342.          number = '.'.. number
  343.          number = convNumber(number,true)
  344.          sleep(number)
  345.         else
  346.          repErr('No Information Provided',linenum)
  347.         end
  348.     end
  349.  
  350. function options(opt1,opt2,opt3,sx,sy)
  351.      opt = 1
  352.      term.setCursorPos(sx,sy)
  353.      print('Arrow keys to switch, Enter to select.')
  354.      sy = sy + 2
  355.      repeat
  356.          term.setCursorPos(sx,sy)
  357.          print(' '.. opt1 ..' ')
  358.          term.setCursorPos(sx + 3 + string.len(opt1),sy)
  359.          print(' '.. opt2 ..' ')
  360.          term.setCursorPos(sx + 6 + string.len(opt1) + string.len(opt2),sy)
  361.          print(' '.. opt3 ..' ')
  362.          if term.isColor() then
  363.              term.setTextColor(colors.red)
  364.             else
  365.              term.setTextColor(colors.black)
  366.             end
  367.          if opt == 1 then
  368.              term.setCursorPos(sx,sy)
  369.              print('[')
  370.              term.setCursorPos(sx + string.len(opt1) + 1,sy)
  371.              print(']')
  372.             elseif opt == 2 then
  373.              term.setCursorPos(sx + 3 + string.len(opt1),sy)
  374.              print('[')
  375.              term.setCursorPos(sx + 4 + string.len(opt1) + string.len(opt2),sy)
  376.              print(']')
  377.             elseif opt == 3 then
  378.              term.setCursorPos(sx + 6 + string.len(opt1) + string.len(opt2),sy)
  379.              print('[')
  380.              term.setCursorPos(sx + 7 + string.len(opt1) + string.len(opt2) + string.len(opt3),sy)
  381.              print(']')
  382.             end
  383.          term.setTextColor(colors.black)
  384.          eve, key = os.pullEvent()
  385.          if eve == 'key' then
  386.              if key == 205 then
  387.                  if opt == 1 then
  388.                      opt = 2
  389.                     elseif opt == 2 then
  390.                      opt = 3
  391.                     end
  392.                 elseif key == 203 then
  393.                  if opt == 2 then
  394.                      opt = 1
  395.                     elseif opt == 3 then
  396.                      opt = 2
  397.                     end
  398.                 end
  399.             end
  400.         until eve == 'key' and key == 28
  401.      return(opt)
  402.     end
  403.  
  404. term.setBackgroundColor(colors.white)
  405. term.setTextColor(colors.black)
  406. repeat
  407.      peris = peripheral.getNames()
  408.      notes = {}
  409.      notenum = 0
  410.      for i = 1, #peris do
  411.          if peripheral.getType(peris[i]) == 'modem' then
  412.              rednet.open(peris[i])
  413.             end
  414.         end
  415.      peris = peripheral.getNames()
  416.      for i = 1, #peris do
  417.          if peripheral.getType(peris[i]) == 'noteBlock' then
  418.              notenum = notenum + 1
  419.              notes[notenum] = peris[i]
  420.             end
  421.         end
  422.      if notenum > 0 then
  423.          repeat
  424.              term.setBackgroundColor(colors.white)
  425.              term.setTextColor(colors.black)
  426.              term.clear()
  427.              term.setCursorPos(1,1)
  428.              print('Loading jogOS Music Mixer version 1.2')
  429.              print('Select an option:')
  430.              sel = options('Edit Song','Play Song','Reload Note Blocks',1,4)
  431.              term.setCursorPos(1,8)
  432.              if sel == 1 then
  433.                  print('Enter the name of the song you would like to edit:')
  434.                  term.setCursorPos(1,10)
  435.                  print('Enter guide, to view a simple tutorial')
  436.                  term.setCursorPos(1,9)
  437.                  song = io.read()
  438.                  shell.run('edit musics/'.. song)
  439.                 elseif sel == 2 then
  440.                  print('Enter the name of the song you would like to play:')
  441.                  song = io.read()
  442.                  if song == '' then
  443.                     else
  444.                      if fs.exists('musics/'.. song) then
  445.                          file = io.open('musics/'.. song,'r')
  446.                          file2 = io.open('musics/'.. song,'r')
  447.                          num = 0
  448.                          flines = {}
  449.                          lineAmnt = 0
  450.                          for line in file2:lines() do
  451.                              lineAmnt = lineAmnt + 1
  452.                             end
  453.                          pCount = 100 / lineAmnt
  454.                          for line in file:lines() do
  455.                              num = num + 1
  456.                              flines[num] = line
  457.                              term.clear()
  458.                              term.setCursorPos(1,1)
  459.                              print('Loading '.. song)
  460.                              compl = num * pCount
  461.                              print(compl ..'% complete')
  462.                             end
  463.                          print('File loaded')
  464.                          if lineAmnt > 0 then
  465.                              term.clear()
  466.                              term.setCursorPos(1,1)
  467.                              print('Apply a redstone signal to the top of the computer to stop the song at anytime')
  468.                              tempo = .1
  469.                              instrument = 0
  470.                              kill = false
  471.                              chunkLbls = {}
  472.                              chunkBegs = {}
  473.                              chunkEnds = {}
  474.                              varLbls = {}
  475.                              varVals = {}
  476.                              linenumt = 1
  477.                              repeat
  478.                                  went = false
  479.                                  playedNote = false
  480.                                  if kill == false then
  481.                                      line = flines[linenumt]
  482.                                      es = 0
  483.                                      got = false
  484.                                      for i = 1, string.len(line) do
  485.                                          if string.sub(line,i,i) == ' ' then
  486.                                             else
  487.                                              if got == false then
  488.                                                  es = i
  489.                                                  got = true
  490.                                                 end
  491.                                             end
  492.                                         end
  493.                                      line = string.sub(line,es,-1)
  494.                                      runLine(line,linenumt)
  495.                                     end
  496.                                  if went == false then
  497.                                      linenumt = linenumt + 1
  498.                                     end
  499.                                  if rs.getInput('top') == true then
  500.                                      linenumt = num + 1
  501.                                     end
  502.                                 until linenumt > num
  503.                              if devMode == true then
  504.                                  io.read()
  505.                                 end
  506.                              if kill == true then
  507.                                  print('Hit enter to continue')
  508.                                  io.read()
  509.                                 end
  510.                             else
  511.                              print('That file is empty')
  512.                              sleep(2)
  513.                             end
  514.                         else
  515.                          print('That file could not be found on this computer')
  516.                          sleep(2)
  517.                         end
  518.                     end
  519.                 elseif sel == 3 then
  520.                  repeat
  521.                      peris = peripheral.getNames()
  522.                      notes = {}
  523.                      notenum = 0
  524.                      for i = 1, #peris do
  525.                          if peripheral.getType(peris[i]) == 'modem' then
  526.                              rednet.open(peris[i])
  527.                             end
  528.                         end
  529.                      peris = peripheral.getNames()
  530.                      for i = 1, #peris do
  531.                          if peripheral.getType(peris[i]) == 'noteBlock' then
  532.                              notenum = notenum + 1
  533.                              notes[notenum] = peris[i]
  534.                             end
  535.                         end
  536.                      if notenum == 0 then
  537.                          term.clear()
  538.                          term.setCursorPos(1,1)
  539.                          print('Make sure that you have an iron noteblock from the peripherals++ mod attached to the computer and try again')
  540.                          print('Press the enter key to continue')
  541.                          io.read()
  542.                         end
  543.                     until notenum > 0
  544.                 end
  545.             until 1 == 2
  546.         else
  547.          term.clear()
  548.          term.setCursorPos(1,1)
  549.          print('Make sure that you have an iron noteblock from the peripherals++ mod attached to the computer and try again')
  550.          print('Press the enter key to continue')
  551.          io.read()
  552.         end
  553.     until 1 == 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement