Skip to content

Recording & Playback

Jan Janak edited this page Jan 28, 2021 · 9 revisions

Audio Recording

import fs from 'fs';
import { PulseAudio, PA_SAMPLE_FORMAT } from '@janakj/pulseaudio.js';

const rate = 44100;
const channels = 2;
const format = PA_SAMPLE_FORMAT.S16LE;

const stream = await pa.createRecordStream({
    sampleSpec : { rate, format, channels }
});

const file = fs.createWriteStream('sample.wav');

const writer = new wav.Writer({
    sampleRate : rate,
    channels,
    bitDepth : sampleSize[format] * 8
});    

stream.pipe(writer);
writer.pipe(file);

setTimeout(async () => {
    console.log('closing');
    stream.unpipe();
    writer.unpipe();
    stream.destroy();
    await pa.disconnect();
}, 5000);

Audio Playback