-
Notifications
You must be signed in to change notification settings - Fork 2
/
sound.py
97 lines (73 loc) · 2.23 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
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
88
89
90
91
92
93
94
95
96
97
import os
import random
def get_os_type():
return os.name
def clear_cmd_terminal(os_name):
if os_name == "nt":
os.system("cls")
else:
os.system("clear")
os_name = get_os_type()
from pygame import mixer
clear_cmd_terminal(os_name) # __|__
bgms_lunar = {}
bgms_underworld = {}
def init_sound():
global bgms_lunar, bgms_underworld
mixer.init()
try:
for bgm in os.listdir("data/bgm/lunar"):
newmsc = mixer.Sound("data/bgm/lunar/" + bgm)
bgms_lunar[bgm[:-4]] = newmsc
for bgm in os.listdir("data/bgm/underworld"):
newmsc = mixer.Sound("data/bgm/underworld/" + bgm)
bgms_underworld[bgm[:-4]] = newmsc
except:
pass
def play_sfx(track, loops=0, channel=1, volume=1):
chn = mixer.Channel(channel)
track_full = "data/sounds/" + str(track) + ".ogg"
snd = mixer.Sound(track_full)
chn.set_volume(volume)
chn.play(snd, loops)
def get_channel_busy(channel):
chn = mixer.Channel(channel)
return chn.get_busy()
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 fade_out_channel(channel_num):
channel = mixer.Channel(channel_num)
channel.fadeout(2000)
def fade_out_bgm(fade_time = 2000):
chn = mixer.Channel(7)
chn.fadeout(fade_time)
def play_bgm(track, world="lunar", channel=7):
global bgms_lunar, bgms_underworld
chn = mixer.Channel(7)
if world == "lunar":
msc = bgms_lunar[track]
else:
msc = bgms_underworld[track]
chn.set_volume(1)
chn.play(msc)
def play_random_bgm(world="lunar", channel=7):
global bgms_lunar, bgms_underworld
if world == "lunar" and not bgms_lunar:
return
elif world == "underworld" and not bgms_underworld:
return
chn = mixer.Channel(7)
if get_channel_busy(7):
chn.fadeout(2000)
if world == "lunar":
msc = random.choice(list(bgms_lunar.values()))
else:
msc = random.choice(list(bgms_underworld.values()))
chn.set_volume(1)
chn.play(msc)