Expand description
phonic is a cross-platform audio playback and DSP library for Rust. It provides a flexible, low-latency audio engine and related tools for desktop and web-based music applications
§Overview
-
Playeris the central component that manages audio playback. It takes an output device instance, plays sounds and manages DSP effects. -
OutputDevicerepresents the audio backend stream. phonic provides implementations for different platforms, such ascpalfor native applications andwebauddiofor WebAssembly. TheDefaultOutputDeviceis an alias for the recommended output device for the current build target. -
Sourceproduces audio signals. You can use the built-inFileSourcefor playing back audio files,SynthSourcefor generating synthesized tones or can create your own source implementation. File sources can be preloaded into memory or streamed on-the-fly. -
Effectapplies DSP effects to audio signals signals. By default, only one mixer is present in the player and will route all sources through it. To create more complex routings you can create sub-mixers viaPlayer::add_mixerand route sources to them. Each mixer instance has its own chain of DSP effects, which can be set up viaPlayer::add_effect. You can use the built in effects impls ineffectsor create your own ones.
§Getting Started
Here’s a basic example of how to play audio files with DSP effects.
use phonic::{
DefaultOutputDevice, Player, FilePlaybackOptions, Error,
effects::{ChorusEffect, ReverbEffect, CompressorEffect},
};
fn main() -> Result<(), Error> {
// Create a player with the default audio output device.
let mut player = Player::new(DefaultOutputDevice::open()?, None);
// Add a new sub-mixer with a chorus and reverb effect to the main mixer.
let sub_mixer_id = {
let mixer_id = player.add_mixer(None)?;
player.add_effect(ChorusEffect::default(), mixer_id)?;
player.add_effect(ReverbEffect::default(), mixer_id)?;
mixer_id
};
// Add a limiter to the main mixer. All sounds, including the sub mixer's output
// will be routed through this effect now.
player.add_effect(CompressorEffect::new_limiter(), None)?;
// Play a file with default options on the sub mixer. It will start playing immediately
// and will be routed through a chorus and reverb effect and the main mixer's limiter.
player.play_file("path/to/your/file.wav",
FilePlaybackOptions::default().target_mixer(sub_mixer_id)
)?;
// Play a file with default options on the main mixer and schedule it two seconds ahead
// of the current output time. It will be routed through the limiter effect only.
let sample_rate = player.output_sample_rate() as u64;
player.play_file("path/to/your/some_other_file.wav",
FilePlaybackOptions::default().start_at_time(
player.output_sample_frame_position() + 2 * sample_rate)
)?;
// The player's audio output stream runs on a separate thread, so we need to keep
// the main thread alive to hear the audio.
std::thread::sleep(std::time::Duration::from_secs(5));
Ok(())
}For more advanced usage, such as monitoring playback, sequencing source playback or managing
creating more complex mixer graphs, please see the examples in the README.md and the /examples
directory of the repository.
Modules§
- effects
- Set of basic, common DSP
Effectimplementations. - outputs
- Default
OutputDeviceimplementations. - parameters
- Effect
Parameterimplementations. - sources
- Set of basic, common File & Synth tone
Sourceimplementations. - utils
- DSP tools, musical unit conversion functions and related UI tools.
Structs§
- File
Playback Options - Options to control playback of a
FileSource. - Player
- Playback controller, which continuously fills an
OutputDevices stream with audio data generated by one or moreSources. - Source
Time - Timing info for
Sourceimpls. - Synth
Playback Options - Options to control playback of a
SynthSource.
Enums§
- Error
- Provides an enumeration of all possible errors reported by phonic.
- File
Playback Message - Events to control playback of a
FileSource - Parameter
Scaling - Effect parameter scaling for float parameters, applied to convert normalized UI or automation values to the internal values.
- Parameter
Type - Describes the type of a
Parameterto e.g. select a proper visual representation in a UI. - Parameter
Value Update - An update for a
Parameter’s value, consumed byEffects in audio time. - Playback
Status Event - Events send back from File or Synth sources via the player to the user.
- Resampling
Quality - The resampler quality an audio source should be resampled with.
- Synth
Playback Message - Events to control playback of a synth source
Traits§
- Clonable
Parameter - Allows creating
dyn Parameterclones. - Effect
- Effects manipulate audio samples in
f32format and can beSendandSynced across threads. - Effect
Message - Carries
Effectspecific payloads/automation, which can’t or should not be expressed asParameter. - File
Source - A source which decodes and plays back an audio file.
- Output
Device - Audio output stream device.
- Parameter
- Describes a single parameter of an
Effectfor use in UIs or for automation. - Source
- Source types produce audio samples in
f32format and can beSendandSynced across threads. - Synth
Source - A source which creates samples from a synthesized signal.
Type Aliases§
- Default
Output Device cpal-outputorweb-output - The enabled audio output type: cpal or web
- Effect
Id - Unique ID for newly added effects.
- Effect
Message Payload - Type used in
Effect::process_messageto receive messages. - Effect
Time - Frame and wall-clock time reference for an audio effect’s process function.
- MixerId
- Unique ID for newly added mixers.
- Playback
Id - Unique ID for played back file or synth sources.
- Playback
Status Context - Custom context type for playback status events.