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 ¶
- func Describe(audio *Audio) string
- func IsMP3Supported() bool
- func Save(audio *Audio, path string) error
- func SaveWAV(audio *Audio, path string) error
- func WriteWAV(w io.Writer, audio *Audio) error
- type Audio
- func AdjustVolume(audio *Audio, factor float64) *Audio
- func ChangeSpeed(audio *Audio, factor float64) *Audio
- func Concatenate(a, b *Audio) (*Audio, error)
- func ConvertFormat(audio *Audio, format SampleFormat) *Audio
- func Copy(audio *Audio) *Audio
- func Cut(audio *Audio, startSec, endSec float64) *Audio
- func FadeIn(audio *Audio, durationSec float64) *Audio
- func FadeOut(audio *Audio, durationSec float64) *Audio
- func GenerateSilence(durationSec float64, sampleRate, channels int) *Audio
- func GenerateTone(freq float64, durationSec float64, sampleRate int) *Audio
- func Load(path string) (*Audio, error)
- func LoadMP3(path string) (*Audio, error)
- func LoadWAV(path string) (*Audio, error)
- func Mix(a, b *Audio) *Audio
- func NewAudio(sampleRate, channels int, format SampleFormat, numSamples int) *Audio
- func Normalize(audio *Audio, target float64) *Audio
- func ReadMP3(r io.Reader) (*Audio, error)
- func ReadWAV(r io.ReadSeeker) (*Audio, error)
- func Resample(audio *Audio, targetSampleRate int) *Audio
- func Reverse(audio *Audio) *Audio
- func ToMono(audio *Audio) *Audio
- func ToStereo(audio *Audio) *Audio
- func Trim(audio *Audio, startSec, endSec float64) *Audio
- func TrimSilence(audio *Audio, thresholdDB float64) *Audio
- type Info
- type SampleFormat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsMP3Supported ¶
func IsMP3Supported() bool
IsMP3Supported returns true if MP3 decoding is available.
func Save ¶
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).
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 ¶
AdjustVolume scales the amplitude by a factor. factor of 2.0 doubles the volume, 0.5 halves it.
func ChangeSpeed ¶
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 ¶
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 GenerateSilence ¶
GenerateSilence creates a silent audio buffer.
func GenerateTone ¶
GenerateTone generates a sine wave tone at the given frequency for the given duration.
func Load ¶
Load loads an audio file, auto-detecting the format from the file extension. Supported formats: .wav, .mp3.
func LoadMP3 ¶
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 Mix ¶
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 ¶
Normalize normalizes the audio to a target peak level (0-1). target of 1.0 means full scale.
func ReadWAV ¶
func ReadWAV(r io.ReadSeeker) (*Audio, error)
ReadWAV reads a WAV file from a reader.
func Trim ¶
Trim removes samples from the beginning and end. startSec and endSec are offsets in seconds.
func TrimSilence ¶
TrimSilence removes leading and trailing silence below the given threshold (in dB). threshold of -40 means samples below -40dB are considered silence.
func (*Audio) NumSamples ¶
NumSamples returns the number of samples per channel.
type Info ¶
Info holds audio file information.
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 )