Skip to content

Commit c00a8d3

Browse files
authored
Merge pull request #74 from MurineShiftWork/fix/sounds-ttl-channel-zero
fix(sounds): keep a valid ttl_channel=0 instead of clobbering to default
2 parents 727624f + 07fb8e2 commit c00a8d3

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/murineshiftwork/logic/sounds.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
self,
8282
sound_device: str | None = None,
8383
sample_rate: int | None = None,
84-
ttl_channel: int = 1,
84+
ttl_channel: int | None = None,
8585
ttl_duration: float = 0.001,
8686
channel_mode: str | None = None,
8787
allow_sys_default_device=True,
@@ -203,7 +203,10 @@ def __init__(
203203
self.sample_rate = _fallback_sr
204204
self.use_wasapi_exclusive = False
205205

206-
self.ttl_channel = ttl_channel or self.default_ttl_channel
206+
# `or` would clobber a valid ttl_channel=0 to the default; select on None instead.
207+
self.ttl_channel = (
208+
self.default_ttl_channel if ttl_channel is None else ttl_channel
209+
)
207210
if self.ttl_channel != 0 and self.ttl_channel != 1:
208211
raise ValueError(
209212
f"'ttl_channel' has to be 0 or 1 for stereo output, but '{self.ttl_channel}' given"

tests/test_sounds.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ def test_default_output_fallback_not_sysdefault(monkeypatch):
125125
assert s.sample_rate == 48000
126126

127127

128+
def test_ttl_channel_zero_is_not_clobbered_to_default(monkeypatch):
129+
"""ttl_channel=0 is a valid channel index; it must survive __init__ (was lost to `or default`)."""
130+
import sys
131+
import types
132+
133+
dev = {"name": "X", "default_samplerate": 48000.0}
134+
fake_sd = types.SimpleNamespace(check_output_settings=lambda **kw: None)
135+
monkeypatch.setitem(sys.modules, "sounddevice", fake_sd)
136+
monkeypatch.setattr(
137+
"murineshiftwork.logic.sounds.find_sound_device", lambda **kw: (5, dev)
138+
)
139+
140+
assert StereoSound(sound_device="X", ttl_channel=0).ttl_channel == 0
141+
# omitted -> falls back to the class default
142+
assert StereoSound(sound_device="X").ttl_channel == StereoSound.default_ttl_channel
143+
144+
128145
def test_falls_back_to_default_rate_when_device_rejects(monkeypatch):
129146
"""Device found but rejecting the configured rate (e.g. XONAR 192000 when its
130147
driver/Windows format is not at 192000): fall back to the device default so

0 commit comments

Comments
 (0)