Crate babycat[][src]

Expand description

Babycat is a library that makes it easy to decode and manipulate many audio files at once.

Babycat is built on top of a lot of other Rust and C libraries, such as:

Babycat provides a consistent audio API for performing many tasks. You are viewing Babycat’s Rust documentation, but Babycat also has bindings for C, Python, and WebAssembly.

Terminology

Babycat uses the same audio terminology as the Apple Core Audio API:

  • A channel is a discrete track of monophonic audio. A monophonic stream has one channel. A stereo stream has two channels. An audio stream that contains 5.1 surround sound will have five normal channels and one Low Frequency Enhancement (LFE) channel.
  • A sample is single numerical value in a single audio channel in an audio stream.
  • A frame is a collection of samples from the same point in time–one sample for each channel.
  • The frame rate (or sample rate) for a stream is the number of frames per second (hertz) of uncompressed audio.

Babycat stores audio as a single array of interleaved samples from channels. A waveform with two channels (e.g. left and right) are stored as:

Examples

Decode multiple audio files in parallel.

use babycat::{DecodeArgs, FloatWaveform, Waveform};

// These are test files in the Babycat Git repository.
let filenames = &[
   "audio-for-tests/andreas-theme/track.mp3",
   "audio-for-tests/blippy-trance/track.mp3",
   "audio-for-tests/voxel-revolution/track.mp3",
];

// Perform the following transformations on EACH track.
let decode_args = DecodeArgs {
    // Upsample the audio to 48khz.
    frame_rate_hz: 48000,
    // Average all audio channels into a single monophonic channel.
    convert_to_mono: true,
    // Only select the first 60 seconds of audio.
    end_time_milliseconds: 60000,
    // If a track is shorter than 60 seconds, pad it with silence.
    zero_pad_ending: true,
    ..Default::default()
};
let batch_args = Default::default();

// Read and decode the tracks in parallel.
let batch = babycat::FloatWaveform::from_many_files(
   filenames,
   decode_args,
   batch_args,
);

// Iterate over the results.
for named_result in batch {
    match &named_result.result {
        Ok(waveform) => {
            // Do further processing.
            waveform.interleaved_samples();
        }
        Err(err) => {
            // Handle decoding errors.
        }
    }
}

Modules

resample

This module is not really part of Babycat’s pubic API, but is made public to make benchmarking Babycat internals easier.

Structs

BatchArgs

Configures multithreading in Babycat.

DecodeArgs

Specifies what transformations to apply to the audio during the decoding process.

FloatWaveform

Represents a fixed-length audio waveform as a Vec<f32>.

IntWaveform

Represents a fixed-length audio waveform as a Vec<i16>.

NamedResult

A wrapper for std::result::Result that names each individual result.

Enums

Error

Constants

DEFAULT_CONVERT_TO_MONO
DEFAULT_END_TIME_MILLISECONDS
DEFAULT_FILE_EXTENSION
DEFAULT_FRAME_RATE_HZ
DEFAULT_MIME_TYPE
DEFAULT_NUM_CHANNELS
DEFAULT_NUM_WORKERS
DEFAULT_RESAMPLE_MODE
DEFAULT_START_TIME_MILLISECONDS
DEFAULT_ZERO_PAD_ENDING
RESAMPLE_MODE_BABYCAT
RESAMPLE_MODE_LANCZOS
RESAMPLE_MODE_LIBSAMPLERATE

Traits

Waveform

Methods common to all types of waveforms.