#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Fenrir TTY screen reader # By Chrys, Storm Dragon, and contributors. import shlex import subprocess from fenrirscreenreader.core import debug from fenrirscreenreader.core.soundDriver import sound_driver class driver(sound_driver): """Generic sound driver for Fenrir screen reader. This driver provides sound playback through external command-line tools like sox, aplay, or other audio utilities. It supports both sound file playback and frequency/tone generation. Features: - Configurable external command execution - Sound file playback (WAV, OGG, etc.) - Frequency/tone generation - Process management and cancellation Attributes: proc: Currently running subprocess for sound playback sound_file_command (str): Command template for playing sound files frequency_command (str): Command template for generating frequencies """ def __init__(self): sound_driver.__init__(self) self.proc = None self.sound_type = "" self.sound_file_command = "" self.frequency_command = "" def initialize(self, environment): """Initialize the generic sound driver. Loads command templates from configuration for sound file playback and frequency generation. Args: environment: Fenrir environment dictionary with settings """ self.env = environment self.sound_file_command = self.env["runtime"][ "SettingsManager" ].get_setting("sound", "generic_play_file_command") self.frequency_command = self.env["runtime"][ "SettingsManager" ].get_setting("sound", "generic_frequency_command") if self.sound_file_command == "": self.sound_file_command = ( "play -q -v fenrir_volume fenrir_sound_file" ) if self.frequency_command == "": self.frequency_command = ( "play -q -v fenrir_volume -n -c1 synth " "fenrir_duration sine fenrir_frequency" ) self._initialized = True def play_frequence( self, frequence, duration, adjust_volume=0.0, interrupt=True ): """Play a tone at the specified frequency. Args: frequence (float): Frequency in Hz duration (float): Duration in seconds adjust_volume (float): Volume adjustment interrupt (bool): Whether to interrupt current sound """ if not self._initialized: return if interrupt: self.cancel() popen_frequency_command = shlex.split(self.frequency_command) for idx, word in enumerate(popen_frequency_command): word = word.replace( "fenrir_volume", str(self.volume * adjust_volume) ) word = word.replace("fenrir_duration", str(duration)) word = word.replace("fenrir_frequency", str(frequence)) popen_frequency_command[idx] = word self.proc = subprocess.Popen( popen_frequency_command, stdin=None, stdout=None, stderr=None, shell=False, ) self.sound_type = "frequence" def play_sound_file(self, file_path, interrupt=True): """Play a sound file. Args: file_path (str): Path to the sound file to play interrupt (bool): Whether to interrupt current sound """ if not self._initialized: return if interrupt: self.cancel() # Validate file path to prevent injection import os if not os.path.isfile(file_path) or ".." in file_path: return popen_sound_file_command = shlex.split(self.sound_file_command) for idx, word in enumerate(popen_sound_file_command): word = word.replace("fenrir_volume", str(self.volume)) word = word.replace("fenrir_sound_file", str(file_path)) popen_sound_file_command[idx] = word self.proc = subprocess.Popen(popen_sound_file_command, shell=False) self.sound_type = "file" def cancel(self): """Cancel currently playing sound. Terminates the subprocess playing sound and cleans up resources. """ if not self._initialized: return if self.sound_type == "": return if self.sound_type == "file": self.proc.kill() try: # Wait for process to finish to prevent zombies self.proc.wait(timeout=1.0) except subprocess.TimeoutExpired: pass # Process already terminated except Exception as e: pass # Handle any other wait errors if self.sound_type == "frequence": self.proc.kill() try: # Wait for process to finish to prevent zombies self.proc.wait(timeout=1.0) except subprocess.TimeoutExpired: pass # Process already terminated except Exception as e: pass # Handle any other wait errors self.sound_type = ""