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
now put this code in it:
find the full code here
now just hear your “music” running:
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: https://www.youtube.com/watch?v=2oz_SwhBixs
next lesson: Lesson 4: Apply some audio effects to your voice