pulse

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 16 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 pulse is the audio service, the Go port of crates/wayle-audio: sinks, sources, and per-application streams kept live from PulseAudio's subscription events, the default devices, per-channel volume and mute, ports, stream routing, and record streams, all over the native protocol (package native), so it works against PulseAudio and pipewire-pulse alike with no libpulse.

Index

Constants

View Source
const (
	ServiceName = "dev.stubbe.lys.Audio1"
	ServicePath = "/dev/stubbe/lys/Audio"
	// Interface is the object's interface name.
	Interface = "dev.stubbe.lys.Audio1"
)

D-Bus identity (crates/wayle-audio/src/dbus/mod.rs).

Variables

View Source
var ErrClosed = errors.New("pulse: service closed")

ErrClosed reports a request made after Close.

View Source
var ErrDisconnected = errors.New("pulse: not connected to the server")

ErrDisconnected reports a request made while the service has no connection to the server.

View Source
var ErrEmptyVolume = errors.New("pulse: volume has no channels")

ErrEmptyVolume rejects a volume with no channels (PulseAudio would answer PA_ERR_INVALID).

View Source
var ErrNoDefaultDevice = errors.New("pulse: no default device")

ErrNoDefaultDevice reports that the server names no default device of the requested kind, or names one not discovered yet.

Functions

This section is empty.

Types

type AudioFormat

type AudioFormat struct {
	Encoding   string
	Properties map[string]string
}

AudioFormat is one supported format: the encoding's name and its properties.

type AudioStream

type AudioStream struct {
	Key             StreamKey
	Name            string
	ApplicationName string
	Binary          string
	PID             uint32
	OwnerModule     OptIndex
	Client          OptIndex
	State           StreamState
	Volume          Volume
	Muted           bool
	Corked          bool
	HasVolume       bool
	VolumeWritable  bool
	DeviceIndex     uint32
	SampleSpec      SampleSpec
	ChannelMap      ChannelMap
	Properties      map[string]string
	Media           MediaInfo
	BufferLatency   time.Duration
	DeviceLatency   time.Duration
	ResampleMethod  string
	Driver          string
	Format          string
}

AudioStream is one application stream (core/stream). The application fields are empty, and PID zero, when the client did not set them.

type Capture

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

Capture is a running record stream that keeps itself pointed at its target: it reopens on the new source when the target resolves differently (the default changed) and after the server kills the stream, and waits while the target does not exist.

func (*Capture) Close

func (c *Capture) Close()

Close stops the capture and deletes its stream.

func (*Capture) Source

func (c *Capture) Source() string

Source names the source being recorded, empty while waiting.

type CaptureSpec

type CaptureSpec struct {
	Name           string
	Rate           uint32
	Channels       uint8
	FragmentFrames uint32
}

CaptureSpec is the PCM a capture delivers: signed 16-bit little-endian samples at Rate, Channels interleaved (1 or 2; the server mixes down or up), in chunks of about FragmentFrames frames. Name becomes the stream's media.name.

type CaptureTarget

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

CaptureTarget selects what a capture records. The zero value is the default sink's monitor.

func CaptureDefaultInput

func CaptureDefaultInput() CaptureTarget

CaptureDefaultInput records the default source, following it.

func CaptureDefaultMonitor

func CaptureDefaultMonitor() CaptureTarget

CaptureDefaultMonitor records whatever the default sink plays, and follows the default when it changes.

func CaptureNamed

func CaptureNamed(name string) (CaptureTarget, error)

CaptureNamed records one device by name: a source, or a sink, whose monitor it then records (PipeWire's target.object semantics).

func (CaptureTarget) String

func (t CaptureTarget) String() string

type ChannelMap

type ChannelMap = native.ChannelMap

ChannelMap is the per-channel position list.

type Client

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

Client is the CLI side (dbus/client.rs's AudioProxy).

func NewClient

func NewClient(conn *dbus.Conn) *Client

NewClient addresses the daemon over conn.

func (*Client) AdjustInputVolume

func (c *Client) AdjustInputVolume(ctx context.Context, delta float64) (float64, error)

AdjustInputVolume shifts the input volume.

func (*Client) AdjustOutputVolume

func (c *Client) AdjustOutputVolume(ctx context.Context, delta float64) (float64, error)

AdjustOutputVolume shifts the output volume.

func (*Client) DefaultSink

func (c *Client) DefaultSink(ctx context.Context) (string, error)

DefaultSink reads the DefaultSink property.

func (*Client) DefaultSource

func (c *Client) DefaultSource(ctx context.Context) (string, error)

DefaultSource reads the DefaultSource property.

func (*Client) GetDefaultSinkInfo

func (c *Client) GetDefaultSinkInfo(ctx context.Context) (map[string]string, error)

GetDefaultSinkInfo describes the default output.

func (*Client) GetDefaultSourceInfo

func (c *Client) GetDefaultSourceInfo(ctx context.Context) (map[string]string, error)

GetDefaultSourceInfo describes the default input.

func (*Client) InputMuted

func (c *Client) InputMuted(ctx context.Context) (bool, error)

InputMuted reads the InputMuted property.

func (*Client) InputVolume

func (c *Client) InputVolume(ctx context.Context) (float64, error)

InputVolume reads the InputVolume property.

func (*Client) ListSinks

func (c *Client) ListSinks(ctx context.Context) ([]DeviceEntry, error)

ListSinks lists the outputs.

func (*Client) ListSources

func (c *Client) ListSources(ctx context.Context) ([]DeviceEntry, error)

ListSources lists the inputs.

func (*Client) OutputMuted

func (c *Client) OutputMuted(ctx context.Context) (bool, error)

OutputMuted reads the OutputMuted property.

func (*Client) OutputVolume

func (c *Client) OutputVolume(ctx context.Context) (float64, error)

OutputVolume reads the OutputVolume property.

func (*Client) SetDefaultSink

func (c *Client) SetDefaultSink(ctx context.Context, index uint32) error

SetDefaultSink makes a sink the default by index.

func (*Client) SetDefaultSource

func (c *Client) SetDefaultSource(ctx context.Context, index uint32) error

SetDefaultSource makes a source the default by index.

func (*Client) SetInputMute

func (c *Client) SetInputMute(ctx context.Context, muted bool) error

SetInputMute sets the input mute.

func (*Client) SetInputVolume

func (c *Client) SetInputVolume(ctx context.Context, volume float64) (float64, error)

SetInputVolume sets the input volume percentage.

func (*Client) SetOutputMute

func (c *Client) SetOutputMute(ctx context.Context, muted bool) error

SetOutputMute sets the output mute.

func (*Client) SetOutputVolume

func (c *Client) SetOutputVolume(ctx context.Context, volume float64) (float64, error)

SetOutputVolume sets the output volume percentage.

func (*Client) SinkCount

func (c *Client) SinkCount(ctx context.Context) (uint32, error)

SinkCount reads the SinkCount property.

func (*Client) SourceCount

func (c *Client) SourceCount(ctx context.Context) (uint32, error)

SourceCount reads the SourceCount property.

func (*Client) ToggleInputMute

func (c *Client) ToggleInputMute(ctx context.Context) (bool, error)

ToggleInputMute flips the input mute.

func (*Client) ToggleOutputMute

func (c *Client) ToggleOutputMute(ctx context.Context) (bool, error)

ToggleOutputMute flips the output mute.

type Daemon

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

Daemon serves the audio service on the session bus (dbus/server.rs's AudioDaemon) for the `lys-shell audio` CLI and scripts.

func NewDaemon

func NewDaemon(svc *Service) *Daemon

NewDaemon wraps the service for export.

func (*Daemon) Export

func (d *Daemon) Export(conn *dbus.Conn) (func(), error)

Export exports the object, its properties, and its introspection data, then requests the well-known name; release drops the name.

type Device

type Device struct {
	Key               DeviceKey
	Name              string
	Description       string
	Card              OptIndex
	OwnerModule       OptIndex
	Driver            string
	State             DeviceState
	Volume            Volume
	BaseVolume        Volume
	VolumeSteps       uint32
	Muted             bool
	Properties        map[string]string
	Ports             []DevicePort
	ActivePort        string // empty when the device has no active port
	Formats           []AudioFormat
	SampleSpec        SampleSpec
	ChannelMap        ChannelMap
	Latency           time.Duration
	ConfiguredLatency time.Duration
	Flags             uint32
}

Device is what sinks and sources share (types/device.rs's DeviceInfo).

type DeviceEntry

type DeviceEntry struct {
	Index       uint32
	Name        string
	Description string
}

DeviceEntry is one ListSinks/ListSources row, (index, name, description) on the wire.

type DeviceKey

type DeviceKey struct {
	Index uint32
	Type  DeviceType
}

DeviceKey identifies a device: PulseAudio indexes sinks and sources separately, so the index alone is ambiguous.

type DeviceNotFoundError

type DeviceNotFoundError struct {
	Key DeviceKey
}

DeviceNotFoundError reports a lookup of a device the service does not hold (error.rs's DeviceNotFound).

func (*DeviceNotFoundError) Error

func (e *DeviceNotFoundError) Error() string

type DevicePort

type DevicePort struct {
	Name        string
	Description string
	Priority    uint32
	Available   bool
}

DevicePort is one port; Available is false only when PulseAudio reports the port unavailable (unknown counts as available).

type DeviceState

type DeviceState int

DeviceState is the device's run state.

const (
	StateRunning DeviceState = iota
	StateIdle
	StateSuspended
	StateOffline
)

Device states; an invalid PulseAudio state reads as Offline (conversion/device.rs's fallback).

func (DeviceState) String

func (s DeviceState) String() string

String is the Rust variant name the DBus info map carries.

type DeviceType

type DeviceType int

DeviceType is input (source) or output (sink) (types/device.rs).

const (
	DeviceInput DeviceType = iota
	DeviceOutput
)

Device types.

func (DeviceType) String

func (t DeviceType) String() string

type InputDevice

type InputDevice struct {
	Device
	MonitorOfSink     OptIndex
	MonitorOfSinkName string // empty unless IsMonitor
}

InputDevice is a source (core/device/input); monitors of sinks are sources too.

func (InputDevice) IsMonitor

func (d InputDevice) IsMonitor() bool

IsMonitor reports whether the source monitors a sink.

type InvalidChannelError

type InvalidChannelError struct {
	Channel int
}

InvalidChannelError rejects a channel index the volume lacks.

func (*InvalidChannelError) Error

func (e *InvalidChannelError) Error() string

type InvalidVolumeError

type InvalidVolumeError struct {
	Channel int
	Volume  float64
}

InvalidVolumeError rejects an out-of-range level (volume/error.rs).

func (*InvalidVolumeError) Error

func (e *InvalidVolumeError) Error() string

type MediaInfo

type MediaInfo struct {
	Title    string
	Artist   string
	Album    string
	IconName string
}

MediaInfo is the stream's media metadata; empty fields are unset.

type OptIndex

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

OptIndex is an object index that may be absent (PA_INVALID_INDEX on the wire, Option<u32> in wayle-audio).

func (OptIndex) Get

func (o OptIndex) Get() (uint32, bool)

Get returns the index and whether it is set.

type OutputDevice

type OutputDevice struct {
	Device
	MonitorSource     uint32
	MonitorSourceName string
}

OutputDevice is a sink (core/device/output).

type SampleSpec

type SampleSpec = native.SampleSpec

SampleSpec is the device or stream sample format.

type Service

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

Service is the live audio state. Every change PulseAudio announces is folded in as it arrives; the getters return snapshots that the caller owns. A lost connection empties the state and is redialled until Close, refilling it. It is safe for concurrent use.

func Connect

func Connect(ctx context.Context, server string) (*Service, error)

Connect dials the server (server "" resolves like libpulse: $PULSE_SERVER, then $XDG_RUNTIME_DIR/pulse/native) and discovers every device, stream, and default before returning, so the first snapshot is complete; a later loss reconnects as Start does.

func Start added in v0.2.0

func Start(server string) *Service

Start returns at once and connects in the background, retrying until the server is up and again whenever the connection is lost; the state is empty while there is none.

func (*Service) AudioStream

func (s *Service) AudioStream(key StreamKey) (AudioStream, error)

AudioStream snapshots one stream.

func (*Service) Capture

func (s *Service) Capture(target CaptureTarget, spec CaptureSpec, onData func([]byte)) (*Capture, error)

Capture starts recording target. onData receives each chunk on the connection's reader goroutine: it must not block, and it owns the slice. A target that does not resolve yet is not an error; the capture waits for it.

func (*Service) Close

func (s *Service) Close() error

Close disconnects for good and closes every subscriber channel.

func (*Service) DefaultInput

func (s *Service) DefaultInput() (InputDevice, bool)

DefaultInput is the default source.

func (*Service) DefaultOutput

func (s *Service) DefaultOutput() (OutputDevice, bool)

DefaultOutput is the default sink. The default resolves by name on every read, so a default naming a sink not discovered yet appears the moment it is, rather than the previous device lingering.

func (*Service) DefaultSink

func (s *Service) DefaultSink(context.Context) (OutputDevice, error)

DefaultSink implements Source.

func (*Service) DefaultSource

func (s *Service) DefaultSource(context.Context) (InputDevice, error)

DefaultSource implements Source.

func (*Service) InputDevice

func (s *Service) InputDevice(index uint32) (InputDevice, error)

InputDevice snapshots one source.

func (*Service) InputDevices

func (s *Service) InputDevices() []InputDevice

InputDevices lists every source by index, monitors included.

func (*Service) MoveStream

func (s *Service) MoveStream(ctx context.Context, key StreamKey, device DeviceKey) error

MoveStream routes a stream to another device: a playback stream to a sink, a recording stream to a source.

func (*Service) OutputDevice

func (s *Service) OutputDevice(index uint32) (OutputDevice, error)

OutputDevice snapshots one sink.

func (*Service) OutputDevices

func (s *Service) OutputDevices() []OutputDevice

OutputDevices lists every sink by index.

func (*Service) PlaybackStreams

func (s *Service) PlaybackStreams() []AudioStream

PlaybackStreams lists the applications playing audio.

func (*Service) RecordingStreams

func (s *Service) RecordingStreams() []AudioStream

RecordingStreams lists the applications recording audio.

func (*Service) SetDefault

func (s *Service) SetDefault(ctx context.Context, key DeviceKey) error

SetDefault makes a device the default of its kind.

func (*Service) SetDeviceMute

func (s *Service) SetDeviceMute(ctx context.Context, key DeviceKey, muted bool) error

SetDeviceMute mutes or unmutes a device.

func (*Service) SetDevicePort

func (s *Service) SetDevicePort(ctx context.Context, key DeviceKey, port string) error

SetDevicePort switches a device's active port (headphones vs speakers).

func (*Service) SetDeviceVolume

func (s *Service) SetDeviceVolume(ctx context.Context, key DeviceKey, v Volume) error

SetDeviceVolume sets a device's per-channel volume.

func (*Service) SetMuted

func (s *Service) SetMuted(ctx context.Context, muted bool) error

SetMuted implements Source.

func (*Service) SetSourceMuted

func (s *Service) SetSourceMuted(ctx context.Context, muted bool) error

SetSourceMuted implements Source.

func (*Service) SetStreamMute

func (s *Service) SetStreamMute(ctx context.Context, key StreamKey, muted bool) error

SetStreamMute mutes or unmutes a stream.

func (*Service) SetStreamVolume

func (s *Service) SetStreamVolume(ctx context.Context, key StreamKey, v Volume) error

SetStreamVolume sets a stream's per-channel volume.

func (*Service) SetVolume

func (s *Service) SetVolume(ctx context.Context, percent float64) error

SetVolume implements Source: the DBus SetOutputVolume semantics, 0-100 over the device's channel count.

func (*Service) Subscribe

func (s *Service) Subscribe(ctx context.Context) (<-chan struct{}, func(), error)

Subscribe ticks after every applied change, a lost or regained connection included; ticks coalesce, so a slow reader sees the latest state on its next read rather than a backlog. The channel closes when ctx ends or the service closes; stop unsubscribes early.

type Source

type Source interface {
	// DefaultSink returns the default output's snapshot, or
	// ErrNoDefaultDevice.
	DefaultSink(ctx context.Context) (OutputDevice, error)
	// DefaultSource returns the default input's snapshot.
	DefaultSource(ctx context.Context) (InputDevice, error)
	// Subscribe ticks on audio state changes (see Service.Subscribe).
	Subscribe(ctx context.Context) (<-chan struct{}, func(), error)
	// SetVolume applies a percentage, clamped to 0-100, to every
	// channel of the default sink.
	SetVolume(ctx context.Context, percent float64) error
	// SetMuted mutes or unmutes the default sink.
	SetMuted(ctx context.Context, muted bool) error
	// SetSourceMuted mutes or unmutes the default source.
	SetSourceMuted(ctx context.Context, muted bool) error
	// OutputDevices, InputDevices, and PlaybackStreams snapshot the
	// lists the audio dropdown renders.
	OutputDevices() []OutputDevice
	InputDevices() []InputDevice
	PlaybackStreams() []AudioStream
	// SetDeviceVolume, SetDeviceMute, and SetDefault control one
	// device; SetStreamVolume and SetStreamMute one stream.
	SetDeviceVolume(ctx context.Context, key DeviceKey, v Volume) error
	SetDeviceMute(ctx context.Context, key DeviceKey, muted bool) error
	SetDefault(ctx context.Context, key DeviceKey) error
	SetStreamVolume(ctx context.Context, key StreamKey, v Volume) error
	SetStreamMute(ctx context.Context, key StreamKey, muted bool) error
	// Capture records a target until closed (see Service.Capture).
	Capture(target CaptureTarget, spec CaptureSpec, onData func([]byte)) (*Capture, error)
}

Source is the shell's seam onto the audio service: the default devices the volume and microphone modules, the OSD, and the audio dropdown render, their controls, and the monitor capture the cava visualizer records. *Service implements it; tests substitute fakes.

type StreamKey

type StreamKey struct {
	Index uint32
	Type  StreamType
}

StreamKey identifies a stream.

type StreamNotFoundError

type StreamNotFoundError struct {
	Key StreamKey
}

StreamNotFoundError reports a lookup of an unknown stream.

func (*StreamNotFoundError) Error

func (e *StreamNotFoundError) Error() string

type StreamState

type StreamState int

StreamState is a stream's state. PulseAudio's introspection only distinguishes corked from running, so those are the two the service produces (conversion/stream.rs).

const (
	StreamRunning StreamState = iota
	StreamCorked
)

Stream states.

func (StreamState) String

func (s StreamState) String() string

type StreamType

type StreamType int

StreamType is playback (sink input) or record (source output).

const (
	StreamPlayback StreamType = iota
	StreamRecord
)

Stream types.

func (StreamType) String

func (t StreamType) String() string

type Volume

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

Volume is a per-channel linear volume (volume/types.rs's Volume): 0.0 is silent, 1.0 normal, up to 4.0 amplified. It is immutable; the With* methods return a changed copy.

func MonoVolume

func MonoVolume(v float64) Volume

MonoVolume is a one-channel volume.

func MutedVolume

func MutedVolume(channels int) Volume

MutedVolume is channels at 0.0.

func NewVolume

func NewVolume(levels []float64) Volume

NewVolume clamps each channel into 0..4, logging the clamps and the levels past the safe 2.0, as Volume::new does.

func NormalVolume

func NormalVolume(channels int) Volume

NormalVolume is channels at 1.0.

func StereoVolume

func StereoVolume(left, right float64) Volume

StereoVolume is a left/right volume.

func VolumeFromPercentage

func VolumeFromPercentage(percent float64, channels int) Volume

VolumeFromPercentage spreads one percentage (100 = 1.0) over channels.

func VolumeWithAmplification

func VolumeWithAmplification(levels []float64) (Volume, error)

VolumeWithAmplification validates instead of clamping (Volume::with_amplification): any level outside 0..4 is an error.

func (Volume) Average

func (v Volume) Average() float64

Average is the channel mean, 0 for no channels.

func (Volume) AveragePercentage

func (v Volume) AveragePercentage() float64

AveragePercentage is the channel mean as a percentage.

func (Volume) Channel

func (v Volume) Channel(i int) (float64, bool)

Channel returns one channel's level.

func (Volume) Channels

func (v Volume) Channels() int

Channels is the channel count.

func (Volume) Equal

func (v Volume) Equal(o Volume) bool

Equal compares channel by channel.

func (Volume) IsMuted

func (v Volume) IsMuted() bool

IsMuted reports every channel at 0.0.

func (Volume) IsNormal

func (v Volume) IsNormal() bool

IsNormal reports every channel at 1.0.

func (Volume) Levels

func (v Volume) Levels() []float64

Levels copies the per-channel levels.

func (Volume) ToPercentage

func (v Volume) ToPercentage() []float64

ToPercentage is each channel as a percentage.

func (Volume) WithChannel

func (v Volume) WithChannel(i int, level float64) (Volume, error)

WithChannel returns a copy with one channel set (clamped like NewVolume); a missing channel is an InvalidChannelError.

Directories

Path Synopsis
Package native is a PulseAudio native-protocol client in pure Go: the pstream framing, the tagstruct encoding, the handshake, the introspection and control commands wayle-audio drives through libpulse, the subscription events, and record streams.
Package native is a PulseAudio native-protocol client in pure Go: the pstream framing, the tagstruct encoding, the handshake, the introspection and control commands wayle-audio drives through libpulse, the subscription events, and record streams.
Package pulsetest is a fake PulseAudio server for tests: a unix socket speaking the native protocol's real frames, backed by a scriptable object model (sinks, sources, streams, defaults) that answers introspection, applies control commands, emits subscription events the way pulsecore does, serves record streams, and can inject server errors, malformed frames, and disconnects.
Package pulsetest is a fake PulseAudio server for tests: a unix socket speaking the native protocol's real frames, backed by a scriptable object model (sinks, sources, streams, defaults) that answers introspection, applies control commands, emits subscription events the way pulsecore does, serves record streams, and can inject server errors, malformed frames, and disconnects.

Jump to

Keyboard shortcuts

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