audio

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: ISC Imports: 19 Imported by: 0

README

Ogg encoding originally from https://github.com/chenbh/skynetbot.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CaptureStream added in v0.2.4

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

CaptureStream captures data from an input device for some time.

func (*CaptureStream) CaptureDone added in v0.2.4

func (cs *CaptureStream) CaptureDone() <-chan struct{}

CaptureDone is closed once capturing is completed.

func (*CaptureStream) Err added in v0.2.4

func (cs *CaptureStream) Err() error

Err is the capturing error. It is only set after capturing is done.

func (*CaptureStream) RecordInfo added in v0.2.4

func (cs *CaptureStream) RecordInfo() RecordInfo

RecordInfo is the information about the finished recording.

func (*CaptureStream) SetVolumeGain added in v0.2.4

func (cs *CaptureStream) SetVolumeGain(gainDB float64)

SetVolumeGain sets the volume gain for captured samples. The new gain is specified in dB.

func (*CaptureStream) Stop added in v0.2.4

func (cs *CaptureStream) Stop()

Stop stops the capture stream independently of the run context stopping.

type Device

type Device struct {
	ID        DeviceID `json:"id"`
	Name      string   `json:"name"`
	IsDefault bool     `json:"is_default"`
}

Device identifies capture/playback device.

func FindDevice

func FindDevice(typ DeviceType, id DeviceID) *Device

FindDevice finds the device with the given ID or returns nil.

type DeviceID added in v0.2.4

type DeviceID string

DeviceID is a generic id for playback and capture devices.

const DefaultDeviceID DeviceID = ""

func (DeviceID) String added in v0.2.4

func (id DeviceID) String() string

type DeviceType

type DeviceType uint32

DeviceType type.

Note: This MUST match malgo.DeviceType definition.

const (
	Playback DeviceType = iota + 1
	Capture
	Duplex
	Loopback
)

DeviceType enumeration.

Note: This MUST match malgo's device type enumeration.

type Devices

type Devices struct {
	Playback []Device `json:"playback"`
	Capture  []Device `json:"capture"`
}

Devices is the list of devices in the computer.

func ListAudioDevices

func ListAudioDevices(log slog.Logger) (Devices, error)

ListAudioDevices lists available audio devices.

type EncodedCapturedFunc added in v0.2.4

type EncodedCapturedFunc func(ctx context.Context, data []byte, timestamp uint32) error

EncodedCapturedFunc is the signature for the callback function that processes captured and opus-encoded packets.

type NoteRecorder

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

NoteRecorder can record and playback audio notes.

func NewRecorder

func NewRecorder(log slog.Logger) (*NoteRecorder, error)

func (*NoteRecorder) Busy

func (ar *NoteRecorder) Busy() (recording bool, playing bool)

Busy returns the state of the recorder.

func (*NoteRecorder) Capture

func (ar *NoteRecorder) Capture(ctx context.Context) error

Capture audio data until the context is canceled or Stop() is called.

func (*NoteRecorder) CaptureDeviceID added in v0.2.4

func (ar *NoteRecorder) CaptureDeviceID() DeviceID

CaptureDeviceID returns the ID of the device used for capturing mic data. If empty, the system-wide default device is used.

func (*NoteRecorder) CaptureStream added in v0.2.4

func (ar *NoteRecorder) CaptureStream(ctx context.Context, f EncodedCapturedFunc) (*CaptureStream, error)

CaptureStream runs a new capture stream, sending data to the callback. This capture stream is independent of other operations.

func (*NoteRecorder) FreeContext

func (ar *NoteRecorder) FreeContext() error

FreeContext releases all resources.

func (*NoteRecorder) GetCaptureGain added in v0.2.4

func (ar *NoteRecorder) GetCaptureGain() float64

GetCaptureGain returns the currently set capture gain.

func (*NoteRecorder) GetPlaybackGain added in v0.2.4

func (ar *NoteRecorder) GetPlaybackGain() float64

GetPlaybackGain returns the global playback gain.

func (*NoteRecorder) HasRecorded

func (ar *NoteRecorder) HasRecorded() bool

HasRecorded returns whether there's a recorded note.

func (*NoteRecorder) OpusFile

func (ar *NoteRecorder) OpusFile() ([]byte, error)

OpusFile encodes the recorded audio note as an opusfile (a .ogg file with opus-encoded audio data).

func (*NoteRecorder) Playback

func (ar *NoteRecorder) Playback(ctx context.Context) error

Playback the recorded audio until it ends or the context is canceled or Stop() is called.

func (*NoteRecorder) PlaybackDeviceID added in v0.2.4

func (ar *NoteRecorder) PlaybackDeviceID() DeviceID

PlaybackDeviceID returns the ID of the device used for playing back audio data. If empty, the system-wide default device is used.

func (*NoteRecorder) PlaybackStream added in v0.2.4

func (ar *NoteRecorder) PlaybackStream(ctx context.Context, soundStateChanged func(bool)) *PlaybackStream

PlaybackStream creates a new playback stream. This playback stream is independent of other operations.

func (*NoteRecorder) RecordInfo

func (ar *NoteRecorder) RecordInfo() RecordInfo

RecordInfo returns information about the latest recording.

func (*NoteRecorder) SetCaptureDevice

func (ar *NoteRecorder) SetCaptureDevice(devID DeviceID) error

SetCaptureDevice sets the capture device to use for recording. If nil, uses the default device.

func (*NoteRecorder) SetCaptureGain added in v0.2.4

func (ar *NoteRecorder) SetCaptureGain(gain float64)

SetCaptureGain sets the capture gain for captures. This only applies to new capture streams.

func (*NoteRecorder) SetPlaybackDevice

func (ar *NoteRecorder) SetPlaybackDevice(devID DeviceID) error

SetPlaybackDevice sets the playback device to use for playing. If nil, uses the default device.

func (*NoteRecorder) SetPlaybackGain added in v0.2.4

func (ar *NoteRecorder) SetPlaybackGain(gain float64)

SetPlaybackGain sets the global playback gain. This is added to the per-stream playback gain.

func (*NoteRecorder) Stop

func (ar *NoteRecorder) Stop()

Stop the current operation (record or playback).

type OggHeader

type OggHeader struct {
	Version     uint8
	IsContinued bool
	IsFirstPage bool
	IsLastPage  bool

	GranulePosition uint64
	BitstreamSerial uint32
	PageSequence    uint32
	CrcChecksum     uint32

	PageSegments uint8
	SegmentTable []uint8
}

type OggPage

type OggPage struct {
	OggHeader
	Segments [][]byte

	// Size of all segments in bytes
	SegmentTotal int
}

type OpusPacket

type OpusPacket []byte

type PlaybackStream added in v0.2.4

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

PlaybackStream plays back opus-encoded data.

func (*PlaybackStream) BufferedCount added in v0.2.4

func (ps *PlaybackStream) BufferedCount() int64

BufferedCount returns the number of buffered packets.

func (*PlaybackStream) ChangePlaybackDevice added in v0.2.4

func (ps *PlaybackStream) ChangePlaybackDevice(devID DeviceID)

ChangePlaybackDevice changes the playback device of this stream to the given one.

func (*PlaybackStream) Err added in v0.2.4

func (ps *PlaybackStream) Err() error

Err returns the playback error. It is only set after playback is done.

func (*PlaybackStream) Input added in v0.2.4

func (ps *PlaybackStream) Input(data []byte, ts uint32)

Input data into the playback stream. Data should be an opus-encoded packet and the timestamp should be following the standard periodSizeMS period.

Note: If the input buffer is full, this drops the packet.

func (*PlaybackStream) MarkInputDone added in v0.2.4

func (ps *PlaybackStream) MarkInputDone(ctx context.Context)

MarkInputDone signals that input data for playback in this stream is done.

func (*PlaybackStream) PlaybackDone added in v0.2.4

func (ps *PlaybackStream) PlaybackDone() <-chan struct{}

PlaybackDone is closed when playback of this stream is finished or canceled.

func (*PlaybackStream) SetVolumeGain added in v0.2.4

func (ps *PlaybackStream) SetVolumeGain(gainDB float64)

SetVolumeGain modifies the volume gain of this stream. Gain is expressed in dB units.

type RecordInfo

type RecordInfo struct {
	SampleCount int `json:"sample_count"`
	DurationMs  int `json:"duration_ms"`
	EncodedSize int `json:"encoded_size"`
	PacketCount int `json:"packet_count"`
}

RecordInfo tracks information about a recording session.

Directories

Path Synopsis
cmd
simstream command

Jump to

Keyboard shortcuts

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