microphone

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 19 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 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

View Source
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 says the microphone cannot record now: on Android no activity of the app is visible to ask for it or start using it, the app declares no recording service (nsgo.yaml's android.recording), or the device refused to open it.
	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

func Allowed

func Allowed() bool

Allowed says this process may record without asking.

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.

Types

type File

type File struct {
	Path     string
	Rate     int
	Channels int
	Duration time.Duration
}

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

func (r *Recording) Err() error

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

func (r *Recording) File() 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

func (r *Recording) Interrupted() bool

Interrupted says the system silences the recording now, which writes nothing meanwhile; only a recording that pauses on interruption runs on through one.

func (*Recording) Stop

func (r *Recording) Stop()

Stop ends the recording and waits for its file to be complete under its final name; calling it again does nothing. No level starts after it returns.

type Stream

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

Stream is a recording in progress.

func Capture

func Capture(rate int, samples func([]int16)) (*Stream, error)

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.

func (*Stream) Ended

func (s *Stream) Ended() <-chan struct{}

Ended closes once no more samples will arrive: after Stop, or once the system took the microphone away, as iOS does for a call or when the app leaves the screen. The recording still needs its Stop then.

func (*Stream) Stop

func (s *Stream) Stop()

Stop ends the recording. Once it returns no more samples arrive, and calling it again does nothing.

Jump to

Keyboard shortcuts

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