SpeakingRateStream blocks the event loop: per-frame Python STFT loop in `_stft` / `_spectral_flux`
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- python
調査の方向性
Locate SpeakingRateStream._stft and _spectral_flux, then trace audio draining through _main_task. Run the existing speaking-rate tests and event-loop timing checks, comparing spectral-flux output and loop-blocking measurements before and after the change. Done means the calculation is vectorized without changing results and queued TTS audio no longer causes the measured stalls.
索引モデルが issue の本文から書いたものです。
説明
TranscriptSynchronizer always runs SpeakingRateDetector on the agent's TTS audio, on the job's event loop. SpeakingRateStream._stft computes the STFT one frame at a time in a Python loop (about 80 np.fft.rfft calls per 1 s window at 24 kHz), and _spectral_flux loops over the frames again. Because RoomIO's _ParticipantAudioOutput.capture_frame queues TTS audio without pacing, _main_task drains a whole lump of a reply (10 windows per second of audio) without yielding.
Measured on livekit-agents 1.8.2, numpy 2.5.2, Apple M3 (the code is unchanged in 1.8.8, 2.0.0b1 and main):
- 0.61 ms per window;
_main_taskdraining 20 s of queued 24 kHz audio holds the loop ~123 ms (2 s: 7.6 ms, 5 s: 26 ms, 10 s: 58 ms). - In a local test conversation driven by synthetic speech, with
LIVEKIT_AGENTS_LOOP_BLOCK_WARN_MS=50: 4-5 loop blocks of 59-131 ms per run, a few seconds into each agent turn. With the 200 ms output buffer, stalls that size can be audible, and on smaller cloud machines they'd be roughly twice as long.
The same computation vectorized gives a bit-identical result (480 random cases across 6 sample rates and 4 lengths) and is ~3.8x faster (0.16 ms per window; 20 s of queued audio holds the loop ~38 ms). It replaces _stft + _spectral_flux:
def _spectral_flux(
self, audio: np.ndarray[tuple[int], np.dtype[np.float32]], sample_rate: int
) -> float:
frame_length = int(sample_rate * 0.025) # 25ms
hop_length = frame_length // 2 # 50% overlap
num_frames = (len(audio) - frame_length) // hop_length + 1
if num_frames < 2:
return 0.0
window = np.hanning(frame_length)
scale_factor = 1.0 / np.sqrt(np.sum(window**2))
frames = np.lib.stride_tricks.sliding_window_view(audio, frame_length)[::hop_length]
magnitudes = np.abs(np.fft.rfft(frames * window, axis=1) * scale_factor)
return float(np.mean(np.sum(np.abs(np.diff(magnitudes, axis=0)), axis=1)))
It might also be worth yielding (await asyncio.sleep(0)) between windows in _main_task, so a long lump never holds the loop, whatever the per-window cost.
Happy to open a PR with this if it's useful.
- 主要言語
- Python
- スター
- 14.6k
- フォーク
- 3.9k
- 平均マージ
- 3日 20時間
- マージ済み PR(30日)
- 193
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
livekit/agents のほかの issue
-
Assistant markup is stripped from chat history when expressive mode is off対応中かも @ShivamSharma43 が 14 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
docs: openai.LLM vs openai.responses.LLM for HTTP OpenAI-compat hosts再び着手できるかも @AbarnaaSree が 54 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
livekit/agents#7022 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
livekit/agents#8031 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Agent stays "speaking" forever when the avatar participant leaves mid-reply対応中かも @regalmoix が 1 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 54/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
Validate non-divisible effective batch sizes in the Unsloth LoRA backend対応中かも @debesh-biswas が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
reflex-dev/reflex#7634 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
documentation
難易度 1/5 1〜3時間 初心者へのやさしさ 82/100
topoteretes/cognee#6239 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
ranaroussi/yfinance#3009 ·
メンテナーはふだん 1 日以内に返信
-
Test discovery aborts on GPU builds without cuStateVec: check_cuStateVec expects a failed result but set_config() now raises RuntimeError対応中かも @dmeliksetian が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
Qiskit/qiskit-aer#2495 ·
メンテナーはふだん 1 日以内に返信