[iagno/arnaudb/wip/gtk4: 50/54] Use Gtk for sounds.



commit ed3e6b179822f1675203047533d727f6d9a6e837
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Apr 29 19:08:14 2020 +0200

    Use Gtk for sounds.

 data/iagno.gresource.xml |  4 +++
 data/meson.build         |  9 -------
 meson.build              |  3 ---
 src/iagno.vala           | 63 +++++++-----------------------------------------
 src/meson.build          |  1 -
 src/vapi/config.vapi     |  1 -
 6 files changed, 13 insertions(+), 68 deletions(-)
---
diff --git a/data/iagno.gresource.xml b/data/iagno.gresource.xml
index c93997f..6b5a88c 100644
--- a/data/iagno.gresource.xml
+++ b/data/iagno.gresource.xml
@@ -4,6 +4,10 @@
     <file preprocess="to-pixdata" alias="reverse.png">../data/reverse.png</file>
     <file preprocess="to-pixdata" alias="reversi.png">../data/reversi.png</file>
   </gresource>
+  <gresource prefix="/org/gnome/Reversi/sounds">
+    <file alias="flip-piece.ogg">../data/sounds/flip-piece.ogg</file>
+    <file alias="gameover.ogg">../data/sounds/gameover.ogg</file>
+  </gresource>
   <gresource prefix="/org/gnome/Reversi/ui">
     <file preprocess="xml-stripblanks" compressed="true" alias="adaptative-window.ui"           
ui/adaptative-window.ui</file>
     <file preprocess="xml-stripblanks" compressed="true" alias="base-headerbar.ui"              
ui/base-headerbar.ui</file>
diff --git a/data/meson.build b/data/meson.build
index 51077bd..0036242 100644
--- a/data/meson.build
+++ b/data/meson.build
@@ -81,15 +81,6 @@ endif
 # Install man page
 install_man('@0@.6'.format(meson.project_name()))
 
-# Install sounds
-install_data(
-    [
-        'sounds/flip-piece.ogg',
-        'sounds/gameover.ogg'
-    ],
-    install_dir: join_paths(get_option('datadir'), meson.project_name(), 'sounds')
-)
-
 # Install themes
 install_data(
     [
diff --git a/meson.build b/meson.build
index a21c8e4..eedf44b 100644
--- a/meson.build
+++ b/meson.build
@@ -15,7 +15,6 @@ valac = meson.get_compiler('vala')
 
 gio_dependency = dependency('gio-2.0', version: '>= 2.42.0')
 glib_dependency = dependency('glib-2.0', version: '>= 2.42.0')
-gsound_dependency = dependency('gsound', version: '>= 1.0.2')
 gtk_dependency = dependency('gtk4', version: '>= 3.96.0')
 libm_dependency = cc.find_library('m', required: false) # some platforms do not have libm separated from libc
 posix_dependency = valac.find_library('posix')
@@ -33,7 +32,6 @@ datadir     = join_paths(get_option('prefix'), get_option('datadir'))
 bindir      = join_paths(get_option('prefix'), get_option('bindir'))
 icondir     = join_paths(datadir, 'icons', 'hicolor')
 pkgdatadir  = join_paths(datadir, meson.project_name())
-soundsdir   = join_paths(pkgdatadir, 'sounds')
 schemadir   = join_paths(datadir, 'glib-2.0', 'schemas')
 appsdir     = join_paths(datadir, 'applications')
 
@@ -41,7 +39,6 @@ conf = configuration_data ()
 conf.set_quoted ('VERSION', meson.project_version())
 conf.set_quoted ('GETTEXT_PACKAGE', meson.project_name())
 conf.set_quoted ('DATA_DIRECTORY', pkgdatadir)
-conf.set_quoted ('SOUND_DIRECTORY', soundsdir)
 conf.set_quoted ('LOCALEDIR', localedir)
 
 configure_file(output: 'config.h', configuration: conf)
diff --git a/src/iagno.vala b/src/iagno.vala
index a218ad6..abfca6f 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -328,9 +328,6 @@ private class Iagno : Gtk.Application, BaseApplication
         new_game_screen.update_menubutton_menu (NewGameScreen.MenuButton.ONE, type_menu);
         new_game_screen.update_menubutton_menu (NewGameScreen.MenuButton.TWO, level_menu);
 
-        if (settings.get_boolean ("sound"))
-            init_sound ();
-
         GLib.Menu appearance_menu = new GLib.Menu ();
         section = new GLib.Menu ();
         /* Translators: hamburger menu "Appearance" submenu entry; a name for the default theme */
@@ -985,76 +982,34 @@ private class Iagno : Gtk.Application, BaseApplication
     * * Sound
     \*/
 
-    private GSound.Context sound_context;
-    private SoundContextState sound_context_state = SoundContextState.INITIAL;
-
     private enum Sound
     {
         FLIP,
         GAMEOVER;
     }
 
-    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;
-        }
-    }
-
     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, theme_manager);
-        }
+            _play_sound (sound, theme_manager);
     }
 
-    private static inline void _play_sound (Sound sound, GSound.Context sound_context, ThemeManager 
theme_manager)
-     // requires (sound_context_state == SoundContextState.WORKING)
+    private MediaStream stream;     // for keeping in memory
+    private inline void _play_sound (Sound sound, ThemeManager theme_manager)
     {
         string name;
         switch (sound)
         {
-            case Sound.FLIP:
-                name = theme_manager.sound_flip;
-                break;
-            case Sound.GAMEOVER:
-                name = theme_manager.sound_gameover;
-                break;
-            default:
-                return;
+            case Sound.FLIP:        name = theme_manager.sound_flip;        break;
+            case Sound.GAMEOVER:    name = theme_manager.sound_gameover;    break;
+            default: assert_not_reached ();
         }
         if (name == "")
             assert_not_reached ();
 
-        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/Reversi/sounds/$name");
+        stream.set_volume (1.0);
+        stream.play ();
     }
 
     /*\
diff --git a/src/meson.build b/src/meson.build
index f3d3a20..8deb9da 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -101,7 +101,6 @@ executable(meson.project_name(),
     dependencies: [
         gio_dependency,
         glib_dependency,
-        gsound_dependency,
         gtk_dependency,
         libm_dependency,
         posix_dependency,
diff --git a/src/vapi/config.vapi b/src/vapi/config.vapi
index ef83c6c..50218f4 100644
--- a/src/vapi/config.vapi
+++ b/src/vapi/config.vapi
@@ -21,5 +21,4 @@
 public const string VERSION;
 public const string GETTEXT_PACKAGE;
 public const string DATA_DIRECTORY;
-public const string SOUND_DIRECTORY;
 public const string LOCALEDIR;


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