Verified Commit 45460a56 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add basic ability to play sounds

parent e95d4abf
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from poolpay import db, paths
from poolpay.card.Pirc522CardReader import Pirc522CardReader
from poolpay.director.Director import Director
from poolpay.display.PygameDisplay import PygameDisplay
from poolpay.sound.SoundLibrary import SoundLibrary
from poolpay.touch.EvdevTouchScreen import EvdevTouchScreen
from poolpay.ui.pygame.play import play
from poolpay.vault.Vault import Vault
@@ -23,7 +24,8 @@ logging.info("Creating Vault, Display and Director")
vault = Vault(paths.vault_file, paths.instance_dir)
display = PygameDisplay(Path("/dev/fb1"), width=480, height=320)
touch_screen = EvdevTouchScreen(Path("/dev/input/event0"))
director = Director(play, display)
sound_library = SoundLibrary()
director = Director(play, display, sound_library)


def launch_application(password: str) -> None:
@@ -44,6 +46,8 @@ def launch_application(password: str) -> None:
            card_reader.on_card_removed(lambda reader: director.card_removed())
            logging.info("Registering touch screen input handler")
            touch_screen.on_screen_touched(director.screen_touched)
            logging.info("Opening sound library")
            sound_library.open()
            logging.info("Registering card placement handler and starting to wait")
            card_reader.on_card_placed(
                lambda reader: director.card_placed(reader.read_id())
@@ -51,6 +55,8 @@ def launch_application(password: str) -> None:
        finally:
            logging.info("Closing database")
            db.close()
            logging.info("Closing sound library")
            sound_library.close()
            logging.info("Stopping server")
            server.stop()

+5 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ from poolpay import db
from poolpay.display.Display import Display
from poolpay.model.Card import Card
from poolpay.price.constants import DRINK
from poolpay.sound.SoundLibrary import SoundLibrary
from poolpay.sound.SoundType import SoundType
from poolpay.ui.Play import Play
from poolpay.ui.scenes.Scene import Scene

@@ -23,6 +25,7 @@ class Director:

    play: Play
    display: Display
    sound_library: SoundLibrary

    # State (public)
    started: bool = field(default=False, init=False)
@@ -43,6 +46,7 @@ class Director:

        if not card:
            self.play.PaymentFailure(self).present()
            self.sound_library[SoundType.FAILURE].play()
            return

        self._last_placed_card = card
@@ -112,6 +116,7 @@ class Director:
            new_balance_cents=new_balance_cents,
            person_name=card.owner.name,
        ).present()
        self.sound_library[SoundType.SUCCESS].play()

    def _do_reversal(self, card: Card) -> None:
        # We just pretend that this is certainly atomic and there is no way that we will ever have a race condition here
+44 −0
Original line number Diff line number Diff line
import random
from collections.abc import Iterable, Sequence
from contextlib import AbstractContextManager
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any

import pygame.mixer
from pygame.mixer import Sound

from poolpay.sound.SoundType import SoundType


@dataclass(frozen=True)
class SoundLibrary(AbstractContextManager):
    base_dir: Path = Path(__file__).parent / "resources"
    __sounds: dict[SoundType, Sequence[Sound]] = field(init=False, default_factory=dict)

    def __getitem__(self, sound_type: SoundType) -> Sound | None:
        if sound_type not in self.__sounds:
            self.__sounds[sound_type] = tuple(
                Sound(p) for p in self._get_sound_paths(sound_type)
            )

        return (
            random.choice(self.__sounds[sound_type])
            if self.__sounds[sound_type]
            else None
        )

    def _get_sound_paths(self, sound_type: SoundType) -> Iterable[Path]:
        return (self.base_dir / sound_type).glob("*")

    def open(self) -> None:
        pygame.mixer.init()

    def close(self) -> None:
        pygame.mixer.quit()

    def __enter__(self) -> None:
        self.open()

    def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
        self.close()
+7 −0
Original line number Diff line number Diff line
from enum import UNIQUE, StrEnum, verify


@verify(UNIQUE)
class SoundType(StrEnum):
    SUCCESS = "success"
    FAILURE = "failure"
+0 −0

Empty file added.