Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- keys random
- by Big SHiny Toys
- ]]--
- local function openDevice(sType)
- for i,v in pairs(rs.getSides()) do
- if peripheral.isPresent(v) and peripheral.getType(v) == sType then
- return peripheral.wrap(v),v
- end
- end
- end
- local function freq(key)
- return (2^((key-49)/12))*440
- end
- local key = 1
- local pitch = freq(key)
- local chan = 1
- local speaker = openDevice("speaker")
- if not speaker then
- error("no speaker found")
- end
- term.clear()
- local function p()
- term.setCursorPos(1,1)
- term.clearLine()
- print(" key "..key.." pitch "..pitch)
- end
- local tKeys = {
- a = 40,
- w = 41,
- s = 42,
- e = 43,
- d = 44,
- f = 45,
- t = 46,
- g = 47,
- y = 48,
- h = 49,
- u = 50,
- k = 51,
- j = 52,
- o = 53,
- l = 54,
- p = 55
- }
- local tTimers = {}
- local hang = 0.3
- for k,v in pairs(tKeys) do
- print(k," ",v)
- end
- local offSet = 0
- local numb = 0
- while true do
- local event = {os.pullEvent()}
- if event[1] == "char" then
- if tKeys[event[2]] then
- local tab = os.startTimer(hang)
- tTimers[tostring(tab)] = numb
- speaker.start(numb,freq(tKeys[event[2]] + (offSet*12)))
- numb = numb + 1
- if numb > 7 then
- numb = 0
- end
- end
- elseif event[1] == "timer" then
- local st = tostring(event[2])
- if tTimers[st] then
- speaker.stop(tTimers[st])
- tTimers[st] = nil
- end
- elseif event[1] == "key" then
- if event[2] == 200 then
- offSet = offSet + 1
- elseif event[2] == 208 then
- offSet = offSet - 1
- end
- end
- end
- --[[
- while true do
- p()
- local event = {os.pullEvent()}
- if event[1] == "char" then
- if event[2] == "u" then
- key = key + 1
- pitch = freq(key)
- speaker.start(chan,pitch)
- elseif event[2] == "d" then
- key = key - 1
- pitch = freq(key)
- speaker.start(chan,pitch)
- elseif event[2] == "s" then
- speaker.stop(chan)
- elseif event[2] == "p" then
- for i = 1,30000 do
- speaker.start(chan,i)
- term.setCursorPos(1,1)
- term.clearLine()
- print(" key "..key.." pitch "..i)
- sleep(0.001)
- end
- end
- end
- end
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement