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. 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 ineffectsor create your own.
§Getting Started
Here’s a basic example of how to play audio files with DSP effects.
use std::time::Duration;
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);
// Memorize some consts for event scheduling.
let now = player.output_sample_frame_position();
let samples_per_sec = player.output_sample_rate() as u64;
// Add a new sub-mixer with a chorus and reverb effect to the main mixer.
let sub_mixer_id = {
let new_mixer_id = player.add_mixer(None)?;
player.add_effect(ChorusEffect::default(), new_mixer_id)?;
player.add_effect(ReverbEffect::default(), new_mixer_id)?;
new_mixer_id
};
// Add a limiter to the main mixer. All sounds, including the sub mixer's output,
// will be routed through this effect now.
let limiter = player.add_effect(CompressorEffect::new_limiter(), None)?;
// Change effect parameters via the returned handles.
// Schedule a parameter change 3 seconds from now (sample-accurate)
limiter.set_parameter(CompressorEffect::MAKEUP_GAIN_ID, 3.0, now + 3 * samples_per_sec);
// Play a file and get a handle to control it.
let file = player.play_file("path/to/your/file.wav",
FilePlaybackOptions::default().target_mixer(sub_mixer_id)
)?;
// Change sources via the returned handles.
// Schedule a stop 2 seconds from now (sample-accurate)
file.stop(now + 2 * samples_per_sec)?;
// Play another file on the main mixer with scheduled start time
let some_other_file = player.play_file("path/to/your/some_other_file.wav",
FilePlaybackOptions::default().start_at_time(now + 2 * samples_per_sec)
)?;
// The player's audio output stream runs on a separate thread, so we keep
// the main thread running here, until all files finished playing.
while file.is_playing() || some_other_file.is_playing() {
std::thread::sleep(Duration::from_millis(100));
}
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§
- Effect
Handle - Automate
Effectparameters or send messages. - File
Playback Handle - Change runtime playback properties of a playing
FileSourceand test if a source is still playing. - File
Playback Options - Options to control playback properties 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 Handle - Change runtime playback properties of a playing
SynthSourceand test if a source is still playing. - Synth
Playback Options - Options to control playback properties of a
SynthSource.
Enums§
- Effect
Movement - How to move an effect within a mixer.
- Error
- Provides an enumeration of all possible errors reported by phonic.
- 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.
- Source
Playback Handle - A unified
FilePlaybackHandleandSynthPlaybackHandle. - Synth
Playback Message - Events to control playback of a
SynthSource
Traits§
- Clonable
Parameter - Allows creating
dynParameterclones. - Effect
- Effects manipulate audio samples in
f32format and can beSendandSynced across threads. Buffers are processed in-place in the audio real-time thread. - 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.