JerryWester

[ComputerCraft] [CC: Tweaked] [WIP] Piano

Jun 11th, 2020
2,706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. --[[
  2. Uses the same keys as this website: https://www.onlinepianist.com/virtual-piano
  3.  
  4. Keys:
  5. Left/Right Arrow Keys: Change Instrument
  6. Up/Down Arrow Keys: Change Volume
  7. E: Exit
  8. ]]
  9.  
  10. 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}
  11. local pianoNotes = {"F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E", "F"}
  12. local speaker = peripheral.find("speaker")
  13. local volume = 3
  14. local exitKey = keys.e
  15. local instruments = {"bass", "snare", "hat", "basedrum", "bell", "flute", "chime", "guitar", "xylophone", "pling", "harp"}
  16. local instrumentNames = {"Bass", "Snare Drum", "Clicks and Sticks", "Bass Drum", "Glockenspiel", "Flute", "Chimes", "Guitar", "Xylophone", "Electric Piano", "Harp / Piano"}
  17. local currentInstrument = #instruments
  18. local changeInstrumentUp = keys.right
  19. local changeInstrumentDown = keys.left
  20. local volumeUp = keys.up
  21. local volumeDown = keys.down
  22.  
  23. local function getPianoKey(inp)
  24.     for i, v in ipairs(pianoKeys) do
  25.         if inp == v then return i end
  26.     end
  27.     return nil
  28. end
  29.  
  30. local function init()
  31.     term.clear()
  32.     term.setCursorPos(1,1)
  33.     if term.isColor() then
  34.         term.setBackgroundColor(colors.black)
  35.         term.setTextColor(colors.white)
  36.     end
  37. end
  38.  
  39. if speaker == nil then
  40.     print("No speaker attached!")
  41. else
  42.     init()
  43.     while true do
  44.         e, p1, p2 = os.pullEvent()
  45.         if e == "key" then
  46.             if p1 == exitKey then
  47.                 init()
  48.                 sleep(0.1)
  49.                 break
  50.             elseif p1 == changeInstrumentUp then
  51.                 currentInstrument = ( ( ( currentInstrument + 1 ) - 1 ) % #instruments ) + 1
  52.                 print("Instrument: " .. instrumentNames[currentInstrument])
  53.             elseif p1 == changeInstrumentDown then
  54.                 currentInstrument = ( ( ( currentInstrument - 1 ) - 1 ) % #instruments ) + 1
  55.                 print("Instrument: " .. instrumentNames[currentInstrument])
  56.             elseif p1 == volumeUp then
  57.                 if not (volume >= 3) then
  58.                     volume = volume + 1
  59.                 end
  60.                 print("Volume: " .. volume)
  61.             elseif p1 == volumeDown then
  62.                 if not (volume <= 0) then
  63.                     volume = volume - 1
  64.                 end
  65.                 print("Volume: " .. volume)
  66.             else
  67.                 local pianoKey = getPianoKey(p1)
  68.                 if pianoKey then
  69.                     print(pianoNotes[((pianoKey-1)%#pianoNotes)+1])
  70.                     speaker.playNote(instruments[((currentInstrument-1)%#instruments)+1], volume, pianoKey-1)
  71.                 end
  72.                 pianoKey = nil
  73.             end
  74.         end
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment