-
Notifications
You must be signed in to change notification settings - Fork 69
/
sound.py
55 lines (42 loc) · 1023 Bytes
/
sound.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
import pygame, os
from time import sleep
PATH = None
SOUND_PATH = "/user_sounds/"
import pygame
m = pygame.mixer
def init(path_in):
global PATH
PATH = path_in
m.pre_init(44100, -16, 2, 2048)
m.init()
def full_name(filename):
name = PATH + SOUND_PATH + filename
if PATH.find('\\') > 0:
name = name.replace('/', '\\')
return name
def is_valid(filename):
final_name = full_name(filename)
try:
sound = m.Sound(final_name)
return True
except:
return False
def play(filename, volume=100.0):
final_name = full_name(filename)
try:
sound = m.Sound(final_name)
vol = float(volume / 100.0)
sound.set_volume(vol)
sound.play()
except:
print("[sound] Could not play sound " + final_name)
def stop():
try:
m.stop()
except:
print("Could not stop sounds")
def fadeout(delay):
try:
m.fadeout(delay)
except:
print("Could not fade out sound")