如何使用网络音频api获得音频跟踪样本的正确幅度(不播放)

时间:2015-05-02 11:11:27

标签: javascript audio web-audio

我对网络音频api提供给我的样本量数据进行了一些实验,结果表明它们与其他程序(例如,审计)提供的数据不同。如果我们将分割样本数组长度与采样率(在我的情况下为leftChannel.length / 44100)中显示音频的持续时间更长,并且大胆显示音频中的大声样本,我的脚本显示安静(有时几乎无声) )。我演奏的剧本报道的作品在大胆中很安静,声音肯定很大。

所以问题是:我的方法是找到大小合适的音频样本吗?我的代码出了什么问题?

我已经阅读了关于为audiotrack创建波形的帖子Create a waveform of the full track with Web Audio API 并且有我使用的很好的例子,所以这是我的代码:



(function($) {

  window.AudioContext = window.AudioContext || window.webkitAudioContext;
  var context = new window.AudioContext(); // Create audio container
  var audioData;
  var checkPoint = 21; //this is point in seconds just for debugging, remove later!!
  function decode(audioData) {
    try{
        context.decodeAudioData(audioData,
        function(decoded){
            drawSound(decoded);
            playAudio(decoded);
        },
        function(){ // do nothing here
        });
    } catch(e) {
        console.log('decode exception',e.message);
    }
  }

  function drawSound(buffer) {
    var leftChannel = buffer.getChannelData(0);
    console.log('audio duration: ' + leftChannel.length/44100 + ' sec');
    for (var i = 0; i < leftChannel.length; i++) {
      var volume = leftChannel[i];
      //draw(volume, i);
      
      //this is just for debugging, remove later!!
      if (Math.abs(i - checkPoint * 44100) < 100) {
        console.log(leftChannel[i]);
      }
      
    }    
  }

  function playAudio(buffer) {
    console.log('start playing audio');
    var audioTrack = context.createBufferSource();
    audioTrack.connect(context.destination);
    audioTrack.buffer = buffer;
    audioTrack.start();
  }

  $(document).ready(function() {
    $('body').append($('<input type="text" id="checkpoint" />'));
    $('#checkpoint').change(function() {
      checkPoint = $(this).val();
    });
    $('body').append($('<input type="file" id="audiofile" />'));
    $('input[type="file"]').change(function() {
      var reader = new FileReader();
      reader.onload = function(e) {
        audioData = this.result;
        decode(audioData);
      };
      reader.readAsArrayBuffer(this.files[0]);      
    });
    
  });
})(jQuery);
&#13;
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  </head>
  <body>
    
  </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

如果您认为这是Chrome中的错误,请在crbug.com/new上提交错误。并提供一个录音带,可以产生不同的结果和不同机器的细节。

另请注意,在chrome中,ffmpeg用于解码文件。解码后的文件长度可能与大胆度不同。

答案 1 :(得分:0)

感谢Raymond Toy,我设法解决了这个问题。它显示buffer.getChannelData(0)返回的结果取决于计算机audiocard的采样率(可以从context.sampleRate中找到)。如果你想找到赛道的长度,你应该这样做:

    public string GetMD5HashAsStringFromFile(string filename)
    {
        using (FileStream file = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            var md5er = System.Security.Cryptography.MD5.Create();
            var md5HashBytes = md5er.ComputeHash(file);
            return BitConverter
                    .ToString(md5HashBytes)
                    .Replace("-", string.Empty)
                    .ToLower();
        }
    }

以秒为单位的样本偏移量为:

var samples = buffer.getChannelData(0);
var duration = samples/context.sampleRate;

我的错误是我在公式中使用了mp3 audiotrack的采样率,这导致计算机上出现错误的音频采样率。

正如Raymond Toy所注意到的,如果你只需要音频的持续时间,你可以更简单地得到它:

var sample = samples[i];
var offsetInSeconds = i/context.sampleRate