From 0172e47a797fd37bc197c3fc1ef0824219035999 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 9 May 2025 11:14:03 -0400 Subject: [PATCH] python: Add Gst.Float wrapper When the float python type is used inside a GstValueArray, it is converted to a G_TYPE_DOUBLE GValue. Sometimes it is important to be able to force G_TYPE_FLOAT GValue, for instance to set the the "mix-matrix" property on audioconvert. Part-of: --- subprojects/gst-python/gi/overrides/Gst.py | 11 +++++++++ .../gst-python/testsuite/test_types.py | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/subprojects/gst-python/gi/overrides/Gst.py b/subprojects/gst-python/gi/overrides/Gst.py index e571e30d35..ebe96db59b 100644 --- a/subprojects/gst-python/gi/overrides/Gst.py +++ b/subprojects/gst-python/gi/overrides/Gst.py @@ -48,6 +48,17 @@ python module to use with Gst 0.10" warnings.warn(warn_msg, RuntimeWarning) +class Float(float): + ''' + A wrapper to force conversion to G_TYPE_FLOAT instead of G_TYPE_DOUBLE when + used in e.g. Gst.ValueArray. + ''' + __gtype__ = GObject.TYPE_FLOAT + + +__all__.append('Float') + + # Ensuring that PyGObject loads the URIHandler interface # so we can force our own implementation soon enough (in gstmodule.c) class URIHandler(Gst.URIHandler): diff --git a/subprojects/gst-python/testsuite/test_types.py b/subprojects/gst-python/testsuite/test_types.py index a5be3ec004..309fe3a549 100644 --- a/subprojects/gst-python/testsuite/test_types.py +++ b/subprojects/gst-python/testsuite/test_types.py @@ -436,3 +436,26 @@ class TestBitmask(TestCase): self.assertEqual(str(r), '0x20') else: self.assertEqual(str(r), '0x20L') + + +class TestFloat(TestCase): + def testSetProperty(self): + ''' Test we can set the mix-matrix property on audioconvert + + It requires the Gst.Float class to force the conversion of python float + to G_TYPE_FLOAT instead of G_TYPE_DOUBLE. + ''' + Gst.init(None) + + audioconvert = Gst.ElementFactory.make("audioconvert") + if not audioconvert: + self.skipTest("audioconvert not available") + + row = Gst.ValueArray([Gst.Float(0.0)]) + matrix = Gst.ValueArray([row]) + audioconvert.set_property("mix-matrix", matrix) + + value = audioconvert.get_property("mix-matrix") + self.assertEqual(len(value), 1) + self.assertEqual(len(value[0]), 1) + self.assertEqual(value[0][0], 0.0)