Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = ...
- filesystem = require("filesystem")
- component = require("component")
- computer = require("computer")
- m = component.proxy(component.list("modem")()) --GLOBAL
- m.open(2412)
- function readfile(filename)
- local f = io.open(filename,'r')
- local str = f:read("*a")
- f:close()
- return str
- end
- sheet = readfile(args) --GLOBAL
- tuning = {'E4','B3','G3','D3','A2','E2'} --GLOBAL
- tempo = 95 --GLOBAL
- signature = "8/4" --GLOBAL
- Note = {} --GLOBAL
- Batch = {} --GLOBAL
- song = {} --GLOBAL
- duration = {} --GLOBAL
- duration.W = 4
- duration.H = 2
- duration.Q = 1
- duration.E = .5
- duration.S = .25
- duration.T = .334
- freq = {} --GLOBAL
- freq['C2'] = 65.41
- freq['C#2'] = 69.30
- freq['D2'] = 73.42
- freq['D#2'] = 77.78
- freq['E2'] = 82.41
- freq['F2'] = 87.31
- freq['F#2'] = 92.50
- freq['G2'] = 98.00
- freq['G#2'] = 103.83
- freq['A2'] = 110.00
- freq['A#2'] = 116.54
- freq['B2'] = 123.47
- freq['C3'] = 130.81
- freq['C#3'] = 138.59
- freq['D3'] = 146.83
- freq['D#3'] = 155.56
- freq['E3'] = 164.81
- freq['F3'] = 174.61
- freq['F#3'] = 185.00
- freq['G3'] = 196.00
- freq['G#3'] = 207.65
- freq['A3'] = 220.00
- freq['A#3'] = 233.08
- freq['B3'] = 246.94
- freq['C4'] = 261.63
- freq['C#4'] = 277.18
- freq['D4'] = 293.66
- freq['D#4'] = 311.13
- freq['E4'] = 329.63
- freq['F4'] = 349.23
- freq['F#4'] = 369.99
- freq['G4'] = 392.00
- freq['G#4'] = 415.30
- freq['A4'] = 440.00
- freq['A#4'] = 466.16
- freq['B4'] = 493.88
- freq['C5'] = 523.25
- freq['C#5'] = 554.37
- freq['D5'] = 587.33
- freq['D#5'] = 622.25
- freq['E5'] = 659.25
- freq['F5'] = 698.46
- freq['F#5'] = 739.99
- freq['G5'] = 783.99
- freq['G#5'] = 830.61
- freq['A5'] = 880.00
- freq['A#5'] = 932.33
- freq['B5'] = 987.77
- freq['C6'] = 1046.50
- freq['C#6'] = 1108.73
- freq['D6'] = 1174.66
- freq['D#6'] = 1244.51
- freq['E6'] = 1318.51
- freq['F6'] = 1396.91
- freq['F#6'] = 1479.98
- freq['G6'] = 1567.98
- freq['G#6'] = 1661.22
- freq['A6'] = 1760.00
- freq['A#6'] = 1864.66
- freq['B6'] = 1975.53
- freq['C7'] = 2093.00
- Note.alphabet = {'C','C#','D','D#','E','F','F#','G','G#','A','A#','B'}
- Note.hexadecimal = {['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15}
- function Note:new(ithStr,ithFret,fracNote)
- o = {}
- o.ithStr = tonumber(ithStr)
- o.ithFret = tonumber(ithFret) or self.hexadecimal[ithFret]
- o.fracNote = fracNote
- o.string = ithStr..ithFret..fracNote
- setmetatable(o,self)
- self.__index = self
- return o
- end
- function Note:getNote()
- --requires access to tuning table
- local strNote = tuning[self.ithStr]
- strNote = self:halfStep(strNote,self.ithFret)
- return strNote
- end
- function Note:getFreq()
- return freq[self:getNote()]
- end
- function Note:getDuration()
- return duration[self.fracNote]*(60/tempo)
- end
- function Note:halfStep(strNote,numReps)
- if numReps == 0 then
- return strNote
- end
- local letter = strNote:sub(1,#strNote-1)
- local octave = strNote:sub(#strNote)
- local key = table.getKey(self.alphabet,letter)
- local index = key + numReps
- local octave = octave + math.floor(index/#self.alphabet)
- index = index%#self.alphabet
- if index == 0 then
- index = 12
- end
- return self.alphabet[index]..octave
- end
- function table:getKey(value) --GLOBAL
- for k,v in pairs(self) do
- if value == v then
- return k
- end
- end
- end
- function Batch:new(status)
- o = {}
- o.status = status
- o.string = ""
- o.duration = 0
- setmetatable(o,self)
- self.__index = self
- return o
- end
- function Batch:insert(strNote)
- self.string = self.string..getExec(strNote)
- end
- function getExec(strNote) --GLOBAL
- local note = Note:new(strNote:sub(1,1),strNote:sub(2,2),strNote:sub(3,3))
- if strNote:sub(1,1) == "0" then
- return "os.sleep("..tostring(note:getDuration())..") "
- else
- return "computer.beep("..tostring(note:getFreq())..","..tostring(note:getDuration())..") "
- end
- end
- function main() --GLOBAL
- local measure = 0
- local batch = Batch:new(false)
- local measureTime = tonumber(signature:sub(1,1)) * (4/tonumber(signature:sub(#signature)))
- for word in string.gmatch(sheet,'%g+') do
- if word:sub(1,1)=="{" then
- batch.status = true
- batch.duration = duration[word:sub(#word)]
- batch:insert(word:sub(2))
- elseif word:sub(#word)=="}" then
- batch:insert(word:sub(1,#word-1))
- m.broadcast(2412,batch.string)
- print(batch.string)
- batch.status = false
- batch.string = ""
- measure = measure + batch.duration
- os.sleep(batch.duration*(60/tempo))
- elseif word == "|" then
- if (measureTime-measure)>0 then
- m.broadcast(2412,"os.sleep("..tostring((measureTime-measure)*(60/tempo))..")")
- print(measureTime-measure)
- os.sleep((measureTime-measure)*(60/tempo))
- end
- measure = 0
- else
- if batch.status then
- batch:insert(word)
- else
- print(getExec(word))
- measure = measure + duration[word:sub(3,3)]
- m.broadcast(2412,getExec(word))
- os.sleep(duration[word:sub(3,3)]*(60/tempo))
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement