audio

package
v0.184.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package audio provides CGO-free microphone capture, audio-format conversion, and speech-to-text. Capture and conversion shell out to ffmpeg (with arecord/sox fallbacks on Linux) to produce 16kHz mono WAV; transcription shells out to a local whisper.cpp binary (whisper-cli / whisper-cpp), downloading binaries and GGML models on demand. Gated by config.SpeechToTextConfig and used by the chat /voice shortcut and Telegram voice-message transcription.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WAVDurationSeconds added in v0.184.0

func WAVDurationSeconds(path string) (float64, error)

WAVDurationSeconds returns the duration of a RIFF/WAVE file in seconds by reading its fmt and data chunk headers. It lets callers report the duration of synthesized speech without external tools.

Types

type BinaryManager added in v0.178.0

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

BinaryManager downloads prebuilt STT helper binaries (whisper-cli, ffmpeg) into ~/.infer/bin on demand, mirroring ModelManager for GGML models.

func NewBinaryManager added in v0.178.0

func NewBinaryManager(cfg config.SpeechToTextConfig) *BinaryManager

NewBinaryManager creates a BinaryManager from the speech-to-text config.

func (*BinaryManager) EnsureBinary added in v0.178.0

func (b *BinaryManager) EnsureBinary(ctx context.Context, name string) (string, error)

EnsureBinary returns the local path to the named binary under ~/.infer/bin, downloading it (checksum-verified) on first use when auto_download is enabled. An existing file is returned as-is.

type Converter

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

Converter converts arbitrary audio files into Whisper-ready 16kHz mono WAV. It is used to turn Telegram voice notes (OGG/Opus) into a format whisper.cpp can read.

func NewConverter

func NewConverter(cfg config.SpeechToTextConfig) *Converter

NewConverter creates a Converter from the speech-to-text config.

func (*Converter) SetBinaryEnsurer added in v0.148.0

func (c *Converter) SetBinaryEnsurer(f func(ctx context.Context, name string) (string, error))

SetBinaryEnsurer installs a fallback that resolves (downloading if needed) a named binary when it is not found on PATH.

func (*Converter) ToWhisperWAV

func (c *Converter) ToWhisperWAV(ctx context.Context, srcPath string) (string, error)

ToWhisperWAV converts srcPath into a new 16kHz mono WAV file and returns its path. The caller owns the returned file and should remove it when done.

type FileTranscriber added in v0.178.0

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

FileTranscriber transcribes an arbitrary audio file by first converting it to 16kHz mono WAV with ffmpeg, then running Whisper. It is used for Telegram voice messages (OGG/Opus), which must be decoded before transcription.

func NewFileTranscriber added in v0.178.0

func NewFileTranscriber(cfg config.SpeechToTextConfig) *FileTranscriber

NewFileTranscriber creates a FileTranscriber from the speech-to-text config.

func (*FileTranscriber) TranscribeFile added in v0.178.0

func (f *FileTranscriber) TranscribeFile(ctx context.Context, audioPath string) (string, error)

TranscribeFile converts audioPath to WAV and returns its transcription. The intermediate WAV file is removed before returning.

type ModelManager added in v0.178.0

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

ModelManager resolves and (optionally) downloads the GGML model file.

func NewModelManager added in v0.178.0

func NewModelManager(cfg config.SpeechToTextConfig) *ModelManager

NewModelManager creates a ModelManager from the speech-to-text config.

func (*ModelManager) EnsureModel added in v0.178.0

func (m *ModelManager) EnsureModel(ctx context.Context) (string, error)

EnsureModel returns the local path to the model file, downloading it on first use when AutoDownload is enabled. Concurrent callers are serialized so a cold cache triggers one download.

type Recorder

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

Recorder captures microphone audio into a 16kHz mono WAV file by shelling out to ffmpeg (with arecord/sox fallbacks on Linux), mirroring the candidate-list pattern used by the clipboard text writer. It adds no CGO.

func NewRecorder

func NewRecorder(cfg config.SpeechToTextConfig) *Recorder

NewRecorder creates a Recorder from the speech-to-text config.

func (*Recorder) EnsureAvailable

func (r *Recorder) EnsureAvailable() error

EnsureAvailable reports whether a microphone capture tool is installed, without recording. It lets callers fail fast (with an actionable error) before prompting the user to speak.

func (*Recorder) Record

func (r *Recorder) Record(ctx context.Context, maxSeconds int) (string, error)

Record captures up to maxSeconds of microphone audio and returns the path to a 16kHz mono WAV file. When speech_to_text.silence_timeout is set, ffmpeg recordings stop shortly after the speaker goes quiet instead of always running for the full cap. The caller owns the returned file and should remove it.

type Synthesizer added in v0.184.0

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

Synthesizer synthesizes speech to a WAV file using a local llama.cpp llama-tts binary running Qwen3-TTS GGUF models. An empty voice sample uses the stock voice; a reference WAV enables zero-shot voice cloning.

func NewSynthesizer added in v0.184.0

func NewSynthesizer(cfg config.TextToSpeechConfig) *Synthesizer

NewSynthesizer creates a synthesizer from the text-to-speech config.

func (*Synthesizer) Synthesize added in v0.184.0

func (s *Synthesizer) Synthesize(ctx context.Context, text, voiceSamplePath, outPath string) error

Synthesize converts text into a WAV at outPath, using the stock voice when voiceSamplePath is empty and cloning the supplied voice otherwise.

type TTSModelManager added in v0.184.0

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

TTSModelManager resolves and (optionally) downloads the TTS GGUF models (backbone + mmproj) into the models dir, mirroring ModelManager for whisper.

func NewTTSModelManager added in v0.184.0

func NewTTSModelManager(cfg config.TextToSpeechConfig) *TTSModelManager

NewTTSModelManager creates a TTSModelManager from the text-to-speech config.

func (*TTSModelManager) EnsureModels added in v0.184.0

func (m *TTSModelManager) EnsureModels(ctx context.Context) (backbone, mmproj string, err error)

EnsureModels returns local paths to the backbone and mmproj GGUF files, downloading them on first use when AutoDownload is enabled. Concurrent callers are serialized so each model is downloaded once.

type WhisperTranscriber added in v0.178.0

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

WhisperTranscriber transcribes a 16kHz mono WAV file using whisper.cpp.

func NewWhisperTranscriber added in v0.178.0

func NewWhisperTranscriber(cfg config.SpeechToTextConfig) *WhisperTranscriber

NewWhisperTranscriber creates a transcriber from the speech-to-text config.

func (*WhisperTranscriber) EnsureAvailable added in v0.178.0

func (w *WhisperTranscriber) EnsureAvailable() error

EnsureAvailable reports whether the whisper binary can be resolved (possibly by downloading it), without transcribing or downloading a model. It lets callers fail fast (with an actionable install hint) before recording audio.

func (*WhisperTranscriber) Transcribe added in v0.178.0

func (w *WhisperTranscriber) Transcribe(ctx context.Context, wavPath string) (string, error)

Transcribe converts the audio at wavPath (16kHz mono WAV) into text.

Jump to

Keyboard shortcuts

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