我怎样才能避免这种“点击”的声音时,我停止播放声音? [英] How can I avoid this 'clicking' sound when I stop playing a sound?

查看:212
本文介绍了我怎样才能避免这种“点击”的声音时,我停止播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望这个问题保持一个编程问题,并没有最终的声音力学问题......这里去...

我为了弄清楚如何通过Web Audio API的工作正在做一些实验。我所试图做的是一个简单的挂断电话的声音在循环播放。问题是,当声音结束后,你可以听到一个很烦人的点击的声音。我不能更好地解释它,但你可以听到它,如果你测试code。

有没有一些方法,我可以避免这种情况?一些过滤器,我可以申请什么的?

\r
\r

VAR audioContext =新(AudioContext || webkitAudioContext)();\r
    \r
    VAR frequencyOffset = 0\r
    功能BOOP(){\r
      //我们的声源是一个简单的三角形振荡\r
      变种振荡器= audioContext.createOscillator(); //创建声源\r
      oscillator.type ='三角';\r
      \r
      //添加增益节点只是为了降低音量一点,使\r
      //听起来不那么刺耳\r
      VAR增益= audioContext.createGain();\r
      oscillator.connect(增益);\r
      gain.connect(audioContext.destination);\r
      \r
      gain.gain.value = 0.1;\r
      //只是为了好玩,让每个itteration频率增加\r
      oscillator.frequency.value = 200 + frequencyOffset;\r
      oscillator.start(0);\r
      \r
      //声音应该持续250毫秒\r
      的setTimeout(函数(){\r
        oscillator.disconnect();\r
        oscillator.stop();\r
        gain.disconnect();\r
      },250);\r
      frequencyOffset + = 1;\r
    }\r
\r
    的setInterval(BOOP,500);

\r

\r
\r


解决方案

看起来像网络音频API使开发人员在玩不突然停止波形和避免噪音和良好的假象停止声源的简便方法。


  1. 创建您的声源(在我的例子振荡器)

  2. 创建一个增益节点,并将其与音源连接。

  3. 启动声源和增益值设置为0。这样一来,你就不会听到声音,即使它在技术上播放

  4. 当你想在源播放设定增益值,以1比0的时候它不应该打。增益节点将处理其余部分,并没有点击后会听到

\r
\r

VAR audioContext =新(AudioContext || webkitAudioContext)();\r
\r
VAR frequencyOffset = 0\r
  //我们的声源是一个简单的三角形振荡\r
变种振荡器= audioContext.createOscillator(); //创建声源\r
oscillator.type ='三角';\r
\r
//添加增益节点只是为了降低音量一点,使\r
//听起来不那么刺耳。这也将使我们能够静音和重放\r
//我们的需求声音\r
变种gainNode = audioContext.createGain();\r
oscillator.connect(gainNode);\r
gainNode.connect(audioContext.destination);\r
\r
gainNode.gain.value = 0;\r
oscillator.frequency.value = 200;\r
oscillator.start(0);\r
\r
功能BOOP(){\r
  gainNode.gain.value = 0.1;\r
  //声音应该持续250毫秒\r
  的setTimeout(函数(){\r
    gainNode.gain.value = 0;\r
  },250);\r
  oscillator.frequency.value ++;\r
}\r
\r
的setInterval(BOOP,500);

\r

\r
\r

I really hope this question stays a programming question and do not end up an Sound Mechanics question... Here goes...

I am doing some experiments in order to figure out how the Web Audio API works. What I am trying to do is a simple "Hang up phone" sound playing in a loop. The problem is that when the sound ends, you can hear a quite annoying 'clicking' sound. I cannot explain it better, but you can hear it if you test the code.

Is there some way I could avoid this? Some filter I could apply or anything?

var audioContext = new (AudioContext || webkitAudioContext)();
    
    var frequencyOffset = 0
    function boop(){
      // Our sound source is a simple triangle oscillator
      var oscillator = audioContext.createOscillator(); // Create sound source  
      oscillator.type = 'triangle';
      
      // Adding a gain node just to lower the volume a bit and to make the
      // sound less ear-piercing
      var gain = audioContext.createGain();
      oscillator.connect(gain);
      gain.connect(audioContext.destination);
      
      gain.gain.value = 0.1;
      // Just for fun let the frequency increase on each itteration
      oscillator.frequency.value = 200 + frequencyOffset;
      oscillator.start(0);
      
      // The sound should last for 250ms
      setTimeout(function(){
        oscillator.disconnect(); 
        oscillator.stop();
        gain.disconnect();
      }, 250);
      frequencyOffset += 1;
    }

    setInterval(boop, 500);

解决方案

Looks like the Web Audio API gives the developer an easy way of stopping a sound source from playing without abruptly stopping the waveform and avoid any noise and sound artifacts.

  1. Create your sound source (in my example an oscillator)
  2. Create a gain node and connect it with the sound source.
  3. Start the sound source and set the gain value to 0. That way, you won't listen to the sound even if it's technically playing
  4. Set the gain value to 1 when you want the source to play and to 0 when it should not play. The gain node will handle the rest, and no clicking will be heard

var audioContext = new(AudioContext || webkitAudioContext)();

var frequencyOffset = 0
  // Our sound source is a simple triangle oscillator
var oscillator = audioContext.createOscillator(); // Create sound source  
oscillator.type = 'triangle';

// Adding a gain node just to lower the volume a bit and to make the
// sound less ear-piercing. It will also allow us to mute and replay
// our sound on demand
var gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);

gainNode.gain.value = 0;
oscillator.frequency.value = 200;
oscillator.start(0);

function boop() {
  gainNode.gain.value = 0.1;
  // The sound should last for 250ms
  setTimeout(function() {
    gainNode.gain.value = 0;
  }, 250);
  oscillator.frequency.value++;
}

setInterval(boop, 500);

这篇关于我怎样才能避免这种“点击”的声音时,我停止播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆