【问题标题】:Web Audio Api: frequency related to elapsed timeWeb Audio Api:与经过时间相关的频率
【发布时间】:2013-04-25 22:52:53
【问题描述】:

我有一个获取音频的脚本。我想将该频率与歌曲的确切时间联系起来。我可以获得 webkitAudioContext currentTime 属性,但这并不准确,因为它在歌曲开始之前将声音保存在缓冲区中时开始计算时间。 这是我的代码:

var context = new webkitAudioContext();

...

function drawSpectrogram(array) {
    // copy the current canvas onto the temp canvas
    var canvas = document.getElementById("canvas");

    tempCtx.drawImage(canvas, 0, 0, 800, 512);
    // iterate over the elements from the array
    for (var i = 0; i < array.length; i++) {
        // draw each pixel with the specific color
        var value = array[i];
        frequency = frequency + value + ";";
        time = time + Math.round((context.currentTime) * 1000000) / 1000000 + ";";               
        ctx.fillStyle = hot.getColor(value).hex();
        // draw the line at the right side of the canvas
        ctx.fillRect(800 - 1, 512 - i, 1, 1);
    }

}

谢谢!

【问题讨论】:

    标签: javascript google-chrome html5-audio web-audio-api


    【解决方案1】:

    在您准备开始播放之前保存currentTime 的值,并将该时间值用作您的“零时间”标记。每当您想知道音乐播放了多长时间时,请从 currentTime 值中减去该时间值(表示设置花费了多长时间)。

    var context = new webkitAudioContext();
    
    // load media, place in buffer, etc...
    
    var startTime = context.currentTime; // everything up to now has been setup
    
    // begin playing...
    
    var currentPlayingTime = context.currentTime - startTime;
    

    只需使用context.currentTime - startTime 即可查看歌曲实际播放了多长时间。

    【讨论】:

      【解决方案2】:

      我不太确定您要做什么,但您可能想看看高分辨率时间 API:http://www.w3.org/TR/hr-time/

      您可以在您感兴趣的缓冲区上调用 .noteOn() 或 .start() 的同时获取当前时间,然后在 DrawSpectrogram 函数中确定执行所需操作的时间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多