Reputation: 45
I would like to design a client/server system where the client is recording audio from a microphone and streaming it to a server for further processing. I'm planning on using gRPC, with a NodeJS client (I don't have control over the server).
But I don't really know how to do that. I found the npm package "microphone-stream" that seemed to perfectly fit my needs (https://www.npmjs.com/package/microphone-stream), because I can record from microphone and get each buffer separately in order to send them to the server. Unfortunately, this package use the getUserMedia API, which means I can't use it in plain NodeJS.
So I found other npm packages (node-microphone and mic) which don't use getUserMedia, but they don't allow to retrieve buffers on the stream easily (or I just don't know how to do it). For example, I can see on "mic" page (https://www.npmjs.com/package/mic) :
micInputStream.on('data', function(data) {
console.log("Recieved Input Stream: " + data.length);
});
Should I parse 'data' into integers (int32) to make gRPC streaming possible ? How can I achieve that ?
Thanks !
Upvotes: 3
Views: 4795
Reputation: 45
Okay, thanks to Michael I manage to achieve what I wanted to do.
First of all, I noticed that if a console.log() my buffer, I got something like this.
micInputStream.on('data', function(data) {
console.log(data); // --> <Buffer 52 49 46 46 24 00 00 80 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 80 3e 00 00 00 fa 00 00 04 00 10 00 64 61 74 61 00 00 00 80>
});
Which led me to the following API :https://nodejs.org/docs/latest/api/buffer.html. From that, I use
data.toString('base64')
in order to get a string, which I can send over gRPC from client-side.
On server-side, I used...
var serverBuffer = Buffer.from([data received over gRPC], 'base64');
...in order to rebuild my buffer. That's it, I am now able to process my client's buffer on server side.
Upvotes: 1