[four-in-a-row] Use GSound.



commit 98dcf3e179793a266764685f4b70731fcb927881
Author: Arnaud B <arnaud bonatti gmail com>
Date:   Sun Aug 4 08:09:53 2019 +0000

    Use GSound.
    
    Instead of Canberra.

 build-aux/org.gnome.Four-in-a-row.json |  20 ++++-
 meson.build                            |   6 +-
 src/four-in-a-row.vala                 | 129 ++++++++++++++++++++-------------
 src/meson.build                        |  11 ++-
 src/vapi/libcanberra-gtk3.vapi         |  33 ---------
 5 files changed, 103 insertions(+), 96 deletions(-)
---
diff --git a/build-aux/org.gnome.Four-in-a-row.json b/build-aux/org.gnome.Four-in-a-row.json
index 181fa58..19b1031 100644
--- a/build-aux/org.gnome.Four-in-a-row.json
+++ b/build-aux/org.gnome.Four-in-a-row.json
@@ -26,13 +26,25 @@
         "/share/vala"
     ],
     "modules": [
+        {
+            "name" : "gsound",
+            "buildsystem" : "autotools",
+            "sources" : [
+                {
+                    "type" : "git",
+                    "url" : "https://gitlab.gnome.org/GNOME/gsound.git";
+                }
+            ]
+        },
         {
             "name": "four-in-a-row",
             "buildsystem": "meson",
-            "sources": [{
-                "type": "git",
-                "url": "https://gitlab.gnome.org/GNOME/four-in-a-row.git";
-            }]
+            "sources": [
+                {
+                    "type": "git",
+                    "url": "https://gitlab.gnome.org/GNOME/four-in-a-row.git";
+                }
+            ]
         }
     ]
 }
diff --git a/meson.build b/meson.build
index 3aeeb2c..f640b04 100644
--- a/meson.build
+++ b/meson.build
@@ -4,18 +4,20 @@ project('four-in-a-row', 'c', 'vala',
 
 add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format (meson.project_name()), language: 'c')
 
+# used to install help
 gnome = import('gnome')
+# used for internationalization
 i18n = import('i18n')
+# used to run post install script
 python3 = import('python3')
 
 application_id = 'org.gnome.Four-in-a-row'
 
 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.13.2')
 rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.32.0')
-canberra_dependency = dependency('libcanberra')
-canberra_gtk3_dependency = dependency('libcanberra-gtk3', version: '>= 0.26')
 
 datadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())
 
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 8dfa783..427eb82 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -43,7 +43,7 @@ class FourInARow : Gtk.Application {
     Board game_board;
     Gtk.ApplicationWindow window;
     /**
-     * socre:
+     * score:
      *
      * The scores for the current instance (Player 1, Player 2, Draw)
      */
@@ -294,47 +294,6 @@ class FourInARow : Gtk.Application {
         process_move(c);
     }
 
-    public void play_sound(SoundID id) {
-        string name;
-
-        if (!Prefs.instance.do_sound)
-            return;
-
-        switch (id) {
-        case SoundID.DROP:
-            name = "slide";
-            break;
-        case SoundID.I_WIN:
-            name = "reverse";
-            break;
-        case SoundID.YOU_WIN:
-            name = "bonus";
-            break;
-        case SoundID.PLAYER_WIN:
-            name = "bonus";
-            break;
-        case SoundID.DRAWN_GAME:
-            name = "reverse";
-            break;
-        case SoundID.COLUMN_FULL:
-            name = "bad";
-            break;
-        default:
-            return;
-        }
-
-        string filename, path;
-
-        filename = name + ".ogg";
-        path = Path.build_filename(SOUND_DIRECTORY, filename);
-
-        CanberraGtk.context_get().play(
-                id,
-                Canberra.PROP_MEDIA_NAME, name,
-                Canberra.PROP_MEDIA_FILENAME, path);
-
-    }
-
     public void process_move3(int c) {
         play_sound(SoundID.DROP);
 
@@ -792,6 +751,83 @@ class FourInARow : Gtk.Application {
         prefsbox = new PrefsBox(window);
         prefsbox.show_all();
     }
+
+    /* Sound-related methods */
+
+    private GSound.Context sound_context;
+    private SoundContextState sound_context_state = SoundContextState.INITIAL;
+
+    private enum SoundID {
+        DROP,
+        I_WIN,
+        YOU_WIN,
+        PLAYER_WIN,
+        DRAWN_GAME,
+        COLUMN_FULL
+    }
+
+    private enum SoundContextState
+    {
+        INITIAL,
+        WORKING,
+        ERRORED
+    }
+
+    private void init_sound() {
+        try {
+            sound_context = new GSound.Context();
+            sound_context_state = SoundContextState.WORKING;
+        } catch (Error e) {
+            warning(e.message);
+            sound_context_state = SoundContextState.ERRORED;
+        }
+    }
+
+    private void play_sound(SoundID id) {
+        if (Prefs.instance.do_sound) {
+            if (sound_context_state == SoundContextState.INITIAL)
+                init_sound();
+            if (sound_context_state == SoundContextState.WORKING)
+                do_play_sound(id, sound_context);
+        }
+    }
+
+    private static void do_play_sound(SoundID id, GSound.Context sound_context) {
+        string name, path;
+
+        switch(id) {
+        case SoundID.DROP:
+            name = "slide";
+            break;
+        case SoundID.I_WIN:
+            name = "reverse";
+            break;
+        case SoundID.YOU_WIN:
+            name = "bonus";
+            break;
+        case SoundID.PLAYER_WIN:
+            name = "bonus";
+            break;
+        case SoundID.DRAWN_GAME:
+            name = "reverse";
+            break;
+        case SoundID.COLUMN_FULL:
+            name = "bad";
+            break;
+        default:
+            return;
+        }
+
+        name += ".ogg";
+        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);
+        }
+    }
 }
 
 public enum AnimID {
@@ -824,15 +860,6 @@ public enum Tile {
     PLAYER2_CURSOR,
 }
 
-public enum SoundID {
-    DROP,
-    I_WIN,
-    YOU_WIN,
-    PLAYER_WIN,
-    DRAWN_GAME,
-    COLUMN_FULL
-}
-
 public int main(string[] argv) {
     Intl.setlocale();
 
diff --git a/src/meson.build b/src/meson.build
index 600f2e5..e282438 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -15,7 +15,6 @@ test('four-in-a-row-tests',
 
 
 sources = files(
-    'vapi/config.vapi',
     'ai.vala',
     'four-in-a-row.vala',
     'game-board-view.vala',
@@ -24,18 +23,18 @@ sources = files(
     'prefs-box.vala',
     'prefs.vala',
     'scorebox.vala',
-    'theme.vala'
+    'theme.vala',
+    'vapi/config.vapi'
 )
 
 executable(
     meson.project_name(),
     sources,
     dependencies: [
+        glib_dependency,
+        gsound_dependency,
         gtk_dependency,
-        rsvg_dependency,
-        canberra_dependency,
-        canberra_gtk3_dependency,
-        glib_dependency
+        rsvg_dependency
     ],
     c_args: [
         '-include', 'config.h'


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