audio

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audio implements a small, deterministic PCM WAV boundary.

It reads uncompressed PCM16 in whatever rate and channel count a file happens to carry, and converts to the mono 16-bit form everything inside the runtime uses. Being strict about the container and permissive about the rate is deliberate: a fixture recorded at 44.1 kHz is a normal thing to be handed, and refusing it would push resampling onto every caller.

Index

Constants

View Source
const (
	// OpenAIPCMSampleRate is the rate the Realtime wire uses for audio/pcm.
	OpenAIPCMSampleRate = uint32(24_000)
	// PCM16BytesPerSample is the size of one sample in one channel.
	PCM16BytesPerSample = uint16(2)
	MonoChannels        = uint16(1)
)

Variables

This section is empty.

Functions

func EncodeWAVMono16

func EncodeWAVMono16(pcm []byte, sampleRateHz uint32) ([]byte, error)

EncodeWAVMono16 wraps mono PCM16LE in a canonical 44-byte WAV header.

Batch transcription endpoints take a container rather than raw samples, so something has to put the header on. It lives here because the decoder does too, and a writer that disagreed with its own reader would be found by a benchmark rather than by a test.

func GenerateFixture

func GenerateFixture(path string) ([32]byte, error)

GenerateFixture writes a deterministic two-tone WAV.

It is the smallest thing that exercises the whole audio path with a known answer: two bursts separated by silence, at a fixed rate, hashing to a stable digest. Reproduction scripts assert that digest so a decode change that silently alters audio is caught by the fixture rather than by a benchmark result three steps later.

func HashFile

func HashFile(path string) ([32]byte, error)

Types

type Decoded

type Decoded struct {
	Metadata Metadata
	// PCM16LE is mono, little-endian, at Metadata.SampleRateHz.
	PCM16LE []byte
}

Decoded is mono PCM16 plus what it came from.

func Decode

func Decode(raw []byte) (Decoded, error)

Decode parses a WAV container from memory.

func ReadFile

func ReadFile(path string) (Decoded, error)

ReadFile decodes a WAV file into mono PCM16.

type Metadata

type Metadata struct {
	SampleRateHz    uint32
	Channels        uint16
	BitsPerSample   uint16
	SampleCount     uint64
	DataLengthBytes uint64
}

Metadata describes a decoded file.

func (Metadata) DurationNS

func (metadata Metadata) DurationNS() uint64

DurationNS is the playing time of the decoded audio.

type PCM16MonoReader

type PCM16MonoReader struct {
	// contains filtered or unexported fields
}

PCM16MonoReader streams a decoded file in fixed frames.

func OpenPCM16Mono

func OpenPCM16Mono(path string) (*PCM16MonoReader, error)

OpenPCM16Mono decodes a file for framed reading.

func (*PCM16MonoReader) Close

func (reader *PCM16MonoReader) Close() error

Close exists so callers can treat the reader like a file handle.

func (*PCM16MonoReader) Metadata

func (reader *PCM16MonoReader) Metadata() Metadata

Metadata describes the open file.

func (*PCM16MonoReader) ReadFrame

func (reader *PCM16MonoReader) ReadFrame(buffer []byte) (int, error)

ReadFrame fills buffer with the next samples, returning io.EOF at the end.

Jump to

Keyboard shortcuts

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