Skip to main content

duplex-audio

Full-duplex audio front-end for Python: speaker/microphone I/O with acoustic echo cancellation (AEC) and noise suppression (NS).

duplex-audio は、スピーカーで音を再生しながらマイクで集音する Python library です。 マイク入力を AEC、NS の順に処理し、再生音のエコーと周囲の雑音を除いた クリーン音声を NumPy 配列で呼び出し側へ渡します。AEC の参照信号には、 duplex-audio が実際に再生した音を使います。

  • 呼び出し側は、復号済みのモノラル PCM と sample rate を渡して音を再生できます。
  • 呼び出し側は、AEC と NS の backend を選び、それぞれを有効・無効にできます。
  • 呼び出し側は、再生 generation ごとに再生の保持停止、再開、無音化を要求できます。

Install

Python 3.12 以上の環境へ、PyPI からインストールします。

pip install duplex-audio

LocalVQE を AEC backend として使う場合は、次の extra を指定します。

pip install "duplex-audio[localvqe]"

localvqe extra は Linux x86_64 でのみ pylocalvqe をインストールします。 pylocalvqe の wheel は native library を同梱しますが、model weight は別途取得します。

オーディオ I/O は、pasimple を使って PipeWire / PulseAudio の Pulse 互換 Unix socket へ接続します。実行環境には libpulse のランタイムと、接続可能な Pulse 互換の 録音・再生環境が必要です。pulse_server、source、sink を DuplexEngine に渡すと、 接続先とマイク入力・再生出力を指定できます。

Models

LocalVQE の model は、pylocalvqe の downloader で取得します。 downloader は固定した model repository revision から weight を取得し、 SHA-256 を検証します。戻り値は取得した model file の path です。

from pylocalvqe.assets import download_model

model = download_model("v1.4-aec-200k", "models")

呼び出し側は、この path を LocalVqeSettings.model_path へ渡します。 v1.4-aec-200k は AEC-only variant なので、独立した RNNoise の NS と組み合わせられます。 AEC と NS をまとめて行う v1.3 を使う場合は、独立 NS の二重掛けを避けるために ns_backend="none" を指定します。

LocalVQE を使わない場合は、model の取得は不要です。 DuplexEngine の既定の AEC backend は、参考実装の NLMS です。

Python usage

次の例は Linux x86_64 で localvqe extra を導入した環境を使います。 1 秒のテスト音を再生しながら 3 秒間集音し、LocalVQE による AEC と RNNoise による NS を通した音を受け取ります。AEC と NS は既定では無効なので、 例では両方を明示的に有効にしています。

import time

import numpy as np
from pylocalvqe.assets import download_model

from duplex_audio import DuplexEngine, LocalVqeSettings

model = download_model("v1.4-aec-200k", "models")
recorded = []

with DuplexEngine(
    aec_backend="localvqe",
    localvqe_settings=LocalVqeSettings(
        model_path=model,
        variant="v1.4-aec-200k",
        threads=4,
    ),
    aec_enabled=True,
    ns_backend="rnnoise",
    ns_enabled=True,
) as engine:
    sample_rate = 24_000
    t = np.arange(sample_rate) / sample_rate
    reference = (0.05 * np.sin(2 * np.pi * 440 * t)).astype(np.float32)
    generation = engine.create_playback_generation()
    engine.enqueue_playback(generation, reference, sample_rate=sample_rate)
    engine.finish_playback(generation)

    deadline = time.monotonic() + 3.0
    while time.monotonic() < deadline:
        clean = engine.read_clean(timeout=0.1)
        if clean is not None:
            recorded.append(clean)

clean = np.concatenate(recorded) if recorded else np.empty(0, dtype=np.float32)
print(clean.shape, clean.dtype)

enqueue_playback() は、1 次元の浮動小数点 PCM とその sample rate を受け取ります。 再生 sample rate の変換は library が行います。finish_playback() は generation の 末尾を確定します。read_clean() はモノラル 16 kHz の float32 配列を block ごとに返し、指定した待ち時間内に音が届かなければ None を返します。 context manager は録音・再生を開始し、終了時に停止します。

呼び出し側は、pause_playback() と resume_playback() で再生キューを保持したまま 再生を止めて再開し、quiesce_playback() で generation の無音化を要求できます。 継続的に再生する場合は、read_playback_event() から再生 event を読み出し、 terminal になった generation を release_playback_generation() で解放します。 stream_health と get_pipeline_stats() は stream の稼働状態と処理の統計を返します。

複数のマイク

capture_channels で録音する ch 数を指定できます。再生は 1ch のままです。 既定の microphone_mix="selected" は、selected_channel (0 始まり、既定 0) の音を 1 本だけ選びます。"sum" は全 ch の和を返します。各 ch は、それぞれ 16 kHz に 変換されてから混ぜられます。

スピーカーを抑える固定の重みを使うには、稼働中の stream で calibration を測り、 全 ch の結果を明示して適用します。測定中は掃引音がスピーカーから再生されます。

with DuplexEngine(capture_channels=4, selected_channel=0) as engine:
    result = engine.measure_calibration()
    if result.status != "success":
        raise RuntimeError(result.error or result.status)
    engine.apply_calibration(
        result.profile.bulk_delay,
        expected_stream_generation=result.stream_generation,
        channel_responses=result.channel_responses,
    )
    engine.set_microphone_mix("beamform")
    frame = engine.read_clean_frame(timeout=0.1)
    if frame is not None:
        print(frame.sample_offset, frame.microphone_mix,
              frame.speaker_match, frame.speaker_match_status)
        clean = frame.samples

set_microphone_mix() は処理を止めずに三つの出し方を切り替えます。切り替えは次の 処理 block から効き、AEC の状態をリセットします。"beamform" は複数 ch の結果を 適用した後に選べます。適用前の指定は RuntimeError になります。固定 FIR は出力に 5 ms の遅れを加え、処理した block をその都度渡します。

CleanFrame は、音、通し番号、sample の位置、その音を作った出し方、スピーカーとの 一致の値を持ちます。一致の値は直近 512 sample (16 kHz で 32 ms) の録音の観測であり、 AEC / NS の前の音から、出し方によらず計算されます。speaker_match_status は ok、 部屋の雑音の基準に対して小さい quiet、未適用の uncalibrated、1ch の構成の single_channel を区別します。ok 以外では speaker_match は None です。

read_clean() と read_clean_frame() は同じ queue から一つずつ取り出します。 queue に残る古い block には、その block を作った時の出し方と値が付いています。 read_capture_zero_run() は全 ch が 0 ちょうどの長さを返し、 read_capture_channel_zero_runs() は ch の順に同じ観測を返します。

License

duplex-audio の license は MIT です。 全文は LICENSE を参照してください。

Metadata

Release files for duplex-audio 0.18.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for duplex-audio 0.18.1
File Interpreter ABI Platform Actions
duplex_audio-0.18.1-py3-none-any.whl Python 3 none any Details for duplex_audio-0.18.1-py3-none-any.whl

Release files / duplex_audio-0.18.1-py3-none-any.whl

Download URL duplex_audio-0.18.1-py3-none-any.whl
Size 53.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be7bffef0877077c870034e37fc1df577f90ac4dd608e6d1b15c68e14a71282b
BLAKE2b-256 checksum
How to use checksums
5962c05b3f09db5943cedf8b9a8c05e5c07d8d83574f61c824be72d8054e0a20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for duplex-audio 0.18.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page