Advertisement
JereTheJuggler

speaker.lua

Dec 8th, 2021 (edited)
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.73 KB | None | 0 0
  1. local durations={
  2.     quarter=60/67
  3. }
  4. local globalOffset = -5
  5. durations.eighth = durations.quarter/2
  6. durations.sixteenth = durations.eighth/2
  7. durations.thirtysecond = durations.sixteenth/2
  8. durations.half = durations.quarter*2
  9. durations.whole = durations.half*2
  10. local pitches = { .5, .52973, .56122, .5946, .62995, .66741, .70709, .75, .7946, .84184, .8919, .94493, 1, 1.05946, 1.12246, 1.1892, 1.25991, 1.33482, 1.41419, 1.5, 1.58919, 1.68368, 1.7838, 1.88986, 2 }
  11. local notes = {
  12.     C1=1,
  13.     Db1=2,
  14.     D1=3,
  15.     Eb1=4,
  16.     E1=5,
  17.     F1=6,
  18.     Gb1=7,
  19.     G1=8,
  20.     Ab1=9,
  21.     A1=10,
  22.     Bb1=11,
  23.     B1=12,
  24.     C2=13,
  25.     Db2=14,
  26.     D2=15,
  27.     Eb2=16,
  28.     E2=17,
  29.     F2=18,
  30.     Gb2=19,
  31.     G2=20,
  32.     Ab2=21,
  33.     A2=22,
  34.     Bb2=23,
  35.     B2=24,
  36.     C3=25
  37. }
  38. local intervals = {
  39.     minor2=1,
  40.     major2=2,
  41.     minor3=3,
  42.     major3=4,
  43.     perfect4=5,
  44.     tritone=6,
  45.     perfect5=7,
  46.     minor6=8,
  47.     major6=9,
  48.     minor7=10,
  49.     major7=11,
  50.     octave=12
  51. }
  52. local chords={
  53.     maj={intervals.major3,intervals.minor3},
  54.     min={intervals.minor3,intervals.major3},
  55.     aug={intervals.major3,intervals.major3},
  56.     dim={intervals.minor3,intervals.minor3},
  57.     maj7={intervals.major3,intervals.minor3,intervals.major3},
  58.     min7={intervals.minor3,intervals.major3,intervals.minor3},
  59.     dom7={intervals.major3,intervals.minor3,intervals.minor3}
  60. }
  61.  
  62. local speakers = {}
  63. function wrapSpeakers()
  64.     speakers = {}
  65.     for _,name in ipairs(peripheral.getNames()) do
  66.         if peripheral.getType(name) == "speaker" then
  67.             table.insert(speakers,peripheral.wrap(name))
  68.         end
  69.     end
  70. end
  71. wrapSpeakers()
  72. local speaker = {}
  73. speaker.setSound = function(self,sound)
  74.     self.sound = sound
  75. end
  76. speaker.play = function(self,note,duration,sound)
  77.     if sound == nil then
  78.         sound = self.sound
  79.     end
  80.     if sound ~= nil and #speakers >= 1 then
  81.         speakers[1].playSound(sound,1,pitches[note+globalOffset])
  82.     end
  83.     sleep(duration)
  84. end
  85. speaker.playChord = function(self,rootNote,structure,duration,sound)
  86.     if sound == nil then
  87.         sound = self.sound
  88.     end
  89.     if sound ~= nil and #speakers >= 1 then
  90.         speakers[1].playSound(sound,1,pitches[rootNote+globalOffset])
  91.         local nextNote = rootNote
  92.         local s = 2
  93.         for _,interval in ipairs(structure) do
  94.             nextNote = nextNote + interval
  95.             if nextNote+globalOffset > 25 or s > #speakers then
  96.                 break
  97.             end
  98.             speakers[s].playSound(sound,1,pitches[nextNote+globalOffset])
  99.             s = s + 1
  100.         end
  101.     end
  102.     sleep(duration)
  103. end
  104. parallel.waitForAll(
  105.     function()
  106.         while true do
  107.             local e = os.pullEvent()
  108.             if e == "peripheral_detach" or e == "peripheral" then
  109.                 wrapSpeakers()
  110.             end
  111.         end
  112.     end,
  113.     function()
  114.         speaker:setSound("rubber_duck:rubber_duck_use")
  115.         while true do
  116.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  117.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  118.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  119.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  120.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  121.  
  122.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  123.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  124.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  125.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  126.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  127.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  128.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  129.  
  130.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  131.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  132.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  133.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  134.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  135.             speaker:playChord(notes.E2,chords.dom7,durations.sixteenth)
  136.             speaker:playChord(notes.E2,chords.dom7,durations.eighth)
  137.  
  138.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  139.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  140.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  141.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  142.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  143.             speaker:playChord(notes.D2,chords.maj7,durations.sixteenth)
  144.             speaker:playChord(notes.D2,chords.maj7,durations.eighth)
  145.  
  146.             speaker:playChord(notes.A1,chords.maj7,durations.eighth)
  147.  
  148.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  149.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  150.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  151.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  152.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  153.  
  154.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  155.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  156.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  157.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  158.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  159.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  160.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  161.  
  162.             speaker:playChord(notes.E2,chords.dom7,durations.eighth)
  163.  
  164.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  165.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  166.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  167.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  168.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  169.  
  170.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  171.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  172.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  173.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  174.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  175.             speaker:playChord(notes.B1,chords.dom7,durations.sixteenth)
  176.             speaker:playChord(notes.B1,chords.dom7,durations.eighth)
  177.  
  178.             speaker:playChord(notes.A1,chords.maj7,durations.eighth)
  179.         end
  180.     end
  181. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement