audio: Set a ReplayGainSink as the playbin's audio sink

And create a rplaygain property that acts as a passthrough for
controlling the element.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-09-07 20:15:43 -04:00
parent 6792971ef7
commit 4f4e9efa28
2 changed files with 42 additions and 1 deletions

View File

@ -9,17 +9,25 @@ from gi.repository import GLib
from gi.repository import Gst
Gst.init(sys.argv)
from . import replaygain
class BassPlayer(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
lib.settings.initialize("audio.replaygain", "disabled")
lib.settings.initialize("audio.volume", 1.0)
self.video = Gst.ElementFactory.make("fakesink")
self.audio = replaygain.ReplayGainSink()
self.video = Gst.ElementFactory.make("fakesink")
self.playbin = Gst.ElementFactory.make("playbin")
self.playbin.connect("about-to-finish", self.__about_to_finish__)
self.playbin.set_property("audio-sink", self.audio)
self.playbin.set_property("video-sink", self.video)
self.playbin.set_property("volume", lib.settings.get_float("audio.volume"))
self.playbin.set_state(Gst.State.READY)
self.set_property("replaygain", lib.settings.get("audio.replaygain"))
self.bus.add_signal_watch()
self.bus.connect("message::eos", self.__eos__)
@ -73,6 +81,15 @@ class BassPlayer(GObject.GObject):
Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
pos)
@GObject.Property
def replaygain(self):
return self.audio.get_property("mode")
@replaygain.setter
def replaygain(self, mode):
lib.settings.set("audio.replaygain", mode)
self.audio.set_property("mode", mode)
@GObject.Property
def uri(self):
return self.playbin.get_property("uri")

View File

@ -7,6 +7,7 @@ from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gst
from . import bass
from . import replaygain
main_context = GLib.main_context_default()
test_album = pathlib.Path("./data/Test Album/")
@ -24,8 +25,10 @@ class TestBassPlayer(unittest.TestCase):
def test_bass_player_init(self):
base = bass.BassPlayer()
self.assertIsInstance(base, GObject.GObject)
self.assertIsInstance(base.audio, replaygain.ReplayGainSink)
self.assertIsInstance(base.video, Gst.Element)
self.assertIsInstance(base.playbin, Gst.Element)
self.assertEqual(base.playbin.get_property("audio-sink"), base.audio)
self.assertEqual(base.playbin.get_property("video-sink"), base.video)
self.assertEqual(base.playbin.get_state(Gst.CLOCK_TIME_NONE)[1],
Gst.State.READY)
@ -87,6 +90,27 @@ class TestBassPlayer(unittest.TestCase):
while main_context.iteration(may_block=False): time.sleep(0.005)
self.assertGreater(base.get_property("position"), 0)
def test_bass_player_replaygain(self):
lib.settings.reset()
base = bass.BassPlayer()
self.assertEqual(lib.settings.get("audio.replaygain"), "disabled")
self.assertEqual(base.get_property("replaygain"), "disabled")
base.set_property("replaygain", "track")
self.assertEqual(base.audio.get_property("mode"), "track")
self.assertEqual(base.get_property("replaygain"), "track")
self.assertEqual(lib.settings.get("audio.replaygain"), "track")
base.set_property("replaygain", "disabled")
self.assertEqual(base.audio.get_property("mode"), "disabled")
self.assertEqual(base.get_property("replaygain"), "disabled")
self.assertEqual(lib.settings.get("audio.replaygain"), "disabled")
base.set_property("replaygain", "album")
self.assertEqual(base.audio.get_property("mode"), "album")
self.assertEqual(base.get_property("replaygain"), "album")
self.assertEqual(lib.settings.get("audio.replaygain"), "album")
def test_bass_player_uri(self):
base = bass.BassPlayer()