[gnome-music/wip/mschraal/tageditor-13feb2020: 1/12] grilowrappers: Introduce support for chromaprint source



commit 2adb12f167b032f1c23f6ce63a838c637039dfb2
Author: Sumaid Syed <sumaidsyed gmail com>
Date:   Sat Nov 23 13:50:29 2019 +0100

    grilowrappers: Introduce support for chromaprint source
    
    The chromaprint is an acoustic fingerprint of a song. Combined with
    the acoustid source, it enables song authentication.
    
    Based on patches by Jean Felder and Marinus Schraal.

 gnomemusic/coregrilo.py                           |   9 +-
 gnomemusic/grilowrappers/grlchromaprintwrapper.py | 108 ++++++++++++++++++++++
 2 files changed, 116 insertions(+), 1 deletion(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index 9c2687ea..f8ff42c0 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -26,6 +26,8 @@ import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import Grl, GLib, GObject
 
+from gnomemusic.grilowrappers.grlchromaprintwrapper import (
+    GrlChromaprintWrapper)
 from gnomemusic.grilowrappers.grlsearchwrapper import GrlSearchWrapper
 from gnomemusic.grilowrappers.grltrackerwrapper import GrlTrackerWrapper
 from gnomemusic.trackerwrapper import TrackerState, TrackerWrapper
@@ -63,10 +65,12 @@ class CoreGrilo(GObject.GObject):
         self._coremodel = coremodel
         self._coreselection = application.props.coreselection
         self._log = application.props.log
-        self._search_wrappers = {}
         self._thumbnail_sources = []
         self._thumbnail_sources_timeout = None
+
         self._wrappers = {}
+        self._mb_wrappers = {}
+        self._search_wrappers = {}
 
         self._tracker_wrapper = TrackerWrapper()
         self._tracker_wrapper.bind_property(
@@ -144,6 +148,9 @@ class CoreGrilo(GObject.GObject):
             self._search_wrappers[source.props.source_id] = GrlSearchWrapper(
                 source, self._coremodel, self._application, self)
             self._log.debug("Adding search source {}".format(source))
+        elif source.props.source_id == "grl-chromaprint":
+            self._mb_wrappers[source.props.source_id] = GrlChromaprintWrapper(
+                source, self)
 
     def _on_source_removed(self, registry, source):
         # FIXME: Handle removing sources.
diff --git a/gnomemusic/grilowrappers/grlchromaprintwrapper.py 
b/gnomemusic/grilowrappers/grlchromaprintwrapper.py
new file mode 100644
index 00000000..ed135191
--- /dev/null
+++ b/gnomemusic/grilowrappers/grlchromaprintwrapper.py
@@ -0,0 +1,108 @@
+# Copyright 2020 The GNOME Music developers
+#
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so.  If you do not wish to do so,
+# delete this exception statement from your version.
+
+import gi
+gi.require_version("Grl", "0.3")
+from gi.repository import GObject, Grl
+
+from gnomemusic.musiclogger import MusicLogger
+
+
+class GrlChromaprintWrapper(GObject.GObject):
+    """Wrapper for the Grilo Chromaprint source.
+    """
+
+    def __init__(self, source, grilo):
+        """Initialize the Chromaprint wrapper
+
+        :param Grl.TrackerSource source: The Tracker source to wrap
+        :param CoreGrilo grilo: The CoreGrilo instance
+        """
+        super().__init__()
+
+        self._source = source
+        self._grilo = grilo
+        self._log = MusicLogger()
+
+        registry = self._grilo.props.registry
+        self._fingerprint_key = registry.lookup_metadata_key("chromaprint")
+
+        self._METADATA_KEYS = [
+            self._fingerprint_key,
+            Grl.METADATA_KEY_DURATION
+        ]
+
+        if self.props.enabled is False:
+            self._log.warning(
+                "Error: chromaprint GStreamer plugin is missing.")
+
+    def get_chromaprint(self, coresong, callback):
+        """Get the chromaprint of a coresong.
+
+        Compute the chromaprint of a song a call
+        a callback fucntion with a Grl.Media which contains the
+        chromaprint.
+
+        :param CoreSong coresong: song to analyze
+        :param function callback:  callback function
+        """
+
+        if self.props.enabled is False:
+            callback(None)
+            return
+
+        chromaprint = coresong.props.media.get_string(self._fingerprint_key)
+        if chromaprint is not None:
+            callback(coresong.props.media)
+            return
+
+        options = Grl.OperationOptions()
+        options.set_resolution_flags(
+            Grl.ResolutionFlags.FAST_ONLY | Grl.ResolutionFlags.IDLE_RELAY)
+
+        def _chromaprint_resolved(source, op_id, media, callback, error):
+            if error:
+                self._log.warning(
+                    "Error {}: {}".format(error.domain, error.message))
+                callback(None)
+                return
+
+            callback(media)
+
+        self._source.resolve(
+            coresong.props.media, self._METADATA_KEYS, options,
+            _chromaprint_resolved, callback)
+
+    @GObject.Property(
+        type=bool, default=False, flags=GObject.ParamFlags.READABLE)
+    def enabled(self):
+        """Get Chromaprint source state.
+
+        A chromaprint can be computed if gstreamer chromaprint
+        support is enabled.
+
+        :returns: chromaprint source state
+        :rtype: bool
+        """
+        return self._fingerprint_key != Grl.METADATA_KEY_INVALID


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]