summaryrefslogtreecommitdiff
path: root/include/py/homekit/audio
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-02-17 03:08:25 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-02-17 03:08:25 +0300
commit0ce2e41a2bad790c5232fafb4b6ed631ca8cd957 (patch)
treefd401495b87cae8c95a4c4edf2c851c8177b6069 /include/py/homekit/audio
parente9fc2c1835f7ac8e072919df81a6661c6308dea9 (diff)
parentb7f1d55c9b4de4d21b11e5615a5dc8be0d4e883c (diff)
merge with master
Diffstat (limited to 'include/py/homekit/audio')
-rw-r--r--include/py/homekit/audio/__init__.py0
-rw-r--r--include/py/homekit/audio/amixer.py91
2 files changed, 91 insertions, 0 deletions
diff --git a/include/py/homekit/audio/__init__.py b/include/py/homekit/audio/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/include/py/homekit/audio/__init__.py
diff --git a/include/py/homekit/audio/amixer.py b/include/py/homekit/audio/amixer.py
new file mode 100644
index 0000000..827e102
--- /dev/null
+++ b/include/py/homekit/audio/amixer.py
@@ -0,0 +1,91 @@
+import subprocess
+
+from ..config import app_config as config
+from threading import Lock
+from typing import Union, List
+
+
+_lock = Lock()
+_default_step = 5
+
+
+def has_control(s: str) -> bool:
+ for control in config.app_config['amixer']['controls']:
+ if control['name'] == s:
+ return True
+ return False
+
+
+def get_caps(s: str) -> List[str]:
+ for control in config.app_config['amixer']['controls']:
+ if control['name'] == s:
+ return control['caps']
+ raise KeyError(f'control {s} not found')
+
+
+def get_all() -> list:
+ controls = []
+ for control in config.app_config['amixer']['controls']:
+ controls.append({
+ 'name': control['name'],
+ 'info': get(control['name']),
+ 'caps': control['caps']
+ })
+ return controls
+
+
+def get(control: str):
+ return call('get', control)
+
+
+def mute(control):
+ return call('set', control, 'mute')
+
+
+def unmute(control):
+ return call('set', control, 'unmute')
+
+
+def cap(control):
+ return call('set', control, 'cap')
+
+
+def nocap(control):
+ return call('set', control, 'nocap')
+
+
+def _get_default_step() -> int:
+ if 'step' in config.app_config['amixer']:
+ return int(config.app_config['amixer']['step'])
+
+ return _default_step
+
+
+def incr(control, step=None):
+ if step is None:
+ step = _get_default_step()
+ return call('set', control, f'{step}%+')
+
+
+def decr(control, step=None):
+ if step is None:
+ step = _get_default_step()
+ return call('set', control, f'{step}%-')
+
+
+def call(*args, return_code=False) -> Union[int, str]:
+ with _lock:
+ result = subprocess.run([config.app_config['amixer']['bin'], *args],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ if return_code:
+ return result.returncode
+
+ if result.returncode != 0:
+ raise AmixerError(result.stderr.decode().strip())
+
+ return result.stdout.decode().strip()
+
+
+class AmixerError(OSError):
+ pass