# Copyright 2023 (c) Anna Schumaker. """A custom Gst.Bin with our audio filter effects.""" from gi.repository import GObject from gi.repository import Gst from . import replaygain class Filter(Gst.Bin): """The audio filter element.""" bg_enabled = GObject.Property(type=bool, default=False) bg_volume = GObject.Property(type=float, default=0.5) rg_mode = GObject.Property(type=str, default="disabled") def __init__(self): """Initialize the audio filter.""" super().__init__() self._replaygain = replaygain.Filter() self._volume = Gst.ElementFactory.make("volume") self.add(self._replaygain) self.add(self._volume) rg_pad = self._replaygain.get_static_pad("src") rg_pad.link(self._volume.get_static_pad("sink")) self.__add_ghost_pad("sink", self._replaygain) self.__add_ghost_pad("src", self._volume) self.connect("notify", self.__notify) def __add_ghost_pad(self, pad: str, elm: Gst.Element) -> None: self.add_pad(Gst.GhostPad.new(pad, elm.get_static_pad(pad))) def __notify(self, filter: Gst.Bin, param: GObject.ParamSpec) -> None: match param.name: case "bg-enabled" | "bg-volume": vol = self.bg_volume if self.bg_enabled else 1.0 if vol != self._volume.get_property("volume"): vs = f"{round(vol * 100)}%" if self.bg_enabled else "off" print(f"audio: setting background listening to {vs}") self._volume.set_property("volume", vol) case "rg-mode": if self.rg_mode != self._replaygain.mode: self._replaygain.mode = self.rg_mode