-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.py
52 lines (40 loc) · 1.15 KB
/
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
from pygame import mixer
import os
bgms = {}
def init_sound():
global bgms
mixer.init()
for bgm in os.listdir("data/bgm"):
newmsc = mixer.Sound("data/bgm/" + bgm)
bgms[bgm[:-4]] = newmsc
def play_sfx(track, loops=0, channel=1, volume=1, fade_ms=0):
chn = mixer.Channel(channel)
track_full = "data/sfx/" + str(track) + ".ogg"
snd = mixer.Sound(track_full)
chn.set_volume(volume)
chn.play(snd, loops, 0, fade_ms)
def get_channel_busy(channel):
chn = mixer.Channel(channel)
return chn.get_busy()
def fade_out_channel(channel_num, ms=500):
channel = mixer.Channel(channel_num)
channel.fadeout(ms)
def fade_out_bgm(fade_time = 2000):
chn = mixer.Channel(7)
chn.fadeout(fade_time)
def is_music_playing():
return get_channel_busy(7)
def set_channel_volume(channel, volume):
channel = mixer.Channel(channel)
channel.set_volume(volume)
def stop_channel(channel):
channel = mixer.Channel(channel)
channel.stop()
def play_bgm(track, channel=7):
global bgms
chn = mixer.Channel(7)
msc = bgms[track]
chn.set_volume(1)
chn.play(msc)
def sound_quit():
mixer.quit()