Documentation
¶
Overview ¶
Package microphone records the system's default input, or on macOS one the app picks from Devices, and builds on every target, reporting where it cannot record. Capture streams 16-bit mono PCM on macOS, iOS, Windows and Linux, which it reaches directly, as base/terminal does. RecordWAV writes 16-bit PCM WAV files on macOS, iOS, Windows, Linux and Android; on Android the runtime's foreground service records, which the package reaches through a channel of internal/driver outside any view, as a recording belongs to the process.
Apps check and ask for the microphone with core.Scope.PermissionStatus and core.Scope.RequestPermission of core.PermissionMicrophone; Allowed and Authorize share their implementation for code that records without an engine.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported says this target cannot record. ErrUnsupported = errors.New("microphone: not supported on this platform") // ErrDenied says the user or the system refused the app the microphone. ErrDenied = errors.New("microphone: access denied") // ErrNoUsageDescription says the process the system holds responsible // for this one, such as the app that started it, declares no reason for // recording, for which the system would abort this process. ErrNoUsageDescription = errors.New("microphone: the responsible app has no NSMicrophoneUsageDescription") ErrUnavailable = errors.New("microphone: unavailable") // ErrBusy says a recording runs already, as an app records one at a time, or another app's capture keeps this one from starting. ErrBusy = errors.New("microphone: busy") // ErrInterrupted ends a recording the system silenced, as a call does, unless RecordOptions.PauseOnInterruption keeps it. ErrInterrupted = errors.New("microphone: interrupted") // ErrFull ends a WAV recording that reached RIFF's 4 GiB, about 12 hours of 16-bit mono at 48 kHz. ErrFull = errors.New("microphone: the WAV file reached its 4 GiB limit") // ErrNoSpace ends a recording whose storage is about to run out. ErrNoSpace = errors.New("microphone: no space left for the recording") // ErrAbandoned is an earlier recording whose Go process ended while it ran: the runtime finished its file. ErrAbandoned = errors.New("microphone: the process that recorded ended") // ErrNoDevice says no input Devices lists has the ID asked for, as the device was disconnected or the target lists none. ErrNoDevice = errors.New("microphone: no input device with that ID") // ErrPartial is an earlier recording whose app was killed while it ran: the next RecordWAV repaired its file, which holds what was written until then. ErrPartial = errors.New("microphone: the recording was cut off when the app was killed") )
Functions ¶
func Authorize ¶
func Authorize() error
Authorize returns nil when an audio capture library is present on Linux, or ErrUnavailable if neither PulseAudio nor ALSA can be loaded.
func Supported ¶
func Supported() bool
Supported says this target can record (libpulse-simple or ALSA is present). While nsgotest's fake microphone is connected, it and Allowed and Authorize answer from the fake.
func WatchDevices ¶
func WatchDevices(onChange func()) (stop func())
WatchDevices calls onChange, on a goroutine of microphone's, never an engine's, whenever an input connects or disconnects or the system's default input changes, until stop is called; an app that updates its views from it hops with Scope.Post, reads Devices again, and hands stop to Scope.Cleanup. Changes that come while onChange runs call it once more after. It hears changes on macOS and from nsgotest's fake microphone, only the fake's while it is connected; elsewhere onChange never runs.
Types ¶
type Device ¶
type Device struct {
// ID names the device to RecordOptions.Device and CaptureFrom. On macOS it is Core Audio's UID, which stays the same across connections and restarts.
ID string
// Name is the device's name as the system shows it, such as "MacBook Pro Microphone".
Name string
// Default says the system records from this device when none is named.
Default bool
}
Device is an input the system records from, such as the built-in microphone, a headset or a USB interface.
func Devices ¶
func Devices() []Device
Devices lists the inputs the app can record from, the system's default among them, in the system's order. It lists them on macOS, and the fake's while nsgotest's fake microphone is connected; elsewhere it lists none, so only the default input records there. Listing needs no permission. The fake's inputs are for RecordWAV, which the fake records; CaptureFrom, which it does not stand in for, always checks the ID against the system's inputs and records from them, so an ID only the fake lists fails there with ErrNoDevice.
type File ¶
File is a recording's file: its path in the app's private storage and the format written, the rate the device gave rather than the one asked for, and the duration written so far.
type Level ¶
type Level struct {
Peak, RMS float32
}
Level is the loudness of the frames a level covers, each in [0, 1].
type RecordOptions ¶
type RecordOptions struct {
// Device is the ID of the input to record from, one of Devices'; empty records from the system's default input. Choosing one leaves the system's default alone, and an ID Devices does not list fails with ErrNoDevice.
Device string
// Rate is the sample rate asked for, in hertz; 0 takes the device's. A device that cannot record at it records at its own, which File reports.
Rate int
// Channels is 1 for mono, the default, or 2 for stereo, which a device without it records as mono.
Channels int
// Level receives the peak and RMS of the frames written over each LevelInterval, on a goroutine of microphone's, never an engine's: an app that updates its views from it hops with Scope.Post. A level it has not taken yet is replaced by the next, so a slow callback never holds up the recording.
Level func(Level)
// LevelInterval is how often Level runs; 0 is a tenth of a second.
LevelInterval time.Duration
// PauseOnInterruption keeps a recording the system silenced, as a call does: it writes nothing while Interrupted says so and goes on after. Without it the interruption ends the recording with ErrInterrupted.
PauseOnInterruption bool
}
RecordOptions configures RecordWAV.
type Recording ¶
type Recording struct {
// contains filtered or unexported fields
}
Recording is a recording to a file, which belongs to the process rather than to a view.
func Earlier ¶
func Earlier() []*Recording
Earlier returns, once each, the recordings of earlier runs that ended unseen: ErrAbandoned's, whose Go process ended while the app's lived on, and ErrPartial's, whose app was killed, which a RecordWAV repaired. Their Ended is closed. The runtime keeps each one's record until Earlier returns it, so a recording a process heard of but did not take, as it ended first, is reported to the next process; one whose file the app deleted is not.
func RecordWAV ¶
func RecordWAV(name string, o RecordOptions) (*Recording, error)
RecordWAV records 16-bit PCM WAV to the file name in the app's recordings directory, private storage under the app's data directory, until it stops or ends by itself. The file goes under a temporary name until then, and its header follows what was written about once a second, so a file whose app was killed plays up to its last second; the next RecordWAV repairs it under its name and Earlier reports it, in that process or a later one, until the app takes it. It asks for the microphone as Authorize does and waits for the answer, so call it off the UI. It records directly on macOS, iOS, Windows and Linux; on Android it records in the runtime's foreground service, through a hidden activity and the screen going off, and starts only while an activity of the app is visible, in an app whose nsgo.yaml sets android.recording; it reports ErrUnsupported elsewhere. From Android 13 the service's notification shows only once the user lets the app post notifications, so the first RecordWAV asks for POST_NOTIFICATIONS with RECORD_AUDIO, or alone where RECORD_AUDIO is granted; a user who refuses notifications still records, without a visible notification.
func (*Recording) Ended ¶
func (r *Recording) Ended() <-chan struct{}
Ended closes once the recording is over and its file complete: after Stop, or once it ended by itself, which Err says why.
func (*Recording) Err ¶
Err says why the recording ended by itself once Ended closed, such as ErrInterrupted, ErrFull or ErrNoSpace: nil while it runs and after Stop.
func (*Recording) File ¶
File is the recording's file, its duration so far until Ended closes: as of the last level, or about once a second for a recording without Level.
func (*Recording) Interrupted ¶
Interrupted says the system silences the recording now, which writes nothing meanwhile; only a recording that pauses on interruption runs on through one.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is a recording in progress.
func Capture ¶
Capture records 16-bit mono at rate hertz from the system's default input, as CaptureFrom does with no device.
func CaptureFrom ¶
CaptureFrom records 16-bit mono PCM at rate hertz from the default input via PulseAudio (or PipeWire's PulseAudio compatibility layer) with an ALSA fallback, passing samples to the callback until the Stream stops or the input stream ends. Linux lists no devices yet, so a device other than "" answers ErrNoDevice.