| 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 threading |
| 8 |
import time |
| 9 |
|
| 10 |
from fenrirscreenreader.core import debug |
| 11 |
from fenrirscreenreader.core.soundDriver import sound_driver |
| 12 |
|
| 13 |
_gstreamerAvailable = False |
| 14 |
try: |
| 15 |
import gi |
| 16 |
from gi.repository import GLib |
| 17 |
|
| 18 |
gi.require_version("Gst", "1.0") |
| 19 |
from gi.repository import Gst |
| 20 |
|
| 21 |
_gstreamerAvailable, args = Gst.init_check(None) |
| 22 |
except Exception as e: |
| 23 |
_gstreamerAvailable = False |
| 24 |
_availableError = str(e) |
| 25 |
|
| 26 |
|
| 27 |
class driver(sound_driver): |
| 28 |
def __init__(self): |
| 29 |
sound_driver.__init__(self) |
| 30 |
self._source = None |
| 31 |
self._sink = None |
| 32 |
|
| 33 |
def initialize(self, environment): |
| 34 |
self.env = environment |
| 35 |
global _gstreamerAvailable |
| 36 |
self._initialized = _gstreamerAvailable |
| 37 |
if not self._initialized: |
| 38 |
global _availableError |
| 39 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 40 |
"Gstreamer not available " + _availableError, |
| 41 |
debug.DebugLevel.ERROR, |
| 42 |
) |
| 43 |
return |
| 44 |
self._player = Gst.ElementFactory.make("playbin", "player") |
| 45 |
bus = self._player.get_bus() |
| 46 |
bus.add_signal_watch() |
| 47 |
bus.connect("message", self._on_player_message) |
| 48 |
|
| 49 |
self._pipeline = Gst.Pipeline(name="fenrir-pipeline") |
| 50 |
bus = self._pipeline.get_bus() |
| 51 |
bus.add_signal_watch() |
| 52 |
bus.connect("message", self._on_pipeline_message) |
| 53 |
|
| 54 |
self._source = Gst.ElementFactory.make("audiotestsrc", "src") |
| 55 |
self._volume = Gst.ElementFactory.make("volume", "volume") |
| 56 |
self._sink = Gst.ElementFactory.make("autoaudiosink", "output") |
| 57 |
self._pipeline.add(self._source) |
| 58 |
self._pipeline.add(self._volume) |
| 59 |
self._pipeline.add(self._sink) |
| 60 |
self._source.link(self._volume) |
| 61 |
self._volume.link(self._sink) |
| 62 |
self.mainloop = GLib.MainLoop() |
| 63 |
self.thread = threading.Thread(target=self.mainloop.run) |
| 64 |
self.thread.start() |
| 65 |
|
| 66 |
def shutdown(self): |
| 67 |
if not self._initialized: |
| 68 |
return |
| 69 |
self.cancel() |
| 70 |
self.mainloop.quit() |
| 71 |
# Wait for the GLib MainLoop thread to finish to prevent shutdown races |
| 72 |
if hasattr(self, "thread") and self.thread.is_alive(): |
| 73 |
# 2 second timeout to prevent hanging |
| 74 |
self.thread.join(timeout=2.0) |
| 75 |
|
| 76 |
def _on_player_message(self, bus, message): |
| 77 |
if not self._initialized: |
| 78 |
return |
| 79 |
if message.type == Gst.MessageType.EOS: |
| 80 |
self._player.set_state(Gst.State.NULL) |
| 81 |
elif message.type == Gst.MessageType.ERROR: |
| 82 |
self._player.set_state(Gst.State.NULL) |
| 83 |
error, info = message.parse_error() |
| 84 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 85 |
"GSTREAMER: _on_player_message" + str(error) + str(info), |
| 86 |
debug.DebugLevel.WARNING, |
| 87 |
) |
| 88 |
|
| 89 |
def _on_pipeline_message(self, bus, message): |
| 90 |
if not self._initialized: |
| 91 |
return |
| 92 |
if message.type == Gst.MessageType.EOS: |
| 93 |
self._pipeline.set_state(Gst.State.NULL) |
| 94 |
elif message.type == Gst.MessageType.ERROR: |
| 95 |
self._pipeline.set_state(Gst.State.NULL) |
| 96 |
error, info = message.parse_error() |
| 97 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 98 |
"GSTREAMER: _on_pipeline_message" + str(error) + str(info), |
| 99 |
debug.DebugLevel.WARNING, |
| 100 |
) |
| 101 |
|
| 102 |
def _on_timeout(self, element): |
| 103 |
if not self._initialized: |
| 104 |
return |
| 105 |
element.set_state(Gst.State.NULL) |
| 106 |
|
| 107 |
def play_sound_file(self, file_name, interrupt=True): |
| 108 |
if not self._initialized: |
| 109 |
return |
| 110 |
if interrupt: |
| 111 |
self.cancel() |
| 112 |
self._player.set_property("volume", self.volume) |
| 113 |
self._player.set_property("uri", "file://%s" % file_name) |
| 114 |
self._player.set_state(Gst.State.PLAYING) |
| 115 |
|
| 116 |
def play_frequence( |
| 117 |
self, frequence, duration, adjust_volume=0.0, interrupt=True |
| 118 |
): |
| 119 |
if not self._initialized: |
| 120 |
return |
| 121 |
if interrupt: |
| 122 |
self.cancel() |
| 123 |
# Always reset pipeline to prevent volume accumulation |
| 124 |
self._pipeline.set_state(Gst.State.NULL) |
| 125 |
duration = duration * 1000 |
| 126 |
# Use dedicated volume element for better control |
| 127 |
# GStreamer volume property behaves very differently than sox for low frequencies |
| 128 |
if adjust_volume > 0.8: # This indicates low frequency indentation beeps |
| 129 |
# Extremely aggressive boost - GStreamer really struggles with low frequencies |
| 130 |
effective_volume = self.volume * adjust_volume * 50.0 # Ridiculous multiplier to match sox |
| 131 |
else: |
| 132 |
effective_volume = self.volume * adjust_volume * 3.0 |
| 133 |
self._volume.set_property("volume", effective_volume) |
| 134 |
self._source.set_property("volume", 1.0) # Set source to full, control via volume element |
| 135 |
self._source.set_property("freq", frequence) |
| 136 |
self._pipeline.set_state(Gst.State.PLAYING) |
| 137 |
GLib.timeout_add(duration, self._on_timeout, self._pipeline) |
| 138 |
|
| 139 |
def cancel(self, element=None): |
| 140 |
if not self._initialized: |
| 141 |
return |
| 142 |
if element: |
| 143 |
element.set_state(Gst.State.NULL) |
| 144 |
return |
| 145 |
self._player.set_state(Gst.State.NULL) |
| 146 |
self._pipeline.set_state(Gst.State.NULL) |