Debian Codegraph @

src/fenrirscreenreader/soundDriver/genericDriver.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import shlex
8 import subprocess
9
10 from fenrirscreenreader.core import debug
11 from fenrirscreenreader.core.soundDriver import sound_driver
12
13
14 class driver(sound_driver):
15 """Generic sound driver for Fenrir screen reader.
16
17 This driver provides sound playback through external command-line tools
18 like sox, aplay, or other audio utilities. It supports both sound file
19 playback and frequency/tone generation.
20
21 Features:
22 - Configurable external command execution
23 - Sound file playback (WAV, OGG, etc.)
24 - Frequency/tone generation
25 - Process management and cancellation
26
27 Attributes:
28 proc: Currently running subprocess for sound playback
29 sound_file_command (str): Command template for playing sound files
30 frequency_command (str): Command template for generating frequencies
31 """
32 def __init__(self):
33 sound_driver.__init__(self)
34 self.proc = None
35 self.sound_type = ""
36 self.sound_file_command = ""
37 self.frequency_command = ""
38
39 def initialize(self, environment):
40 """Initialize the generic sound driver.
41
42 Loads command templates from configuration for sound file playback
43 and frequency generation.
44
45 Args:
46 environment: Fenrir environment dictionary with settings
47 """
48 self.env = environment
49 self.sound_file_command = self.env["runtime"][
50 "SettingsManager"
51 ].get_setting("sound", "generic_play_file_command")
52 self.frequency_command = self.env["runtime"][
53 "SettingsManager"
54 ].get_setting("sound", "generic_frequency_command")
55 if self.sound_file_command == "":
56 self.sound_file_command = (
57 "play -q -v fenrir_volume fenrir_sound_file"
58 )
59 if self.frequency_command == "":
60 self.frequency_command = (
61 "play -q -v fenrir_volume -n -c1 synth "
62 "fenrir_duration sine fenrir_frequency"
63 )
64 self._initialized = True
65
66 def play_frequence(
67 self, frequence, duration, adjust_volume=0.0, interrupt=True
68 ):
69 """Play a tone at the specified frequency.
70
71 Args:
72 frequence (float): Frequency in Hz
73 duration (float): Duration in seconds
74 adjust_volume (float): Volume adjustment
75 interrupt (bool): Whether to interrupt current sound
76 """
77 if not self._initialized:
78 return
79 if interrupt:
80 self.cancel()
81 popen_frequency_command = shlex.split(self.frequency_command)
82 for idx, word in enumerate(popen_frequency_command):
83 word = word.replace(
84 "fenrir_volume", str(self.volume * adjust_volume)
85 )
86 word = word.replace("fenrir_duration", str(duration))
87 word = word.replace("fenrir_frequency", str(frequence))
88 popen_frequency_command[idx] = word
89 self.proc = subprocess.Popen(
90 popen_frequency_command,
91 stdin=None,
92 stdout=None,
93 stderr=None,
94 shell=False,
95 )
96 self.sound_type = "frequence"
97
98 def play_sound_file(self, file_path, interrupt=True):
99 """Play a sound file.
100
101 Args:
102 file_path (str): Path to the sound file to play
103 interrupt (bool): Whether to interrupt current sound
104 """
105 if not self._initialized:
106 return
107 if interrupt:
108 self.cancel()
109 # Validate file path to prevent injection
110 import os
111
112 if not os.path.isfile(file_path) or ".." in file_path:
113 return
114 popen_sound_file_command = shlex.split(self.sound_file_command)
115 for idx, word in enumerate(popen_sound_file_command):
116 word = word.replace("fenrir_volume", str(self.volume))
117 word = word.replace("fenrir_sound_file", str(file_path))
118 popen_sound_file_command[idx] = word
119 self.proc = subprocess.Popen(popen_sound_file_command, shell=False)
120 self.sound_type = "file"
121
122 def cancel(self):
123 """Cancel currently playing sound.
124
125 Terminates the subprocess playing sound and cleans up resources.
126 """
127 if not self._initialized:
128 return
129 if self.sound_type == "":
130 return
131 if self.sound_type == "file":
132 self.proc.kill()
133 try:
134 # Wait for process to finish to prevent zombies
135 self.proc.wait(timeout=1.0)
136 except subprocess.TimeoutExpired:
137 pass # Process already terminated
138 except Exception as e:
139 pass # Handle any other wait errors
140 if self.sound_type == "frequence":
141 self.proc.kill()
142 try:
143 # Wait for process to finish to prevent zombies
144 self.proc.wait(timeout=1.0)
145 except subprocess.TimeoutExpired:
146 pass # Process already terminated
147 except Exception as e:
148 pass # Handle any other wait errors
149 self.sound_type = ""