Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Allows playing music on multiple speakers simultaneously
- local speakers = {} -- Table to hold connected speakers
- -- Function to add a speaker to the system
- function addSpeaker(speaker)
- table.insert(speakers, speaker)
- end
- -- Function to play a note on a specific speaker
- function playNoteOnSpeaker(speaker, note, duration)
- speaker.playSound(note)
- sleep(duration) -- Sleep for the duration of the note
- speaker.playSound("minecraft:block.note_block.harp", 0) -- Stop the sound by playing a silent note
- end
- -- Function to play notes on all connected speakers simultaneously
- function playMusicOnSpeakers(music)
- local coroutines = {} -- Table to hold coroutines
- -- Create a coroutine for each speaker
- for i, notes in ipairs(music) do
- local speaker = speakers[i] -- Get the corresponding speaker
- if speaker then
- coroutines[i] = coroutine.create(function()
- for _, noteInfo in ipairs(notes) do
- playNoteOnSpeaker(speaker, noteInfo.note, noteInfo.duration)
- end
- end)
- end
- end
- -- Start all coroutines
- for _, co in ipairs(coroutines) do
- coroutine.resume(co)
- end
- end
- -- Example usage:
- -- Connect speakers to your computer and add them to the system
- addSpeaker(peripheral.wrap("top"))
- addSpeaker(peripheral.wrap("bottom"))
- -- Define your music notes and durations for each speaker
- local music = {
- {
- {note = "minecraft:block.note_block.harp", duration = 0.5}, -- Note for the first speaker
- {note = "minecraft:block.note_block.snare", duration = 0.5}, -- Note for the second speaker
- },
- {
- {note = "minecraft:block.note_block.flute", duration = 0.5}, -- Note for the first speaker
- {note = "minecraft:block.note_block.basedrum", duration = 0.5}, -- Note for the second speaker
- },
- }
- -- Play the music on all connected speakers simultaneously
- playMusicOnSpeakers(music)
Advertisement
Add Comment
Please, Sign In to add comment