|0.8.0||Jun 22, 2018|
|0.6.0||Feb 8, 2018|
|0.5.2||Aug 22, 2017|
|0.5.1||Jul 11, 2017|
|0.3.1||Oct 23, 2015|
#3 in Audio
5,160 downloads per month
Rust playback library.
- Playback is handled by cpal.
- MP3 decoding is handled by minimp3.
- WAV decoding is handled by hound.
- Vorbis decoding is handled by lewton.
- Flac decoding is handled by claxon.
The documentation contains an introduction to the library.
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
- Choose an output with the
play_raw function expects the source to produce
f32s, which may not be the case. If you
get a compilation error, try calling
.convert_samples() on the source to fix it.
For example, here is how you would play an audio file:
use std::fs::File; use std::io::BufReader; use rodio::Source; let device = rodio::default_output_device().unwrap(); let file = File::open("sound.ogg").unwrap(); let source = rodio::Decoder::new(BufReader::new(file)).unwrap(); rodio::play_raw(&device, source.convert_samples());
In order to make it easier to control the playback, the rodio library also provides a type
Sink which represents an audio track.
use rodio::Sink; let device = rodio::default_output_device().unwrap(); let sink = Sink::new(&device); // Add a dummy source of the sake of the example. let source = rodio::source::SineWave::new(440); sink.append(source);
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 type also provides utilities such as playing/pausing or
controlling the volume.
Source trait provides various filters, similarly to the
use rodio::Source; use std::time::Duration; // Repeats the first five seconds of the sound forever. # let source = rodio::source::SineWave::new(440); let source = source.take_duration(Duration::from_secs(5)).repeat_infinite();
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 the number of sinks that can be created (except for the fact that creating too many will slow down your program).
- cgmath 0.14
- cpal 0.8
- lazy_static 1.0.0
- flac claxon 0.3.0
- wav hound 3.3.1
- vorbis lewton 0.5
- mp3 minimp3 0.3.0