Skip to content
Jan Janak edited this page Jan 28, 2021 · 9 revisions
import { PulseAudio } from 'pulseaudio.js';

const pa = new PulseAudio();
await pa.connect();

Server Configuration

Use the method getServerInfo to obtain information about the PulseAudio servers:

console.log(await pa.getServerInfo());
{
  name: 'pulseaudio',
  version: '13.99.2',
  username: 'janakj',
  hostname: 'ubuntu',
  sampleSpec: { format: 3, channels: 2, rate: 44100 },
  defaultSink: 'alsa_output.pci-0000_00_05.0.analog-stereo',
  defaultSource: 'alsa_input.pci-0000_00_05.0.analog-stereo',
  cookie: 3762914620,
  defaultChannelMap: [ 1, 2 ]
}

Sources (Inputs)

getAllSources

The method getAllSources can be used to obtain an array of all inputs (sources) currently defined on the PulseAudio server:

console.log(await pa.getAllSources());
[
  {
    index: 0,
    name: 'alsa_output.pci-0000_00_05.0.analog-stereo.monitor',
    description: 'Monitor of Built-in Audio Analog Stereo',
    sampleSpec: { format: 3, channels: 2, rate: 48000 },
    channelMap: [ 1, 2 ],
    module: 7,
    volume: { current: [Array], base: 65536, steps: 65537 },
    mute: false,
    monitor: { index: 0, name: 'alsa_output.pci-0000_00_05.0.analog-stereo' },
    latency: { current: 5476377146921058304n, requested: 5476377146921058304n },
    driver: 'module-alsa-card.c',
    flags: 34,
    properties: {
      device: [Object],
      alsa: [Object],
      sysfs: [Object],
      'module-udev-detect': [Object]
    },
    state: 2,
    card: 0,
    ports: [],
    activePort: null,
    formats: [ [Object] ]
  },
  {
    index: 1,
    name: 'alsa_input.pci-0000_00_05.0.analog-stereo',
    description: 'Built-in Audio Analog Stereo',
    sampleSpec: { format: 3, channels: 2, rate: 44100 },
    channelMap: [ 1, 2 ],
    module: 7,
    volume: { current: [Array], base: 27636, steps: 65537 },
    mute: false,
    monitor: { index: 4294967295, name: null },
    latency: { current: 5476377146921058304n, requested: 5476377146921058304n },
    driver: 'module-alsa-card.c',
    flags: 55,
    properties: {
      alsa: [Object],
      device: [Object],
      sysfs: [Object],
      'module-udev-detect': [Object]
    },
    state: 2,
    card: 0,
    ports: [ [Object], [Object], [Object], [Object], [Object] ],
    activePort: 'analog-input-mic;input-microphone-1',
    formats: [ [Object] ]
  }
]

getSourceInfo

Use the method getSourceInfo to obtain information about a specific source. You can specify the source either by its name (string) or by its index (number). If you omit the argument, the method will return information about the default source.

console.log(await pa.getSourceInfo(1));
{
  index: 1,
  name: 'alsa_input.pci-0000_00_05.0.analog-stereo',
  description: 'Built-in Audio Analog Stereo',
  sampleSpec: { format: 3, channels: 2, rate: 44100 },
  channelMap: [ 1, 2 ],
  module: 7,
  volume: { current: [ 43800, 43800 ], base: 27636, steps: 65537 },
  mute: false,
  monitor: { index: 4294967295, name: null },
  latency: { current: 5476377146921058304n, requested: 5476377146921058304n },
  driver: 'module-alsa-card.c',
  flags: 55,
  properties: {
    alsa: {
      resolution_bits: '16',
      class: 'generic',
      subclass: 'generic-mix',
      name: 'Intel 82801AA-ICH',
      id: 'Intel ICH',
      subdevice: '0',
      subdevice_name: 'subdevice #0',
      device: '0',
      card: '0',
      card_name: 'Intel 82801AA-ICH',
      long_card_name: 'Intel 82801AA-ICH with AD1980 at irq 21',
      driver_name: 'snd_intel8x0'
    },
    device: {
      api: 'alsa',
      class: 'sound',
      bus_path: 'pci-0000:00:05.0',
      bus: 'pci',
      vendor: [Object],
      product: [Object],
      form_factor: 'internal',
      string: 'front:0',
      buffering: [Object],
      access_mode: 'mmap',
      profile: [Object],
      description: 'Built-in Audio Analog Stereo',
      icon_name: 'audio-card-pci'
    },
    sysfs: { path: '/devices/pci0000:00/0000:00:05.0/sound/card0' },
    'module-udev-detect': { discovered: '1' }
  },
  state: 2,
  card: 0,
  ports: [
    {
      name: 'analog-input-mic;input-microphone-1',
      description: 'Microphone / Microphone 1',
      priority: 8720,
      available: 0
    },
    {
      name: 'analog-input-mic;input-microphone-2',
      description: 'Microphone / Microphone 2',
      priority: 8719,
      available: 0
    },
    {
      name: 'analog-input-linein',
      description: 'Line In',
      priority: 8100,
      available: 0
    },
    {
      name: 'analog-input-aux',
      description: 'Analog Input',
      priority: 8000,
      available: 0
    },
    {
      name: 'analog-input-video',
      description: 'Video',
      priority: 7000,
      available: 0
    }
  ],
  activePort: 'analog-input-mic;input-microphone-1',
  formats: [ { encoding: 1, properties: {} } ]
}

lookupSource

The method lookupSource can be used to obtain the unique index (number) of the given source, for example:

console.log(await pa.lookupSource('alsa_input.pci-0000_00_05.0.analog-stereo'));
1

setDefaultSource

You can designate one source on the PulseAudio server as default with the method setDefaultSource. The default source will be used whenever a client starts a recording stream without providing an explicit source name. At any time, there can be only one default source on the PulseAudio server.

await pa.setDefaultSource('alsa_input.pci-0000_00_05.0.analog-stereo');

Sinks (Outputs)

getAllSinks

Use the method getAllSinks() to obtain an array of all outputs (sinks) defined on the PulseAudio server:

console.log(await pa.getAllSinks());
[
  {
    index: 0,
    name: 'alsa_output.pci-0000_00_05.0.analog-stereo',
    description: 'Built-in Audio Analog Stereo',
    sampleSpec: { format: 3, channels: 2, rate: 48000 },
    channelMap: [ 1, 2 ],
    module: 7,
    volume: { current: [Array], base: 65536, steps: 65537 },
    mute: false,
    monitor: {
      index: 0,
      name: 'alsa_output.pci-0000_00_05.0.analog-stereo.monitor'
    },
    latency: { current: 5476377146921058304n, requested: 5476377146921058304n },
    driver: 'module-alsa-card.c',
    flags: 55,
    properties: {
      alsa: [Object],
      device: [Object],
      sysfs: [Object],
      'module-udev-detect': [Object]
    },
    state: 2,
    card: 0,
    ports: [ [Object], [Object] ],
    activePort: 'analog-output;output-amplifier-on',
    formats: [ [Object] ]
  }
]

getSinkInfo

To get information about a specific sink (output) selected by either its unique index (number) or name (string), use the method getSinkInfo. With no arguments, the method returns information about the currently configured default sink.

console.log(await pa.getSinkInfo(0));
{
  index: 0,
  name: 'alsa_output.pci-0000_00_05.0.analog-stereo',
  description: 'Built-in Audio Analog Stereo',
  sampleSpec: { format: 3, channels: 2, rate: 48000 },
  channelMap: [ 1, 2 ],
  module: 7,
  volume: { current: [ 99957, 99957 ], base: 65536, steps: 65537 },
  mute: false,
  monitor: {
    index: 0,
    name: 'alsa_output.pci-0000_00_05.0.analog-stereo.monitor'
  },
  latency: { current: 5476377146921058304n, requested: 5476377146921058304n },
  driver: 'module-alsa-card.c',
  flags: 55,
  properties: {
    alsa: {
      resolution_bits: '16',
      class: 'generic',
      subclass: 'generic-mix',
      name: 'Intel 82801AA-ICH',
      id: 'Intel ICH',
      subdevice: '0',
      subdevice_name: 'subdevice #0',
      device: '0',
      card: '0',
      card_name: 'Intel 82801AA-ICH',
      long_card_name: 'Intel 82801AA-ICH with AD1980 at irq 21',
      driver_name: 'snd_intel8x0'
    },
    device: {
      api: 'alsa',
      class: 'sound',
      bus_path: 'pci-0000:00:05.0',
      bus: 'pci',
      vendor: [Object],
      product: [Object],
      form_factor: 'internal',
      string: 'front:0',
      buffering: [Object],
      access_mode: 'mmap',
      profile: [Object],
      description: 'Built-in Audio Analog Stereo',
      icon_name: 'audio-card-pci'
    },
    sysfs: { path: '/devices/pci0000:00/0000:00:05.0/sound/card0' },
    'module-udev-detect': { discovered: '1' }
  },
  state: 2,
  card: 0,
  ports: [
    {
      name: 'analog-output;output-amplifier-on',
      description: 'Analog Output / Amplifier',
      priority: 9910,
      available: 0
    },
    {
      name: 'analog-output;output-amplifier-off',
      description: 'Analog Output / No Amplifier',
      priority: 9900,
      available: 0
    }
  ],
  activePort: 'analog-output;output-amplifier-on',
  formats: [ { encoding: 1, properties: {} } ]
}

lookupSink

The method lookupSink can be used to obtain the unique index (number) of the given sink, for example:

console.log(await pa.lookupSink('alsa_output.pci-0000_00_05.0.analog-stereo'));
0

setDefaultSink

You can designate one sink on the PulseAudio server as default with the method setDefaultSink. The default sink will be used whenever a client starts a playback stream without providing an explicit sink name. At any time, there can be only one default sink on the PulseAudio server.

await pa.setDefaultSink('alsa_output.pci-0000_00_05.0.analog-stereo');