无法在JavaScript中限制振荡器的持续时间

时间:2012-03-01 14:02:30

标签: javascript audio buffer

使用audiolib.js将这个振荡器的持续时间限制在10秒左右我遇到了一些麻烦。当我使用dsp.js库时,我使用bufferSize限制了持续时间,但我完全不知道如何使用audiolib.js库来实现...任何帮助都会很棒!虽然我在,但是有人能告诉我最大和最小频率和幅度吗?

$(document).ready(function () {

    //var context = new webkitAudioContext();
    var playing;
    var dev = audioLib.AudioDevice(audioCallback, 2);
    var osc = audioLib.Oscillator(dev.sampleRate, 440);
    //var bfo = audioLib.Oscillator(dev.sampleRate, 1.0);
    //osc.addAutomation('frequency', bfo, 0.5, 'modulation');
    osc.waveShape = 'pulse';

    function audioCallback(buffer, channelCount) {
        if (playing) {
            //bfo.generateBuffer(buffer.length / channelCount);
            osc.append(buffer, channelCount);
            //remove the audiocallback function
        }
    }

    $('#playButton').click(function () {
        playing = true;
    });

});

干杯!

1 个答案:

答案 0 :(得分:1)

采样率是每秒采样器的数量,因此您可以通过将采样率乘以10秒来计算要播放的样本数量:

var maxSamples = dev.sampleRate * 10;

然后您可以像这样使用它:

$(document).ready(function () {

    var playing;
    var dev = audioLib.AudioDevice(audioCallback, 2);
    var osc = audioLib.Oscillator(dev.sampleRate, 440);
    var maxSamples = dev.SampleRate * 10;
    var totalSamples = 0;
    osc.waveShape = 'pulse';

    function audioCallback(buffer, channelCount) {
        if (playing) {
            osc.append(buffer, channelCount);
            totalSamples += buffer.length / channelCount;
            if (totalSamples >= maxSamples) {
                // remove audioCallback
                dev.kill();
            }
        }
    }

    $('#playButton').click(function () {
        playing = true;
    });

});