0

I created a .wav file from a recording from my microphone using this javascript library Unfortuntely the file is 7-bit encoded .wav file. I send this file to my node.js server and now I need to send this file to google's speech to text api to be processed but google only accepts base64 linear 16 .wav files. What can I do?

Here is how I create and send the audio file to my server:

navigator.mediaDevices.getUserMedia({
    audio: true,video:false
})
.then((stream) => {


context = new AudioContext()

var source = context.createMediaStreamSource(stream)
config = {
    numChannels:1
}
var rec = new Recorder(source,config)
rec.record()

$('#stop').click(()=>{
rec.stop()
blob = rec.exportWAV(export)

function export(blob)
{

    fd = new FormData()
    fd.append('file',blob)
    $.ajax({
        type: "POST",
        url: "http:localhost:3000/send",
        data: fd,
        contentType:false,
        processData: false,
        encType:"multipart/form-data",
        success: function(data){
            console.log('success')

        }
    });


}



})

Here is how I handle it on my node.js server:

var upload = multer()
var app = express({ dest: '/' })

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");

    next();
  });
var upload = multer().any()


app.get('/',(req,res)=>{
res.send('Hello World!')

 })

app.post('/send',(req,res)=>{
    upload(req, res, function (err) {
        if (err) {
          console.log(err)
          return
        }

         file = req.files[0]

         buffer = file.buffer


var wstream = fs.createWriteStream('audiofile.wav');
wstream.write(buffer)

wstream.end();
path = "path/audiofile.wav"
jeo.e
  • 213
  • 1
  • 3
  • 10
  • Related: https://stackoverflow.com/q/51687308/362536 And: https://stackoverflow.com/questions/51689270/how-to-use-web-audio-api-to-create-wav-file Why not use the methods I posted in your previous questions and skip that library? Also, I highly doubt you're somehow getting 7-bit audio in a WAV file. As far as I'm aware, that's not a valid bit depth. (Maybe that's what it's been quantized to, but it should be stored as 8-bit at least.) – Brad Aug 08 '18 at 01:43
  • It is 7 bit encoded. – jeo.e Aug 08 '18 at 01:45
  • For short audio files, I was able to get it to work by doing .toString('base64') on the buffer. But with longer audio files I have to upload them to google cloud first. This becomes a problem because when I try to write the .toString('base64') to a .wav file and play it is corrupted and does not play. – jeo.e Aug 08 '18 at 01:49
  • Sorry, but you're wrong. The library you linked to is hardcoded at 16-bit. https://github.com/mattdiamond/Recorderjs/blob/master/lib/recorder.js#L157 What makes you think it's 7-bit? – Brad Aug 08 '18 at 01:49
  • This is why I thought it was 7-bit: https://imgur.com/a/wDB6GBd – jeo.e Aug 08 '18 at 01:52
  • That is from console.log() of the file object that was processed by multer middleware in express.js – jeo.e Aug 08 '18 at 01:53
  • Honestly all these formats are so confusing to me I just need google to send me back a transcription on my audio file and I cannot get it to work. – jeo.e Aug 08 '18 at 01:54
  • What you're seeing is a property of the blob object and doesn't have anything to do with the WAVE data packed within. Your library is already creating your audio in the format you need. You just need to get a base64 representation of it and POST to Google as part of the JSON in the request body. It's impossible to tell you how to do that specifically because you haven't shown us any of the relevant code. On Stack Overflow, this is a requirement. If you're interested in consulting or someone to do this for you, feel free to e-mail me at brad@audiopump.co. – Brad Aug 08 '18 at 01:56
  • Ok please take a look at my code and let me know how to do it. I will be so thankful! Also let me know if you need anymore information. – jeo.e Aug 08 '18 at 02:05

0 Answers0