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(volume) { console.log("Output volume changed to " + volume); };
    dev.on('vmout', fn);
    // later…
    dev.off('vmout', 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"`.

isInUse

boolean
Whether any process (not just Hammerspoon) is currently using this device for input or output - e.g. a call or dictation app has opened the microphone, or music is playing. Listen for the `"inUse"` event to be told when this changes.

jackConnected

boolean
Whether a jack (e.g. headphones) is connected to this device. Always `false` if the device does not support jack sensing. Output jack state is reported for devices with output, otherwise input jack state. Listen for the `"jack"` event to be told when this changes.

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, and setting a value outside `0.0`–`1.0` logs an error and leaves the volume unchanged (Hammerspoon v1 used `0`–`100`).

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. Setting a value outside `0.0`–`1.0` logs an error and leaves the balance unchanged.

inputVolume

NSNumber
Input (microphone) volume scalar in the range `0.0`–`1.0`, or `null` if the device has no controllable input volume. Setting a value outside `0.0`–`1.0` logs an error and leaves the volume unchanged (Hammerspoon v1 used `0`–`100`).

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() -> [String: Any]

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

currentInputDataSource() -> [String: Any]

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

outputDataSources() -> [[String: Any]]

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

inputDataSources() -> [[String: Any]]

All available input data sources as an array of `{ id, name }` objects.
inputDataSources() -> [[String: Any]]
[[String: Any]]
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()

on(event, listener) -> None

Register a listener for a named per-device property-change event. ``currentOutputDataSource()``. ``currentInputDataSource()``. receives the new ``isInUse`` value. receives the new ``jackConnected`` value. any properties you have cached for it may be stale. The listener receives nothing. The value is read when the listener is called, and is `null` if the device can't report it. `"inUse"` and `"jack"` always pass a boolean, `false` if the device can't report it, as ``isInUse`` and ``jackConnected`` do.
on(event, listener) -> None
Name Type Description
event string The event to listen for
listener function Called when the event occurs, with the new value of the property that changed (see above); for `"config"` it is passed nothing
None
Throws an Error on failure; wrap calls in try/catch to handle it.
const dev = hs.audiodevice.defaultOutputDevice()
try {
    dev.on('vmout', (volume) => console.log("Output volume is now " + volume))

    // Be told whenever any app opens or releases the microphone
    const mic = hs.audiodevice.defaultInputDevice()
    mic.on('inUse', (inUse) => console.log("Microphone is " + (inUse ? "in use" : "idle")))
} catch (err) {
    console.error(err.message)
}

off(event, listener) -> None

Remove a previously registered per-device event listener.
off(event, listener) -> None
Name Type Description
event string The event the listener was registered for
listener function The function originally passed to `on`
None
dev.off('vmout', myListener)

once(event, listener) -> None

Register a listener that fires at most once for a per-device event.
once(event, listener) -> None
Name Type Description
event string The event to listen for
listener function Called once, then automatically removed. Passed the new value, as for `on()`
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    dev.once('vmout', () => console.log("Volume changed once"))
} catch (err) {
    console.error(err.message)
}

toJSON() -> {[key: string]: any}

Returns a plain object describing this device, for JSON serialisation `JSON.stringify()` calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with the device's `id`, `name`, `uid`, `isInput`, `isOutput`, `transportType`, `isInUse`, `jackConnected`, `inputChannels`, `outputChannels`, `volume`, `muted`, `balance`, `inputVolume`, `inputMuted` and `sampleRate`. Values the device doesn't support are `null`
console.log(JSON.stringify(hs.audiodevice.defaultOutputDevice(), null, 2))