2017-09-24 232 views
0

我需要转换缓冲音频文件.m4a的缓冲音频文件.WAV用于发送给Google通过的NodeJS转换.M4A为.wav

var toWav = require('audiobuffer-to-wav') 
var xhr = require('xhr') 
var context = new AudioContext() 

// request the MP3 as binary 
xhr({ 
    uri: 'audio/track.mp3', 
    responseType: 'arraybuffer' 
}, function (err, body, resp) { 
    if (err) throw err 
    // decode the MP3 into an AudioBuffer 
    audioContext.decodeAudioData(resp, function (buffer) { 
    // encode AudioBuffer to WAV 
    var wav = toWav(buffer) 

    // do something with the WAV ArrayBuffer ... 
    }) 
}) 

我有错误

AudioContext is not defined 
+0

然后会发生什么? – TGrif

+0

所以我尝试使用audiobuffer-to-wav但是AudioContext()是错误的 – otaroo

+0

具体是什么错误? – TGrif

回答

0

如果语音API你需要使用这个库服务器端,你可以使用web-audio-api包模拟AudioContext功能。

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); 
    // do something with the WAV ArrayBuffer ... 
}); 
+0

我在后端使用节点 – otaroo

+0

是的,我明白了。 – TGrif

+0

我将用服务器端的用法更新我的答案。 – TGrif