API Docs

Module for discovering and controlling audio devices.

Finding devices

const all = hs.audiodevice.all();
const out = hs.audiodevice.defaultOutputDevice();
const mic = hs.audiodevice.defaultInputDevice();

Selecting a device

const usb = hs.audiodevice.findDeviceByName("USB Audio CODEC");
if (usb) usb.setDefaultOutputDevice();

Watching for system-level changes

Available events: "dOut" (default output changed), "dIn" (default input changed), "dSErr" (default alert sound device changed), "dev+" (a device was added), "dev-" (a device was removed). "dOut", "dIn" and "dSErr" listeners receive the new default device (or null if there is none), "dev+" listeners the added device, and "dev-" listeners an {id, uid, name} object describing the removed device, which can no longer be used.

var fn = function(device) { console.log("Default output is now " + (device ? device.name : "none")) };
hs.audiodevice.on("dOut", fn);
// later…
hs.audiodevice.off("dOut", fn);

Types

This module provides the following types:

Event Types

The events this module's watchers emit, and when. In TypeScript, each type is a union of its event names that you can use as a type of your own:

HSAudioDevicePropertyEvent

An event name for HSAudioDevice.on(), HSAudioDevice.off() and HSAudioDevice.once()

Event Description
"vmout" The device's output volume changed
"vmin" The device's input volume changed
"mout" The device's output was muted or unmuted
"min" The device's input was muted or unmuted
"rate" The device's sample rate changed
"dsout" The device's output data source changed
"dsin" The device's input data source changed
"balance" The device's output stereo balance changed
"inUse" The device started or stopped being used by any process
"jack" A jack (e.g. headphones) was connected to or disconnected from the device
"config" The device's configuration changed (e.g. its streams or channel layout)

HSAudioDeviceEvent

An event name for hs.audiodevice.on(), hs.audiodevice.off() and hs.audiodevice.once()

Event Description
"dOut" The default output device changed
"dIn" The default input device changed
"dSErr" The device for alerts and sound effects changed
"dev+" An audio device was added
"dev-" An audio device was removed

Properties

This module has no properties.

Methods

hs.audiodevice.all() -> HSAudioDevice[]

All audio devices attached to the system.

hs.audiodevice.all() -> HSAudioDevice[]
An array of HSAudioDevice objects
const devs = hs.audiodevice.all()
devs.forEach(d => console.log(d.name))

hs.audiodevice.allOutputDevices() -> HSAudioDevice[]

All audio devices that have at least one output stream.

hs.audiodevice.allOutputDevices() -> HSAudioDevice[]
An array of HSAudioDevice objects
const outputs = hs.audiodevice.allOutputDevices()

hs.audiodevice.allInputDevices() -> HSAudioDevice[]

All audio devices that have at least one input stream.

hs.audiodevice.allInputDevices() -> HSAudioDevice[]
An array of HSAudioDevice objects
const inputs = hs.audiodevice.allInputDevices()

hs.audiodevice.defaultOutputDevice() -> HSAudioDevice

The current system default output device.

hs.audiodevice.defaultOutputDevice() -> HSAudioDevice
An HSAudioDevice, or null if none is set
const out = hs.audiodevice.defaultOutputDevice()
console.log(out && out.name)

hs.audiodevice.defaultInputDevice() -> HSAudioDevice

The current system default input device.

hs.audiodevice.defaultInputDevice() -> HSAudioDevice
An HSAudioDevice, or null if none is set
const mic = hs.audiodevice.defaultInputDevice()

hs.audiodevice.defaultEffectDevice() -> HSAudioDevice

The current system alert sound device.

hs.audiodevice.defaultEffectDevice() -> HSAudioDevice
An HSAudioDevice, or null if none is set
const effect = hs.audiodevice.defaultEffectDevice()

hs.audiodevice.findDeviceByName(name) -> HSAudioDevice

Find the first audio device whose name matches the given string.

hs.audiodevice.findDeviceByName(name) -> HSAudioDevice
Name Type Description
name string The device name to search for
An HSAudioDevice if found, null otherwise
const usb = hs.audiodevice.findDeviceByName("USB Audio CODEC")

hs.audiodevice.findDeviceByUID(uid) -> HSAudioDevice

Find the audio device with the given unique identifier.

hs.audiodevice.findDeviceByUID(uid) -> HSAudioDevice
Name Type Description
uid string The device UID to search for
An HSAudioDevice if found, null otherwise
const dev = hs.audiodevice.findDeviceByUID("BuiltInSpeakerDevice")

hs.audiodevice.on(event, listener) -> None

Register a listener for a named system-level audio configuration event.

hs.audiodevice.on(event, listener) -> None
Name Type Description
event HSAudioDeviceEvent The event to listen for
listener (device: HSAudioDevice | {id: number, uid: string, name: string} | null) => void Called when the event occurs. For "dOut", "dIn" and "dSErr" it is passed the new default device (or null if there is none), for "dev+" the device that was added, and for "dev-" an {id, uid, name} object describing the device that was removed (it can no longer be used)
Event Description
"dOut" The default output device changed
"dIn" The default input device changed
"dSErr" The device for alerts and sound effects changed
"dev+" An audio device was added
"dev-" An audio device was removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.audiodevice.on('dOut', (device) => console.log("Default output is now " + (device ? device.name : "none")))
    hs.audiodevice.on('dev+', (device) => console.log("Added " + device.name))
    hs.audiodevice.on('dev-', (removed) => console.log("Removed " + removed.name))
} catch (err) {
    console.error(err.message)
}

hs.audiodevice.off(event, listener) -> None

Remove a previously registered system-level audio event listener.

hs.audiodevice.off(event, listener) -> None
Name Type Description
event HSAudioDeviceEvent The event the listener was registered for
listener (device: HSAudioDevice | {id: number, uid: string, name: string} | null) => void The function originally passed to on
Event Description
"dOut" The default output device changed
"dIn" The default input device changed
"dSErr" The device for alerts and sound effects changed
"dev+" An audio device was added
"dev-" An audio device was removed
None
const onDefaultOutChange = () => console.log("changed")
hs.audiodevice.on('dOut', onDefaultOutChange)
// later…
hs.audiodevice.off('dOut', onDefaultOutChange)

hs.audiodevice.once(event, listener) -> None

Register a listener that fires at most once for a system-level audio configuration event.

hs.audiodevice.once(event, listener) -> None
Name Type Description
event HSAudioDeviceEvent The event to listen for
listener (device: HSAudioDevice | {id: number, uid: string, name: string} | null) => void Called the next time a matching event occurs, then automatically removed. Passed the same value as for on()
Event Description
"dOut" The default output device changed
"dIn" The default input device changed
"dSErr" The device for alerts and sound effects changed
"dev+" An audio device was added
"dev-" An audio device was removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    hs.audiodevice.once('dev+', (device) => console.log("First device added: " + device.name))
} catch (err) {
    console.error(err.message)
}