Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Audioplayer script by GroupXyz
- -- Version: 2.0
- -- Play audio files!
- function loadFileList(interface)
- local fileList = {}
- for _, file in ipairs(fs.list(interface)) do
- if not fs.isDir(interface.."/"..file) then
- table.insert(fileList, file)
- end
- end
- return fileList
- end
- function displayFileList(fileList)
- term.clear()
- term.setCursorPos(1, 1)
- print("----------Audioplayer by GroupXyz----------")
- print("Please place your audio files in the folder 'sounds/'")
- print("Files:")
- for i, file in ipairs(fileList) do
- print(i..". "..file)
- end
- end
- function playFile(interface, selectedFile, loop)
- local filePath = interface.."/"..selectedFile
- if fs.exists(filePath) and not fs.isDir(filePath) then
- local speaker = peripheral.find("speaker")
- if speaker then
- local dfpwm = require("cc.audio.dfpwm")
- local decoder = dfpwm.make_decoder()
- local function playBuffer(buffer)
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- if loop then
- while true do
- for chunk in io.lines(filePath, 16 * 1024) do
- local buffer = decoder(chunk)
- playBuffer(buffer)
- sleep(0.05)
- end
- end
- else
- for chunk in io.lines(filePath, 16 * 1024) do
- local buffer = decoder(chunk)
- playBuffer(buffer)
- sleep(0.05)
- end
- end
- print("File played successfully.")
- else
- print("Speaker not found.")
- end
- else
- print("File not found.")
- end
- end
- function createInterface(interface)
- if not fs.exists(interface) then
- fs.makeDir(interface)
- end
- end
- -- Main program
- local interface = "sounds/"
- createInterface(interface)
- local fileList = loadFileList(interface)
- while true do
- displayFileList(fileList)
- print("Which file do you want to play? (Type 'repeat' to loop, 0 to exit)")
- local choice = read()
- if choice == "0" then
- break
- elseif choice == "repeat" then
- print("Enter the file number to repeat:")
- local repeatChoice = tonumber(read())
- if fileList[repeatChoice] then
- playFile(interface, fileList[repeatChoice], true)
- else
- print("Invalid selection.")
- end
- elseif tonumber(choice) and fileList[tonumber(choice)] then
- playFile(interface, fileList[tonumber(choice)], false)
- else
- print("Invalid selection.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement