71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
"""
|
|
Volume control module.
|
|
Regulates system volume on a scale from 1 to 10.
|
|
"""
|
|
import subprocess
|
|
import re
|
|
|
|
NUMBER_MAP = {
|
|
"один": 1, "раз": 1, "два": 2, "три": 3, "четыре": 4,
|
|
"пять": 5, "шесть": 6, "семь": 7, "восемь": 8, "девять": 9, "десять": 10
|
|
}
|
|
|
|
|
|
def set_volume(level: int) -> bool:
|
|
"""
|
|
Set system volume (1-10 corresponding to 10%-100%).
|
|
|
|
Args:
|
|
level: Integer between 1 and 10
|
|
|
|
Returns:
|
|
True if successful, False otherwise
|
|
"""
|
|
if not isinstance(level, int):
|
|
print(f"❌ Ошибка: Уровень громкости должен быть целым числом, получено {type(level)}")
|
|
return False
|
|
|
|
if level < 1:
|
|
level = 1
|
|
elif level > 10:
|
|
level = 10
|
|
|
|
percentage = level * 10
|
|
|
|
try:
|
|
# Set volume using amixer
|
|
# -q: quiet
|
|
# sset: set simple control
|
|
# Master: control name
|
|
# %: percentage
|
|
cmd = ["amixer", "-q", "sset", "Master", f"{percentage}%"]
|
|
subprocess.run(cmd, check=True)
|
|
print(f"🔊 Громкость установлена на {level} ({percentage}%)")
|
|
return True
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ Ошибка при установке громкости: {e}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ Неизвестная ошибка громкости: {e}")
|
|
return False
|
|
|
|
|
|
def parse_volume_text(text: str) -> int | None:
|
|
"""
|
|
Parse volume level from text (digits or Russian words).
|
|
Returns integer 1-10 or None if not found.
|
|
"""
|
|
text = text.lower()
|
|
|
|
# 1. Check for digits
|
|
num_match = re.search(r'\b(10|[1-9])\b', text)
|
|
if num_match:
|
|
return int(num_match.group())
|
|
|
|
# 2. Check for words
|
|
for word, value in NUMBER_MAP.items():
|
|
if word in text:
|
|
return value
|
|
|
|
return None
|