javascript - 声音淡出,但不淡入——为什么?

标签 javascript audio web-audio-api

我想我了解 Web Audio API 的主要概念,以及声音的一般工作原理。即使我设法使声音“淡出”,我也无法弄清楚为什么在我写的以下片段中它没有“淡入”来表示问题:

(function ()
{
    'use strict';
    var context = new AudioContext(),
        wave = context.createOscillator(),
        gain = context.createGain(),
        ZERO = 0.000001;

    wave.connect(gain);
    gain.connect(context.destination);

    wave.type = 'sine';
    wave.frequency.value = 200;
    gain.gain.value = ZERO;

    wave.start(context.currentTime);

    gain.gain.exponentialRampToValueAtTime(1.00, 1.0);
    gain.gain.exponentialRampToValueAtTime(ZERO, 3.0);
})();

注意:同样的问题也出现在 Firefox (Linux) 和 Chrome (Windows) 上

最佳答案

gain.gain.value = ZERO 行替换为:

gain.gain.setValueAtTime(ZERO, 0);

将解决问题。

基本原理在 actual specificationexponentialRampToValueAtTime() 函数:

Schedules an exponential continuous change in parameter value from the previous scheduled parameter value to the given value

因此,如果没有先前的预定参数值(只有固定值),则该函数无法进行插值。这同样适用于 linearRampToValueAtTime 函数。

这也可能对 MDN documentation 有用:

AudioParam.value ... Though it can be set, any modifications happening while there are automation events scheduled — that is events scheduled using the methods of the AudioParam — are ignored, without raising any exception

关于javascript - 声音淡出,但不淡入——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127720/

相关文章:

javascript - ZingChart 负 X 轴显示

javascript - 我需要使用 JavaScript 或 HTML 链接到当前 HTML 页面中的另一个 HTML 页面

java - 在java中,发出选择性的嘟嘟声

java - 混合 8 位 8000Hz PCM_ULAW 样本的最简单方法是什么?

actionscript-3 - as3删除flv播放类我仍然可以听到声音

web-audio-api - 当浏览器不支持cancelAndHoldAtTime时,是否有正确的替代方案?

javascript - 为什么我的第二个音频文件无法使用 currentTime 属性播放?

javascript - 调用ajax后如何将结果显示到新的HTML页面中

Javascript 从函数返回 Promise - Promise 链与变量

safari - Web 音频 API 在 Safari 中输出 302 重定向资源的静音