跳到内容

音频流

pipeline pipeline

音频流管线是一个播放音频片段的线程管线。该管线设计用于在本地机器上运行,因为它需要访问输出设备进行写入。

示例

以下展示了使用该管线的一个简单示例。

from txtai.pipeline import AudioStream

# Create and run pipeline
audio = AudioStream()
audio(data)

该管线可能需要额外的系统依赖。请参阅本节了解更多信息。

请参阅以下链接以获取更详细的示例。

Notebook 描述
语音到语音 RAG ▶️ 包含 RAG 的完整语音到语音工作流 Open In Colab

配置驱动的示例

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

config.yml

# Create pipeline using lower case class name
audiostream:

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

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("audiostream", [["numpy data", "sample rate"]]))

使用 API 运行

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

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"audiostream", "elements":[["numpy data", "sample rate"]]}'

方法

该管线的 Python 文档。

__init__(rate=None)

创建一个 AudioStream 管线。

参数

名称 类型 描述 默认值
rate

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

None
源代码位于 txtai/pipeline/audio/audiostream.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(self, rate=None):
    """
    Creates an AudioStream pipeline.

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

    if not AUDIOSTREAM:
        raise ImportError(
            (
                'AudioStream pipeline is not available - install "pipeline" extra to enable. '
                "Also check that the portaudio system library is available."
            )
        )

    # Target sample rate
    self.rate = rate

    self.queue = Queue()
    self.thread = Thread(target=self.play)
    self.thread.start()

__call__(segment)

将音频片段排队给音频播放器。

参数

名称 类型 描述 默认值
segment

(音频, 采样率)|列表

必需

返回

类型 描述

segment

源代码位于 txtai/pipeline/audio/audiostream.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def __call__(self, segment):
    """
    Queues audio segments for the audio player.

    Args:
        segment: (audio, sample rate)|list

    Returns:
        segment
    """

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

    for x in segments:
        self.queue.put(x)

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