2014-01-22 79 views
1

我迫切需要幫助理解如何讀取WAV文件的音量級別。基本上,我需要一種方法來確定音頻文件中特定時間的音量(例如0.01秒),以便找到特定的「峯值」。讀取node.js中的WAV音量數據

我使用節點WAV(https://github.com/TooTallNate/node-wav)和下面的代碼:

var file = fs.createReadStream('test.wav'), 
    reader = new wav.Reader(), 
    bufSize = 4410; 

reader.on('readable', function() { 
    while (null !== (chunk = reader.read(bufSize))) { 
    // I'M TOTALLY LOST HERE 
    } 
}); 

基本上,我不知道由bufSize使用(我敢肯定,這取決於如何「微調「我想要結果..另外我知道我必須以某種方式平均每個塊中的數據,並且我嘗試了平均值等,但似乎無法獲得任何有用的數據..

任何幫助將會讚賞!

回答

1

我不認爲你需要bufSize在這裏,只是遵循的wav.Reader的例子:

var fs = require('fs'); 
var wav = require('wav'); 
var file = fs.createReadStream('track01.wav'); 
var reader = new wav.Reader(); 

// the "format" event gets emitted at the end of the WAVE header 
reader.on('format', function (format) { 
    // do your calculation here, using format 
}); 

// pipe the WAVE file to the Reader instance 
file.pipe(reader); 

無論如何,根據WAV file format,大小和數據塊的數目可以從一個WAV文件的標頭塊中提取。