HSAudioDevice
TypeAn 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
transportType
The transport mechanism: "built-in", "usb", "bluetooth", "bluetooth-le",
"hdmi", "display-port", "firewire", "airplay", "avb",
"thunderbolt", "virtual", "aggregate", "pci", or "unknown".
isInUse
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
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.
volume
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).
balance
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
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).
availableSampleRates
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]
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]
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]]
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]]
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() |
true on successconst 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() |
true on successconst 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
true on successconst usb = hs.audiodevice.findDeviceByName("USB Audio CODEC")
usb.setDefaultOutputDevice()
setDefaultInputDevice() -> boolean
Make this device the system default input device.
setDefaultInputDevice() -> boolean
true on successconst mic = hs.audiodevice.findDeviceByName("External Mic")
mic.setDefaultInputDevice()
setDefaultEffectDevice() -> boolean
Make this device the system alert sound (effect) device.
setDefaultEffectDevice() -> boolean
true on successconst dev = hs.audiodevice.defaultOutputDevice()
dev.setDefaultEffectDevice()
on(event, listener) -> None
Register a listener for a named per-device property-change event.
Available events:
"vmout"— output volume changed. The listener receives the newvolume."vmin"— input volume changed. The listener receives the newinputVolume."mout"— output mute state changed. The listener receives the newmutedvalue."min"— input mute state changed. The listener receives the newinputMutedvalue."rate"— sample rate changed. The listener receives the newsampleRate."dsout"— output data source changed. The listener receives the newcurrentOutputDataSource()."dsin"— input data source changed. The listener receives the newcurrentInputDataSource()."balance"— output stereo balance changed. The listener receives the newbalance."inUse"— the device started or stopped being used by any process. The listener receives the newisInUsevalue."jack"— a jack (e.g. headphones) was connected or disconnected. The listener receives the newjackConnectedvalue."config"— the device's configuration changed (e.g. its streams or channel layout); 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 |
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 |
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() |
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}
id, name, uid, isInput, isOutput, transportType, isInUse, jackConnected, inputChannels, outputChannels, volume, muted, balance, inputVolume, inputMuted and sampleRate. Values the device doesn't support are nullconsole.log(JSON.stringify(hs.audiodevice.defaultOutputDevice(), null, 2))