Expand description
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:
- Get an output stream handle to a physical device. For example, get a stream to the system’s
default sound device with
OutputStreamBuilder::open_default_stream()
. - 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 theSource
trait. - Add the source to the output stream using
OutputStream::mixer()
on the output stream handle.
Here is a complete example of how you would play an audio file:
use std::fs::File;
use rodio::{Decoder, OutputStream, source::Source};
// Get an output stream handle to the default physical sound device.
// Note that the playback stops when the stream_handle is dropped.//!
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()
.expect("open default audio stream");
let sink = rodio::Sink::connect_new(&stream_handle.mixer());
// Load a sound from a file, using a path relative to Cargo.toml
let file = File::open("examples/music.ogg").unwrap();
// Decode that sound file into a source
let source = Decoder::try_from(file).unwrap();
// Play the sound directly on the device
stream_handle.mixer().add(source);
// The sound plays in a separate audio thread,
// so we need to keep the main thread alive while it's playing.
std::thread::sleep(std::time::Duration::from_secs(5));
rodio::play()
helps to simplify the above
use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, source::Source};
// Get an output stream handle to the default physical sound device.
// Note that the playback stops when the stream_handle is dropped.
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()
.expect("open default audio stream");
// Load a sound from a file, using a path relative to Cargo.toml
let file = BufReader::new(File::open("examples/music.ogg").unwrap());
// Note that the playback stops when the sink is dropped
let sink = rodio::play(&stream_handle.mixer(), file).unwrap();
// The sound plays in a separate audio thread,
// so we need to keep the main thread alive while it's playing.
std::thread::sleep(std::time::Duration::from_secs(5));
§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. Sink
plays its input sources sequentially,
one after another. To play sounds in simultaneously in parallel, use mixer::Mixer
instead.
To play a song Create a Sink
connect it to the output stream,
and .append()
your sound to it.
use std::time::Duration;
use rodio::{OutputStream, Sink};
use rodio::source::{SineWave, Source};
// _stream must live as long as the sink
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()
.expect("open default audio stream");
let sink = rodio::Sink::connect_new(&stream_handle.mixer());
// Add a dummy source of the sake of the example.
let source = SineWave::new(440.0).take_duration(Duration::from_secs_f32(0.25)).amplify(0.20);
sink.append(source);
// The sound plays in a separate thread. This call will block the current thread until the sink
// has finished playing all its queued sounds.
sink.sleep_until_end();
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, similar 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();
§Alternative Decoder Backends
Symphonia is an alternative decoder library that can be used in place of many of the default backends. Currently, the main benefit is that Symphonia is the only backend that supports M4A and AAC, but it may be used to implement additional optional functionality in the future.
To use, enable either the symphonia-all
feature to enable all Symphonia codecs
or enable specific codecs using one of the symphonia-{codec name}
features.
If you enable one or more of the Symphonia codecs, you may want to set default-features = false
in order
to avoid adding extra crates to your binary.
See the available feature flags for all options.
§Optional Features
Rodio provides several optional features that are guarded with feature gates.
§Feature “tracing”
The “tracing” feature replaces the print to stderr when a stream error happens with a recording an error event with tracing.
§Feature “noise”
The “noise” feature adds support for white and pink noise sources. This feature requires the “rand” crate.
§Feature “playback”
The “playback” feature adds support for playing audio. This feature requires the “cpal” crate.
§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 device. 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 on the number of sinks that can be created (except for the fact that creating too many will slow down your program).
Re-exports§
pub use crate::decoder::Decoder;
pub use crate::source::Source;
pub use crate::stream::play;
pub use crate::stream::OutputStream;
pub use crate::stream::OutputStreamBuilder;
pub use crate::stream::PlayError;
pub use crate::stream::StreamError;
pub use cpal;
Modules§
- buffer
- A simple source of samples coming from a buffer.
- conversions
- This module contains functions that convert from one PCM format to another.
- decoder
- Decodes audio samples from various audio file formats.
- math
- Math utilities for audio processing.
- mixer
- Mixer that plays multiple sounds at the same time.
- queue
- Queue that plays sounds one after the other.
- source
- Sources of sound and various filters.
- static_
buffer - A simple source of samples coming from a static buffer.
- stream
- Output audio via the OS via mixers or play directly
Structs§
- Device
- The
Device
implementation associated with the platform’s dynamically dispatchedHost
type. - Devices
- The
Devices
iterator associated with the platform’s dynamically dispatchedHost
type. - Sink
- Handle to a device that outputs sounds.
- Spatial
Sink - A sink that allows changing the position of the source and the listeners
ears while playing. The sources played are then transformed to give a simple
spatial effect. See
Spatial
for details. - Supported
Stream Config - Describes a single supported stream configuration, retrieved via either a
SupportedStreamConfigRange
instance or one of theDevice::default_input/output_config
methods.
Enums§
- Devices
Error - An error that might occur while attempting to enumerate the available devices on a system.
Traits§
- Device
Trait - A device that is capable of audio input and/or output.
Type Aliases§
- Channel
Count - Number of channels in a stream.
- Input
Devices - A host’s device iterator yielding only input devices.
- Output
Devices - A host’s device iterator yielding only output devices.
- Sample
- Represents value of a single sample.
Silence corresponds to the value
0.0
. The expected amplitude range is -1.0…1.0. Values below and above this range are clipped in conversion to other sample types. Use conversion traits from dasp_sample crate or crate::conversions::SampleTypeConverter to convert between sample types if necessary. - Sample
Rate - Stream sample rate (a frame rate or samples per second per channel).