Documentation
¶
Overview ¶
Package audio plays and records sound (SPEC §28.3): a System lists the devices, opens a playback or a capture Device, and the streams of a device are mixed into it. Each stream is converted to the format of its device (the channels, the rate and the speed) by the code of SDL 3's audio streams, which is ported to Go in internal/audiocvt, and all the streams of a device are mixed in float32 and clipped. Reading sound files is in package audio/wav; other codecs are not in Glutter.
Samples are interleaved: one sample for each channel of a frame, a number of frames a second (Format.Rate). A stream takes float32 samples from -1 to 1, or signed 16-bit and 32-bit integers, which are converted when they are put. The channels are in SDL's order: mono (FC); stereo (FL FR); 2.1 (FL FR LFE); quad (FL FR BL BR); 4.1 (FL FR LFE BL BR); 5.1 (FL FR FC LFE BL BR); 6.1 (FL FR FC LFE BC SL SR); 7.1 (FL FR FC LFE BL BR SL SR).
The backend of a device runs on a thread of its own, so Stream.Put and Stream.Get may be called from any goroutine, and the function of a callback stream runs on that thread and must not touch the framework. Everything else is for the UI thread, as the rest of the framework (SPEC §13). Flutter has no audio of its own; this package is Glutter's.
Flutter: no counterpart
Index ¶
- Constants
- type Device
- func (d *Device) AddListener(fn func()) (remove func())
- func (d *Device) Capture(in []float32)
- func (d *Device) Close() error
- func (d *Device) Fill(out []float32)
- func (d *Device) Format() Format
- func (d *Device) Gain() float64
- func (d *Device) Kind() DeviceKind
- func (d *Device) NewCallbackStream(format Format, fill func(out []float32)) *Stream
- func (d *Device) NewStream(format Format) *Stream
- func (d *Device) Pause() error
- func (d *Device) Resume() error
- func (d *Device) SetGain(g float64)
- func (d *Device) State() DeviceState
- func (d *Device) StateChanged(s platform.AudioState)
- type DeviceInfo
- type DeviceKind
- type DeviceOpts
- type DeviceState
- type Format
- type Stream
- func (st *Stream) Clear()
- func (st *Stream) Close()
- func (st *Stream) Flush()
- func (st *Stream) Format() Format
- func (st *Stream) Gain() float64
- func (st *Stream) Get(out []float32) int
- func (st *Stream) Put(samples any) error
- func (st *Stream) Queued() time.Duration
- func (st *Stream) SetGain(g float64)
- func (st *Stream) SetSpeed(ratio float64)
- func (st *Stream) Speed() float64
- type System
- func (s *System) AddListener(fn func()) (remove func())
- func (s *System) Available() bool
- func (s *System) Close()
- func (s *System) Devices(kind DeviceKind) []DeviceInfo
- func (s *System) OpenCapture(ctx context.Context, opts ...DeviceOpts) (*Device, error)
- func (s *System) OpenPlayback(ctx context.Context, opts ...DeviceOpts) (*Device, error)
Constants ¶
const ( // MinRate is the lowest sample rate of a stream. // // Flutter: no counterpart MinRate = 1000 // MaxRate is the highest sample rate of a stream. // // Flutter: no counterpart MaxRate = 768000 // MaxChannels is the most channels of a stream (7.1). // // Flutter: no counterpart MaxChannels = audiocvt.MaxChannels )
The limits of a Format.
const ( // // Flutter: no counterpart TagUnavailable = platform.TagAudioUnavailable // TagDeviceOpenFailed is the tag of the error of a device that cannot be opened. // // Flutter: no counterpart TagDeviceOpenFailed = platform.TagAudioDeviceOpenFailed // TagDeviceLost is the tag of the error of a call on a device that is gone. // // Flutter: no counterpart TagDeviceLost = platform.TagAudioDeviceLost // TagFormatUnsupported is the tag of the error of a format that a device cannot work in, and of samples that are not a whole // number of frames or not of a type that a stream takes. // // Flutter: no counterpart TagFormatUnsupported = platform.TagAudioFormatUnsupported // TagCapturePermissionDenied is the tag of the error of a recording that the user did not allow. // // Flutter: no counterpart TagCapturePermissionDenied = platform.TagAudioCapturePermissionDenied // TagStreamClosed is the tag of the error of a call on a stream that was closed, or whose device was. // // Flutter: no counterpart TagStreamClosed = "AudioStreamClosed" // TagWrongKind is the tag of the error of Put on the stream of a capture device, or Get on the stream of a playback device. // // Flutter: no counterpart TagWrongKind = "AudioStreamWrongKind" )
The tags of the errors of this package (SPEC §28.7).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is an open playback or capture device. Streams made with NewStream are mixed into a playback device (or fed from a capture device). It is a platform.AudioHandler: the backend calls it on the audio thread.
Flutter: no counterpart
func (*Device) AddListener ¶
func (d *Device) AddListener(fn func()) (remove func())
AddListener adds a function that is called, on the UI thread, when the state of the device changes (it is paused or resumed, suspended or running again, lost or closed). It returns the function that removes it.
Flutter: no counterpart
func (*Device) Capture ¶
Capture implements platform.AudioHandler: it is the audio thread, and gives the recording to the streams.
func (*Device) Close ¶
Close ends the device and its streams. When it returns no more of the app's callbacks run. It may be called more than once.
Flutter: no counterpart
func (*Device) Fill ¶
Fill implements platform.AudioHandler: it is the audio thread, and mixes the streams into out.
func (*Device) Format ¶
Format returns the format of the device: the one the streams are converted to (or from). It may differ from the one that was asked for in DeviceOpts, and does not change while the device is open.
Flutter: no counterpart
func (*Device) Kind ¶
func (d *Device) Kind() DeviceKind
Kind says whether the device plays or records.
Flutter: no counterpart
func (*Device) NewCallbackStream ¶
NewCallbackStream makes a stream of a playback device whose samples come from fill: the device calls it, on the audio thread, when it needs more, with a buffer of interleaved float32 frames in the format of the stream, to be filled completely (what it leaves is silence). fill must not touch the framework, wait for the UI thread or allocate. A callback stream has no Put. It panics with AudioFormatUnsupported when the format is outside the limits or the device records.
Flutter: no counterpart
func (*Device) NewStream ¶
NewStream makes a stream of the given format for the device. The streams of a playback device are played mixed together; the format is the one that Put takes. For a capture device the format is the one that Get gives. It panics with AudioFormatUnsupported when the format is outside the limits (a rate from MinRate to MaxRate and 1 to MaxChannels channels).
Flutter: no counterpart
func (*Device) Pause ¶
Pause stops the device from playing or recording; Resume starts it again. The streams keep what they have queued.
Flutter: no counterpart
func (*Device) SetGain ¶
SetGain sets the volume of the whole device: 1 is as it is, 0 is silence; a negative gain is 0. It is applied to the mix of the streams, which are clipped after it.
Flutter: no counterpart
func (*Device) State ¶
func (d *Device) State() DeviceState
State returns the state of the device. It may be called from any goroutine.
Flutter: no counterpart
func (*Device) StateChanged ¶
func (d *Device) StateChanged(s platform.AudioState)
StateChanged implements platform.AudioHandler.
type DeviceInfo ¶
type DeviceInfo struct {
// ID names the device for DeviceOpts.ID.
ID string
// Name is what to show to the user.
Name string
Kind DeviceKind
// Default says that the system uses this device when none is asked for.
Default bool
// Format is the format that the device likes best; zero when the backend does not say.
Format Format
}
DeviceInfo describes a device that the system has.
Flutter: no counterpart
type DeviceKind ¶
type DeviceKind uint8
DeviceKind says whether a device plays sound or records it.
Flutter: no counterpart
const ( // Playback is a device that plays sound. // // Flutter: no counterpart Playback DeviceKind = iota // Capture is a device that records sound. // // Flutter: no counterpart Capture )
func (DeviceKind) String ¶
func (k DeviceKind) String() string
String names the kind.
Flutter: no counterpart
type DeviceOpts ¶
type DeviceOpts struct {
// ID is the device to open (DeviceInfo.ID); empty is the default device, which the device follows when the system's default
// changes.
ID string
// Format is the format the app would like the device to work in; a zero field leaves it to the backend. The device may work
// in another format (Device.Format says); the streams are converted either way.
Format Format
// BufferFrames is the size of the device's buffer in frames; 0 lets the backend choose a latency within the targets of SPEC §28.6.
BufferFrames int
// PauseInBackground makes the device pause while the app is hidden. The default is true on Android and false elsewhere.
PauseInBackground foundation.Opt[bool]
}
DeviceOpts are the options of OpenPlayback and OpenCapture.
Flutter: no counterpart
type DeviceState ¶
type DeviceState uint8
DeviceState is the state of a device.
Flutter: no counterpart
const ( // DeviceStateRunning is a device that plays or records. // // Flutter: no counterpart DeviceStateRunning DeviceState = iota // DeviceStatePaused is a device that was paused with Device.Pause, or by the platform because the app is hidden. // // Flutter: no counterpart DeviceStatePaused // DeviceStateSuspended is a device that waits for the user: on the web a page may not make sound before the user has // pressed a key or clicked. The device runs by itself afterwards, and the listeners of the device are told. // // Flutter: no counterpart DeviceStateSuspended // DeviceStateLost is a device that is gone (unplugged, or the audio server ended) and that was not replaced. // // Flutter: no counterpart DeviceStateLost // DeviceStateClosed is a device that was closed. // // Flutter: no counterpart DeviceStateClosed )
type Format ¶
Format is the format of audio: the sample rate in frames a second and the number of channels, from 1 to 8.
Flutter: no counterpart
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is a flow of samples into a playback device, which mixes it with the other streams of the device, or out of a capture device. A stream has the format of the app's samples; the conversion to the device's format (channels, rate and speed) is done when the device needs the samples. Put and Get may be called from any goroutine.
Flutter: no counterpart
func (*Stream) Clear ¶
func (st *Stream) Clear()
Clear drops what is queued, so that it is not played (or not read).
Flutter: no counterpart
func (*Stream) Close ¶
func (st *Stream) Close()
Close takes the stream out of its device; what is queued is dropped. It may be called more than once.
Flutter: no counterpart
func (*Stream) Flush ¶
func (st *Stream) Flush()
Flush says that no more samples come for now, so that the last ones are played out too: a stream whose rate or speed differs from the device's holds back the last few frames until the next ones come, which a sound that ends has none of. Put after Flush starts a new sound.
Flutter: no counterpart
func (*Stream) Format ¶
Format returns the format of the samples of the stream: the ones that Put takes, that the function of a callback stream makes, or that Get gives.
Flutter: no counterpart
func (*Stream) Get ¶
Get reads the sound that a capture device recorded into out, converted to the format of the stream, and returns the number of frames that it wrote (out holds len(out)/Channels frames at most). It does not wait: it returns 0 when nothing is queued. The gain of the stream applies. A stream of a playback device returns 0.
Flutter: no counterpart
func (*Stream) Put ¶
Put queues samples to be played: a []float32 (from -1 to 1), []int16 or []int32, interleaved, in the format of the stream, a whole number of frames. It fails with AudioFormatUnsupported for any other type or for samples that are not a whole number of frames, AudioStreamWrongKind on the stream of a capture device or a callback stream, AudioStreamClosed on a stream that was closed, and AudioDeviceLost when the device is gone. The samples are copied.
Flutter: no counterpart
func (*Stream) Queued ¶
Queued returns how much sound is queued: for a playback stream what has been put and not played, for a capture stream what has been recorded and not read.
Flutter: no counterpart
func (*Stream) SetGain ¶
SetGain sets the volume of the stream: 1 is as it is, 0 is silence; a negative gain is 0. It takes effect at the next frames the device asks for.
Flutter: no counterpart
type System ¶
type System struct {
// contains filtered or unexported fields
}
System is the audio of the app: the devices of the system, and the devices that the app opened. Get it with Of. It is made when it is first asked for, and closed with the app.
Flutter: no counterpart
func ForBinding ¶
func ForBinding(b *widgets.WidgetsBinding) *System
ForBinding returns the audio of the binding, for code that has no BuildContext.
Flutter: no counterpart
func Of ¶
func Of(bc widgets.BuildContext) *System
Of returns the audio of the app (the one of the binding). An app on a platform that has no audio service gets a System whose calls fail with AudioUnavailable.
Flutter: no counterpart
func (*System) AddListener ¶
func (s *System) AddListener(fn func()) (remove func())
AddListener adds a function that is called, on the UI thread, when the list of devices or the default device changed. It returns the function that removes it.
Flutter: no counterpart
func (*System) Available ¶
Available says whether the platform has an audio service.
Flutter: no counterpart
func (*System) Close ¶
func (s *System) Close()
Close closes every device that the app opened and stops listening to the system. The engine calls it when the app ends; after it, the opens fail with AudioUnavailable. It may be called more than once.
Flutter: no counterpart
func (*System) Devices ¶
func (s *System) Devices(kind DeviceKind) []DeviceInfo
Devices lists the devices of a kind that the system has now; it is empty when the platform has no audio service or no server runs.
Flutter: no counterpart
func (*System) OpenCapture ¶
OpenCapture opens a capture device, as OpenPlayback does a playback one. It also fails with AudioCapturePermissionDenied when the user does not allow the recording. Where the system asks the user (Android, the web) the call waits for the answer, until ctx ends: call it from a goroutine that is not the UI one.
Flutter: no counterpart
func (*System) OpenPlayback ¶
OpenPlayback opens a playback device: the default one, which it follows, or DeviceOpts.ID. It fails with AudioUnavailable when the platform has no audio service or no server runs, AudioDeviceOpenFailed when the device cannot be opened (also when ctx ends first) and AudioFormatUnsupported when no format works. At most one DeviceOpts may be given.
Flutter: no counterpart
Directories
¶
| Path | Synopsis |
|---|---|
|
Package wav reads WAV files (SPEC §28.3): RIFF files with linear PCM of 8, 16, 24 or 32 bits (also in the WAVE_FORMAT_EXTENSIBLE form) or IEEE float of 32 or 64 bits, with 1 to 8 channels.
|
Package wav reads WAV files (SPEC §28.3): RIFF files with linear PCM of 8, 16, 24 or 32 bits (also in the WAVE_FORMAT_EXTENSIBLE form) or IEEE float of 32 or 64 bits, with 1 to 8 channels. |