From eaccc76c29133695def3cd352724a17071273db9 Mon Sep 17 00:00:00 2001 From: jakob Date: Tue, 20 Aug 2019 12:41:54 +0200 Subject: [PATCH] Reload samples every main loop step --- campatmo.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/campatmo.py b/campatmo.py index 47b2365..1f63ab5 100755 --- a/campatmo.py +++ b/campatmo.py @@ -8,17 +8,13 @@ from pygame import mixer tracks_per_type = 2 single_track_types = ["lines"] +sound_volume = 0.5 +VERBOSITY = 0 -def load_files(): +def load_sounds(mixer): type_names = glob("samples/*/") type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names} - return type_files - - -def main(): - type_files = load_files() - mixer.init(frequency=22050 * 2) types = {} i = 0 mixer.set_num_channels( @@ -29,7 +25,7 @@ def main(): ] ) ) - print(f"{mixer.get_num_channels()} channels set") + 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): @@ -39,8 +35,16 @@ def main(): for name, files in type_files.items(): sounds[name] = [] for f in files: - sounds[name].append(mixer.Sound(f)) + sound = mixer.Sound(f) + sound.set_volume(sound_volume) + sounds[name].append(sound) random.shuffle(sounds[name]) + return types, sounds + + +def main(): + mixer.init(frequency=22050 * 2) + types, sounds = load_sounds(mixer) try: while True: for name, type_ in types.items(): @@ -48,7 +52,10 @@ def main(): continue try: next_sound_index = max( - [sounds[name].index(tt.get_queue() or tt.get_sound()) for tt in type_] + [ + sounds[name].index(tt.get_queue() or tt.get_sound()) + for tt in type_ + ] ) except ValueError: next_sound_index = 0 @@ -62,10 +69,16 @@ def main(): if c.get_queue() is None and len(sounds[name]) > 1: c.queue(sounds[name][next_sound_index + 1]) time.sleep(1) + types, sounds = load_sounds(mixer) except KeyboardInterrupt: print("exit") sys.exit(0) +def v(*msg): + if VERBOSITY >= 1: + print(*msg) + + if __name__ == "__main__": main()