API Docs

An audio device attached to the system.

Obtain instances via hs.audiodevice module methods — do not instantiate directly.

Getting and setting volume

const dev = hs.audiodevice.defaultOutputDevice();
if (dev) {
    console.log(dev.volume);    // 0.0 – 1.0, or null
    dev.volume = 0.5;
}

Watching for changes

const dev = hs.audiodevice.defaultOutputDevice();
if (dev) {
    var fn = function(event) { console.log("Device event:", event); };
    dev.addWatcher(fn);
    // later…
    dev.removeWatcher(fn);
}

Properties

id

number
The CoreAudio object ID of this device.

name

string
The human-readable name of this device (e.g. `"Built-in Output"`).

uid

string
The persistent unique identifier for this device.

isOutput

boolean
Whether this device has output streams (can play audio).

isInput

boolean
Whether this device has input streams (can record audio).

transportType

string
The transport mechanism: `"built-in"`, `"usb"`, `"bluetooth"`, `"bluetooth-le"`, `"hdmi"`, `"display-port"`, `"firewire"`, `"airplay"`, `"avb"`, `"thunderbolt"`, `"virtual"`, `"aggregate"`, `"pci"`, or `"unknown"`.

outputChannels

number
Number of output channels, or 0 if the device has no output.

inputChannels

number
Number of input channels, or 0 if the device has no input.

volume

NSNumber
Output volume scalar in the range `0.0`–`1.0`, or `null` if the device has no controllable output volume. Setting `null` is a no-op.

muted

boolean
Whether output is muted. Always `false` if the device has no mutable output.

balance

NSNumber
Output stereo balance in the range `0.0` (full left)–`1.0` (full right), or `null` if balance control is not available.

inputVolume

NSNumber
Input (microphone) volume scalar in the range `0.0`–`1.0`, or `null` if the device has no controllable input volume.

inputMuted

boolean
Whether input is muted. Always `false` if the device has no mutable input.

sampleRate

NSNumber
The current nominal sample rate in Hz (e.g. `44100`), or `null` if unknown.

availableSampleRates

NSNumber[]
All sample rates (in Hz) that this device supports. For devices that support a range, both the minimum and maximum are included.

Methods

currentOutputDataSource() -> NSDictionary

The current output data source as `{ id, name }`, or `null` if unavailable.
currentOutputDataSource() -> NSDictionary
NSDictionary
A dictionary containing the id and name of the current output data source
const dev = hs.audiodevice.defaultOutputDevice()
console.log(dev.currentOutputDataSource())

currentInputDataSource() -> NSDictionary

The current input data source as `{ id, name }`, or `null` if unavailable.
currentInputDataSource() -> NSDictionary
NSDictionary
A dictionary containing the id and name of the current input data source
const mic = hs.audiodevice.defaultInputDevice()
console.log(mic.currentInputDataSource())

outputDataSources() -> NSDictionary[]

All available output data sources as an array of `{ id, name }` objects.
outputDataSources() -> NSDictionary[]
NSDictionary[]
A dictionary containing the ids and names of all available output data sources
const dev = hs.audiodevice.defaultOutputDevice()
console.log(dev.outputDataSources())

inputDataSources() -> NSDictionary[]

All available input data sources as an array of `{ id, name }` objects.
inputDataSources() -> NSDictionary[]
NSDictionary[]
A dictionary containing the ids and names of all available input data sources
const mic = hs.audiodevice.defaultInputDevice()
console.log(mic.inputDataSources())

setCurrentOutputDataSource(sourceID) -> boolean

Select an output data source by its numeric ID.
setCurrentOutputDataSource(sourceID) -> boolean
Name Type Description
sourceID number The `id` value from ``outputDataSources()``
boolean
`true` on success
const dev = hs.audiodevice.defaultOutputDevice()
const sources = dev.outputDataSources()
dev.setCurrentOutputDataSource(sources[0].id)

setCurrentInputDataSource(sourceID) -> boolean

Select an input data source by its numeric ID.
setCurrentInputDataSource(sourceID) -> boolean
Name Type Description
sourceID number The `id` value from ``inputDataSources()``
boolean
`true` on success
const mic = hs.audiodevice.defaultInputDevice()
const sources = mic.inputDataSources()
mic.setCurrentInputDataSource(sources[0].id)

setDefaultOutputDevice() -> boolean

Make this device the system default output device.
setDefaultOutputDevice() -> boolean
boolean
`true` on success
const usb = hs.audiodevice.findDeviceByName("USB Audio CODEC")
usb.setDefaultOutputDevice()

setDefaultInputDevice() -> boolean

Make this device the system default input device.
setDefaultInputDevice() -> boolean
boolean
`true` on success
const mic = hs.audiodevice.findDeviceByName("External Mic")
mic.setDefaultInputDevice()

setDefaultEffectDevice() -> boolean

Make this device the system alert sound (effect) device.
setDefaultEffectDevice() -> boolean
boolean
`true` on success
const dev = hs.audiodevice.defaultOutputDevice()
dev.setDefaultEffectDevice()

addWatcher(listener) -> None

Register a listener for a per-device property-change event.
addWatcher(listener) -> None
Name Type Description
listener JSValue A JavaScript function that receives an event name string
None
const dev = hs.audiodevice.defaultOutputDevice()
dev.addWatcher((event) => console.log("Event:", event))

removeWatcher(listener) -> None

Remove a previously registered per-device listener.
removeWatcher(listener) -> None
Name Type Description
listener JSValue The JavaScript function that was passed to ``addWatcher(_:)``
None
const dev = hs.audiodevice.defaultOutputDevice()
dev.removeWatcher(myHandler)