Add arbitrary audio support to speakers (#982)
Speakers can now play arbitrary PCM audio, sampled at 48kHz and with a
resolution of 8 bits. Programs can build up buffers of audio locally,
play it using `speaker.playAudio`, where it is encoded to DFPWM, sent
across the network, decoded, and played on the client.
`speaker.playAudio` may return false when a chunk of audio has been
submitted but not yet sent to the client. In this case, the program
should wait for a speaker_audio_empty event and try again, repeating
until it works.
While the API is a little odd, this gives us fantastic flexibility (we
can play arbitrary streams of audio) while still being resilient in the
presence of server lag (either TPS or on the computer thread).
Some other notes:
- There is a significant buffer on both the client and server, which
means that sound take several seconds to finish after playing has
started. One can force it to be stopped playing with the new
`speaker.stop` call.
- This also adds a `cc.audio.dfpwm` module, which allows encoding and
decoding DFPWM1a audio files.
- I spent so long writing the documentation for this. Who knows if it'll
be helpful!
2021-12-13 22:56:59 +00:00
|
|
|
---
|
|
|
|
module: [kind=event] speaker_audio_empty
|
|
|
|
see: speaker.playAudio To play audio using the speaker
|
|
|
|
---
|
|
|
|
|
|
|
|
## Return Values
|
|
|
|
1. @{string}: The event name.
|
|
|
|
2. @{string}: The name of the speaker which is available to play more audio.
|
|
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
This uses @{io.lines} to read audio data in blocks of 16KiB from "example_song.dfpwm", and then attempts to play it
|
|
|
|
using @{speaker.playAudio}. If the speaker's buffer is full, it waits for an event and tries again.
|
|
|
|
|
2021-12-21 22:20:45 +00:00
|
|
|
```lua {data-peripheral=speaker}
|
Add arbitrary audio support to speakers (#982)
Speakers can now play arbitrary PCM audio, sampled at 48kHz and with a
resolution of 8 bits. Programs can build up buffers of audio locally,
play it using `speaker.playAudio`, where it is encoded to DFPWM, sent
across the network, decoded, and played on the client.
`speaker.playAudio` may return false when a chunk of audio has been
submitted but not yet sent to the client. In this case, the program
should wait for a speaker_audio_empty event and try again, repeating
until it works.
While the API is a little odd, this gives us fantastic flexibility (we
can play arbitrary streams of audio) while still being resilient in the
presence of server lag (either TPS or on the computer thread).
Some other notes:
- There is a significant buffer on both the client and server, which
means that sound take several seconds to finish after playing has
started. One can force it to be stopped playing with the new
`speaker.stop` call.
- This also adds a `cc.audio.dfpwm` module, which allows encoding and
decoding DFPWM1a audio files.
- I spent so long writing the documentation for this. Who knows if it'll
be helpful!
2021-12-13 22:56:59 +00:00
|
|
|
local dfpwm = require("cc.audio.dfpwm")
|
|
|
|
local speaker = peripheral.find("speaker")
|
|
|
|
|
|
|
|
local decoder = dfpwm.make_decoder()
|
|
|
|
for chunk in io.lines("data/example.dfpwm", 16 * 1024) do
|
|
|
|
local buffer = decoder(chunk)
|
|
|
|
|
|
|
|
while not speaker.playAudio(buffer) do
|
|
|
|
os.pullEvent("speaker_audio_empty")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|