Browse Source

Replace simpleaudio with QSoundEffects

Maarten van den Berg 4 years ago
parent
commit
79c2a4c05c
2 changed files with 31 additions and 19 deletions
  1. 25 11
      piket_client/gui.py
  2. 6 8
      piket_client/sound.py

+ 25 - 11
piket_client/gui.py

2
 Provides the graphical front-end for Piket.
2
 Provides the graphical front-end for Piket.
3
 """
3
 """
4
 import collections
4
 import collections
5
+import itertools
5
 import logging
6
 import logging
6
 import math
7
 import math
7
 import os
8
 import os
8
 import sys
9
 import sys
9
-from typing import Deque
10
+from typing import Deque, Iterator
10
 
11
 
11
 import qdarkstyle
12
 import qdarkstyle
12
 
13
 
26
     QWidget,
27
     QWidget,
27
 )
28
 )
28
 from PySide2.QtGui import QIcon
29
 from PySide2.QtGui import QIcon
29
-from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot
30
+from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot, QUrl
31
+from PySide2.QtMultimedia import QSoundEffect
30
 
32
 
31
 # pylint: enable=E0611
33
 # pylint: enable=E0611
32
 
34
 
35
 except ImportError:
37
 except ImportError:
36
     dbus = None
38
     dbus = None
37
 
39
 
38
-from piket_client.sound import PLOP_WAVE, UNDO_WAVE
40
+from piket_client.sound import PLOP_PATH, UNDO_PATH
39
 from piket_client.model import (
41
 from piket_client.model import (
40
     Person,
42
     Person,
41
     ConsumptionType,
43
     ConsumptionType,
49
 LOG = logging.getLogger(__name__)
51
 LOG = logging.getLogger(__name__)
50
 
52
 
51
 
53
 
52
-def plop() -> None:
53
-    """ Asynchronously play the plop sound. """
54
-    PLOP_WAVE.play()
55
-
56
-
57
 class NameButton(QPushButton):
54
 class NameButton(QPushButton):
58
     """ Wraps a QPushButton to provide a counter. """
55
     """ Wraps a QPushButton to provide a counter. """
59
 
56
 
99
         LOG.debug("Button clicked.")
96
         LOG.debug("Button clicked.")
100
         result = self.person.add_consumption(self.active_id)
97
         result = self.person.add_consumption(self.active_id)
101
         if result:
98
         if result:
102
-            plop()
99
+            self.window().play_plop()
103
             self.setText(self.current_label)
100
             self.setText(self.current_label)
104
             self.consumption_created.emit(result)
101
             self.consumption_created.emit(result)
105
         else:
102
         else:
177
 
174
 
178
     consumption_type_changed = Signal(str)
175
     consumption_type_changed = Signal(str)
179
 
176
 
177
+    plop_loop: Iterator[QSoundEffect]
178
+    undo_loop: Iterator[QSoundEffect]
179
+
180
     def __init__(self) -> None:
180
     def __init__(self) -> None:
181
         LOG.debug("Initializing PiketMainWindow.")
181
         LOG.debug("Initializing PiketMainWindow.")
182
         super().__init__()
182
         super().__init__()
292
 
292
 
293
         self.addToolBar(self.toolbar)
293
         self.addToolBar(self.toolbar)
294
 
294
 
295
+        # Load sounds
296
+        plops = [QSoundEffect(self) for _ in range(7)]
297
+        for qse in plops:
298
+            qse.setSource(QUrl.fromLocalFile(str(PLOP_PATH)))
299
+        self.plop_loop = itertools.cycle(plops)
300
+
301
+        undos = [QSoundEffect(self) for _ in range(5)]
302
+        for qse in undos:
303
+            qse.setSource(QUrl.fromLocalFile(str(UNDO_PATH)))
304
+        self.undo_loop = itertools.cycle(undos)
305
+
295
         # Initialize main widget
306
         # Initialize main widget
296
         self.main_widget = NameButtons(self.ct_ag.actions()[0].data(), self)
307
         self.main_widget = NameButtons(self.ct_ag.actions()[0].data(), self)
297
         self.consumption_type_changed.connect(self.main_widget.consumption_type_changed)
308
         self.consumption_type_changed.connect(self.main_widget.consumption_type_changed)
337
                 person.set_active(True)
348
                 person.set_active(True)
338
 
349
 
339
             else:
350
             else:
340
-                person = Person(full_name=name, display_name=None, )
351
+                person = Person(full_name=name, display_name=None,)
341
                 person.create()
352
                 person.create()
342
 
353
 
343
             assert self.main_widget is not None
354
             assert self.main_widget is not None
379
 
390
 
380
     def do_undo(self) -> None:
391
     def do_undo(self) -> None:
381
         """ Undo the last marked consumption. """
392
         """ Undo the last marked consumption. """
382
-        UNDO_WAVE.play()
393
+        next(self.undo_loop).play()
383
 
394
 
384
         to_undo = self.undo_queue.pop()
395
         to_undo = self.undo_queue.pop()
385
         LOG.warning("Undoing consumption %s", to_undo)
396
         LOG.warning("Undoing consumption %s", to_undo)
423
         icon = QIcon(os.path.join(self.icons_dir, filename))
434
         icon = QIcon(os.path.join(self.icons_dir, filename))
424
         return icon
435
         return icon
425
 
436
 
437
+    def play_plop(self) -> None:
438
+        next(self.plop_loop).play()
439
+
426
 
440
 
427
 def main() -> None:
441
 def main() -> None:
428
     """ Main entry point of GUI client. """
442
     """ Main entry point of GUI client. """

+ 6 - 8
piket_client/sound.py

2
 Provides functions related to playing sounds.
2
 Provides functions related to playing sounds.
3
 """
3
 """
4
 
4
 
5
-import os
5
+import pathlib
6
 
6
 
7
-import simpleaudio as sa
8
 
7
 
9
-
10
-SOUNDS_DIR = os.path.join(os.path.dirname(__file__), "sounds")
8
+SOUND_PATH = pathlib.Path(__file__).parent / "sounds"
11
 """ Contains the absolute path to the sounds directory. """
9
 """ Contains the absolute path to the sounds directory. """
12
 
10
 
13
-PLOP_WAVE = sa.WaveObject.from_wave_file(os.path.join(SOUNDS_DIR, "plop.wav"))
14
-""" SimpleAudio WaveObject containing the plop sound. """
11
+PLOP_PATH = SOUND_PATH / "plop.wav"
12
+""" Path to the "plop" sound. """
15
 
13
 
16
-UNDO_WAVE = sa.WaveObject.from_wave_file(os.path.join(SOUNDS_DIR, "undo.wav"))
17
-""" SimpleAudio WaveObject containing the undo sound. """
14
+UNDO_PATH = SOUND_PATH / "undo.wav"
15
+""" Path to the "undo" sound". """