使用网络音频平滑改变音量 API
Smooth volume change with Web Audio API
javascript 这里是新手。所以我一直在摆弄 Web Audio API 试图解决问题,我发现 exponentialRampToValueAtTime 做了我想要的,除了它似乎只做了一次(?)
拿这个通用代码:
context = new AudioContext();
oscillator = context.createOscillator();
contextGain = context.createGain();
oscillator.type = 'sine';
oscillator.frequency = 440
oscillator.connect(contextGain);
contextGain.connect(context.destination);
oscillator.start(0);
contextGain.gain.value = 1 默认情况下所以如果我 运行 contextGain.gain.exponentialRampToValueAtTime(0.1,context.currentTime + 2)
它从 1 下降到 0.1 很好很顺利。但是如果我试图让它回到 1,说 contextGain.gain.exponentialRampToValueAtTime(1,context.currentTime + 2)
它反而突然跳到 1。为什么会这样?有什么办法可以让我根据需要多次执行此斜坡操作吗?提前致谢。
您需要先调用 setValueAtTime()
来标记更改的开始:
contextGain.gain.setValueAtTime(contextGain.gain.value, context.currentTime);
contextGain.gain.exponentialRampToValueAtTime(0.1, context.currentTime + 2);
没有这个,你的渐变从过去开始。
这是必需的,因为音频参数的逐渐变化从上一个事件开始。之前的事件是使用方法 setValueAtTime()
、linearRampToValueAtTime()
、exponentialRampToValueAtTime()
等
的先前增益更改
The exponentialRampToValueAtTime()
method of the AudioParam
Interface schedules a gradual exponential change in the value of the AudioParam
. The change starts at the time specified for the previous event, follows an exponential ramp to the new value given in the value
parameter, and reaches the new value at the time given in the endTime
parameter.
这是一个小演示:
var context = new AudioContext();
var oscillator = context.createOscillator();
var gain = context.createGain();
oscillator.type = 'sine';
oscillator.frequency = 440
oscillator.connect(gain);
gain.connect(context.destination);
document.getElementById('bplay').addEventListener('click', function() {
context.resume();
oscillator.start(0);
gain.gain.setValueAtTime(0.01, context.currentTime);
gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 2);
});
document.getElementById('bdown').addEventListener('click', function() {
gain.gain.cancelScheduledValues(context.currentTime);
gain.gain.setValueAtTime(gain.gain.value, context.currentTime);
gain.gain.exponentialRampToValueAtTime(0.01, context.currentTime + 2);
});
document.getElementById('bup').addEventListener('click', function() {
gain.gain.cancelScheduledValues(context.currentTime);
gain.gain.setValueAtTime(gain.gain.value, context.currentTime);
gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 2);
});
<button type="button" id="bplay">Play</button>
<button type="button" id="bdown">Volume down</button>
<button type="button" id="bup">Volume up</button>
javascript 这里是新手。所以我一直在摆弄 Web Audio API 试图解决问题,我发现 exponentialRampToValueAtTime 做了我想要的,除了它似乎只做了一次(?) 拿这个通用代码:
context = new AudioContext();
oscillator = context.createOscillator();
contextGain = context.createGain();
oscillator.type = 'sine';
oscillator.frequency = 440
oscillator.connect(contextGain);
contextGain.connect(context.destination);
oscillator.start(0);
contextGain.gain.value = 1 默认情况下所以如果我 运行 contextGain.gain.exponentialRampToValueAtTime(0.1,context.currentTime + 2)
它从 1 下降到 0.1 很好很顺利。但是如果我试图让它回到 1,说 contextGain.gain.exponentialRampToValueAtTime(1,context.currentTime + 2)
它反而突然跳到 1。为什么会这样?有什么办法可以让我根据需要多次执行此斜坡操作吗?提前致谢。
您需要先调用 setValueAtTime()
来标记更改的开始:
contextGain.gain.setValueAtTime(contextGain.gain.value, context.currentTime);
contextGain.gain.exponentialRampToValueAtTime(0.1, context.currentTime + 2);
没有这个,你的渐变从过去开始。
这是必需的,因为音频参数的逐渐变化从上一个事件开始。之前的事件是使用方法 setValueAtTime()
、linearRampToValueAtTime()
、exponentialRampToValueAtTime()
等
The
exponentialRampToValueAtTime()
method of theAudioParam
Interface schedules a gradual exponential change in the value of theAudioParam
. The change starts at the time specified for the previous event, follows an exponential ramp to the new value given in thevalue
parameter, and reaches the new value at the time given in theendTime
parameter.
这是一个小演示:
var context = new AudioContext();
var oscillator = context.createOscillator();
var gain = context.createGain();
oscillator.type = 'sine';
oscillator.frequency = 440
oscillator.connect(gain);
gain.connect(context.destination);
document.getElementById('bplay').addEventListener('click', function() {
context.resume();
oscillator.start(0);
gain.gain.setValueAtTime(0.01, context.currentTime);
gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 2);
});
document.getElementById('bdown').addEventListener('click', function() {
gain.gain.cancelScheduledValues(context.currentTime);
gain.gain.setValueAtTime(gain.gain.value, context.currentTime);
gain.gain.exponentialRampToValueAtTime(0.01, context.currentTime + 2);
});
document.getElementById('bup').addEventListener('click', function() {
gain.gain.cancelScheduledValues(context.currentTime);
gain.gain.setValueAtTime(gain.gain.value, context.currentTime);
gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 2);
});
<button type="button" id="bplay">Play</button>
<button type="button" id="bdown">Volume down</button>
<button type="button" id="bup">Volume up</button>