Skip to main content

Input

Struct Input 

Source
pub struct Input { /* private fields */ }
Expand description

What a track plays, given to Call::play or Call::enqueue.

  • Ogg/Opus (.opus and .ogg files with Opus inside, as ffmpeg -c:a libopus or opusenc make 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

Source

pub fn ogg(reader: impl AsyncRead + Send + Unpin + 'static) -> Input

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)));
Source

pub async fn ogg_file(path: impl AsRef<Path>) -> Result<Input, 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?);
Source

pub fn pcm(reader: impl AsyncRead + Send + Unpin + 'static) -> Input

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)));
Source

pub fn ffmpeg(source: impl AsRef<OsStr>) -> Input

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§

Source§

impl Debug for Input

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Classify for T

§

type Classified = T

§

fn classify(self) -> T

§

impl<T> Declassify for T

§

type Declassified = T

§

fn declassify(self) -> T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more