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 ¶
- Variables
- type Conn
- type MicSource
- type Options
- type PlayStream
- func (s *PlayStream) Close()
- func (s *PlayStream) Cork(on bool) error
- func (s *PlayStream) Drain(timeout time.Duration) error
- func (s *PlayStream) Err() error
- func (s *PlayStream) Flush() error
- func (s *PlayStream) Frame() int
- func (s *PlayStream) SetVolume(gain float64) error
- func (s *PlayStream) Timing() (Timing, error)
- func (s *PlayStream) Underflows() int64
- func (s *PlayStream) Wait(timeout time.Duration) (int, error)
- func (s *PlayStream) Write(p []byte) error
- type Provider
- func (p *Provider) AudioCaps(id string) (capture.AudioCaps, bool)
- func (p *Provider) Devices() []capture.DeviceInfo
- func (p *Provider) Name() string
- func (p *Provider) OpenAudio(id string) (capture.AudioSource, error)
- func (p *Provider) OpenVideo(string) (capture.VideoSource, error)
- func (p *Provider) Refresh()
- func (p *Provider) VideoCaps(string) (capture.VideoCaps, bool)
- type ServerError
- type Source
- type Timing
Constants ¶
This section is empty.
Variables ¶
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 (*Conn) DefaultSource ¶
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).
type MicSource ¶
type MicSource struct {
// contains filtered or unexported fields
}
MicSource records from one source with its own connection.
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.
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) 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 ¶
NewProviderWith is NewProvider with explicit options (tests, tools).
func (*Provider) AudioCaps ¶
AudioCaps implements capture.CapsProvider: the source's own rate, mono or stereo.
func (*Provider) Devices ¶
func (p *Provider) Devices() []capture.DeviceInfo
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.