Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft + Computronics Sound System

Sep 1st, 2024 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. -- Initialize Peripherals
  2. local monitor = peripheral.find("monitor")
  3. local tape = peripheral.find("tape_drive")
  4.  
  5. if not monitor or not tape then
  6.     error("Missing peripheral(s): Make sure monitor and tape drive are connected.")
  7. end
  8.  
  9. monitor.setTextScale(0.5)
  10. monitor.setBackgroundColor(colors.black)
  11. monitor.clear()
  12.  
  13. -- Function to draw the audio visualizer
  14. local function drawVisualizer(progress)
  15.     local width, height = monitor.getSize()
  16.     local numBars = width
  17.     local barHeight = math.floor(height * progress)
  18.    
  19.     for i = 1, numBars do
  20.         for j = 1, height do
  21.             monitor.setCursorPos(i, height - j + 1)
  22.             if j <= barHeight then
  23.                 monitor.setBackgroundColor(colors.green)
  24.             else
  25.                 monitor.setBackgroundColor(colors.black)
  26.             end
  27.             monitor.write(" ")
  28.         end
  29.     end
  30. end
  31.  
  32. -- Function to play the tape and visualize the "audio"
  33. local function playAndVisualize()
  34.     tape.play()
  35.     local length = tape.getSize()
  36.  
  37.     while true do
  38.         local position = tape.getPosition()
  39.         if position >= length then
  40.             break
  41.         end
  42.         local progress = position / length
  43.         drawVisualizer(progress)
  44.         os.sleep(0.1)
  45.     end
  46.    
  47.     monitor.clear()
  48. end
  49.  
  50. -- Function to handle tape switching
  51. local function switchTape()
  52.     print("Ejecting tape... Please insert a new tape and press Enter.")
  53.     tape.stop()
  54.     tape.eject()
  55.     read() -- Wait for user input to insert new tape
  56.     tape.play()
  57.     playAndVisualize()
  58. end
  59.  
  60. -- Main function to run the player
  61. local function main()
  62.     while true do
  63.         print("Playing tape... Press 'S' to stop and switch tape.")
  64.         playAndVisualize()
  65.  
  66.         -- Wait for the user to press a key
  67.         local event, key = os.pullEvent("key")
  68.         if key == keys.s then
  69.             switchTape()
  70.         end
  71.     end
  72. end
  73.  
  74. -- Start the main function
  75. main()
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement