Skip to content

Commit

Permalink
SUPRIYA-409: Fix hardware device options (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephine-wolf-oberholtzer authored Dec 20, 2024
1 parent 98bf6de commit a15bb46
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
22 changes: 15 additions & 7 deletions supriya/scsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import threading
from dataclasses import dataclass
from pathlib import Path
from typing import IO, Callable, Dict, Iterator, List, Optional, Set, Tuple, cast
from typing import IO, Callable, Dict, Iterator, List, Optional, Set, Tuple, Union, cast

import psutil
import uqbar.io
Expand Down Expand Up @@ -120,16 +120,20 @@ def get_sync_ids(self, client_id: int) -> Tuple[int, int]:

def serialize(self) -> List[str]:
result = [str(find(self.executable))]
pairs: Dict[str, Optional[str]] = {}
pairs: Dict[str, Optional[Union[List[str], str]]] = {}
if self.realtime:
if self.protocol == "tcp":
pairs["-t"] = str(self.port)
else:
pairs["-u"] = str(self.port)
if self.input_device:
pairs["-H"] = str(self.input_device)
if self.output_device != self.input_device:
result.append(str(self.output_device))
if self.input_device == self.output_device:
if self.input_device:
pairs["-H"] = str(self.input_device)
else:
pairs["-H"] = [
str(self.input_device or ""),
str(self.output_device or ""),
]
if self.maximum_logins != 64:
pairs["-l"] = str(self.maximum_logins)
if self.password:
Expand Down Expand Up @@ -179,7 +183,11 @@ def serialize(self) -> List[str]:
if self.threads and find(self.executable).stem == "supernova":
pairs["-t"] = str(self.threads)
for key, value in sorted(pairs.items()):
result.extend([key, value] if value is not None else [key])
result.append(key)
if isinstance(value, str):
result.append(value)
elif isinstance(value, list):
result.extend(value)
return result

### PUBLIC PROPERTIES ###
Expand Down
88 changes: 88 additions & 0 deletions tests/test_scsynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import stat
from tempfile import NamedTemporaryFile, TemporaryDirectory
from typing import Dict, List

import pytest

Expand Down Expand Up @@ -40,3 +41,90 @@ def test_find_on_path(mock_env_scsynth_path, monkeypatch):
got = scsynth.find()
expected = scsynth_path.resolve().absolute()
assert got == expected


@pytest.mark.parametrize(
"kwargs, expected",
[
(
{},
[
"/path/to/scsynth",
"-R",
"0",
"-l",
"1",
"-u",
"57110",
],
),
# only input device defined
(
{"input_device": "Device X"},
[
"/path/to/scsynth",
"-H",
"Device X",
"",
"-R",
"0",
"-l",
"1",
"-u",
"57110",
],
),
# only output device defined
(
{"output_device": "Device Y"},
[
"/path/to/scsynth",
"-H",
"",
"Device Y",
"-R",
"0",
"-l",
"1",
"-u",
"57110",
],
),
# input and output devices defined, but different
(
{"input_device": "Device P", "output_device": "Device Q"},
[
"/path/to/scsynth",
"-H",
"Device P",
"Device Q",
"-R",
"0",
"-l",
"1",
"-u",
"57110",
],
),
# input and output devices defined, and identical
(
{"input_device": "Device Z", "output_device": "Device Z"},
[
"/path/to/scsynth",
"-H",
"Device Z",
"-R",
"0",
"-l",
"1",
"-u",
"57110",
],
),
],
)
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
assert actual == expected

0 comments on commit a15bb46

Please sign in to comment.