4

I am trying to save a recorded audio to a audio file at server.

Reference: http://www.smartjava.org/content/record-audio-using-webrtc-chrome-and-speech-recognition-websockets

Code:

var rec;

function testaudio(){
 navigator.getUserMedia({audio: true}, success, error);
function success(stream){
    var context = new webkitAudioContext();
    console.log(context);
    console.log(context.source);
    console.log(context.destination);
            var mediaStreamSource = context.createMediaStreamSource(stream);
    console.log(mediaStreamSource);
            rec = new Recorder(mediaStreamSource);
    console.log(rec);
}
function error(e){console.log('error in testing audio.. !!1'+e);}   

}

function record(){ rec.record();

console.log("recording..");

}

function stopAndSend() {

rec.stop();

console.log("recording stopped!!!");
rec.exportWAV(function(blob){

console.log(blob);
    socket.emit('audioSave', {recordedData:blob}); //sending blob to server, socket is created using socket.io

});

}

server side code:

   socket.on('audioSave', function(data){
      var audio=data.recordedData;
      /*
      Here i want to save this audio in an audio file , i tried this
        fs.writeFile('out.wav', audio, function(err)
    {
        if(err) {console.log("!!!!!!!!!!!!!!!error in writing file..."+ err);}

        console.log(" Audio File created ");
    }
    );
           This method creates  text file
       */
   });

how can I write audio blob to an audio file?

kamlesh
  • 791
  • 1
  • 10
  • 19
  • I'm the creator of RecorderJS... could you give more detail about what you're seeing server-side? Are you seeing an error, and if so, what's the text of the error? – Matt Diamond Dec 29 '12 at 23:22
  • hi Matt, at server side I am not getting an error as such, when I am using fs.writeFile(), i get a file out.wav, but it's a text file having content [object Object](on opening this file with text editor), If I try to run this on media player(vlc) nothing happens. – kamlesh Dec 31 '12 at 05:43
  • Have you figured this out? I want to do the same and looking for the server side code. Thanks. – Light Jun 05 '13 at 06:18
  • If you're still at all curious on how to get it to work, check out this post: http://stackoverflow.com/a/24003932/2901178 – Daniel Que Jun 02 '14 at 21:57

1 Answers1

2

Are you using socket.io? I don't believe it supports binary data, which is what you're attempting to send. I would suggest checking out BinaryJS, or if that doesn't work, converting the Blob to a string format via Base64 encoding (or some other similar method).

Matt Diamond
  • 11,646
  • 5
  • 29
  • 36
  • Yes, I am using socket.io. So what you mean is that blob at client should be converted to a base64 string and then to sent to server. On server convert this string to blob and then save it in audio file. could you help me with above conversions. one more thing, is there a way to save blob at client side by creating an audio file? – kamlesh Jan 01 '13 at 10:10
  • hi Matt, how can I extract 'blob' obtained in rec.exportWAV(function(blob){}); out of the browser, what I mean is how can I write this blob in some file at client side? – kamlesh Feb 20 '13 at 10:29
  • Have you tried sending the blob to the server with BinaryJS and writing it directly to disk in Node? – Matt Diamond Feb 20 '13 at 18:30
  • I have not used BinaryJS as I want to avoid server, my aim is to save a video conference happening through peerConnection, since after initial connection server does not come into picture for a peer connection, I am trying to save audio from this live audio/video at client side. – kamlesh Feb 21 '13 at 06:10
  • Ah, sorry, I misunderstood. I would suggest creating a Blob URL and providing the user with a link to that URL (see: https://developer.mozilla.org/en-US/docs/DOM/window.URL.createObjectURL). – Matt Diamond Feb 21 '13 at 18:16
  • You can also use the forceDownload utility function that comes with RecorderJS. – Matt Diamond Feb 21 '13 at 18:22
  • I have used forceDownload utility given in RecorderJS, and getting an output.wav file, now when I am playing this file in windows media player as well as VLC player, progress bar shows that file is played on, however there is no sound. – kamlesh Feb 28 '13 at 10:15
  • Strange... it could be related to your system settings... I would check out this discussion on Github: https://github.com/mattdiamond/Recorderjs/issues/10 – Matt Diamond Feb 28 '13 at 19:17
  • Have you figured this out? I want to do the same and looking for the server side code. Thanks. – Light Jun 05 '13 at 06:19