SHOW:
|
|
- or go back to the newest paste.
| 1 | local dfpwm = require("cc.audio.dfpwm")
| |
| 2 | local speakers = { peripheral.find("speaker") }
| |
| 3 | local drive = peripheral.find("drive")
| |
| 4 | local decoder = dfpwm.make_decoder() | |
| 5 | ||
| 6 | local menu = require "menu" | |
| 7 | ||
| 8 | local uri = nil | |
| 9 | local volume = settings.get("media_center.volume")
| |
| 10 | local selectedSong = nil | |
| 11 | ||
| 12 | if drive == nil or not drive.isDiskPresent() then | |
| 13 | local savedSongs = fs.list("songs/")
| |
| 14 | ||
| 15 | if #savedSongs == 0 then | |
| 16 | error("ERR - No disk was found in the drive, or no drive was found. No sound files were found saved to device.")
| |
| 17 | else | |
| 18 | local entries = {
| |
| 19 | [1] = {
| |
| 20 | label = "[CANCEL]", | |
| 21 | callback = function() | |
| 22 | error() | |
| 23 | end | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | for i, fp in ipairs(savedSongs) do | |
| 28 | table.insert(entries, {
| |
| 29 | label = fp:match("^([^.]+)"),
| |
| 30 | callback = function() | |
| 31 | selectedSong = fp | |
| 32 | ||
| 33 | menu.exit() | |
| 34 | end | |
| 35 | }) | |
| 36 | end | |
| 37 | ||
| 38 | menu.init({
| |
| 39 | main = {
| |
| 40 | entries = entries | |
| 41 | } | |
| 42 | }) | |
| 43 | ||
| 44 | menu.thread() | |
| 45 | ||
| 46 | if selectedSong ~= nil then | |
| 47 | local fp = "songs/" .. selectedSong | |
| 48 | ||
| 49 | if fs.exists(fp) then | |
| 50 | local file = fs.open(fp, "r") | |
| 51 | ||
| 52 | uri = file.readAll() | |
| 53 | ||
| 54 | file.close() | |
| 55 | else | |
| 56 | print("Song was not found on device!")
| |
| 57 | ||
| 58 | return | |
| 59 | end | |
| 60 | else error() end | |
| 61 | end | |
| 62 | else | |
| 63 | local songFile = fs.open("disk/song.txt", "r")
| |
| 64 | uri = songFile.readAll() | |
| 65 | ||
| 66 | songFile.close() | |
| 67 | end | |
| 68 | ||
| 69 | --if uri == nil or not uri:find("^https") then
| |
| 70 | --print("ERR - Invalid URI!")
| |
| 71 | --return | |
| 72 | --end | |
| 73 | ||
| 74 | function playChunk(chunk) | |
| 75 | local returnValue = nil | |
| 76 | local callbacks = {}
| |
| 77 | ||
| 78 | for i, speaker in pairs(speakers) do | |
| 79 | if i > 1 then | |
| 80 | table.insert(callbacks, function() | |
| 81 | speaker.playAudio(chunk, volume or 1.0) | |
| 82 | end) | |
| 83 | else | |
| 84 | table.insert(callbacks, function() | |
| 85 | returnValue = speaker.playAudio(chunk, volume or 1.0) | |
| 86 | end) | |
| 87 | end | |
| 88 | end | |
| 89 | ||
| 90 | parallel.waitForAll(table.unpack(callbacks)) | |
| 91 | ||
| 92 | return returnValue | |
| 93 | end | |
| 94 | ||
| 95 | print("Playing '" .. selectedSong or drive.getDiskLabel() .. "' at volume " .. (volume or 1.0))
| |
| 96 | ||
| 97 | local quit = false | |
| 98 | ||
| 99 | function play() | |
| 100 | while true do | |
| 101 | - | --local response = http.get(uri, nil, true) |
| 101 | + | |
| 102 | - | |
| 102 | + | |
| 103 | - | local response = uri |
| 103 | + | |
| 104 | - | local chunkSize = 4 * 1024 |
| 104 | + | local speaker = peripheral.find("speaker")
|
| 105 | - | local chunk = response.read(chunkSize) |
| 105 | + | for input in io.lines("songs/"..selectedSong, 16 * 1024) do
|
| 106 | - | while chunk ~= nil do |
| 106 | + | local decoded = decoder(input) |
| 107 | - | local buffer = decoder(chunk) |
| 107 | + | while not speaker.playAudio(decoded) do |
| 108 | os.pullEvent("speaker_audio_empty")
| |
| 109 | - | while not playChunk(buffer) do |
| 109 | + | end |
| 110 | - | os.pullEvent("speaker_audio_empty")
|
| 110 | + | |
| 111 | end | |
| 112 | end | |
| 113 | - | chunk = response.read(chunkSize) |
| 113 | + | |
| 114 | function readUserInput() | |
| 115 | local commands = {
| |
| 116 | ["stop"] = function() | |
| 117 | quit = true | |
| 118 | end | |
| 119 | } | |
| 120 | ||
| 121 | while true do | |
| 122 | local input = string.lower(read()) | |
| 123 | local commandName = "" | |
| 124 | local cmdargs = {}
| |
| 125 | ||
| 126 | local i = 1 | |
| 127 | for word in input:gmatch("%w+") do
| |
| 128 | if i > 1 then | |
| 129 | table.insert(cmdargs, word) | |
| 130 | else | |
| 131 | commandName = word | |
| 132 | end | |
| 133 | end | |
| 134 | ||
| 135 | local command = commands[commandName] | |
| 136 | ||
| 137 | if command ~= nil then | |
| 138 | command(table.unpack(cmdargs)) | |
| 139 | else print('"' .. cmdargs[1] .. '" is not a valid command!') end
| |
| 140 | end | |
| 141 | end | |
| 142 | ||
| 143 | function waitForQuit() | |
| 144 | while not quit do | |
| 145 | sleep(0.1) | |
| 146 | end | |
| 147 | end | |
| 148 | ||
| 149 | parallel.waitForAny(play, readUserInput, waitForQuit) | |
| 150 |