0

I am able to store a wav file in cloudant database as a multipart. Here is the code :

  var doc = {
    _id: timestmp.toString(),
    name:  strName
  };

  var attach = {
  name: strName,
  data: fs.readFileSync(input),
  content_type: mimeType
};
try {
  db_data.multipart.insert(doc, [attach], now.toString(), function (err, result) {

    if(err){
      console.log('Error inserting to STT data table' + err);
      response.sendStatus(200);
    }
    else {
      console.log('Inserted to db');
}
});

When I download the wav file from cloudant database manually, I am able to play the file. I am trying to do the same using Node js code. But the wav file exported to my system is not playing. Here is the code :

db_data.multipart.get(doc._id, function (err, buffer) {
        var inputWav = "input.wav";

        if (!err) {
           fs.writeFileSync(inputWav, Buffer.from(buffer), "audio/wav");

        }
});

Can anyone help ? Basically, I am trying to convert the buffer output returned from cloudant database to audio/wav file mimetype. How to do that ?

Malar Kandasamy
  • 839
  • 2
  • 10
  • 17
  • try using https://www.npmjs.com/package/audiobuffer-to-wav npm module. Or you can refer this question https://stackoverflow.com/questions/46623517/write-a-wav-file-in-node-js-from-an-audiobuffer – Sarath Kumar Aug 11 '18 at 11:22
  • I tried that. I am getting this error : /node_modules/audiobuffer-to-wav/index.js:14 result = buffer.getChannelData(0) ^ TypeError: buffer.getChannelData is not a function at audioBufferToWav (//node_modules/audiobuffer-to-wav/index.js:14:21) – Malar Kandasamy Aug 11 '18 at 15:17
  • I am stuck in retrieving multipart from cloudant using Node JS. Hence, I used REST API to download the wav file. But its not downloading wav file from https URL. When I enter the https URL directly in browser, it prompts me to save file locally. So, the URL is correct. – Malar Kandasamy Aug 13 '18 at 06:53
  • This is my code: var request1 = require('request');var filestream = fs.createWriteStream("input.wav"); var authenticationHeader = "Basic " + new Buffer(user + ":" + pass).toString("base64"); request1( { url : "https://example.com/data/1533979044129/female", headers : { "Authorization" : authenticationHeader } }, function (error, httpResponse, body) { const statusCode = httpResponse.statusCode; httpResponse.pipe(filestream); httpResponse.on('end', function () { console.log("file complete"); filestream.close(); }); }); it give 0 bytes as input.wav. please help – Malar Kandasamy Aug 13 '18 at 06:59
  • is that URL is public so that I could check and help you out? – Sarath Kumar Aug 13 '18 at 07:04
  • No. Its a private https URL – Malar Kandasamy Aug 13 '18 at 08:12
  • I asked the new question using REST API and NodeJS here : https://stackoverflow.com/questions/51818270/how-to-download-a-multipart-wav-file-from-cloudant-database-and-save-locally-usi – Malar Kandasamy Aug 13 '18 at 08:37

1 Answers1

0

Finally, I am able to retrieve the multipart from cloudant database. Here is the code for that :

db_data.attachment.get(doc._id, doc.strName, function(err, body) {
  if (!err) {
   fs.writeFile('temp.wav', body, function(err) {
    if (err) {
     console.log("error while writing file!")
    }
   });
  } else {
   console.error("error retrieveing the document..", err);
  }
 });
Malar Kandasamy
  • 839
  • 2
  • 10
  • 17