geng_rodio/lib.rs
1//! Audio playback library.
2//!
3//! The main concept of this library is the [`Source`] trait, which
4//! represents a sound (streaming or not). In order to play a sound, there are three steps:
5//!
6//! - Create an object that represents the streaming sound. It can be a sine wave, a buffer, a
7//! [`decoder`], etc. or even your own type that implements the [`Source`] trait.
8//! - Get an output stream handle to a physical device. For example, get a stream to the system's
9//! default sound device with [`OutputStream::try_default()`]
10//! - Call [`.play_raw(source)`](OutputStreamHandle::play_raw) on the output stream handle.
11//!
12//! The [`play_raw`](OutputStreamHandle::play_raw) function expects the source to produce [`f32`]s,
13//! which may not be the case. If you get a compilation error, try calling
14//! [`.convert_samples()`](Source::convert_samples) on the source to fix it.
15//!
16//! For example, here is how you would play an audio file:
17//!
18//! ```no_run
19//! use std::fs::File;
20//! use std::io::BufReader;
21//! use rodio::{Decoder, OutputStream, source::Source};
22//!
23//! // Get a output stream handle to the default physical sound device
24//! let (_stream, stream_handle) = OutputStream::try_default().unwrap();
25//! // Load a sound from a file, using a path relative to Cargo.toml
26//! let file = BufReader::new(File::open("examples/music.ogg").unwrap());
27//! // Decode that sound file into a source
28//! let source = Decoder::new(file).unwrap();
29//! // Play the sound directly on the device
30//! stream_handle.play_raw(source.convert_samples());
31//!
32//! // The sound plays in a separate audio thread,
33//! // so we need to keep the main thread alive while it's playing.
34//! std::thread::sleep(std::time::Duration::from_secs(5));
35//! ```
36//!
37//! ## Sink
38//!
39//! In order to make it easier to control the playback, the rodio library also provides a type
40//! named [`Sink`] which represents an audio track.
41//!
42//! Instead of playing the sound with [`play_raw`](OutputStreamHandle::play_raw), you can add it to
43//! a [`Sink`] instead.
44//!
45//! - Get a [`Sink`] to the output stream, and [`.append()`](Sink::append) your sound to it.
46//!
47//! ```no_run
48//! use std::fs::File;
49//! use std::io::BufReader;
50//! use std::time::Duration;
51//! use rodio::{Decoder, OutputStream, Sink};
52//! use rodio::source::{SineWave, Source};
53//!
54//! let (_stream, stream_handle) = OutputStream::try_default().unwrap();
55//! let sink = Sink::try_new(&stream_handle).unwrap();
56//!
57//! // Add a dummy source of the sake of the example.
58//! let source = SineWave::new(440.0).take_duration(Duration::from_secs_f32(0.25)).amplify(0.20);
59//! sink.append(source);
60//!
61//! // The sound plays in a separate thread. This call will block the current thread until the sink
62//! // has finished playing all its queued sounds.
63//! sink.sleep_until_end();
64//! ```
65//!
66//! The [`append`](Sink::append) method will add the sound at the end of the
67//! sink. It will be played when all the previous sounds have been played. If you want multiple
68//! sounds to play simultaneously, you should create multiple [`Sink`]s.
69//!
70//! The [`Sink`] type also provides utilities such as playing/pausing or controlling the volume.
71//!
72//! ## Filters
73//!
74//! The [`Source`] trait provides various filters, similar to the standard [`Iterator`] trait.
75//!
76//! Example:
77//!
78//! ```
79//! use rodio::Source;
80//! use std::time::Duration;
81//!
82//! // Repeats the first five seconds of the sound forever.
83//! # let source = rodio::source::SineWave::new(440.0);
84//! let source = source.take_duration(Duration::from_secs(5)).repeat_infinite();
85//! ```
86//!
87//! ## Alternative Decoder Backends
88//!
89//! [Symphonia](https://github.com/pdeljanov/Symphonia) is an alternative decoder library that can be used in place
90//! of many of the default backends.
91//! Currently, the main benefit is that Symphonia is the only backend that supports M4A and AAC,
92//! but it may be used to implement additional optional functionality in the future.
93//!
94//! To use, enable either the `symphonia-all` feature to enable all Symphonia codecs
95//! or enable specific codecs using one of the `symphonia-{codec name}` features.
96//! If you enable one or more of the Symphonia codecs, you may want to set `default-features = false` in order
97//! to avoid adding extra crates to your binary.
98//! See the [available feature flags](https://docs.rs/crate/rodio/latest/features) for all options.
99//!
100//! ## How it works under the hood
101//!
102//! Rodio spawns a background thread that is dedicated to reading from the sources and sending
103//! the output to the device. Whenever you give up ownership of a [`Source`] in order to play it,
104//! it is sent to this background thread where it will be read by rodio.
105//!
106//! All the sounds are mixed together by rodio before being sent to the operating system or the
107//! hardware. Therefore there is no restriction on the number of sounds that play simultaneously or
108//! the number of sinks that can be created (except for the fact that creating too many will slow
109//! down your program).
110//!
111#![cfg_attr(test, deny(missing_docs))]
112pub use cpal::{
113 self, traits::DeviceTrait, Device, Devices, DevicesError, InputDevices, OutputDevices,
114 SupportedStreamConfig,
115};
116
117mod conversions;
118mod sink;
119mod spatial_sink;
120mod stream;
121
122pub mod buffer;
123pub mod decoder;
124pub mod dynamic_mixer;
125pub mod queue;
126pub mod source;
127pub mod static_buffer;
128
129pub use crate::conversions::Sample;
130pub use crate::decoder::Decoder;
131pub use crate::sink::Sink;
132pub use crate::source::Source;
133pub use crate::spatial_sink::SpatialSink;
134pub use crate::stream::{OutputStream, OutputStreamHandle, PlayError, StreamError};