audioutil

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package audioutil provides audio processing utilities for WAV and MP3 files:

  • WAV read/write: 8-bit, 16-bit, 24-bit, 32-bit PCM; mono/stereo
  • MP3 decode: load MP3 files into Audio buffers (via go-mp3)
  • Volume: adjust gain, fade in/out, normalize
  • Trim: remove silence, cut to range
  • Mix: merge multiple audio streams
  • Resample: linear interpolation
  • Convert: mono↔stereo, bit depth, WAV↔MP3 round-trip
  • Effects: fade, speed change, reverse
  • Info: duration, sample rate, channels, bit depth

Quick start

// Load WAV or MP3 (auto-detected by extension)
audio, err := audioutil.Load("input.mp3")
audio = audioutil.AdjustVolume(audio, 2.0)
audio = audioutil.TrimSilence(audio, -40)
audioutil.SaveWAV(audio, "output.wav")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Describe

func Describe(audio *Audio) string

Describe returns a human-readable description of the audio.

func IsMP3Supported

func IsMP3Supported() bool

IsMP3Supported returns true if MP3 decoding is available.

func Save

func Save(audio *Audio, path string) error

Save saves an audio file in the specified format, inferring from the file extension. Supported: .wav (full support), .mp3 (not supported for encoding — use WAV instead).

func SaveWAV

func SaveWAV(audio *Audio, path string) error

SaveWAV saves an Audio buffer as a WAV file.

func WriteWAV

func WriteWAV(w io.Writer, audio *Audio) error

WriteWAV writes an Audio buffer as WAV data to a writer.

Types

type Audio

type Audio struct {
	SampleRate int          // samples per second (e.g. 44100)
	Channels   int          // 1 = mono, 2 = stereo
	Format     SampleFormat // bit depth
	Samples    [][]float64  // [channel][sample] float64 samples normalized to [-1, 1]
}

Audio represents an in-memory audio buffer.

func AdjustVolume

func AdjustVolume(audio *Audio, factor float64) *Audio

AdjustVolume scales the amplitude by a factor. factor of 2.0 doubles the volume, 0.5 halves it.

func ChangeSpeed

func ChangeSpeed(audio *Audio, factor float64) *Audio

ChangeSpeed changes the playback speed by a factor without changing pitch. factor of 2.0 plays twice as fast (shorter duration). Note: This is a simple implementation that resamples and changes pitch.

func Concatenate

func Concatenate(a, b *Audio) (*Audio, error)

Concatenate joins two audio buffers end-to-end. Both must have the same sample rate and channel count.

func ConvertFormat

func ConvertFormat(audio *Audio, format SampleFormat) *Audio

ConvertFormat changes the bit depth of the audio.

func Copy

func Copy(audio *Audio) *Audio

Copy creates a deep copy of an Audio buffer.

func Cut

func Cut(audio *Audio, startSec, endSec float64) *Audio

Cut extracts a portion of the audio from startSec to endSec.

func FadeIn

func FadeIn(audio *Audio, durationSec float64) *Audio

FadeIn applies a linear fade-in over the given duration in seconds.

func FadeOut

func FadeOut(audio *Audio, durationSec float64) *Audio

FadeOut applies a linear fade-out over the given duration in seconds.

func GenerateSilence

func GenerateSilence(durationSec float64, sampleRate, channels int) *Audio

GenerateSilence creates a silent audio buffer.

func GenerateTone

func GenerateTone(freq float64, durationSec float64, sampleRate int) *Audio

GenerateTone generates a sine wave tone at the given frequency for the given duration.

func Load

func Load(path string) (*Audio, error)

Load loads an audio file, auto-detecting the format from the file extension. Supported formats: .wav, .mp3.

func LoadMP3

func LoadMP3(path string) (*Audio, error)

LoadMP3 decodes an MP3 file and returns an Audio buffer. The output is 16-bit PCM at the MP3's native sample rate. go-mp3 always decodes to stereo (2 channels) regardless of the original channel count, so the returned Audio will have Channels=2 unless the source is mono (Channels=1).

func LoadWAV

func LoadWAV(path string) (*Audio, error)

LoadWAV loads a WAV file and returns an Audio buffer.

func Mix

func Mix(a, b *Audio) *Audio

Mix mixes two audio buffers together. If they have different lengths, the result has the length of the longer one. Sample rates should match.

func NewAudio

func NewAudio(sampleRate, channels int, format SampleFormat, numSamples int) *Audio

NewAudio creates a new Audio buffer with the given parameters.

func Normalize

func Normalize(audio *Audio, target float64) *Audio

Normalize normalizes the audio to a target peak level (0-1). target of 1.0 means full scale.

func ReadMP3

func ReadMP3(r io.Reader) (*Audio, error)

ReadMP3 decodes MP3 data from a reader and returns an Audio buffer.

func ReadWAV

func ReadWAV(r io.ReadSeeker) (*Audio, error)

ReadWAV reads a WAV file from a reader.

func Resample

func Resample(audio *Audio, targetSampleRate int) *Audio

Resample changes the sample rate using linear interpolation.

func Reverse

func Reverse(audio *Audio) *Audio

Reverse reverses the audio.

func ToMono

func ToMono(audio *Audio) *Audio

ToMono converts stereo (or multi-channel) audio to mono by averaging channels.

func ToStereo

func ToStereo(audio *Audio) *Audio

ToStereo converts mono audio to stereo by duplicating the channel.

func Trim

func Trim(audio *Audio, startSec, endSec float64) *Audio

Trim removes samples from the beginning and end. startSec and endSec are offsets in seconds.

func TrimSilence

func TrimSilence(audio *Audio, thresholdDB float64) *Audio

TrimSilence removes leading and trailing silence below the given threshold (in dB). threshold of -40 means samples below -40dB are considered silence.

func (*Audio) Duration

func (a *Audio) Duration() float64

Duration returns the duration in seconds.

func (*Audio) NumSamples

func (a *Audio) NumSamples() int

NumSamples returns the number of samples per channel.

type Info

type Info struct {
	SampleRate  int
	Channels    int
	BitDepth    int
	NumSamples  int
	DurationSec float64
}

Info holds audio file information.

func GetInfo

func GetInfo(path string) (*Info, error)

GetInfo returns information about a WAV file without fully decoding.

func (Info) String

func (i Info) String() string

String returns a human-readable description.

type SampleFormat

type SampleFormat int

SampleFormat represents the bit depth of audio samples.

const (
	Format8Bit  SampleFormat = 8
	Format16Bit SampleFormat = 16
	Format24Bit SampleFormat = 24
	Format32Bit SampleFormat = 32
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL