Documentation
¶
Overview ¶
Package audio carries Planck's game audio: capture on the host, Opus encode and decode, a jitter buffer on the client, and playback. All PCM is interleaved 48 kHz stereo int16.
Index ¶
- Constants
- Variables
- func AppPIDs(names []string) ([]int, error)
- func FrameSamples(d time.Duration) int
- func MatchApps(apps []App, names []string) []int
- func MuteOutput() (func() error, error)
- func PutPCM16(b []byte, pcm []int16) int
- type App
- type CaptureOptions
- type Capturer
- type Decoder
- type Device
- type Encoder
- type EncoderConfig
- type FrameDecoder
- type JitterBuffer
- type JitterConfig
- type JitterStats
- type MicCapturer
- type MicSink
- type PCMDecoder
- type Player
- type Playout
- type PopStatus
- type VirtualAudioStatus
- type VirtualMic
Constants ¶
const ( SampleRate = 48000 Channels = 2 )
const ( ModeProcessLoopback = "process-loopback" // Windows, without Planck's process tree ModeEndpointLoopback = "endpoint-loopback" // Windows default render endpoint, Planck included ModeProcessTap = "process-tap" // macOS Core Audio tap, without Planck ModeScreenCaptureKit = "screencapturekit" // macOS, without Planck ModeMicrophone = "microphone" )
Capture paths a capturer's Mode reports.
const ( DefaultFrameDuration = 10 * time.Millisecond DefaultBitrate = 128000 // MaxPacketSize is the largest single-frame Opus packet (RFC 6716 3.2.1). MaxPacketSize = 1275 )
const CanExcludeProcesses = false
const MicChannels = 1
MicChannels is the channel count of microphone PCM: the client captures and the host plays mono.
const MicPCMFrameDuration = 5 * time.Millisecond
MicPCMFrameDuration is the frame of the raw PCM mic codec: 240 mono samples, 480 bytes, one datagram each.
const PlanckVirtualAudio = "Planck Virtual Audio"
PlanckVirtualAudio is the name of the device the Planck Virtual Audio drivers in drivers/audio create, on Windows and macOS alike.
const VirtualMicHint = "Virtual mic not detected — install Planck Virtual Audio (or Steam Streaming Microphone / another virtual audio device)"
VirtualMicHint is what a host without a virtual input device tells the client. Planck does not install one itself: Planck Virtual Audio ships in drivers/audio/windows and the user installs it.
Variables ¶
var ( // ErrNoVirtualMic: no user-installed virtual input device matched. ErrNoVirtualMic = errors.New("audio: " + VirtualMicHint) // ErrVirtualMicSetup: a virtual mic was found but will not pass audio // as it is set up; the error says what to change. ErrVirtualMicSetup = errors.New("audio: virtual mic needs setup") // ErrUnsupported: this platform has no backend for the call yet. ErrUnsupported = errors.New("audio: not supported on this platform") )
var ErrClosed = errors.New("audio: closed")
Functions ¶
func FrameSamples ¶
FrameSamples is the number of samples per channel in one frame of d.
func MatchApps ¶
MatchApps are the PIDs of apps whose name starts with one of names, in any case: "Discord" takes "Discord Helper (Renderer)" too, where an Electron app plays its sound.
func MuteOutput ¶
Types ¶
type App ¶
App is a running process, by its executable's name without a path or ".exe".
func RunningApps ¶
type CaptureOptions ¶
type CaptureOptions struct {
// ExcludePIDs are more processes to leave out, such as a voice chat
// app. The macOS process tap honours them; Windows process loopback
// takes a single process tree, so there they are not supported
// (CanExcludeProcesses).
ExcludePIDs []int
// DeviceID records what one output device plays, by Device ID, Planck
// included, on Windows; empty, or a device that is gone, records
// everything but Planck. macOS records every device.
DeviceID string
}
CaptureOptions tunes desktop audio capture. Every backend leaves out Planck's own process, so the host never hears what it plays itself.
type Capturer ¶
type Capturer interface {
// Start begins capture and returns once the device is running. onPCM
// receives interleaved stereo samples and the time of the first sample
// in nanoseconds on the platform capture clock (mach absolute time on
// macOS, QueryPerformanceCounter on Windows), the clock video frames are
// stamped with. The stamps differ by platform: ScreenCaptureKit's lag
// delivery by about 25ms, while WASAPI loopback reports the endpoint's
// play-out time, about 27ms after delivery on bigboi. pcm is reused
// after onPCM returns. Capture stops when ctx ends or Close is called.
Start(ctx context.Context, onPCM func(pcm []int16, pts int64)) error
Close() error
}
Capturer records what the host is playing.
func NewCapturer ¶
func NewCapturer(CaptureOptions) (Capturer, error)
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder turns Opus packets back into 48 kHz PCM frames and conceals lost ones. It is not safe for concurrent use.
func NewDecoder ¶
NewDecoder makes a stereo decoder for frames of frameDuration (10ms or 20ms; zero selects DefaultFrameDuration), the length Conceal fills.
func NewDecoderChannels ¶
NewDecoderChannels is NewDecoder for a channel count of 1 or 2.
func (*Decoder) Decode ¶
Decode writes one frame into pcm, which must hold FrameSamples()*Channels() values, and returns the samples per channel written.
func (*Decoder) FrameSamples ¶
type Device ¶
type Device struct {
// ID is stable across runs: a Core Audio UID on macOS, an MMDevice
// endpoint ID on Windows.
ID string
Name string
IsDefault bool
}
Device is an audio device of this machine.
func InputDevices ¶
func OutputDevices ¶
func PickDevice ¶
PickDevice is the device of devs with id, or the default one when id is empty or names none of them; gone reports that id named a device that is not there. An empty Device stands for the default when devs has none marked.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder turns 48 kHz PCM frames into Opus packets. By default it uses the Opus low-delay application (CELT only), which has the smallest algorithmic delay. It is not safe for concurrent use.
func NewEncoder ¶
func NewEncoder(cfg EncoderConfig) (*Encoder, error)
func (*Encoder) Encode ¶
Encode compresses exactly one frame (FrameSamples() times the channel count values) into packet and returns the packet length. packet should hold MaxPacketSize bytes.
func (*Encoder) FrameSamples ¶
FrameSamples is the number of samples per channel Encode expects.
type EncoderConfig ¶
type EncoderConfig struct {
FrameDuration time.Duration // 10ms or 20ms; zero selects DefaultFrameDuration
Bitrate int // bits per second; zero selects DefaultBitrate
Channels int // 1 or 2; zero selects Channels
// Voice selects the Opus VoIP application, tuned for speech, in place
// of low delay.
Voice bool
}
type FrameDecoder ¶
type FrameDecoder interface {
FrameSamples() int
Channels() int
Decode(packet []byte, pcm []int16) (int, error)
Conceal(pcm []int16) (int, error)
}
FrameDecoder turns one packet into one frame of PCM, or conceals a lost one. *Decoder (Opus) and *PCMDecoder are FrameDecoders.
type JitterBuffer ¶
type JitterBuffer struct {
// contains filtered or unexported fields
}
JitterBuffer reorders Opus packets by sequence number and hands them out one frame per play-out tick. Its target depth grows by a frame on each underrun and shrinks by a frame after a quiet stretch, staying within [MinDepth, MaxDepth]. Push and Pop may run on different goroutines.
func NewJitterBuffer ¶
func NewJitterBuffer(cfg JitterConfig) *JitterBuffer
func (*JitterBuffer) Pop ¶
func (j *JitterBuffer) Pop(dst []byte) (int, PopStatus)
Pop is called once per frame duration by the play-out clock. On Packet it copies the payload into dst (which should hold MaxPacketSize bytes) and returns its length.
func (*JitterBuffer) Push ¶
func (j *JitterBuffer) Push(seq uint16, payload []byte)
Push stores a copy of payload as packet seq.
func (*JitterBuffer) Restart ¶
func (j *JitterBuffer) Restart()
Restart drops what is buffered; the next packet starts a new sequence, as a new stream from the same host numbers its packets afresh.
func (*JitterBuffer) Stats ¶
func (j *JitterBuffer) Stats() JitterStats
type JitterConfig ¶
type JitterStats ¶
type JitterStats struct {
Depth time.Duration // frames queued, counting gaps, times frame duration
Target time.Duration
Received uint64
Late uint64 // arrived after its play-out slot
Duplicate uint64
Lost uint64 // play-out slots concealed while later packets were queued
Underruns uint64 // play-out slots concealed with nothing queued
Overruns uint64 // depth passed MaxDepth and was cut back to target
Dropped uint64 // packets skipped to cut depth
Resets uint64 // sequence jumps, either way, too large to bridge
Concealment uint64 // Lost + Underruns
}
type MicCapturer ¶
type MicCapturer interface {
Start(ctx context.Context, onPCM func(pcm []int16, pts int64)) error
Close() error
}
MicCapturer records the client's default microphone as interleaved 48 kHz mono int16. Start and Close behave as Capturer's; pts is on the same clock.
func NewMicCapturer ¶
func NewMicCapturer() (MicCapturer, error)
func NewMicCapturerOn ¶
func NewMicCapturerOn(string) (MicCapturer, error)
type MicSink ¶
type MicSink interface {
Player
// Device is the name of the device apps record from.
Device() string
}
MicSink plays the client's microphone (48 kHz mono int16) into a virtual input device on the host, so voice chat and games hear it.
func OpenMicSink ¶
func OpenMicSink(VirtualMic) (MicSink, error)
type PCMDecoder ¶
type PCMDecoder struct {
// contains filtered or unexported fields
}
PCMDecoder is the FrameDecoder of the raw PCM16 mic codec. It conceals a lost frame by repeating the last one at half volume each time; Playout fades to silence after a few.
func NewPCMDecoder ¶
func NewPCMDecoder(frameDuration time.Duration, channels int) *PCMDecoder
func (*PCMDecoder) Channels ¶
func (d *PCMDecoder) Channels() int
func (*PCMDecoder) FrameSamples ¶
func (d *PCMDecoder) FrameSamples() int
type Playout ¶
type Playout struct {
// contains filtered or unexported fields
}
Playout turns a JitterBuffer and a FrameDecoder into a steady PCM stream: one frame per Next call, decoded, concealed, or silent.
func NewPlayout ¶
func NewPlayout(jb *JitterBuffer, dec FrameDecoder) *Playout
type VirtualAudioStatus ¶
type VirtualAudioStatus struct {
// Installed: the Planck Virtual Audio driver is installed, whether or
// not its device is running.
Installed bool
// Active: a Planck Virtual Audio device is running now.
Active bool
// DeviceName is the output device Auto picks, "" when there is none.
DeviceName string
// DriverName is the virtual audio driver behind DeviceName, as
// "Planck Virtual Audio" or "Steam Streaming Microphone".
DriverName string
// InstallHint says how to get Planck Virtual Audio running; "" when
// it is.
InstallHint string
}
VirtualAudioStatus is whether Planck Virtual Audio is on this machine, and which virtual mic a host on Auto plays a client's microphone into.
func VirtualAudio ¶
func VirtualAudio() (VirtualAudioStatus, error)
VirtualAudio reports Planck Virtual Audio's driver and device, and the virtual mic Auto would use.
type VirtualMic ¶
type VirtualMic struct {
ID string
Name string
Input string
// contains filtered or unexported fields
}
VirtualMic is a user-installed virtual audio device: Planck writes to its playback side (ID, Name) and apps record from its input side (Input).
func FindVirtualMic ¶
func FindVirtualMic(want string) (VirtualMic, error)
FindVirtualMic looks for a virtual input device among the output devices: the one whose ID is want or whose name contains it, or a known one when want is empty.