Nodejs audio, lesson 3: math and music, baudio for the win

August 04, 2021

Before you read this, be sure you read the lesson 1 and lesson 2

What is audio? in unix everything is a file, even programs and devices. the way those things comunicate with each other, are strings (or numbers in this case).

that means basically you can “play” everything. That’s more or less the concept behind the npm package baudio.

disclaimer: I m gonna beefily cover this topic, so if you don’t understand it, just skim or skip. The porpouse here is just to show you that your microphone or music files are not the only thing you can use to produce audio.

create a file with

touch lesson_03__math_music_baudio.js

now put this code in it:

nodejs audio lesson 3 math and music baudio for the win 0

find the full code here

now just hear your “music” running:

node lesson_03__math_music_baudio.js

you are gonna listen some white noise.

if you want to hear a different sound, just run

node lesson_03__math_music_baudio.js 4

pass a number from 0 to 4 for different noises.

What we did?

As usual, our speaker is a writable stream. baudio is basically a library that given a a callback is generating a readable stream.

const music = baudio(musicSelected);

musicSeleceted in the end is the selected callback to generate the music we want, we use the operator .pipe as usual to play our music in our speakers.

Some notes about baudio

basically doing ”music” with baudio means implement a function that is accepting a t (is a point in time, is a float number that is gonna be bigger at every call) and must return a float number between -1 and 1.

If you are curious about the topic and you can to compose something more complicated that what I did , checkout this:

next lesson: Lesson 4: Apply some audio effects to your voice

Discuss on 𝕏 | Subscribe to RSS Feed

Profile picture

My name is Jurgo Boemo. I'm a Node.js developer at Vonage. My team does the Conversation API, and I try to convince people it's a good idea. This is my tech blog.