4

I am new in Node.js programming and I am trying to convert a m4a file to wav file.

I used audiobuffer-to-wav, and web-audio-api. This is my code:

const fs = require('fs');
const toWav = require('audiobuffer-to-wav');
const AudioContext = require('web-audio-api').AudioContext;
const audioContext = new AudioContext;

let resp = fs.readFileSync('sample.m4a');

audioContext.decodeAudioData(resp, buffer => {
  let wav = toWav(buffer);
  console.log(buffer.length);
  console.log(wav); 
});

I want to know how to save the wav variable into a file in Node.js

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
fastworker399
  • 423
  • 9
  • 26

1 Answers1

7
const fs = require('fs');
const toWav = require('audiobuffer-to-wav');
const AudioContext = require('web-audio-api').AudioContext;
const audioContext = new AudioContext;
 let resp = fs.readFileSync('sample.m4a');

var audioStack = [];

var header = require('waveheader');

audioContext.decodeAudioData(resp, buffer => {
    let wav = toWav(buffer); 
    var chunk = new Uint8Array(wav);
    console.log(chunk); 
    fs.appendFile('bb.wav', new Buffer(chunk), function (err) {
    });

});

This is my answer. Thanks Kalamarico.

fastworker399
  • 423
  • 9
  • 26
  • You have required "waveheader", but you don't appear to be using it to write a header to the stream. I think you would want to track the stream end, and then add the size of the data sub-chunk to the header also. – f1lt3r Aug 15 '18 at 05:08