Skip to content

Commit

Permalink
Harden scsynth.Options (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephine-wolf-oberholtzer authored Feb 16, 2025
1 parent ef10d43 commit 08e66f4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 18 deletions.
17 changes: 5 additions & 12 deletions supriya/scsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ class Options:
protocol: str = "udp"
random_number_generator_count: int = 64
realtime: bool = True
remote_control_volume: bool = False
restricted_path: Optional[str] = None
safety_clip: Optional[Union[int, Literal["inf"]]] = None
sample_rate: Optional[int] = None
threads: Optional[int] = None
threads: int = 6
ugen_plugins_path: Optional[str] = None
verbosity: int = 0
wire_buffer_count: int = 64
Expand All @@ -85,14 +84,6 @@ class Options:
### INITIALIZER ###

def __post_init__(self):
if self.input_bus_channel_count is None:
object.__setattr__(self, "input_bus_channel_count", 8)
if self.output_bus_channel_count is None:
object.__setattr__(self, "output_bus_channel_count", 8)
if self.input_bus_channel_count < 0:
raise ValueError(self.input_bus_channel_count)
if self.output_bus_channel_count < 0:
raise ValueError(self.output_bus_channel_count)
if self.audio_bus_channel_count < (
self.input_bus_channel_count + self.output_bus_channel_count
):
Expand Down Expand Up @@ -135,6 +126,8 @@ def serialize(self) -> List[str]:
result = [str(find(self.executable))]
pairs: Dict[str, Optional[Union[List[str], str]]] = {}
if self.realtime:
if self.ip_address != DEFAULT_IP_ADDRESS:
pairs["-B"] = self.ip_address
if self.protocol == "tcp":
pairs["-t"] = str(self.port)
else:
Expand Down Expand Up @@ -189,8 +182,8 @@ def serialize(self) -> List[str]:
pairs["-P"] = str(self.restricted_path)
if self.safety_clip is not None:
pairs["-s"] = str(self.safety_clip)
if self.threads and find(self.executable).stem == "supernova":
pairs["-t"] = str(self.threads)
if self.threads != 6 and find(self.executable).stem == "supernova":
pairs["-T"] = str(self.threads)
if self.ugen_plugins_path:
pairs["-U"] = str(self.ugen_plugins_path)
if 0 < self.verbosity:
Expand Down
106 changes: 100 additions & 6 deletions tests/test_scsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
@pytest.mark.parametrize(
"kwargs, expected",
[
# nothing specified
(
{},
[
"/path/to/scsynth",
"/path/to/executable",
"-R",
"0",
"-l",
Expand All @@ -62,7 +63,7 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
(
{"input_device": "Device X"},
[
"/path/to/scsynth",
"/path/to/executable",
"-H",
"Device X",
"",
Expand All @@ -78,7 +79,7 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
(
{"output_device": "Device Y"},
[
"/path/to/scsynth",
"/path/to/executable",
"-H",
"",
"Device Y",
Expand All @@ -94,7 +95,7 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
(
{"input_device": "Device P", "output_device": "Device Q"},
[
"/path/to/scsynth",
"/path/to/executable",
"-H",
"Device P",
"Device Q",
Expand All @@ -110,7 +111,7 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
(
{"input_device": "Device Z", "output_device": "Device Z"},
[
"/path/to/scsynth",
"/path/to/executable",
"-H",
"Device Z",
"-R",
Expand All @@ -121,10 +122,103 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
"57110",
],
),
# many flags specified
(
{
"audio_bus_channel_count": 1024 * 2,
"block_size": 64 * 2,
"buffer_count": 1024 * 2,
"control_bus_channel_count": 16384 * 2,
"executable": "supernova",
"hardware_buffer_size": 512,
"initial_node_id": 100,
"input_bus_channel_count": 16,
"input_device": "Hypothetical Input Device",
"input_stream_mask": "01101010",
"ip_address": "0.0.0.0",
"load_synthdefs": False,
"maximum_logins": 23,
"maximum_node_count": 1024 * 2,
"maximum_synthdef_count": 1024 * 2,
"memory_locking": True,
"memory_size": 8192 * 2,
"output_bus_channel_count": 8 * 2,
"output_device": "Hypothetical Output Device",
"output_stream_mask": "10010101",
"password": "changeme",
"protocol": "tcp",
"random_number_generator_count": 64 * 2,
"restricted_path": "/restricted/path",
"safety_clip": "inf",
"sample_rate": 44100,
"threads": 8,
"ugen_plugins_path": "/path/to/ugens",
"verbosity": 1,
"wire_buffer_count": 64 * 22,
"zero_configuration": True,
},
[
"/path/to/executable",
"-B",
"0.0.0.0",
"-D",
"0",
"-H",
"Hypothetical Input Device",
"Hypothetical Output Device",
"-I",
"01101010",
"-L",
"-O",
"10010101",
"-P",
"/restricted/path",
"-S",
"44100",
"-T",
"8",
"-U",
"/path/to/ugens",
"-Z",
"512",
"-a",
"2048",
"-b",
"2048",
"-c",
"32768",
"-d",
"2048",
"-i",
"16",
"-l",
"23",
"-m",
"16384",
"-n",
"2048",
"-o",
"16",
"-p",
"changeme",
"-r",
"128",
"-s",
"inf",
"-t",
"57110",
"-v",
"1",
"-w",
"1408",
"-z",
"128",
],
),
],
)
def test_Options(kwargs: Dict, expected: List[str]) -> None:
options = scsynth.Options(**kwargs)
actual = list(options)
actual[0] = "/path/to/scsynth" # replace to make it portable
actual[0] = "/path/to/executable" # replace to make it portable
assert actual == expected

0 comments on commit 08e66f4

Please sign in to comment.