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

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

The PulseAudio server emits asynchronous notifications to all connected clients whenever the state or configuration of a resource changes. For example, an event is emitted when a new client connects or disconnects, when a new playback stream is created, or whenever a client changes the volume of a source or sink. The PulseAudio object created above implements the EventEmitter interface. For each asynchronous notification received from the PulseAudio server, the object emits an event.

For example, the following code could be used to receive a notification whenever a new playback stream is started. The callback receives the unique index (number) of the resource. The index can be passed to methods such as setSinkInputVolume or setSinkInputMute.

pa.on('event.sink_input.new', index => {
    console.log(`New playback stream ${index} was created`);
});

The general format of the name of emitted events is event.<facility>.<operation>, where <facility> identifies the server resource and is one of "sink", "source", "sink_input", "source_output", "module", "client", "sample_cache", "server", "autoload", "card". <operation> is one of "new", "change", "remove".

The handler function receives two arguments. The first argument is the unique index of the server resource. The second argument is number that represents the PulseAudio event.

Each event is also emitted under the name event.<facility>, event.<operation>, and the catchall name event. These aliases can be used for more flexible event filtering. The handlers for these events receive only one argument. The argument will be an object whose attributes will include 'index', 'event', 'facility', and 'operation'.