我使用此JavaScript library从麦克风的录音中创建了.wav文件,很遗憾,该文件是7位编码的.wav文件。我将此文件发送到我的node.js服务器,现在我需要将此文件发送到google的语音到文本api进行处理,但是google只接受base64线性16 .wav文件。我能做什么?
这是我创建音频文件并将其发送到服务器的方法:
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')
}
});
}
})
这是我在node.js服务器上处理它的方式:
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"