-
Notifications
You must be signed in to change notification settings - Fork 6
/
sounds.py
62 lines (49 loc) · 1.57 KB
/
sounds.py
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
import pygame
import resources
import logging
import random
if not pygame.mixer.get_init():
pygame.mixer.init()
__logger = logging.getLogger('Sounds')
extensions = ['.ogg', '.wav']
sounds = {}
def parse_cfg(fpath):
try:
with open(fpath, 'r') as f:
__logger.debug('Found config file %s' % fpath)
for line in f:
k, v = line.split('=')
if k == 'volume' and 0 <= float(v) <= 1.0:
sounds[fpath.stem].set_volume(float(v))
except FileNotFoundError:
pass
for f in resources.list_sounds():
if f.is_file() and f.suffix in extensions:
sounds[f.stem] = pygame.mixer.Sound(str(f))
parse_cfg(f.with_suffix('.cfg'))
elif f.is_dir():
sounds[f.name] = []
for fd in f.iterdir():
if fd.suffix in extensions:
sounds[f.name].append(pygame.mixer.Sound(str(fd)))
parse_cfg(fd.with_suffix('.cfg'))
__logger.debug("Sounds initialized!")
def play(sound, *args):
try:
sounds[sound].play(*args)
except AttributeError:
random.choice(sounds[sound]).play(*args)
except KeyError:
__logger.error("Could not play sound %s: file not found.", sound)
def stop(sound):
try:
sounds[sound].stop()
except AttributeError:
for s in sounds[sound]:
s.stop()
except KeyError:
__logger.error("Could not stop sound %s.", sound)
def get(sound):
if isinstance(sounds[sound], pygame.mixer.Sound):
return sounds[sound]
return random.choice(sounds[sound])