-
Notifications
You must be signed in to change notification settings - Fork 3
Recording & Playback
Jan Janak edited this page Jan 28, 2021
·
9 revisions
import { PulseAudio } from '@janakj/pulseaudio.js';
const pa = new PulseAudio("Test client for @janakj/pulseaudio.js");
await pa.connect();
Recording audio with pulseaudio.js is straightforward. Create a new record stream with the method createRecordStream
, pipe the stream to a wav write, and pipe the wav writer to a file. When you are done recording, unpipe the streams and destroy the record stream.
import * as fs from 'fs';
import * as wav from 'wav';
import { PA_SAMPLE_FORMAT, sampleSize } from '@janakj/pulseaudio.js';
// Configure the sample format: 44.1 kHz, stereo, 16 bits per sample
const rate = 44100, channels = 2, format = PA_SAMPLE_FORMAT.S16LE;
// Create a new record stream with the given sample format connected
// to the default source (sound card)
const stream = await pa.createRecordStream({
sampleSpec : { rate, format, channels }
});
// Create a new wav file writer with the above sample format
const writer = new wav.Writer({
sampleRate : rate,
channels,
bitDepth : sampleSize[format] * 8
});
// Write the recorded audio to file audio.wav
const file = fs.createWriteStream('audio.wav');
// Pipe the record stream to the write and the writer to the
// filesystem stream
stream.pipe(writer);
writer.pipe(file);
// Keep recording for 10 seconds. Once the time is up, unpipe
// the streams and destroy the record the stream
setTimeout(() => {
stream.unpipe();
writer.unpipe();
stream.destroy();
}, 10000);
It is also possible the specify the maximum length of the recording in bytes by passing a maximumLength
property to createRecordStream
. In that case, the record stream will end automatically upon recording the given number of bytes, without the need to set a timer.