-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioManager.py
More file actions
87 lines (69 loc) · 2.34 KB
/
audioManager.py
File metadata and controls
87 lines (69 loc) · 2.34 KB
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
84
85
86
87
import pygame
#EXAMPLES:
#zap = audioManager.SoundEffect('badswap')
#tune = audioManager.Song('subwoofer')
#tune.play()
#zap.play()
music_volume = 1.0
sound_effects_volume = 1.0
master_volume = 1.0
class SoundEffect:
def __init__(self, filename):
pygame.mixer.init()
self.sound = pygame.mixer.Sound("resources/"+filename+".wav")
def play(self): # plays sfx at volume between[0, 1], default max volume
self.sound.set_volume(sound_effects_volume*master_volume)
self.sound.play()
def stop(self): # stops all sound
self.sound.stop(self)
def fadeOut(self, delay=1): # fades out audio after delay seconds (1 second by default)
self.sound.fadeout(self, delay*1000)
class Song:
def __init__(self, filename):
self = pygame.mixer.music.load(filename+".mp3")
def play(self): # plays song once
pygame.mixer.music.set_volume(master_volume*music_volume)
pygame.mixer.music.play(0)
def loop(self): # plays song on loop
pygame.mixer.music.set_volume(music_volume*master_volume)
pygame.mixer.music.play(-1)
def fadeOut(self, delay=1): # will slowly fade out audio after delay seconds, defaults to 1 second
pygame.mixer.music.fadeout(delay*1000)
def stop(self):
pygame.mixer.music.stop()
def playSound(filename):
sound = pygame.mixer.Sound("resources/"+filename+".wav")
sound.set_volume(sound_effects_volume*master_volume)
sound.play()
return sound
def playSong(filename): # plays song
pygame.mixer.music.load("resources/"+filename+".mp3")
pygame.mixer.music.set_volume(music_volume*master_volume)
pygame.mixer.music.play(0)
def loopSong(filename): # plays song on loop
pygame.mixer.music.load("resources/"+filename+".mp3")
pygame.mixer.music.set_volume(music_volume*master_volume)
pygame.mixer.music.play(-1)
def stopMusic(): # stops all sound
pygame.mixer.music.stop()
def set_master_volume(vol):
if vol>1.0:
vol = 1
if vol<0.0:
vol = 0
global master_volume
master_volume=vol
def set_music_volume(vol):
if vol>1.0:
vol = 1
if vol<0.0:
vol = 0
global music_volume
music_volume = vol
def set_sound_effects_volume(vol):
if vol>1.0:
vol = 1
if vol<0.0:
vol = 0
global sound_effects_volume
sound_effects_volume = vol