[gnome-taquin] Use GSound.



commit 9b000f65e381ac75f2c298a08bdef9463c86c47d
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jul 24 21:02:05 2019 +0200

    Use GSound.
    
    Instead of Canberra.

 build-aux/org.gnome.Taquin.json | 10 +++++
 meson.build                     | 10 +++--
 src/meson.build                 |  7 ++--
 src/taquin-main.vala            | 86 ++++++++++++++++++++++++++++++++++++-----
 4 files changed, 95 insertions(+), 18 deletions(-)
---
diff --git a/build-aux/org.gnome.Taquin.json b/build-aux/org.gnome.Taquin.json
index 63c3dbe..e43f524 100644
--- a/build-aux/org.gnome.Taquin.json
+++ b/build-aux/org.gnome.Taquin.json
@@ -19,6 +19,16 @@
     ],
     "cleanup": ["/share/man"],
     "modules": [
+        {
+            "name" : "gsound",
+            "buildsystem" : "autotools",
+            "sources" : [
+                {
+                    "type" : "git",
+                    "url" : "https://gitlab.gnome.org/GNOME/gsound.git";
+                }
+            ]
+        },
         {
             "name": "gnome-taquin",
             "buildsystem": "meson",
diff --git a/meson.build b/meson.build
index 1b5f59e..cc05102 100644
--- a/meson.build
+++ b/meson.build
@@ -3,8 +3,11 @@ project('gnome-taquin', 'vala', 'c',
 
 project_id = 'org.gnome.Taquin'
 
+# used to install help
 gnome = import('gnome')
+# used for internationalization
 i18n = import('i18n')
+# used to run post install script
 python3 = import('python3')
 
 cc = meson.get_compiler('c')
@@ -12,12 +15,11 @@ valac = meson.get_compiler('vala')
 
 gio_dependency = dependency('gio-2.0', version: '>= 2.40.0')
 glib_dependency = dependency('glib-2.0', version: '>= 2.40.0')
+gsound_dependency = dependency('gsound', version: '>= 1.0.2')
 gtk_dependency = dependency('gtk+-3.0', version: '>= 3.22.23')
-rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.32.0')
-canberra_dependency = dependency('libcanberra')
-canberra_gtk3_dependency = dependency('libcanberra-gtk3', version: '>= 0.26')
-posix_dependency = valac.find_library('posix')
 libm_dependency = cc.find_library('m', required: false) # some platforms do not have libm separated from libc
+posix_dependency = valac.find_library('posix')
+rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.32.0')
 
 desktop_file_validate = find_program('desktop-file-validate', required: false)
 appstream_util = find_program('appstream-util', required: false)
diff --git a/src/meson.build b/src/meson.build
index a8d911a..f5f2b20 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -44,11 +44,10 @@ executable(meson.project_name(),[
     ],
     dependencies: [
         glib_dependency,
+        gsound_dependency,
         gtk_dependency,
-        rsvg_dependency,
-        canberra_dependency,
-        canberra_gtk3_dependency,
+        libm_dependency,
         posix_dependency,
-        libm_dependency
+        rsvg_dependency
     ]
 )
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index 71823dd..494dea1 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -206,6 +206,9 @@ private class Taquin : Gtk.Application, BaseApplication
         // TODO window.add_action (settings.create_action ("size"));        // Problem: cannot use this way 
for an integer from a menu; works for radiobuttons in Iagno
         // TODO window.add_action (settings.create_action ("theme"));
 
+        if (settings.get_boolean ("sound"))
+            init_sound ();
+
         add_window (window);
         start_game ();
     }
@@ -282,7 +285,7 @@ private class Taquin : Gtk.Application, BaseApplication
         requires (game != null)
     {
         ((!) game).restart ();
-        play_sound ("sliding-n");
+        play_sound (Sound.SLIDING_N);
         move_done = false;
     }
 
@@ -290,7 +293,7 @@ private class Taquin : Gtk.Application, BaseApplication
         requires (game != null)
     {
         ((!) game).undo ();
-        play_sound ("sliding-1");
+        play_sound (Sound.SLIDING_1);
     }
 
     /*\
@@ -300,14 +303,14 @@ private class Taquin : Gtk.Application, BaseApplication
     private void move_cb (bool x_axis, int8 number, int8 x_gap, int8 y_gap, uint moves_count, bool 
disable_animation)
     {
         window.move_done (moves_count);
-        play_sound ("sliding-1");       // TODO sliding-n??
+        play_sound (Sound.SLIDING_1);   // TODO sliding-n??
         move_done = true;
     }
 
     private void game_complete_cb ()
     {
         window.finish_game ();
-        play_sound ("gameover");
+        play_sound (Sound.GAME_OVER);
     }
 
     private bool move_done = false;
@@ -389,14 +392,77 @@ private class Taquin : Gtk.Application, BaseApplication
     * * Sound
     \*/
 
-    private void play_sound (string name)
+    private GSound.Context sound_context;
+    private SoundContextState sound_context_state = SoundContextState.INITIAL;
+
+    private enum Sound
     {
-        if (!settings.get_boolean ("sound"))
-            return;
+        SLIDING_1,
+        SLIDING_N,
+        GAME_OVER;
+    }
+
+    private enum SoundContextState
+    {
+        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;
+        }
+    }
 
-        CanberraGtk.play_for_widget (view, 0,
-                                     Canberra.PROP_MEDIA_NAME, name,
-                                     Canberra.PROP_MEDIA_FILENAME, Path.build_filename (SOUND_DIRECTORY, 
"%s.ogg".printf (name)));
+    private void play_sound (Sound sound)
+    {
+        if (settings.get_boolean ("sound"))
+        {
+            if (sound_context_state == SoundContextState.INITIAL)
+                init_sound ();
+            if (sound_context_state == SoundContextState.WORKING)
+                _play_sound (sound, sound_context);
+        }
+    }
+
+    private static void _play_sound (Sound sound, GSound.Context sound_context)
+     // requires (sound_context_state == SoundContextState.WORKING)
+    {
+        string name;
+        switch (sound)
+        {
+            case Sound.SLIDING_1:
+                name = "sliding-1.ogg";
+                break;
+            case Sound.SLIDING_N:
+                name = "sliding-n.ogg";
+                break;
+            case Sound.GAME_OVER:
+                name = "gameover.ogg";
+                break;
+            default:
+                return;
+        }
+        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);
+        }
     }
 
     /*\


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