diff --git a/campatmo.py b/campatmo.py index 1f63ab5..902680c 100755 --- a/campatmo.py +++ b/campatmo.py @@ -42,32 +42,36 @@ def load_sounds(mixer): return types, sounds +def manage_type_queue(name, type_channels, sounds): + try: + next_sound_index = max( + [ + 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]) + next_sound_index = 0 + for c in type_channels: + if c.get_queue() is not None and len(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]) + + def main(): mixer.init(frequency=22050 * 2) types, sounds = load_sounds(mixer) try: while True: - for name, type_ in types.items(): + for name, type_channels in types.items(): if len(sounds[name]) == 0: continue - try: - next_sound_index = max( - [ - sounds[name].index(tt.get_queue() or tt.get_sound()) - for tt in type_ - ] - ) - except ValueError: - next_sound_index = 0 - if next_sound_index >= len(sounds[name]): - random.shuffle(sounds[name]) - next_sound_index = 0 - for c in type_: - if c.get_queue() is not None and len(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]) + manage_type_queue(name, type_channels, sounds) time.sleep(1) types, sounds = load_sounds(mixer) except KeyboardInterrupt: