网络音频 API - 提高增益以避免点击声

问题描述 投票:0回答:1

我试图通过减弱增益来消除应用程序中的点击次数。使用下面的代码,我可以听到声音变化,但它不会消失。

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;
}

要查看其运行版本,请访问此站点:

RulerandCompass.net

现场的musical.js文件没有增益调整的更新。该网站是一个用于创建标尺和圆规图案的绘图工具。

要进行绘制,请使用圆形工具单击一个点,然后单击另一个点。交点彼此之间具有几何关系。使用如下所示的音乐线工具来绘制音乐。音高是通过将基本音高 (220hz) 除以第一个半径与新线的比率来确定的。

在键盘上输入“p”即可播放乐曲。

javascript audio web-audio-api
1个回答
1
投票

在使用其他自动化功能之前,您需要

setValueAtTime

要淡出,您需要

gainNode.gain.setTargetAtTime(0, audioContext.currentTime, 0.1)
,其中 0.1 是一阶滤波器的时间常数,请参阅 https://en.wikipedia.org/wiki/Time_constant#Exponential_decay

© www.soinside.com 2019 - 2024. All rights reserved.