Skip to main content

Crate symphonium

Crate symphonium 

Source
Expand description

An unofficial easy-to-use wrapper around Symphonia for loading audio files. It also handles resampling at load-time.

The resulting DecodedAudio resources are stored in their native sample format whenever possible to save on memory, and have convenience methods to fill a buffer with f32 samples from any arbitrary position in the resource in realtime during playback. Alternatively you can use the DecodedAudioF32 resource if you only need samples in the f32 format.

§Example

// A struct used to load audio files.
let target_sample_rate = NonZeroU32::new(44100).unwrap();

// An optional cache to re-use decoders and resamplers.
let cache = SymphoniumCache::default();

// Probe the audio file.
let probed = symphonium::probe_from_file(
    &file_path,
    // A custom codec prober. Set to `None` to use the default one from symphonia.
    None,
)
.unwrap();

// Decode the probed data.
let audio_data = symphonium::decode(
    probed,
    &DecodeConfig::default(),
    // Set to `None` to keep the original sample rate of the file.
    Some(target_sample_rate),
    // Set to `None` if no cache is needed.
    Some(&cache),
    // A custom codec registry. Set to `None` to use the default one from symphonia.
    None,
)
.unwrap();

// Fill a stereo buffer with samples starting at frame 100.
let mut buf_l = vec![0.0f32; 512];
let mut buf_r = vec![0.0f32; 512];
audio_data.fill_stereo(100, &mut buf_l, &mut buf_r);

// Alternatively, if you don't need to save memory, you can
// decode directly to an `f32` format.
let probed = symphonium::probe_from_file(&file_path, None).unwrap();
let audio_data_f32 = symphonium::decode_f32(
        probed,
        &DecodeConfig::default(),
        Some(target_sample_rate),
        Some(&cache),
        None,
    )
    .unwrap();

// Print info about the data (`data` is a `Vec<Vec<f32>>`).
println!("num channels: {}", audio_data_f32.data.len());
println!("num frames: {}", audio_data_f32.data[0].len());

§Features

By default, only wav and ogg support is enabled. If you need more formats, enable them as features in your Cargo.toml file like this:

symphonium = { version = "0.8", features = ["mp3", "flac"] }

Available codecs:

  • aac
  • adpcm
  • alac
  • flac
  • mp1
  • mp2
  • mp3
  • pcm
  • vorbis

Available container formats:

  • caf
  • isomp4
  • mkv
  • ogg
  • aiff
  • wav

Alternatively you can enable the all feature if you want everything, or the open-standards feature if you want all of the royalty-free open-source standards.

Re-exports§

pub use symphonia;

Modules§

cache
error
resample

Structs§

DecodeConfig
Additional settings for decoding audio data.
DecodeStretchedConfig
Additional settings for stretching audio data.
DecodedAudio
A resource of raw audio samples stored in deinterleaved format.
DecodedAudioF32
A resource of raw f32 audio samples stored in deinterleaved format.
FillChannelError
An error occured while using DecodedAudio::fill_channel.
ProbedAudioSource
An audio source which has had its metadata probed, but has not been decoded yet.

Enums§

DecodedAudioType
The format of the raw audio samples stored in deinterleaved format.
ResampleQuality
The quality of the resampling algorithm used for a PacketResampler or a Resampler created with resampler_from_quality.

Statics§

DEFAULT_MAX_BYTES
The default maximum size of an audio file in bytes.

Functions§

decode
Decode a probed audio source, and resample if the source sample rate does not match the given target sample rate.
decode_f32
Decode the probed audio source and convert to an f32 sample format.
decode_stretched
Decode the probed audio source and convert to an f32 sample format. The sample will be stretched (pitch/time shifted) by the given amount.
probe_from_file
Load an audio file from the given path and probe its metadata.
probe_from_source
Load an audio source from RAM and probe its metadata.