Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Uses the same keys as this website: https://www.onlinepianist.com/virtual-piano
- Keys:
- Left/Right Arrow Keys: Change Instrument
- Up/Down Arrow Keys: Change Volume
- E: Exit
- ]]
- local pianoKeys = {keys.five, keys.t, keys.six, keys.y, keys.seven, keys.u, keys.i, keys.nine, keys.o, keys.zero, keys.p, keys.z, keys.s, keys.x, keys.d, keys.c, keys.f, keys.v, keys.b, keys.h, keys.n, keys.j, keys.m, keys.comma, keys.l}
- local pianoNotes = {"F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E", "F"}
- local speaker = peripheral.find("speaker")
- local volume = 3
- local exitKey = keys.e
- local instruments = {"bass", "snare", "hat", "basedrum", "bell", "flute", "chime", "guitar", "xylophone", "pling", "harp"}
- local instrumentNames = {"Bass", "Snare Drum", "Clicks and Sticks", "Bass Drum", "Glockenspiel", "Flute", "Chimes", "Guitar", "Xylophone", "Electric Piano", "Harp / Piano"}
- local currentInstrument = #instruments
- local changeInstrumentUp = keys.right
- local changeInstrumentDown = keys.left
- local volumeUp = keys.up
- local volumeDown = keys.down
- local function getPianoKey(inp)
- for i, v in ipairs(pianoKeys) do
- if inp == v then return i end
- end
- return nil
- end
- local function init()
- term.clear()
- term.setCursorPos(1,1)
- if term.isColor() then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- end
- if speaker == nil then
- print("No speaker attached!")
- else
- init()
- while true do
- e, p1, p2 = os.pullEvent()
- if e == "key" then
- if p1 == exitKey then
- init()
- sleep(0.1)
- break
- elseif p1 == changeInstrumentUp then
- currentInstrument = ( ( ( currentInstrument + 1 ) - 1 ) % #instruments ) + 1
- print("Instrument: " .. instrumentNames[currentInstrument])
- elseif p1 == changeInstrumentDown then
- currentInstrument = ( ( ( currentInstrument - 1 ) - 1 ) % #instruments ) + 1
- print("Instrument: " .. instrumentNames[currentInstrument])
- elseif p1 == volumeUp then
- if not (volume >= 3) then
- volume = volume + 1
- end
- print("Volume: " .. volume)
- elseif p1 == volumeDown then
- if not (volume <= 0) then
- volume = volume - 1
- end
- print("Volume: " .. volume)
- else
- local pianoKey = getPianoKey(p1)
- if pianoKey then
- print(pianoNotes[((pianoKey-1)%#pianoNotes)+1])
- speaker.playNote(instruments[((currentInstrument-1)%#instruments)+1], volume, pianoKey-1)
- end
- pianoKey = nil
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment