Crate wasapi

Source
Expand description

§Wasapi bindings for Rust

The aim of this crate is to provide easy and safe access to the Wasapi API for audio playback and capture.

The presented API is all safe Rust, but structs and functions closely follow the original Windows API.

For details on how to use Wasapi, please see the Windows documentation.

Bindings are generated automatically using the windows crate.

§Supported functionality

These things have been implemented so far:

  • Audio playback and capture
  • Shared and exclusive modes
  • Event-driven and polled buffering
  • Loopback capture
  • Notifications for volume change, device disconnect etc

§Included examples

ExampleDescription
playsinePlays a sine wave in shared mode on the default output device.
playsine_eventsSimilar to playsine but also listens to notifications.
playsine_pollSimilar to playsine but uses polling instead of event driven timing mode.
playnoise_exclusivePlays white noise in exclusive mode on the default output device. Shows how to handle HRESULT errors.
playnoise_exclusive_pollSimilar to playnoise_exclusive but uses polling instead of event driven timing mode.
loopbackShows how to simultaneously capture and render sound, with separate threads for capture and render.
recordRecords audio from the default device, and saves the raw samples to a file.
devicesLists all available audio devices and displays the default devices.
record_applicationRecords audio from a single application, and saves the raw samples to a file.

Structs§

AudioCaptureClient
Struct wrapping an IAudioCaptureClient.
AudioClient
Struct wrapping an IAudioClient.
AudioClock
Struct wrapping an IAudioClock.
AudioRenderClient
Struct wrapping an IAudioRenderClient.
AudioSessionControl
Struct wrapping an IAudioSessionControl.
BufferFlags
Struct representing the _AUDCLNT_BUFFERFLAGS enum values.
Device
Struct wrapping an IMMDevice.
DeviceCollection
Struct wrapping an IMMDeviceCollection.
DeviceCollectionIter
Iterator for DeviceCollection
EventCallbacks
A structure holding the callbacks for notifications
EventRegistration
Struct for keeping track of the registered notifications.
GUID
A globally unique identifier (GUID) used to identify COM and WinRT interfaces.
Handle
Struct wrapping a [HANDLE] to an Event Object.
WaveFormat
Struct wrapping a WAVEFORMATEXTENSIBLE format descriptor.

Enums§

DeviceState
Possible states for an [IMMDevice], an enum representing the DEVICE_STATE_XXX constants
Direction
Audio direction, playback or capture.
DisconnectReason
Reason for session disconnect, an enum representing the DisconnectReasonXxx values of the AudioSessionDisconnectReason enum
Role
Wrapper for ERole. Console is the role used by most applications
SampleType
Sample type, float or integer
SessionState
Possible states for an AudioSessionControl, an enum representing the AudioSessionStateXxx constants
ShareMode
Sharemode for device
StreamMode
Helper enum for initializing an AudioClient. There are four main modes that can be specified, corresponding to the four possible combinations of sharing mode and timing. The enum variants only expose the parameters that can be set in each mode.
TimingMode
Timing mode for device
WasapiError

Functions§

calculate_period_100ns
Calculate a period in units of 100ns that corresponds to the given number of buffer frames at the given sample rate. See the IAudioClient documentation.
deinitialize
Close the COM library on the current thread.
get_default_device
Get the default playback or capture device for the console role
get_default_device_for_role
Get the default playback or capture device for a specific role
initialize_mta
Initializes COM for use by the calling thread for the multi-threaded apartment (MTA).
initialize_sta
Initializes COM for use by the calling thread for a single-threaded apartment (STA).
make_channelmasks
Return a vector with suggested channel masks for the given number of channels. Used to find a format that a device accepts in exclusive mode. The values are sorted according to how likely they are to be accepted, with the most likely first.
make_simple_channelmask
Make a simple channel mask by adding the correct number of bits. Above the 18 channel positions that are defined it returns a zero.