2016-08-12 65 views
0

我使用Azure認知服務的Bing文本到語音api如何將原始數據轉換爲javascript中的音頻

post_option.headers = { 
     'content-type' : 'application/ssml+xml', 
     'Content-Length' : post_data.length, 
     'X-Microsoft-OutputFormat' : 'riff-8khz-8bit-mono-mulaw', 
     'Authorization': 'Bearer ' + OxfordAccessToken.access_token, 
     'X-Search-AppId': '', 
     'X-Search-ClientID': '', 
     "User-Agent": "TTSNodeJS" 
    }; 

var post_req = https.request(post_option, function(res){ 
     var _data=""; 
     res.on('data', function(buffer){ 
      //get the wave 
     _data += buffer; 
     }); 

     // end callback 
     res.on('end', function(){ 

     console.log('wave data.length: ' + _data.length); 
     }); 

     post_req.on('error', function(e) { 
     console.log('problem with request: ' + e.message); 
     }); 
    }); 

我收到了字符串格式的音頻原始數據。我想將數據保存爲.mp3或.mp4文件。我可以通過使用下面的代碼將原始數據保存爲.wav。

var fs = require('fs') 
fs.writeFile('./audio.wav', data, 'binary', function(err) { 
if(err) console.log(err); 
else console.log("File saved"); 
}); 

但是,最終的二進制音頻文件充滿了噪音,無法使用。當使用16位標頭時,音頻文件會有更多噪音。

我需要幫助保存輸出數據到.mp3/.mp4音頻文件沒有噪音,請提出一種方法繼續。

回答

0

我利用request模塊來實現此功能,所創建的音頻文件清晰無噪音,正如您所提到的。這裏是我的測試代碼片段:

 var SsmlTemplate = "<speak version='1.0' xml:lang='en-us'><voice xml:lang='%s' xml:gender='%s' name='%s'>%s</voice></speak>"; 
     var post_data = util.format(SsmlTemplate, 'en-US', 'Female', 'Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)', 'This is a demo to call microsoft text to speach service in javascript.'); 
     console.log('\n\ntts post_data: ' + post_data + '\n'); 

     var post_option = { 
      url: "https://speech.platform.bing.com/synthesize", 
      method: 'POST', 
      body :post_data 
     }; 
     post_option.headers = { 
      'content-type': 'application/ssml+xml', 
      // 'Content-Length': post_data.length, 
      'X-Microsoft-OutputFormat': 'riff-16khz-16bit-mono-pcm', 
      'Authorization': 'Bearer ' + OxfordAccessToken.access_token, 
      'X-Search-AppId': '07D3234E49CE426DAA29772419F436CA', 
      'X-Search-ClientID': '1ECFAE91408841A480F00935DC390960', 
      "User-Agent": "TTSNodeJS" 
     }; 

     var post_req = request.post(post_option).on('response', function(response) { 
      console.log(response.statusCode) // 200 
      console.log(response.headers) 
      }).pipe(fs.createWriteStream('audio.mp3',{defaultEncoding:'binary'})); 

否則,您能否提供您的測試模板和您的本地環境。任何問題,請隨時讓我知道。

+0

我知道這是一箇舊的帖子,但是,我需要做到這一點,但以相反的方式。我需要將一個mp3文件轉換爲原始數據,你知道任何解決方案嗎? – cmarrero01