Refactor campatmo into own CampAtmo class
This commit is contained in:
parent
172f31223c
commit
d4765556fc
72
campatmo.py
72
campatmo.py
|
@ -6,74 +6,80 @@ from glob import glob
|
||||||
from pygame import mixer
|
from pygame import mixer
|
||||||
|
|
||||||
|
|
||||||
tracks_per_type = 2
|
|
||||||
single_track_types = ["lines"]
|
|
||||||
sound_volume = 0.5
|
|
||||||
VERBOSITY = 0
|
VERBOSITY = 0
|
||||||
|
|
||||||
|
|
||||||
def load_sounds(mixer):
|
class CampAtmo:
|
||||||
|
tracks_per_type = 2
|
||||||
|
single_track_types = ["lines"]
|
||||||
|
sound_volume = 0.5
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
mixer.init(frequency=22050 * 2)
|
||||||
|
self.load_sounds()
|
||||||
|
|
||||||
|
def load_sounds(self):
|
||||||
type_names = glob("samples/*/")
|
type_names = glob("samples/*/")
|
||||||
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
|
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
|
||||||
types = {}
|
self.types = {}
|
||||||
i = 0
|
i = 0
|
||||||
mixer.set_num_channels(
|
mixer.set_num_channels(
|
||||||
sum(
|
sum(
|
||||||
[
|
[
|
||||||
1 if name in single_track_types else tracks_per_type
|
1 if name in self.single_track_types else self.tracks_per_type
|
||||||
for name in type_files
|
for name in type_files
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
v(f"{mixer.get_num_channels()} channels set")
|
v(f"{mixer.get_num_channels()} channels set")
|
||||||
for name in type_files:
|
for name in type_files:
|
||||||
types[name] = []
|
self.types[name] = []
|
||||||
for k in range(1 if name in single_track_types else tracks_per_type):
|
for k in range(1 if name in self.single_track_types else self.tracks_per_type):
|
||||||
types[name].append(mixer.Channel(i))
|
self.types[name].append(mixer.Channel(i))
|
||||||
i += 1
|
i += 1
|
||||||
sounds = {}
|
self.sounds = {}
|
||||||
for name, files in type_files.items():
|
for name, files in type_files.items():
|
||||||
sounds[name] = []
|
self.sounds[name] = []
|
||||||
for f in files:
|
for f in files:
|
||||||
sound = mixer.Sound(f)
|
sound = mixer.Sound(f)
|
||||||
sound.set_volume(sound_volume)
|
sound.set_volume(self.sound_volume)
|
||||||
sounds[name].append(sound)
|
self.sounds[name].append(sound)
|
||||||
random.shuffle(sounds[name])
|
random.shuffle(self.sounds[name])
|
||||||
return types, sounds
|
|
||||||
|
|
||||||
|
def manage_type_queue(self, name, type_channels):
|
||||||
def manage_type_queue(name, type_channels, sounds):
|
|
||||||
try:
|
try:
|
||||||
next_sound_index = max(
|
next_sound_index = max(
|
||||||
[
|
[
|
||||||
sounds[name].index(tt.get_queue() or tt.get_sound())
|
self.sounds[name].index(tt.get_queue() or tt.get_sound())
|
||||||
for tt in type_channels
|
for tt in type_channels
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
next_sound_index = 0
|
next_sound_index = 0
|
||||||
if next_sound_index >= len(sounds[name]):
|
if next_sound_index >= len(self.sounds[name]):
|
||||||
random.shuffle(sounds[name])
|
random.shuffle(self.sounds[name])
|
||||||
next_sound_index = 0
|
next_sound_index = 0
|
||||||
for c in type_channels:
|
for c in type_channels:
|
||||||
if c.get_queue() is not None and len(sounds[name]) > 1:
|
if c.get_queue() is not None and len(self.sounds[name]) > 1:
|
||||||
continue
|
continue
|
||||||
c.queue(sounds[name][next_sound_index])
|
c.queue(self.sounds[name][next_sound_index])
|
||||||
if c.get_queue() is None and len(sounds[name]) > 1:
|
if c.get_queue() is None and len(self.sounds[name]) > 1:
|
||||||
c.queue(sounds[name][next_sound_index + 1])
|
c.queue(self.sounds[name][next_sound_index + 1])
|
||||||
|
|
||||||
|
def run_forever(self):
|
||||||
|
while True:
|
||||||
|
for name, type_channels in self.types.items():
|
||||||
|
if len(self.sounds[name]) == 0:
|
||||||
|
continue
|
||||||
|
self.manage_type_queue(name, type_channels)
|
||||||
|
time.sleep(1)
|
||||||
|
self.load_sounds()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
mixer.init(frequency=22050 * 2)
|
atmo = CampAtmo()
|
||||||
types, sounds = load_sounds(mixer)
|
|
||||||
try:
|
try:
|
||||||
while True:
|
atmo.run_forever()
|
||||||
for name, type_channels in types.items():
|
|
||||||
if len(sounds[name]) == 0:
|
|
||||||
continue
|
|
||||||
manage_type_queue(name, type_channels, sounds)
|
|
||||||
time.sleep(1)
|
|
||||||
types, sounds = load_sounds(mixer)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("exit")
|
print("exit")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
Loading…
Reference in New Issue