如何使用Web Audio API将单声道音频转换为立体声音频

时间:2014-01-31 07:10:58

标签: web-audio

我正在获取音频和视频所在的远程媒体流。在这个流中我得到单声道音频,而我需要立体声音频。

那么如何使用Web Audio API将单声道音频转换为立体声音频

1 个答案:

答案 0 :(得分:2)

2016年8月更新:

<小时/> 显然.connect()电话的行为发生了变化;在过去,输入/输出索引会随着每次调用自动递增,但现在它们只是默认为0 - 所以当未指定时,调用将始终将输出0连接到输入0.


如果输入流有两个通道,但只使用其中一个通道,则必须手动将该通道路由到左右扬声器。通过使用ChannelSplitter,可以将立体声channels中的两个connection(与MediaElementSource的连接)分成两个单独的单connections。然后左侧(或右侧,取决于您的使用)通道连接可以轻松地路由到ChannelMerger的左右连接(由于扇出支持允许单个输出连接到多个不同的输入),这'将所有mono input connections组合回一个stero output connection。旧回答中显示的增益节点是不必要的。

如上所述,可以通过为connect(AudioNode destination, optional unsigned long output = 0, optional unsigned long input = 0);调用指定正确的索引来建立这些连接。

//create a source node to capture the audio from your video element
source = context.createMediaElementSource(document.querySelector('video'));

//Create the splitter and the merger
splitter = context.createChannelSplitter();
merger = context.createChannelMerger();

//route the source audio to the splitter. This is a stereo connection.
source.connect(splitter);

//route output 0 (left) from the splitter to input 0 (left) on the merger. This is a mono connection, carrying the left output signal to the left input of the Merger.
splitter.connect(merger, 0, 0);
//route output 0 (left) from the splitter to input 1 (right) on the merger. This is a mono connection as well, carrying the left output signal to the right input of the Merger.
splitter.connect(merger, 0, 1);

//finally, connect the merger to the destination. This is a stereo connection.
merger.connect(context.destination);

这是图中的样子。请记住,输入和拆分器之间的连接,以及合并和目标是立体声连接(或更多,取决于配置 - 当您有2.1,5.1或7.1设置时,合并和目标之间的连接可以包含3,6或分别为8个通道),而Splitter和Merger之间的两个连接是单声道连接。

+--------------+    +------------+    +-------------------+    +-----------+
| Stereo input |===>| Splitter   |    | Merger            |===>|destination|
+--------------+    |   channel0 |--->|   channel0 (left) |    +-----------+
                    |   channel1 | \  |                   |
                    |   etc      |  ->|   channel1 (right)|
                    +------------+    +-------------------+

我不是百分百肯定,但这可能适用于channel merger node。您只需将gainnode连接到输入1和2.(两次调用.connect。)

编辑(我现在有时间,所以答案更完整):

你是否真的收到一个频道音频,因为webAudio应该自动混合,根据this document,其中声明:For example, if a mono audio stream is connected to a stereo input it should just mix to left and right channels appropriately.。如果您收到一个立体声流,其中只有一个频道包含数据,您需要将其分成两个频道,然后将频道与音频左右连接:(working example here

gain = context.createGain();
splitter = context.createChannelSplitter();
merger = context.createChannelMerger();
merger.connect(context.destination);

source.connect(splitter);
splitter.connect(gain);
gain.connect(merger);
gain.connect(merger);

mergersplitter上发生的情况是,当您拨打.connect时,您会选择下一个频道,但您只想获取第一个频道,然后将其拆分。所以我们将它路由到增益节点,并从那里拆分:

                +----------------+    +----+     +------------------+ 
+----------+    |    Splitter    |    |gain|     |  Merger          |
|mono input|--->|       channel0 |--->|    |---->| channel0 (left)  |   +-----------+
+----------+    |       channel1 |    |    |     |                  |-->|destination|
                |       etc      |    |    |---->| channel1 (right) |   +-----------+
                +----------------+    +----+     +------------------+ 

<击>