Crate rodio [−] [src]
Audio playback library.
The main concept of this library is the Source
trait, which
represents a sound (streaming or not). In order to play a sound, there are three steps:
- Create an object that represents the streaming sound. It can be a sine wave, a buffer, a
decoder, etc. or even your own type that implements
the
Source
trait. - Choose an output which the
get_endpoints_list
orget_default_endpoint
functions. - Call
play_raw(output, source)
.
For example, here is how you would play an audio file:
use std::fs::File; use std::io::BufReader; let endpoint = rodio::get_default_endpoint().unwrap(); let file = File::open("sound.ogg").unwrap(); let source = rodio::Decoder::new(BufReader::new(file)).unwrap(); rodio::play_raw(&endpoint, source);
Sink
In order to make it easier to control the playback, the rodio library also provides a type
named Sink
which represents an audio track.
Instead of playing the sound with play_raw
, you can add it to a
Sink
instead.
use rodio::Sink; let endpoint = rodio::get_default_endpoint().unwrap(); let sink = Sink::new(&endpoint); // Add a dummy source of the sake of the example. let source = rodio::source::SineWave::new(440); sink.append(source);
The append
method will add the sound at the end of the
sink. It will be played when all the previous sounds have been played. If you want multiple
sounds to play simultaneously, you should create multiple Sink
s.
The Sink
type also provides utilities such as playing/pausing or
controlling the volume.
Filters
The Source
trait provides various filters, similarly to the
standard Iterator
trait.
Example:
use rodio::Source; use std::time::Duration; // Repeats the first five seconds of the sound forever. let source = source.take_duration(Duration::from_secs(5)).repeat_infinite();
How it works under the hood
Rodio spawns a background thread that is dedicated to reading from the sources and sending
the output to the endpoint. Whenever you give up ownership of a Source
in order to play it,
it is sent to this background thread where it will be read by rodio.
All the sounds are mixed together by rodio before being sent to the operating system or the hardware. Therefore there is no restriction on the number of sounds that play simultaneously or the number of sinks that can be created (except for the fact that creating too many will slow down your program).
Reexports
pub use decoder::Decoder; |
pub use source::Source; |
Modules
buffer |
A simple source of samples coming from a buffer. |
decoder |
Decodes samples from an audio file. |
dynamic_mixer |
Queue that plays sounds one after the other. |
queue |
Queue that plays sounds one after the other. |
source |
Sources of sound and various filters. |
Structs
Endpoint |
An opaque type that identifies an end point. |
Sink |
Handle to an endpoint that outputs sounds. |
Traits
Sample |
Represents a value of a single sample. |
Functions
get_default_endpoint |
Return the default endpoint, or |
get_endpoints_list |
Return an iterator to the list of formats that are supported by the system. |
play_once |
Plays a sound once. Returns a |
play_raw |
Plays a source to an end point until it ends. |