跳至内容

音频混合器

pipeline pipeline

音频混合器流水线将多个音频流混合成一个单一流。

示例

以下展示了一个使用此流水线的简单示例。

from txtai.pipeline import AudioMixer

# Create and run pipeline
mixer = AudioMixer()
mixer(((audio1, rate1), (audio2, rate2)))

查看下方链接获取更详细的示例。

Notebook 描述
生成式音频 使用生成式音频工作流进行故事讲述 Open In Colab

配置驱动的示例

流水线可以通过 Python 或配置运行。流水线可以在 配置 中使用流水线的名称小写进行实例化。配置驱动的流水线可以通过 工作流API 运行。

config.yml

# Create pipeline using lower case class name
audiomixer:

# Run pipeline with workflow
workflow:
  audiomixer:
    tasks:
      - action: audiomixer

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("audiomixer", [[[audio1, rate1], [audio2, rate2]]]))

使用 API 运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"audiomixer", "elements":[[[audio1, rate1], [audio2, rate2]]]}'

方法

此流水线的 Python 文档。

__init__(rate=None)

创建一个 AudioMixer 流水线。

参数

名称 类型 描述 默认值
rate

可选的目标采样率,否则使用每个音频段的输入目标采样率

None
源代码位于 txtai/pipeline/audio/audiomixer.py
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, rate=None):
    """
    Creates an AudioMixer pipeline.

    Args:
        rate: optional target sample rate, otherwise uses input target rate with each audio segment
    """

    if not SCIPY:
        raise ImportError('AudioMixer pipeline is not available - install "pipeline" extra to enable.')

    # Target sample rate
    self.rate = rate

__call__(segment, scale1=1, scale2=1)

将多个音频流混合成一个单一流。

参数

名称 类型 描述 默认值
segment

((audio1, 采样率), (audio2, 采样率))|list

必需
scale1

segment1 的可选缩放因子

1
scale2

segment2 的可选缩放因子

1

返回

类型 描述

(音频, 采样率) 列表

源代码位于 txtai/pipeline/audio/audiomixer.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def __call__(self, segment, scale1=1, scale2=1):
    """
    Mixes multiple audio streams into a single stream.

    Args:
        segment: ((audio1, sample rate), (audio2, sample rate))|list
        scale1: optional scaling factor for segment1
        scale2: optional scaling factor for segment2

    Returns:
        list of (audio, sample rate)
    """

    # Convert single element to list
    segments = [segment] if isinstance(segment, tuple) else segment

    results = []
    for segment1, segment2 in segments:
        audio1, rate1 = segment1
        audio2, rate2 = segment2

        # Resample audio, as necessary
        target = self.rate if self.rate else rate1
        audio1 = Signal.resample(audio1, rate1, target)
        audio2 = Signal.resample(audio2, rate2, target)

        # Mix audio into single segment
        results.append((Signal.mix(audio1, audio2, scale1, scale2), target))

    # Return single element if single element passed in
    return results[0] if isinstance(segment, tuple) else results