audiomedia

package
v1.53.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package audiomedia analyzes immutable local audio and owns cancellable playback. Callers retain ownership of the verified snapshot and must not modify it while an operation runs. The package never reopens its pathname or closes its file.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidAudio       = errors.New("audio is malformed or incomplete")
	ErrUnsupportedFormat  = errors.New("audio format is unsupported")
	ErrAnalysisFailed     = errors.New("audio analysis failed")
	ErrPlaybackFailed     = errors.New("audio playback failed")
	ErrDecoderUnavailable = errors.New("audio decoding requires FFmpeg; install FFmpeg or use uncompressed WAV or PCM")
	ErrPlayerUnavailable  = errors.New("audio playback requires FFplay; install FFmpeg with FFplay")
)
View Source
var (
	ErrCaptureUnavailable = errors.New("microphone capture needs FFmpeg; install FFmpeg with audio device support")
	ErrCaptureFailed      = errors.New("microphone capture failed; check microphone access, the selected device, and FFmpeg audio device support")
	ErrCaptureDevice      = errors.New("choose a microphone device name or index without line breaks")
	ErrCaptureUnsupported = errors.New("microphone capture is unavailable on this operating system")
)

Functions

func CaptureFilePCM added in v1.53.1

func CaptureFilePCM(ctx context.Context, file *os.File, onChunk func([]byte) error) error

CaptureFilePCM sends decoded mono PCM16 at playback speed. The selected file reaches FFmpeg through stdin, without exposing a pathname or enabling other locations.

func CapturePCM added in v1.53.1

func CapturePCM(ctx context.Context, device string, onChunk func([]byte) error) error

CapturePCM captures 24 kHz mono signed little-endian PCM16 through FFmpeg. The callback receives owned 200 ms chunks, serially, with no pending queue. It must return promptly or honor a caller-owned cancellation context. A final shorter chunk contains every complete sample received, including on cancellation. A graceful stop can use a separate bounded callback drain context. CapturePCM waits for its helper and callbacks before returning.

func OpenCaptureFile added in v1.53.1

func OpenCaptureFile(path string) (*os.File, error)

OpenCaptureFile pins the selected recording before the session connects. The caller closes the returned file when translation ends.

func Play

func Play(ctx context.Context, file *os.File, format string, offset time.Duration) error

Play starts local audio only when called explicitly. Native PCM/WAV offsets skip complete sample frames. Compressed offsets use output-side decoder seek. Every helper reads verified bytes through pipes; no path or URL reaches it.

func PlayPCM added in v1.53.1

func PlayPCM(ctx context.Context, source io.ReadCloser, rate, channels int) error

PlayPCM plays a live signed little-endian PCM16 stream with explicit metadata. It owns source.Close on every exit. Close must unblock a pending Read; an io.PipeReader satisfies this contract. No input queue or recording is retained.

func PlayPCMFrom added in v1.53.1

func PlayPCMFrom(ctx context.Context, source *io.SectionReader, rate, channels int, offset time.Duration) error

PlayPCMFrom replays retained PCM16 starting at a complete sample frame. The caller owns the immutable recording and its underlying descriptor.

func PlayWithProgress added in v1.40.3

func PlayWithProgress(ctx context.Context, file *os.File, format string, offset time.Duration, report func(time.Duration)) error

PlayWithProgress reports positions from FFplay's audio clock in the original recording. The player estimates device buffering; reports are not exact audible positions. No report arrives until the player supplies a usable clock. Reports run serially, must return promptly, and stop before this function returns. Callers must reject stale reports when replacing playback and handle no reports. A nil report preserves Play's behavior. The caller still owns the file.

func ValidateCapture added in v1.53.1

func ValidateCapture(device string) error

ValidateCapture checks the device selection and helper availability without opening a device. FFmpeg builds can omit the required platform input backend.

func ValidatePlayback added in v1.53.1

func ValidatePlayback() error

ValidatePlayback checks helper discovery without opening an output device.

func Waveform

func Waveform(peaks []float64, width, height int) string

Waveform draws uncolored amplitudes on a fixed 0..1 scale. Multiple rows use symmetric Braille columns; a single row uses an eight-level block sparkline. Dimensions are bounded independently from accepted audio size. Zero samples produce a baseline; absent audio produces no invented waveform.

Types

type Analysis

type Analysis struct {
	Format   string    `json:"format"`
	Bytes    int64     `json:"bytes"`
	Duration float64   `json:"duration"`
	Peaks    []float64 `json:"peaks"`
}

func Analyze

func Analyze(ctx context.Context, file *os.File, format string) (Analysis, error)

Analyze scans the supplied snapshot without changing its seek offset. A caller may use a context deadline to bound this optional work after preserving audio. Duration counts decoded frames. Each peak is the maximum absolute sample over all channels in its interval; opposite-phase stereo cannot cancel the waveform.

type PCM16Envelope added in v1.53.1

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

PCM16Envelope measures interleaved PCM16 from a single stream. It retains the same bounded peak bins as saved-file analysis.

func (*PCM16Envelope) Add added in v1.53.1

func (e *PCM16Envelope) Add(data []byte, channels int)

func (*PCM16Envelope) Peaks added in v1.53.1

func (e *PCM16Envelope) Peaks() []float64

type UnavailableError

type UnavailableError struct{ Tool string }

UnavailableError identifies a missing optional helper without exposing paths.

func (*UnavailableError) Error

func (e *UnavailableError) Error() string

func (*UnavailableError) Unwrap

func (e *UnavailableError) Unwrap() error

Jump to

Keyboard shortcuts

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