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
|
||||
|
||||
|
||||
tracks_per_type = 2
|
||||
single_track_types = ["lines"]
|
||||
sound_volume = 0.5
|
||||
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_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
|
||||
types = {}
|
||||
self.types = {}
|
||||
i = 0
|
||||
mixer.set_num_channels(
|
||||
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
|
||||
]
|
||||
)
|
||||
)
|
||||
v(f"{mixer.get_num_channels()} channels set")
|
||||
for name in type_files:
|
||||
types[name] = []
|
||||
for k in range(1 if name in single_track_types else tracks_per_type):
|
||||
types[name].append(mixer.Channel(i))
|
||||
self.types[name] = []
|
||||
for k in range(1 if name in self.single_track_types else self.tracks_per_type):
|
||||
self.types[name].append(mixer.Channel(i))
|
||||
i += 1
|
||||
sounds = {}
|
||||
self.sounds = {}
|
||||
for name, files in type_files.items():
|
||||
sounds[name] = []
|
||||
self.sounds[name] = []
|
||||
for f in files:
|
||||
sound = mixer.Sound(f)
|
||||
sound.set_volume(sound_volume)
|
||||
sounds[name].append(sound)
|
||||
random.shuffle(sounds[name])
|
||||
return types, sounds
|
||||
sound.set_volume(self.sound_volume)
|
||||
self.sounds[name].append(sound)
|
||||
random.shuffle(self.sounds[name])
|
||||
|
||||
|
||||
def manage_type_queue(name, type_channels, sounds):
|
||||
def manage_type_queue(self, name, type_channels):
|
||||
try:
|
||||
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
|
||||
]
|
||||
)
|
||||
except ValueError:
|
||||
next_sound_index = 0
|
||||
if next_sound_index >= len(sounds[name]):
|
||||
random.shuffle(sounds[name])
|
||||
if next_sound_index >= len(self.sounds[name]):
|
||||
random.shuffle(self.sounds[name])
|
||||
next_sound_index = 0
|
||||
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
|
||||
c.queue(sounds[name][next_sound_index])
|
||||
if c.get_queue() is None and len(sounds[name]) > 1:
|
||||
c.queue(sounds[name][next_sound_index + 1])
|
||||
c.queue(self.sounds[name][next_sound_index])
|
||||
if c.get_queue() is None and len(self.sounds[name]) > 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():
|
||||
mixer.init(frequency=22050 * 2)
|
||||
types, sounds = load_sounds(mixer)
|
||||
atmo = CampAtmo()
|
||||
try:
|
||||
while True:
|
||||
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)
|
||||
atmo.run_forever()
|
||||
except KeyboardInterrupt:
|
||||
print("exit")
|
||||
sys.exit(0)
|
||||
|
|
Loading…
Reference in New Issue