Nodejs audio, lesson 2: ear your voice from your speakers

August 03, 2021

In the previous lesson we did the setup of our project, and we learned how to acquire audio from our mic and dump it into a file.

In this article, we are gonna use your microphone.

Let’s start creating a file for this lesson:

touch lesson_02__speaker.js

then lets install an npm packege calledspeaker:

npm install -s speaker

remember, to make this package work, you neet to be sure you have installed sox. That’s the same tool we have installed in the previous lesson to make the mic package work, so if you did the lesson 1, you don’t need to install sox now.

now inside the lesson_02_spkeaker.js add the following code:

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

go here to find the full code

run the file with the command:

node lesson_02__speaker.js

if you talk now, you will ear back your voice. press ctrl + c to close the application.

What have we done?

the mic is initialized as the previous lesson.

with those line we are gonna initialize the speaker:

const speaker = new Speaker({  
    sampleRate: audioConfig.rate,  
    channels: audioConfig.channels,  
    bitDepth: audioConfig.bitDepth  
  });

the object `speaker` is now a Writable stream so we can “write” our audio in our speakers with the line:

micInputStream.pipe(speaker);

next lesson: Nodejs audio lesson 3: Math and Music, baudio for the win


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.