Conversation
ruff check . # ← 應無任何錯誤
pytest -q -k device_disconnect
# → 看到 ". 1 passed"
2 files reformatted, 49 files left unchanged
All checks passed!
F [100%]
====================================================== FAILURES =======================================================
_________________________________________ test_write_raises_if_device_missing _________________________________________
def test_write_raises_if_device_missing():
buf = np.zeros(512, np.float32)
fake = "___FAKE_DEVICE___"
with pytest.raises(RuntimeError, match="disconnected"):
> with AudioStream() as s: # 啟動預設裝置
E RuntimeError: At least one of `input_device_name` or `output_device_name` must be provided.
tests/test_device_disconnect.py:11: RuntimeError
During handling of the above exception, another exception occurred:
def test_write_raises_if_device_missing():
buf = np.zeros(512, np.float32)
fake = "___FAKE_DEVICE___"
> with pytest.raises(RuntimeError, match="disconnected"):
E AssertionError: Regex pattern did not match.
E Regex: 'disconnected'
E Input: 'At least one of `input_device_name` or `output_device_name` must be provided.'
tests/test_device_disconnect.py:10: AssertionError
=============================================== short test summary info ===============================================
FAILED tests/test_device_disconnect.py::test_write_raises_if_device_missing - AssertionError: Regex pattern did not match.
1 failed, 26606 deselected in 3.80s
(.venv) wlee45@cajal:~/Projects/recSysPR/pedalboard$
ruff check . pytest -q -k device_disconnect # 應該是 ". 1 passed" 或 "s 1 skipped" 51 files left unchanged All checks passed! s [100%] 1 skipped, 26606 deselected in 3.73s (.venv) wlee45@cajal:~/Projects/recSysPR/pedalboard$
|
@TeleViaBox, thanks for your attention to the issue I posted and for trying to find a solution.
I believe the real solution requires working at a lower level than Python. I tried to fix the AudioStream.h file with some help from AI, since I'm not so familiar with C++: AudioStream.h.zip In my app's code, I currently use a watchdog pattern to detect if the playback position hasn't changed while streaming after a certain timeout. Then, it checks if the current audio output device is still present in the device list. If not, an error message is shown and the app exits. |
What
AudioStream.writeso that it raises RuntimeError("…disconnected")when the configured output device disappears.
_device_helpers.is_device_alive()(sounddevice-based).test_device_disconnect.py.Why
Fixes #411.
Previously, if the user unplugged speakers/headphones during streaming,
AudioStream.write()blocked forever. This killed Safe-and-Sound pipelines(and any real-time player) because there was no way to detect the failure.
How to reproduce (before this patch)
After (with this patch)
AudioStream.write()now raises:RuntimeError: Output device '…' disconnected.
Test plan
pytest -q tests/test_device_disconnect.py # 1 passed (or skipped on headless)
CI matrix (Linux/macOS/Windows) is green.