我试图通过淡化增益来摆脱我的应用程序中的点击。使用下面的代码,我可以听到声音移动,但它不会淡出。
var oscillator = audioContext.createOscillator();
oscillator.connect(gainNode);
oscillator.type = 'sine'; // sine, triangle, sawtooth
console.log(self.currentPitch);
if(isFinite(self.currentPitch)==true){
oscillator.frequency.value = self.currentPitch;
// connect it to the output
oscillator.connect(audioContext.destination);
// start the note
oscillator.start(0);
setTimeout(function(){
var now = audioContext.currentTime;
//gainNode.gain.value=0;
gainNode.gain.linearRampToValueAtTime(0,now+.05);
oscillator.stop(now+.05);
}, self.duration*1000);
以下是完整的代码:
var Musical = function(){
var self=this;
self.basePitch= 420;
self.currentPitch=420;
self.baseLineLength=$("#guidecircles circle").first().attr("r");
self.currentLineLength=100;
self.duration=.2; //time in seconds
self.playPosition=0;
self.playTimer=false;
try {
if (! window.AudioContext) {
if (! window.webkitAudioContext) {
self.bad_browser();
return;
}
window.AudioContext = window.webkitAudioContext;
}
var audioContext = new AudioContext();
}
catch(e) {
console.log('Web Audio API is not supported in this browser');
}
var gainNode = audioContext.createGain();
gainNode.connect(audioContext.destination);
/* Playing a tone */
self.playTone=function(){
self.setCurrentPitch();
// create the oscillator
var oscillator = audioContext.createOscillator();
oscillator.connect(gainNode);
oscillator.type = 'sine'; // sine, triangle, sawtooth
console.log(self.currentPitch);
if(isFinite(self.currentPitch)==true){
oscillator.frequency.value = self.currentPitch;
// connect it to the output
oscillator.connect(audioContext.destination);
// start the note
oscillator.start(0);
setTimeout(function(){
var now = audioContext.currentTime;
//gainNode.gain.value=0;
gainNode.gain.linearRampToValueAtTime(0,now+.05);
oscillator.stop(now+.05);
}, self.duration*1000);
}
/* if(typeof oscillator !="undefined"){
}*/
return self;
}
/* Get current pitch */
self.setCurrentPitch=function(){
/* find ratio of current line length to base line length */
var ratio=parseFloat(self.baseLineLength/self.currentLineLength);
/* apply ratio to base pitch and set the current pitch */
self.currentPitch=self.basePitch*ratio;
console.log(self.baseLineLength,self.currentLineLength,"ratio:"+ratio);
}
/* play music */
self.play=function(){
self.playTimer=setInterval(function(){ playNext() }, self.duration*1000);
return self;
}
var playNext=function(){
var toneLine=$("#musicallines line").eq(self.playPosition);
$("#musicallines line").removeClass('playing');
toneLine.addClass('playing');
if(self.playPosition>($("#musicallines line").length-1)){
clearInterval(self.playTimer);
self.playPosition=0;
} else {
self.playPosition++;
self.currentLineLength=toneLine.LineEquation().getMagnitude();
self.playTone();
}
}
self.bad_browser=function(){
alert("Your browser does not support web audio");
}
return self;
}
要查看其正在运行的版本,请访问此站点:
实际网站上的musical.js文件没有增益调整的更新。该网站是一个绘图工具,用于创建标尺和指南针图案。
要绘制,使用圆形工具单击一个点然后另一个点。交点具有彼此的几何关系。使用下面选择的音乐线条工具制作音乐。通过将基准距(220hz)除以第一半径与新线的比率来确定间距。
在键盘上输入“p”以播放合成。
答案 0 :(得分:1)
在使用其他自动化功能之前,您需要setValueAtTime
。
要淡出,您需要gainNode.gain.setTargetAtTime(0, audioContext.currentTime, 0.1)
,其中0.1是一阶滤镜的时间常数,请参阅https://en.wikipedia.org/wiki/Time_constant#Exponential_decay。