How to pan audio in iOS' Web Audio implementation?

这一生的挚爱 提交于 2021-01-05 06:03:20

问题


The WebAudio spec gives API calls for both two-channel L/R panning (StereoPannerNode) and panning in 3D space (PannerNode). Despite this, I can't find an approach that works on iOS.

Regarding normal L/R panning, MDN says that the 2D audioContext.createStereoPanner() is not implemented in iOS. (Or on desktop Safari.) Maybe you could create one by directly calling the PannerNode constructor, but I'm doubtful that works, at least on iOS.

AudioContext panning audio of playing media shows a way to do L/R panning without StereoPannerNode, using channel mergers and channel splitters. However, MDN says that createChannelMerger isn't implemented on iOS. Maybe you could directly calling the ChannelMergeNode constructor, but I'm also doubtful that this would work, at least on iOS.

As for panning in 3D, MDN says iOS doesn't support audioContext.createPanner. (Whereas desktop Safari does.) Again, maybe you could use the PannerNode constructor directly, but I'm doubtful. So I don't think 3D panning will work either.


回答1:


The StereoPannerNode isn't available yet, but the 3D PannerNode should work fine (not as constructor yet, but with the .createPanner() method). For example this is how you could achieve stereo panning with the 3D panner on iOS:

var pan = -1;  // This should be in range [-1, 1]

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

someSource.connect(panner);
panner.connect(context.destination);

I think that the MDN support tables aren't accurate or outdated a bit. Look in the PannerNode docs at MDN, the support table looks differently that the ones you've linked.



来源:https://stackoverflow.com/questions/52809552/how-to-pan-audio-in-ios-web-audio-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!