audio

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package audio provides sound playback using the oto audio library.

Index

Constants

View Source
const SampleRate = 44100

SampleRate is the playback rate in Hz. Must match the oto context sample rate.

Variables

View Source
var Sounds = map[string]SoundDefinition{
	"warning": {
		Name:        "warning",
		Description: "Two-tone alternating warning signal",
		Segments: []ToneSegment{
			{Frequency: 800, Duration: 150 * time.Millisecond, Volume: 0.7},
			{Frequency: 600, Duration: 150 * time.Millisecond, Volume: 0.7},
			{Frequency: 800, Duration: 150 * time.Millisecond, Volume: 0.7},
			{Frequency: 600, Duration: 150 * time.Millisecond, Volume: 0.7},
		},
	},
	"success": {
		Name:        "success",
		Description: "Ascending major chord chime",
		Segments: []ToneSegment{
			{Frequency: 523.25, Duration: 120 * time.Millisecond, Volume: 0.6},
			{Frequency: 659.25, Duration: 120 * time.Millisecond, Volume: 0.6},
			{Frequency: 783.99, Duration: 250 * time.Millisecond, Volume: 0.7},
		},
	},
	"error": {
		Name:        "error",
		Description: "Low descending buzz indicating failure",
		Segments: []ToneSegment{
			{Frequency: 400, Duration: 200 * time.Millisecond, Volume: 0.8},
			{Frequency: 300, Duration: 200 * time.Millisecond, Volume: 0.8},
			{Frequency: 200, Duration: 300 * time.Millisecond, Volume: 0.9},
		},
	},
	"info": {
		Name:        "info",
		Description: "Single clean informational beep",
		Segments: []ToneSegment{
			{Frequency: 880, Duration: 200 * time.Millisecond, Volume: 0.5},
		},
	},
	"alert": {
		Name:        "alert",
		Description: "Rapid high-pitched attention signal",
		Segments: []ToneSegment{
			{Frequency: 1200, Duration: 80 * time.Millisecond, Volume: 0.7},
			{Frequency: 0, Duration: 40 * time.Millisecond, Volume: 0},
			{Frequency: 1200, Duration: 80 * time.Millisecond, Volume: 0.7},
			{Frequency: 0, Duration: 40 * time.Millisecond, Volume: 0},
			{Frequency: 1200, Duration: 80 * time.Millisecond, Volume: 0.7},
		},
	},
	"notification": {
		Name:        "notification",
		Description: "Gentle two-note doorbell chime",
		Segments: []ToneSegment{
			{Frequency: 659.25, Duration: 200 * time.Millisecond, Volume: 0.5},
			{Frequency: 523.25, Duration: 300 * time.Millisecond, Volume: 0.4},
		},
	},
	"blip": {
		Name:        "blip",
		Description: "Ultra-short confirmation blip",
		Segments: []ToneSegment{
			{Frequency: 1000, Duration: 80 * time.Millisecond, Volume: 0.7},
		},
	},
}

Sounds is the registry of all available sound effects.

Functions

func GeneratePCM

func GeneratePCM(def SoundDefinition) []byte

GeneratePCM produces stereo 16-bit signed little-endian PCM data for the given sound.

func LoadWAV added in v0.2.11

func LoadWAV(path string) ([]byte, error)

LoadWAV reads a WAV file and returns raw stereo 16-bit signed LE PCM at 44100 Hz. Supports PCM format (format code 1) with 8-bit, 16-bit, or 24-bit samples, mono or stereo. Resamples to 44100 Hz via linear interpolation if needed.

func Play

func Play(name string, volume float64) error

Play plays a sound, blocking until playback completes. If name matches a built-in sound, plays the generated tone; otherwise treats name as a WAV file path. volume is a multiplier from 0.0 (silent) to 1.0 (full volume).

Types

type SoundDefinition

type SoundDefinition struct {
	Name        string
	Description string
	Segments    []ToneSegment
}

SoundDefinition describes a named sound composed of one or more tone segments.

type ToneSegment

type ToneSegment struct {
	Frequency float64
	Duration  time.Duration
	Volume    float64 // 0.0 to 1.0
}

ToneSegment defines a single tone burst with frequency, duration, and volume.

Jump to

Keyboard shortcuts

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