pub struct Input { /* private fields */ }Expand description
What a track plays, given to Call::play or
Call::enqueue.
- Ogg/Opus (
.opusand.oggfiles with Opus inside, asffmpeg -c:a libopusoropusencmake them), from a file (ogg_file) or from any [AsyncRead] (ogg): a pipe, a socket, an HTTP body, memory. At volume 1.0 its packets are sent as they are, without decoding: Discord wants 48 kHz Opus, which Ogg/Opus always is. - Raw PCM (
pcm): 16-bit little-endian, 48 kHz, interleaved stereo, encoded to Opus by the call. - Anything FFmpeg reads (
ffmpeg): MP3, FLAC, WAV, AAC, video files, HTTP(S) URLs, … FFmpeg must be installed; it decodes, djs-voice encodes.
The input is read ahead of playback, at most about a second, so a slow source makes the
track skip rather than the call stall. A track from a file or FFmpeg can loop
(TrackHandle::set_loop) and seek
(TrackHandle::seek), as they can be opened again; a track
from a reader cannot (D-360).
discord.js counterpart: createAudioResource of @discordjs/voice; songbird: Input.
§Examples
use djs_voice::Input;
call.play(Input::ogg_file("music/intro.opus").await?);
call.enqueue(Input::ffmpeg("music/song.mp3"));Implementations§
Source§impl Input
impl Input
Sourcepub fn ogg(reader: impl AsyncRead + Send + Unpin + 'static) -> Self
pub fn ogg(reader: impl AsyncRead + Send + Unpin + 'static) -> Self
Ogg/Opus audio read from reader.
Nothing is read until the track plays; an input that turns out not to be Ogg/Opus
(or fails to read) ends its track then. ogg_file checks a file at
once. The track cannot loop or seek (D-360).
discord.js counterpart: createAudioResource(stream, { inputType: StreamType.OggOpus }).
§Examples
use djs_voice::Input;
// Any `AsyncRead`: here bytes already in memory.
let bytes: Vec<u8> = download().await;
call.play(Input::ogg(std::io::Cursor::new(bytes)));Sourcepub async fn ogg_file(path: impl AsRef<Path>) -> Result<Self, InputError>
pub async fn ogg_file(path: impl AsRef<Path>) -> Result<Self, InputError>
Ogg/Opus audio from the file at path, opened and checked now: the result is an
error if it cannot be read or does not start like Ogg/Opus. The track can loop and
seek: the file is opened again for them.
discord.js counterpart: createAudioResource(createReadStream(path), { inputType: StreamType.OggOpus }).
§Errors
InputError::Io when the file cannot be opened or read, InputError::NotOgg or
InputError::NotOpus when it is not Ogg/Opus.
§Examples
let track = call.play(djs_voice::Input::ogg_file("intro.opus").await?);Sourcepub fn pcm(reader: impl AsyncRead + Send + Unpin + 'static) -> Self
pub fn pcm(reader: impl AsyncRead + Send + Unpin + 'static) -> Self
Raw PCM read from reader: 16-bit signed little-endian samples, 48 kHz, two
interleaved channels (left, right), as ffmpeg -f s16le -ar 48000 -ac 2 writes it.
The call encodes it to Opus; a last frame shorter than 20 ms is completed with
silence. The track cannot loop or seek (D-360).
discord.js counterpart: createAudioResource(stream, { inputType: StreamType.Raw }).
§Examples
// One second of silence: 48 000 samples × 2 channels × 2 bytes.
let silence = vec![0_u8; 192_000];
call.play(djs_voice::Input::pcm(std::io::Cursor::new(silence)));Sourcepub fn ffmpeg(source: impl AsRef<OsStr>) -> Self
pub fn ffmpeg(source: impl AsRef<OsStr>) -> Self
Anything FFmpeg can read: a file path or a URL (https://…, with reconnection on
errors), in any format it decodes; video files play their audio.
ffmpeg is run from PATH when the track starts to play (not now, so a queue of
tracks does not run a program for each), and killed when the track ends. Its errors
end the track (TrackEnd::Failed): no program
(InputError::FfmpegMissing), or a source it cannot read
(InputError::Ffmpeg). The track can loop and seek: FFmpeg is run again for them.
discord.js counterpart: createAudioResource(path) (StreamType.Arbitrary, through
prism-media’s FFmpeg); songbird: File and HttpRequest inputs.
§Examples
use djs_voice::Input;
call.enqueue(Input::ffmpeg("music/song.mp3"));
call.enqueue(Input::ffmpeg("https://example.com/radio.aac"));Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Input
impl !Sync for Input
impl !UnwindSafe for Input
impl Freeze for Input
impl Send for Input
impl Unpin for Input
impl UnsafeUnpin for Input
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Classify for T
impl<T> Classify for T
type Classified = T
fn classify(self) -> T
§impl<T> Declassify for T
impl<T> Declassify for T
type Declassified = T
fn declassify(self) -> T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more