hammerspoon2-docs
    Preparing search index...

    Class HSAudioDevice

    An audio device attached to the system.

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

    const dev = hs.audiodevice.defaultOutputDevice();
    if (dev) {
    console.log(dev.volume); // 0.0 – 1.0, or null
    dev.volume = 0.5;
    }
    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);
    }
    Index
    availableSampleRates: number[]

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

    balance: number | null

    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.

    id: number

    The CoreAudio object ID of this device.

    inputChannels: number

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

    inputMuted: boolean

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

    inputVolume: number | null

    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).

    isInput: boolean

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

    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.

    isOutput: boolean

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

    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.

    muted: boolean

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

    name: string

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

    outputChannels: number

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

    sampleRate: number | null

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

    transportType: string

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

    uid: string

    The persistent unique identifier for this device.

    volume: number | null

    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).

    • The current input data source as { id, name }, or null if unavailable.

      Returns Record<string, any> | null

      A dictionary containing the id and name of the current input data source

    • The current output data source as { id, name }, or null if unavailable.

      Returns Record<string, any> | null

      A dictionary containing the id and name of the current output data source

    • All available input data sources as an array of { id, name } objects.

      Returns Record<string, any>[]

      A dictionary containing the ids and names of all available input data sources

    • Remove a previously registered per-device event listener.

      Parameters

      • event:
            | "vmout"
            | "vmin"
            | "mout"
            | "min"
            | "rate"
            | "dsout"
            | "dsin"
            | "balance"
            | "inUse"
            | "jack"
            | "config"

        The event the listener was registered for

      • listener: (...args: any[]) => any

        The function originally passed to on

      Returns void

    • Register a listener for a named per-device property-change event.

      Available events:

      • "vmout" — output volume changed. The listener receives the new volume.
      • "vmin" — input volume changed. The listener receives the new inputVolume.
      • "mout" — output mute state changed. The listener receives the new muted value.
      • "min" — input mute state changed. The listener receives the new inputMuted value.
      • "rate" — sample rate changed. The listener receives the new sampleRate.
      • "dsout" — output data source changed. The listener receives the new currentOutputDataSource().
      • "dsin" — input data source changed. The listener receives the new currentInputDataSource().
      • "balance" — output stereo balance changed. The listener receives the new balance.
      • "inUse" — the device started or stopped being used by any process. The listener receives the new isInUse value.
      • "jack" — a jack (e.g. headphones) was connected or disconnected. The listener receives the new jackConnected value.
      • "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.

      Parameters

      • event:
            | "vmout"
            | "vmin"
            | "mout"
            | "min"
            | "rate"
            | "dsout"
            | "dsin"
            | "balance"
            | "inUse"
            | "jack"
            | "config"

        The event to listen for

      • listener: (value?: number | boolean | { id: number; name: string } | null) => void

        Called when the event occurs, with the new value of the property that changed (see above); for "config" it is passed nothing

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • Register a listener that fires at most once for a per-device event.

      Parameters

      • event:
            | "vmout"
            | "vmin"
            | "mout"
            | "min"
            | "rate"
            | "dsout"
            | "dsin"
            | "balance"
            | "inUse"
            | "jack"
            | "config"

        The event to listen for

      • listener: (value?: number | boolean | { id: number; name: string } | null) => void

        Called once, then automatically removed. Passed the new value, as for on()

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • All available output data sources as an array of { id, name } objects.

      Returns Record<string, any>[]

      A dictionary containing the ids and names of all available output data sources

    • Select an input data source by its numeric ID.

      Parameters

      • sourceID: number

        The id value from inputDataSources()

      Returns boolean

      true on success

    • Select an output data source by its numeric ID.

      Parameters

      • sourceID: number

        The id value from outputDataSources()

      Returns boolean

      true on success

    • Make this device the system alert sound (effect) device.

      Returns boolean

      true on success

    • Make this device the system default input device.

      Returns boolean

      true on success

    • Make this device the system default output device.

      Returns boolean

      true on success

    • Returns a plain object describing this device, for JSON serialisation

      JSON.stringify() calls this automatically, so you rarely need to call it yourself.

      Returns Record<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