Documentation
¶
Overview ¶
Package microphone records the system's default input 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") // 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 ¶
Types ¶
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 {
// 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 PCM at rate hertz 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.