[gnome-nibbles] Use GSound.



commit ac4f380570f3f38aa1863fb905cb82dbddc1dcdb
Author: Arnaud B <arnaud bonatti gmail com>
Date:   Thu Jul 25 20:23:03 2019 +0000

    Use GSound.
    
    Instead of Canberra.

 meson.build                    |  7 ++---
 org.gnome.Nibbles.json         | 10 +++++++
 src/meson.build                |  9 +++---
 src/nibbles-view.vala          | 64 ++++++++++++++++++++++++++++++++++--------
 src/vapi/libcanberra-gtk3.vapi | 37 ------------------------
 5 files changed, 70 insertions(+), 57 deletions(-)
---
diff --git a/meson.build b/meson.build
index 6d49218..2c63db3 100644
--- a/meson.build
+++ b/meson.build
@@ -17,14 +17,13 @@ podir = join_paths(meson.source_root(), 'po')
 # Dependencies
 glib_version = '2.40.0'
 
+clutter_dep = dependency('clutter-1.0', version: '>= 1.22.0')
+clutter_gtk_dep = dependency('clutter-gtk-1.0', version: '>= 1.4.0')
 gee_dep = dependency('gee-0.8')
 gio_dep = dependency('gio-2.0', version: '>= @0@'.format(glib_version))
 glib_dep = dependency('glib-2.0', version: '>= @0@'.format(glib_version))
+gsound_dep = dependency('gsound', version: '>= 1.0.2')
 gtk_dep = dependency('gtk+-3.0', version: '>= 3.18.0')
-clutter_dep = dependency('clutter-1.0', version: '>= 1.22.0')
-clutter_gtk_dep = dependency('clutter-gtk-1.0', version: '>= 1.4.0')
-canberra_dep = dependency('libcanberra', version: '>= 0.30')
-canberra_gtk3_dep = dependency('libcanberra-gtk3', version: '>= 0.30')
 libgnome_games_support_dep = dependency('libgnome-games-support-1')
 
 valac = meson.get_compiler('vala')
diff --git a/org.gnome.Nibbles.json b/org.gnome.Nibbles.json
index 63fbb52..4f81b5f 100644
--- a/org.gnome.Nibbles.json
+++ b/org.gnome.Nibbles.json
@@ -22,6 +22,16 @@
     "cleanup": ["/include", "/lib/*.la", "/lib/pkgconfig",
                 "/share/man", "/share/vala"],
     "modules": [
+        {
+            "name" : "gsound",
+            "buildsystem" : "autotools",
+            "sources" : [
+                {
+                    "type" : "git",
+                    "url" : "https://gitlab.gnome.org/GNOME/gsound.git";
+                }
+            ]
+        },
         {
             "name": "libgnome-games-support",
             "sources": [
diff --git a/src/meson.build b/src/meson.build
index 688fc58..3f1b248 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -13,16 +13,15 @@ gnome_nibbles_sources = [
 ]
 
 gnome_nibbles_deps = [
+    clutter_dep,
+    clutter_gtk_dep,
     gee_dep,
     gio_dep,
     glib_dep,
+    gsound_dep,
     gtk_dep,
-    clutter_dep,
-    clutter_gtk_dep,
-    canberra_dep,
-    canberra_gtk3_dep,
     libgnome_games_support_dep,
-    posix_dep,
+    posix_dep
 ]
 
 gnome_nibbles_vala_args = [
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 1d7df20..d76617e 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -110,9 +110,6 @@ private class WarpTexture: GtkClutter.Texture
 
 public class NibblesView : GtkClutter.Embed
 {
-    /* Sound */
-    public bool is_muted;
-
     /* Pixmaps */
     private Gdk.Pixbuf wall_pixmaps[11];
     private Gdk.Pixbuf worm_pixmaps[6];
@@ -894,19 +891,64 @@ public class NibblesView : GtkClutter.Embed
     * * Sound
     \*/
 
-    private void play_sound (string name)
+    internal bool is_muted { internal set; private get; default = true; }
+
+    private GSound.Context sound_context;
+    private SoundContextState sound_context_state = SoundContextState.INITIAL;
+
+    private enum SoundContextState
     {
-        if (is_muted)
-            return;
+        INITIAL,
+        WORKING,
+        ERRORED
+    }
+
+    private void init_sound ()
+     // requires (sound_context_state == SoundContextState.INITIAL)
+    {
+        try
+        {
+            sound_context = new GSound.Context ();
+            sound_context_state = SoundContextState.WORKING;
+        }
+        catch (Error e)
+        {
+            warning (e.message);
+            sound_context_state = SoundContextState.ERRORED;
+        }
+    }
 
-        var filename = @"$(name).ogg";
-        var path = Path.build_filename (SOUND_DIRECTORY, filename, null);
+    private void play_sound (string name)
+    {
+        if (!is_muted)
+        {
+            if (sound_context_state == SoundContextState.INITIAL)
+                init_sound ();
+            if (sound_context_state == SoundContextState.WORKING)
+                _play_sound (name, sound_context);
+        }
+    }
 
-        CanberraGtk.play_for_widget (this, 0,
-                                     Canberra.PROP_MEDIA_NAME, name,
-                                     Canberra.PROP_MEDIA_FILENAME, path);
+    private static void _play_sound (string _name, GSound.Context sound_context)
+     // requires (sound_context_state == SoundContextState.WORKING)
+    {
+        string name = _name + ".ogg";
+        string path = Path.build_filename (SOUND_DIRECTORY, name);
+        try
+        {
+            sound_context.play_simple (null, GSound.Attribute.MEDIA_NAME, name,
+                                             GSound.Attribute.MEDIA_FILENAME, path);
+        }
+        catch (Error e)
+        {
+            warning (e.message);
+        }
     }
 
+    /*\
+    * * Utility
+    \*/
+
     public static string colorval_name (int colorval)
     {
         return _(color_lookup[colorval]);


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