Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sound = peripheral.find("sound")
- local modes = sound.getModes()
- local delay = 200
- local sleepTime = delay/1000
- local last = false
- local modeOverride = modes.square
- local openChannels = 0
- local sineDif = 600
- local sawDif = 300
- local squareDif = 0
- local tArgs = {...}
- if #tArgs > 0 then
- if modes[tArgs[1]] then
- modeOverride = modes[tArgs[1]]
- else
- local b = tonumber(tArgs[1])
- if type(b) == "number" then
- delay = b
- sleepTime = delay/1000
- else
- printError("Expected mode or delay for argument 1")
- printError("Usage: " .. shell.getRunningProgram() .. " <mode / delay> [delay]")
- printError("Valid types: <sawtooth,square,sine>")
- return -1
- end
- end
- if tArgs[2] then
- if type(tonumber(tArgs[2])) == "number" then
- delay = tonumber(tArgs[2])
- sleepTime = delay/1000
- else
- printError("Expected delay for argument 2")
- printError("Usage: " .. shell.getRunningProgram() .. " <mode / delay> [delay]")
- return -1
- end
- end
- end
- for i = 1,8 do
- sound.close(i)
- sound.setVolume(i,0)
- end
- local notes = {}
- local function loadNotes()
- local h = fs.open("notes","r")
- local lines = 0
- if h then
- while h.readLine() do
- lines = lines + 1
- end
- h.close()
- openChannels = lines
- if lines > 8 then
- error("Only 8 channels.")
- end
- end
- --[[
- SAW
- SIN
- SQU
- ]]
- h = fs.open("notes","r")
- if h and lines > 0 then
- for i = 1,openChannels do
- print("Reading...")
- local a = h.readLine()
- print(a)
- sound.open(i)
- sound.setVolume(i,1)
- notes[i] = {}
- local b = string.sub(a,1,3)
- a = string.sub(a,4)
- if b == "SAW" then
- notes[i].t = modes.sawtooth
- elseif b == "SQU" then
- notes[i].t = modes.square
- elseif b == "SIN" then
- notes[i].t = modes.sine
- elseif b == "TRI" then
- notes[i].t = modes.triangle
- else notes[i].t = modes.noise
- end
- os.sleep(0.5)
- local o = 1
- for k in a:gmatch(".") do
- print(i,k)
- notes[i][o] = k
- o = o + 1
- end
- print("Done")
- end
- h.close()
- else
- error("Could not open file.")
- end
- end
- local function ls(c,f,m)
- c = c or 1
- f = f or 500
- last = f
- m = m or modeOverride or modes.sine
- if m == modes.sine then
- f = f + sineDif
- elseif m == modes.sawtooth then
- f = f + sawDif
- end
- sound.setFrequency(c,f)
- sound.setWave(c,m)
- sound.setVolume(c,1)
- end
- local function playNote(c,n,tp)
- if n then
- n = string.lower(n)
- local function no()
- print("no for now.")
- end
- if n == "a" then
- ls(c,55,tp)
- elseif n == "b" then
- ls(c,61.73541,tp)
- elseif n == "c" then
- ls(c,32.70320,tp)
- elseif n == "d" then
- ls(c,36.70810,tp)
- elseif n == "e" then
- ls(c,41.20344,tp)
- elseif n == "f" then
- ls(c,43.65353,tp)
- elseif n == "g" then
- ls(c,48.99943,tp)
- elseif n == "_" then
- print("Silence, but wait a minute...")
- ls(c,0.1,tp)
- elseif n == "r" then
- print("Repeat last note")
- if last then
- ls(c,last,tp)
- else
- sound.setVolume(c,0)
- print("No note to repeat.")
- end
- else
- sound.setVolume(c,0)
- print("Unavailable:",n)
- end
- else
- sound.setVolume(c,0)
- print("blankness my dudes: ",n)
- end
- end
- local function playNotes(tab)
- for o = 1,#tab[1] do
- for i = 1,openChannels do
- print("---")
- print("Playing note",tab[i][o])
- playNote(i,tab[i][o],tab[i].t)
- end
- sound.delay(delay)
- sound.process()
- os.sleep(sleepTime)
- end
- end
- loadNotes()
- print(textutils.serialize(notes))
- while true do
- playNotes(notes)
- --os.sleep()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement