JacobHodgkins420

MultiSpeaker

Feb 18th, 2024 (edited)
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. -- Allows playing music on multiple speakers simultaneously
  2.  
  3. local speakers = {} -- Table to hold connected speakers
  4.  
  5. -- Function to add a speaker to the system
  6. function addSpeaker(speaker)
  7. table.insert(speakers, speaker)
  8. end
  9.  
  10. -- Function to play a note on a specific speaker
  11. function playNoteOnSpeaker(speaker, note, duration)
  12. speaker.playSound(note)
  13. sleep(duration) -- Sleep for the duration of the note
  14. speaker.playSound("minecraft:block.note_block.harp", 0) -- Stop the sound by playing a silent note
  15. end
  16.  
  17. -- Function to play notes on all connected speakers simultaneously
  18. function playMusicOnSpeakers(music)
  19. local coroutines = {} -- Table to hold coroutines
  20.  
  21. -- Create a coroutine for each speaker
  22. for i, notes in ipairs(music) do
  23. local speaker = speakers[i] -- Get the corresponding speaker
  24. if speaker then
  25. coroutines[i] = coroutine.create(function()
  26. for _, noteInfo in ipairs(notes) do
  27. playNoteOnSpeaker(speaker, noteInfo.note, noteInfo.duration)
  28. end
  29. end)
  30. end
  31. end
  32.  
  33. -- Start all coroutines
  34. for _, co in ipairs(coroutines) do
  35. coroutine.resume(co)
  36. end
  37. end
  38.  
  39. -- Example usage:
  40. -- Connect speakers to your computer and add them to the system
  41. addSpeaker(peripheral.wrap("top"))
  42. addSpeaker(peripheral.wrap("bottom"))
  43.  
  44. -- Define your music notes and durations for each speaker
  45. local music = {
  46. {
  47. {note = "minecraft:block.note_block.harp", duration = 0.5}, -- Note for the first speaker
  48. {note = "minecraft:block.note_block.snare", duration = 0.5}, -- Note for the second speaker
  49. },
  50. {
  51. {note = "minecraft:block.note_block.flute", duration = 0.5}, -- Note for the first speaker
  52. {note = "minecraft:block.note_block.basedrum", duration = 0.5}, -- Note for the second speaker
  53. },
  54. }
  55.  
  56. -- Play the music on all connected speakers simultaneously
  57. playMusicOnSpeakers(music)
  58.  
Advertisement
Add Comment
Please, Sign In to add comment