summaryrefslogtreecommitdiff
path: root/src/sliceitoff/sfx/sfx.py
blob: 362d3eddefcf075e91d34fa0cef76e8fae1d45a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
""" sfx.sfx - pygame.mixer initialization and sound effects handling """
import os
from random import choice
from pathlib import Path
import pygame

from sliceitoff.settings import settings

DEBUG = os.getenv("DEBUG")

class Sfx:
    """ Sound Effects and Music? """
    def __init__(self):
        self.initialized = False
        self.sound = {}
        self.bgm = None
        try:
            self.sfx_volume = int(
                    settings.get_value_or_default("sfx_volume"))
            self.music_volume = int(
                    settings.get_value_or_default("music_volume"))
        except ValueError:
            self.sfx_volume = 10
            self.music_volume = 10
        try:
            pygame.mixer.pre_init(channels=2, buffer=512, frequency=48000)
        except pygame.error:
            pass

    def init(self, base_path):
        """ To be called after pygame is initialized. Actual mixer init and
            sample loading happens here """
        try:
            pygame.mixer.init()
            self.initialized = True
            for mp3_file in Path(base_path).glob('*.mp3'):
                self.sound[str(mp3_file.stem)] = pygame.mixer.Sound(mp3_file)
                if DEBUG:
                    print("Loading sound:", mp3_file, str(mp3_file.stem))
        except pygame.error:
            pass

    def sfx_up(self):
        """ Turn volume up. If its turned to 11 it resets back to 0. """
        self.sfx_volume += 1
        self.sfx_volume %= 11
        settings.replace_value("sfx_volume", self.sfx_volume)
        self.play(choice(("baby", "laser", "glass")))

    def music_up(self):
        """ Turn volume up. If its turned to 11 it resets back to 0. """
        self.music_volume += 1
        self.music_volume %= 11
        settings.replace_value("music_volume", self.music_volume)
        if self.initialized and self.bgm:
            self.sound[self.bgm].set_volume(self.music_volume / 10)

    def play(self, sample):
        """ Just plays named sample loaded from assets directory """
        if self.initialized:
            self.sound[sample].set_volume(self.sfx_volume / 10)
            self.sound[sample].play()

    def music(self, music):
        """ Plays sample as music. There is only one music at the time """
        if not self.initialized:
            return
        if self.bgm == music:
            return
        if self.bgm:
            self.sound[self.bgm].fadeout(500)
        self.bgm = music
        if self.bgm:
            self.sound[self.bgm].set_volume(self.music_volume / 10)
            self.sound[self.bgm].play()

# Initialize only one time
try:
    # pylint: disable = used-before-assignment
    # This is intented behaviour
    sfx
except NameError:
    sfx = Sfx()