[four-in-a-row/arnaudb/wip/gtk4: 41/42] Use Gtk for sounds.



commit 2b3fcbbe4396efe80cb08ec67b9749b8bfa4f458
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Apr 29 18:52:34 2020 +0200

    Use Gtk for sounds.

 data/meson.build                |  9 -----
 meson.build                     |  3 --
 src/four-in-a-row.gresource.xml |  6 ++++
 src/four-in-a-row.vala          | 79 +++++++++++------------------------------
 src/meson.build                 |  1 -
 src/vapi/config.vapi            |  1 -
 6 files changed, 27 insertions(+), 72 deletions(-)
---
diff --git a/data/meson.build b/data/meson.build
index 0038eee..72017e3 100644
--- a/data/meson.build
+++ b/data/meson.build
@@ -61,13 +61,4 @@ install_data(
   install_dir : join_paths(get_option('datadir'), 'glib-2.0', 'schemas')
 )
 
-install_data(
-    [
-        'sounds/bad.ogg',
-        'sounds/bonus.ogg',
-        'sounds/reverse.ogg',
-        'sounds/slide.ogg'
-    ],
-    install_dir: soundsdir
-)
 subdir('icons')
diff --git a/meson.build b/meson.build
index 91c18d6..689f114 100644
--- a/meson.build
+++ b/meson.build
@@ -19,7 +19,6 @@ 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('gtk4',        version: '>= 3.98.2')
 posix_dependency    = valac.find_library('posix')
 rsvg_dependency     = dependency('librsvg-2.0', version: '>= 2.32.0')
@@ -32,12 +31,10 @@ localedir   = join_paths(get_option('prefix'), get_option('localedir'))
 datadir     = join_paths(get_option('prefix'), get_option('datadir'))
 bindir      = join_paths(get_option('prefix'), get_option('bindir'))
 pkgdatadir  = join_paths(datadir, meson.project_name())
-soundsdir   = join_paths(pkgdatadir, 'sounds')
 
 conf = configuration_data ()
 conf.set_quoted('VERSION', meson.project_version())
 conf.set_quoted('GETTEXT_PACKAGE', meson.project_name())
-conf.set_quoted('SOUND_DIRECTORY', soundsdir)
 conf.set_quoted('LOCALEDIR', localedir)
 conf.set_quoted('DATA_DIRECTORY', pkgdatadir)
 
diff --git a/src/four-in-a-row.gresource.xml b/src/four-in-a-row.gresource.xml
index 7f9b5f5..12d61bd 100644
--- a/src/four-in-a-row.gresource.xml
+++ b/src/four-in-a-row.gresource.xml
@@ -8,6 +8,12 @@
     <file preprocess="xml-stripblanks" compressed="true" 
alias="tileset_50x50_hcontrast.svg">../data/themes/tileset_50x50_hcontrast.svg</file>
     <file alias="bg_toplight.png">../data/bg_toplight.png</file>
   </gresource>
+  <gresource prefix="/org/gnome/Four-in-a-row/sounds">
+    <file alias="bad.ogg"       >../data/sounds/bad.ogg</file>
+    <file alias="bonus.ogg"     >../data/sounds/bonus.ogg</file>
+    <file alias="reverse.ogg"   >../data/sounds/reverse.ogg</file>
+    <file alias="slide.ogg"     >../data/sounds/slide.ogg</file>
+  </gresource>
   <gresource prefix="/org/gnome/Four-in-a-row/ui">
     <file alias="four-in-a-row.css">../data/four-in-a-row.css</file>
     <file preprocess="xml-stripblanks" compressed="true" 
alias="adaptative-window.ui">../data/ui/adaptative-window.ui</file>
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index bb287b5..8929113 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -236,9 +236,6 @@ private class FourInARow : Gtk.Application
         game_board = new Board ((uint8) size, (uint8) target);
         clear_board ();
 
-        if (settings.get_boolean ("sound"))
-            init_sound ();
-
         settings.bind ("key-drop",  this,   "keypress-drop",  SettingsBindFlags.GET | 
SettingsBindFlags.NO_SENSITIVITY);
         settings.bind ("key-right", this,   "keypress-right", SettingsBindFlags.GET | 
SettingsBindFlags.NO_SENSITIVITY);
         settings.bind ("key-left",  this,   "keypress-left",  SettingsBindFlags.GET | 
SettingsBindFlags.NO_SENSITIVITY);
@@ -628,7 +625,7 @@ private class FourInARow : Gtk.Application
 
     private void process_move3 (uint8 c)
     {
-        play_sound (SoundID.DROP);
+        play_sound (Sound.DROP);
 
         vstr += (c + 1).to_string ();
         moves++;
@@ -666,16 +663,16 @@ private class FourInARow : Gtk.Application
             set_gameover (true);
             winner = player;
             if (one_player_game)
-                play_sound (is_player_human () ? SoundID.YOU_WIN : SoundID.I_WIN);
+                play_sound (is_player_human () ? Sound.YOU_WIN : Sound.I_WIN);
             else
-                play_sound (SoundID.PLAYER_WIN);
+                play_sound (Sound.PLAYER_WIN);
             window.allow_hint (false);
         }
         else if (moves == /* BOARD_ROWS */ (size - 1) * /* BOARD_COLUMNS */ size)
         {
             set_gameover (true);
             winner = NOBODY;
-            play_sound (SoundID.DRAWN_GAME);
+            play_sound (Sound.DRAWN_GAME);
         }
     }
 
@@ -699,7 +696,7 @@ private class FourInARow : Gtk.Application
             timeout = Timeout.add (SPEED_DROP, temp.exec);
         }
         else
-            play_sound (SoundID.COLUMN_FULL);
+            play_sound (Sound.COLUMN_FULL);
     }
 
     private void process_move (uint8 c)
@@ -1195,10 +1192,8 @@ private class FourInARow : Gtk.Application
     * * sound
     \*/
 
-    private GSound.Context sound_context;
-    private SoundContextState sound_context_state = SoundContextState.INITIAL;
-
-    private enum SoundID {
+    private enum Sound
+    {
         DROP,
         I_WIN,
         YOU_WIN,
@@ -1207,61 +1202,29 @@ private class FourInARow : Gtk.Application
         COLUMN_FULL;
     }
 
-    private enum SoundContextState {
-        INITIAL,
-        WORKING,
-        ERRORED;
-    }
-
-    private inline 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)
+    private void play_sound (Sound sound)
     {
         if (sound_on)
-        {
-            if (sound_context_state == SoundContextState.INITIAL)
-                init_sound ();
-            if (sound_context_state == SoundContextState.WORKING)
-                do_play_sound (id, sound_context);
-        }
+            _play_sound (sound);
     }
 
-    private static void do_play_sound (SoundID id, GSound.Context sound_context)
+    private MediaStream stream;     // for keeping in memory
+    private void _play_sound (Sound sound)
     {
         string name;
-
-        switch (id)
+        switch (sound)
         {
-            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;
+            case Sound.DROP:        name = "slide";     break;
+            case Sound.I_WIN:       name = "reverse";   break;
+            case Sound.YOU_WIN:     name = "bonus";     break;
+            case Sound.PLAYER_WIN:  name = "bonus";     break;
+            case Sound.DRAWN_GAME:  name = "reverse";   break;
+            case Sound.COLUMN_FULL: name = "bad";       break;
             default: assert_not_reached ();
         }
-
-        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);
-        }
+        stream = MediaFile.for_resource (@"/org/gnome/Four-in-a-row/sounds/$name.ogg");
+        stream.set_volume (1.0);
+        stream.play ();
     }
 
     /*\
diff --git a/src/meson.build b/src/meson.build
index f0084ca..ac46ece 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -39,7 +39,6 @@ executable(
     sources + resources,
     dependencies: [
         glib_dependency,
-        gsound_dependency,
         gtk_dependency,
         posix_dependency,
         rsvg_dependency
diff --git a/src/vapi/config.vapi b/src/vapi/config.vapi
index 684f110..0889400 100644
--- a/src/vapi/config.vapi
+++ b/src/vapi/config.vapi
@@ -19,5 +19,4 @@
 private const string GETTEXT_PACKAGE;
 private const string DATA_DIRECTORY;
 private const string VERSION;
-private const string SOUND_DIRECTORY;
 private const string LOCALEDIR;


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