AudioHelper<T>

Helpers & Utilities · AiDotNet.Helpers

Helper class for loading and saving audio as tensors.

For Beginners

This class converts audio files into tensors for neural networks. Audio is loaded as [channels, samples] or [batch, channels, samples] tensors. Values are normalized to [-1, 1] range by default.

How It Works

Supports common audio formats without external dependencies:

  • WAV: Uncompressed PCM audio (most common for ML)
  • RAW: Raw PCM samples with specified parameters

Methods

Method Summary
AudioLoadResult<T> DecodeWav(byte[] bytes, bool normalize) Decodes a WAV file held in memory (for example the body of a text-to-speech API response).
AudioLoadResult<T> LoadAudio(string filePath, bool normalize, int? targetSampleRate) Loads an audio file and returns it as a tensor with metadata.
AudioLoadResult<T> LoadRaw(string filePath, int sampleRate, int channels, int bitsPerSample, bool normalize) Loads raw PCM audio data.
AudioLoadResult<T> LoadWav(Stream stream) Calls the fully specified overload with the omitted parameters’ defaults.
AudioLoadResult<T> LoadWav(Stream stream, bool normalize) Reads a WAV file from a stream: PCM (8, 16, 24, 32 bits), IEEE float (32, 64 bits) and their WAVE_FORMAT_EXTENSIBLE forms.
AudioLoadResult<T> LoadWav(string filePath) Calls the fully specified overload with the omitted parameters’ defaults.
AudioLoadResult<T> LoadWav(string filePath, bool normalize) Reads a WAV file from a stream: PCM (8, 16, 24, 32 bits), IEEE float (32, 64 bits) and their WAVE_FORMAT_EXTENSIBLE forms.
Tensor<T> Resample(Tensor<T> audio, int sourceSampleRate, int targetSampleRate) Resamples audio to a different sample rate using linear interpolation.
Tensor<T> ResampleBandLimited(IEngine engine, Tensor<T> audio, int from, int to) Band-limited resampling as torchaudio’s transforms.Resample (Hann-windowed sinc, 6 zero crossings, roll-off 0.99) of audio``[samples] from from Hz to to Hz, computed as a strided convolution so it is differentiable on the gradient t…
Tensor<T> ResampleKaiser(IEngine engine, Tensor<T> audio, int from, int to, int lowpassFilterWidth, double rolloff, double beta) torchaudio’s functional.resample with resampling_method="sinc_interp_kaiser": the windowed-sinc resampler of ResampleBandLimited with a Kaiser window I₀(β √(1 − (t / w)²)) / I₀(β) over lowpassFilterWidth zero crossings.
Tensor<T> ToMono(Tensor<T> audio) Converts stereo audio to mono by averaging channels.
void SaveWav(Tensor<T> audio, string filePath, int sampleRate, int bitsPerSample, bool denormalize) Saves audio tensor as a WAV file.