野生动物园中带有gainNode的createPanner [英] createPanner with gainNode in safari

查看:144
本文介绍了野生动物园中带有gainNode的createPanner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次向左或向右平移并为其设置音量,我已经使用其他浏览器完成了此操作,但是在safari上,createStereoPanner不是一个功能,因此我将safari用于createPanner 现在,问题是我想使用panner的增益来设置音量,当前它同时播放增益和pan,它应该为panner设置增益

I want to pan left or right at a time and also set the volume for it, I have done it with other browsers but on safari createStereoPanner is not a function so i used createPanner for safari Now, Problem is that i want to use gain with panner to set volume currently its playing both gain and pan separately it should set gain for panner

这是我的代码

            audioElement.setAttribute('src', '/Asset/sounds/calibrate.mp3');
    audioElement.volume = 0.5;
    audioElement.play().then(function (d) {
        audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        source = audioCtx.createMediaElementSource(audioElement);
        if (isSafari) {
            gainNode = audioCtx.createGain();
            gainNode.gain.setValueAtTime(audioElement.volume, audioCtx.currentTime);
            source.connect(gainNode);
            gainNode.connect(audioCtx.destination);

            panNode = audioCtx.createPanner();
            panNode.panningModel = 'HRTF';
            source.connect(panNode);
            panNode.connect(audioCtx.destination);
            //panNode.setPosition(10, 0, 0);

        }
        else {
            panNode = audioCtx.createStereoPanner();
            panNode.pan.value = 0;
            source.connect(panNode);
            panNode.connect(audioCtx.destination);
        }
    });

im播放音频时如何处理

im playing audio how to handle it

推荐答案

webKit中PannerNode的实现还差一点.这已经在上一个SO帖子中得到了解答. ,尽管没有完整的可用示例,并且没有增益节点的复杂性.

The implementation of the PannerNode in webKit is still a little off. This has already been answered in a previous SO post, though without a full working example and the added complexity of the gain node.

panner的输出始终是立体声的,因此它应该是链中的最后一个.连接应该去

The output of panner is always stereo, so it should be the last in the chain. The connections should go

Source Sound -> GainNode -> PannerNode -> AudioContext.destination

下面提供了示例代码,并且可以在此处找到JSFiddle .此示例硬切换到正确的频道.

Example code is given below and a JSFiddle can be Found Here. This example hard pans to the right channel.

<!DOCTYPE html>
<html>

  <head>
    <script>
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var gainNode = audioCtx.createGain();
var gain = 0.01;
gainNode.gain.setValueAtTime(gain, audioCtx.currentTime);
//---------------------------------------------------------------------
var pan = 1; // This should be in range [-1, 1]

if (audioCtx.createStereoPanner)
{
  var panner = audioCtx.createPanner();
  panner.pan.value = pan;
}
else
{
  var panner = audioCtx.createPanner();
  panner.panningModel = 'equalpower';
  panner.setPosition(pan, 0, 1 - Math.abs(pan));
}

gainNode.connect(panner);
panner.connect(audioCtx.destination);
    </script>
  </head>
  <!-- ===================================================================== -->

  <body>

    <div>
      <button onclick="myFunction()">
        Click me
      </button>
    </div>
    <script>
function myFunction()
{
  //---------------------------------------------------------------------
  var duration = 0.5; // in seconds
  //---------------------------------------------------------------------
  var oscillator = audioCtx.createOscillator();
  oscillator.type = 'square';
  oscillator.frequency.value = 500;
  oscillator.connect(gainNode);
  oscillator.start(audioCtx.currentTime);
  oscillator.stop(audioCtx.currentTime + duration);
  //---------------------------------------------------------------------
}
    </script>
  </body>
  <!-- ===================================================================== -->

</html>

这篇关于野生动物园中带有gainNode的createPanner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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