pulse

package
v0.1.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 14 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 a small pure-Go client for the PulseAudio native protocol over a unix socket. It speaks to PulseAudio and to PipeWire's pulse server alike and needs no library: enough of the protocol to enumerate sources, find the default one and record from it.

The protocol version announced is 13: the oldest version with property lists, which every server since PulseAudio 0.9.22 and PipeWire accepts. A server always answers in the version the client announces, so the layouts below are the version-13 ones and do not change when the server is newer.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoServer = errors.New("pulse: no server")

ErrNoServer is returned when no PulseAudio / PipeWire server can be reached. Callers treat it as "no microphone", not as a failure.

Functions

This section is empty.

Types

type Conn

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

Conn is one connection to the server.

func Dial

func Dial(o Options) (*Conn, error)

Dial connects, authenticates and names the client.

func (*Conn) Close

func (c *Conn) Close() error

Close drops the connection (and with it every stream).

func (*Conn) DefaultSource

func (c *Conn) DefaultSource() (string, error)

DefaultSource returns the name of the server's default source.

func (*Conn) OpenPlayback

func (c *Conn) OpenPlayback(name string, rate uint32, channels int, latency time.Duration) (*PlayStream, error)

OpenPlayback creates a playback stream of 32-bit float little-endian PCM (the server converts it to what the device wants). latency is the buffer the server keeps (about the delay between Write and sound).

func (*Conn) Sources

func (c *Conn) Sources() ([]Source, error)

Sources lists every source the server has, monitors included.

type MicSource

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

MicSource records from one source with its own connection.

func (*MicSource) Err

func (m *MicSource) Err() error

func (*MicSource) Info

func (m *MicSource) Info() capture.DeviceInfo

func (*MicSource) Start

func (m *MicSource) Start(c capture.AudioConstraints) (<-chan *capture.AudioChunk, error)

Start connects and creates the record stream. The audio is signed 16-bit PCM at the source's own rate; the channel count is c.Channels (1 or 2, default 1) and the server remixes if the source has more.

func (*MicSource) Stop

func (m *MicSource) Stop() error

Stop ends the stream and the connection.

type Options

type Options struct {
	Getenv      func(string) string // default os.Getenv
	UID         int                 // default os.Getuid
	AppName     string              // default "Sweef"
	DialTimeout time.Duration       // default 1s
	// Dialer overrides the unix-socket dial (tests).
	Dial func(path string, timeout time.Duration) (net.Conn, error)
}

Options configure Dial.

type PlayStream

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

PlayStream is a playback stream to the server's default sink. The server asks for data (REQUEST) as its buffer drains; Wait blocks until it has asked, Write sends what was asked for.

func (*PlayStream) Close

func (s *PlayStream) Close()

Close deletes the stream on the server.

func (*PlayStream) Cork

func (s *PlayStream) Cork(on bool) error

Cork pauses (true) or resumes (false) playback; a corked stream receives no requests.

func (*PlayStream) Drain

func (s *PlayStream) Drain(timeout time.Duration) error

Drain returns when everything written has been played.

func (*PlayStream) Err

func (s *PlayStream) Err() error

Err is why the stream ended (nil while it is alive).

func (*PlayStream) Flush

func (s *PlayStream) Flush() error

Flush drops what the server has buffered.

func (*PlayStream) Frame

func (s *PlayStream) Frame() int

Frame is the size in bytes of one sample frame.

func (*PlayStream) SetVolume

func (s *PlayStream) SetVolume(gain float64) error

SetVolume sets the stream's volume as a linear gain (0 to 1; the server's scale is cubic).

func (*PlayStream) Timing

func (s *PlayStream) Timing() (Timing, error)

Timing asks the server (GET_PLAYBACK_LATENCY).

func (*PlayStream) Underflows

func (s *PlayStream) Underflows() int64

Underflows counts the times the server ran out of data.

func (*PlayStream) Wait

func (s *PlayStream) Wait(timeout time.Duration) (int, error)

Wait blocks until the server has asked for data, the stream ended or timeout passed. It returns the number of bytes the server wants (0 on timeout).

func (*PlayStream) Write

func (s *PlayStream) Write(p []byte) error

Write sends whole frames of PCM (p is cut to a whole number of frames); the server's outstanding request shrinks by what was sent.

type Provider

type Provider struct {

	// TTL is how long a source list is reused (default 1 s). The server is queried at most once per TTL however
	// often the engine asks; a failed query is remembered three times as long so that a machine with no sound
	// server does not pay a connection attempt per call.
	TTL time.Duration
	// contains filtered or unexported fields
}

Provider is the capture.Provider for microphones on a PulseAudio or PipeWire-pulse server.

func NewProvider

func NewProvider() *Provider

NewProvider returns a provider using the environment's server settings.

func NewProviderWith

func NewProviderWith(o Options) *Provider

NewProviderWith is NewProvider with explicit options (tests, tools).

func (*Provider) AudioCaps

func (p *Provider) AudioCaps(id string) (capture.AudioCaps, bool)

AudioCaps implements capture.CapsProvider: the source's own rate, mono or stereo.

func (*Provider) Devices

func (p *Provider) Devices() []capture.DeviceInfo

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) OpenAudio

func (p *Provider) OpenAudio(id string) (capture.AudioSource, error)

func (*Provider) OpenVideo

func (p *Provider) OpenVideo(string) (capture.VideoSource, error)

func (*Provider) Refresh

func (p *Provider) Refresh()

Refresh forces the next Devices call to ask the server again.

func (*Provider) VideoCaps

func (p *Provider) VideoCaps(string) (capture.VideoCaps, bool)

type ServerError

type ServerError struct{ Code uint32 }

ServerError is an ERROR reply.

func (*ServerError) Error

func (e *ServerError) Error() string

type Source

type Source struct {
	Index       uint32
	Name        string
	Description string
	Rate        uint32
	Channels    int
	Format      byte
	// Monitor is true for the monitor of an output (it records what is playing, not a microphone).
	Monitor bool
	// Group identifies the physical device (device.bus_path, else the part of the name shared with its output).
	Group string
	Form  string // device.form_factor property, e.g. "microphone", "headset", "webcam"
}

Source is one entry of the server's source list.

type Timing

type Timing struct {
	// Latency is the time from a byte being written to it being heard: what the server still holds of this stream
	// plus the device's own latency.
	Latency time.Duration
	Playing bool
	Written int64 // bytes the server received
	Read    int64 // bytes the device consumed
}

Timing is a snapshot of where the stream is.

Jump to

Keyboard shortcuts

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