Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize Peripherals
- local monitor = peripheral.find("monitor")
- local tape = peripheral.find("tape_drive")
- if not monitor or not tape then
- error("Missing peripheral(s): Make sure monitor and tape drive are connected.")
- end
- monitor.setTextScale(0.5)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- Function to draw the audio visualizer
- local function drawVisualizer(progress)
- local width, height = monitor.getSize()
- local numBars = width
- local barHeight = math.floor(height * progress)
- for i = 1, numBars do
- for j = 1, height do
- monitor.setCursorPos(i, height - j + 1)
- if j <= barHeight then
- monitor.setBackgroundColor(colors.green)
- else
- monitor.setBackgroundColor(colors.black)
- end
- monitor.write(" ")
- end
- end
- end
- -- Function to play the tape and visualize the "audio"
- local function playAndVisualize()
- tape.play()
- local length = tape.getSize()
- while true do
- local position = tape.getPosition()
- if position >= length then
- break
- end
- local progress = position / length
- drawVisualizer(progress)
- os.sleep(0.1)
- end
- monitor.clear()
- end
- -- Function to handle tape switching
- local function switchTape()
- print("Ejecting tape... Please insert a new tape and press Enter.")
- tape.stop()
- tape.eject()
- read() -- Wait for user input to insert new tape
- tape.play()
- playAndVisualize()
- end
- -- Main function to run the player
- local function main()
- while true do
- print("Playing tape... Press 'S' to stop and switch tape.")
- playAndVisualize()
- -- Wait for the user to press a key
- local event, key = os.pullEvent("key")
- if key == keys.s then
- switchTape()
- end
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement