Advertisement
guitarplayer616

OpenMusic

Dec 13th, 2019
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. args = ...
  2. filesystem = require("filesystem")
  3. sheet = "60E 50E 40E 42E 43H 00Q 60E 50E 40E 42E 43H 00Q 63E 53E 40E 42E 30Q 00E 30S 40S 60E 50E 40E 42E 43E 32E 43E 42E"
  4. --sheet = sheet .. "60E 50E 40E 42E 43H 00Q" .. "60E 50E 40E 42E 43H 00Q" .. "63E 53E 43E 30E 32H 00Q" .. "50H 00Q 32E 21E 23E 32E 23E 10E 11Q 23Q"
  5. --sheet = sheet .. "53E 23E 21E 23E 10Q 00E 23S 10S" .. "11E 11E 10E 23E 10E 30E 21Q" .."32E 30E 32E 21E 32Q 00E 32S 21S"
  6.  
  7. if args and filesystem.exists(args) then
  8.     local f = io.open(args,'r')
  9.     sheet = f.read("*a")
  10.     f.close()
  11. end
  12.  
  13.  
  14. tuning = {'E4','B3','G3','D3','A2','D2'}
  15. tempo = 71
  16.  
  17. Note = {}
  18. song = {}
  19.  
  20. duration = {}
  21. duration.W = (60/tempo)*4
  22. duration.H = (60/tempo)*2
  23. duration.Q = (60/tempo)*1
  24. duration.E = (60/tempo)*.5
  25. duration.S = (60/tempo)*.25
  26.  
  27. freq = {}
  28. freq['C2'] = 65.41
  29. freq['C#2'] = 69.30
  30. freq['D2'] = 73.42
  31. freq['D#2'] = 77.78
  32. freq['E2'] = 82.41
  33. freq['F2'] = 87.31
  34. freq['F#2'] = 92.50
  35. freq['G2'] = 98.00
  36. freq['G#2'] = 103.83
  37. freq['A2'] = 110.00
  38. freq['A#2'] = 116.54
  39. freq['B2'] = 123.47
  40. freq['C3'] = 130.81
  41. freq['C#3'] = 138.59
  42. freq['D3'] = 146.83
  43. freq['D#3'] = 155.56
  44. freq['E3'] = 164.81
  45. freq['F3'] = 174.61
  46. freq['F#3'] = 185.00
  47. freq['G3'] = 196.00
  48. freq['G#3'] = 207.65
  49. freq['A3'] = 220.00
  50. freq['A#3'] = 233.08
  51. freq['B3'] = 246.94
  52. freq['C4'] = 261.63
  53. freq['C#4'] = 277.18
  54. freq['D4'] = 293.66
  55. freq['D#4'] = 311.13
  56. freq['E4'] = 329.63
  57. freq['F4'] = 349.23
  58. freq['F#4'] = 369.99
  59. freq['G4'] = 392.00
  60. freq['G#4'] = 415.30
  61. freq['A4'] = 440.00
  62. freq['A#4'] = 466.16
  63. freq['B4'] = 493.88
  64. freq['C5'] = 523.25
  65. freq['C#5'] = 554.37
  66. freq['D5'] = 587.33
  67. freq['D#5'] = 622.25
  68. freq['E5'] = 659.25
  69. freq['F5'] = 698.46
  70. freq['F#5'] = 739.99
  71. freq['G5'] = 783.99
  72. freq['G#5'] = 830.61
  73. freq['A5'] = 880.00
  74. freq['A#5'] = 932.33
  75. freq['B5'] = 987.77
  76. freq['C6'] = 1046.50
  77. freq['C#6'] = 1108.73
  78. freq['D6'] = 1174.66
  79. freq['D#6'] = 1244.51
  80. freq['E6'] = 1318.51
  81. freq['F6'] = 1396.91
  82. freq['F#6'] = 1479.98
  83. freq['G6'] = 1567.98
  84. freq['G#6'] = 1661.22
  85. freq['A6'] = 1760.00
  86. freq['A#6'] = 1864.66
  87. freq['B6'] = 1975.53
  88. freq['C7'] = 2093.00
  89.  
  90. Note.alphabet = {'C','C#','D','D#','E','F','F#','G','G#','A','A#','B'} 
  91.  
  92. function Note:new(ithStr,ithFret,fracNote)
  93.     o = {}
  94.     o.ithStr = tonumber(ithStr)
  95.     o.ithFret = tonumber(ithFret)
  96.     o.fracNote = fracNote
  97.     o.string = ithStr..ithFret..fracNote
  98.     setmetatable(o,self)
  99.     self.__index = self
  100.     return o
  101. end
  102.  
  103. function Note:getNote()
  104.     --requires access to tuning table
  105.     local strNote = tuning[self.ithStr]
  106.     strNote = self:halfStep(strNote,self.ithFret)
  107.     return strNote
  108. end
  109.  
  110. function Note:getFreq()
  111.     return freq[self:getNote()]
  112. end
  113.  
  114. function Note:halfStep(strNote,numReps)
  115.     if numReps == 0 then
  116.         return strNote
  117.     end
  118.     local letter = strNote:sub(1,#strNote-1)
  119.     local octave = strNote:sub(#strNote)
  120.     local key = table.getKey(self.alphabet,letter)
  121.     local index = key + numReps
  122.     local octave = octave + math.floor(index/#self.alphabet)
  123.     return self.alphabet[index%#self.alphabet]..octave
  124. end
  125.  
  126. function table:getKey(value)
  127.     for k,v in pairs(self) do
  128.         if value == v then
  129.             return k
  130.         end
  131.     end
  132. end
  133.  
  134. function main()
  135.     local measure = 0
  136.     local signature = "4/4"
  137.     for word in string.gmatch(sheet,'%g+') do
  138.         --if (word == "|") and (4-measure~=0) then
  139.             --table.insert(song,Note:new(0,4-measure,'E'))
  140.             --measure = 0
  141.         --else
  142.             table.insert(song,Note:new(word:sub(1,1),word:sub(2,2),word:sub(3,3)))
  143.             --if word:sub(3,3) == "W" then
  144.             --  measure = measure + 4
  145.             --elseif word:sub(3,3) == "H" then
  146.             --  measure = measure + 2
  147.             --elseif word:sub(3,3) == "Q" then
  148.             --  measure = measure + 1
  149.             --elseif word:sub(3,3) == "E" then
  150.             --  measure = measure + 0.5
  151.             --elseif word:sub(3,3) == "S" then
  152.             --  measure = measure + 0.25
  153.             --end
  154.         --end
  155.     end
  156.  
  157.     for _,note in ipairs(song) do
  158.         if note.ithStr == 0 then
  159.             os.sleep(duration[note.fracNote])
  160.         else
  161.             print(note.string.." "..note:getNote().." "..note:getFreq())
  162.             computer.beep(note:getFreq(),duration[note.fracNote])
  163.         end
  164.     end
  165. end
  166.  
  167. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement