Extract queue manage function

This commit is contained in:
Jakob 2019-08-20 13:12:59 +02:00
parent eaccc76c29
commit 172f31223c
1 changed files with 23 additions and 19 deletions

View File

@ -42,19 +42,12 @@ def load_sounds(mixer):
return types, sounds return types, sounds
def main(): def manage_type_queue(name, type_channels, sounds):
mixer.init(frequency=22050 * 2)
types, sounds = load_sounds(mixer)
try:
while True:
for name, type_ in types.items():
if len(sounds[name]) == 0:
continue
try: try:
next_sound_index = max( next_sound_index = max(
[ [
sounds[name].index(tt.get_queue() or tt.get_sound()) sounds[name].index(tt.get_queue() or tt.get_sound())
for tt in type_ for tt in type_channels
] ]
) )
except ValueError: except ValueError:
@ -62,12 +55,23 @@ def main():
if next_sound_index >= len(sounds[name]): if next_sound_index >= len(sounds[name]):
random.shuffle(sounds[name]) random.shuffle(sounds[name])
next_sound_index = 0 next_sound_index = 0
for c in type_: 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(sounds[name]) > 1:
continue continue
c.queue(sounds[name][next_sound_index]) c.queue(sounds[name][next_sound_index])
if c.get_queue() is None and len(sounds[name]) > 1: if c.get_queue() is None and len(sounds[name]) > 1:
c.queue(sounds[name][next_sound_index + 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_channels in types.items():
if len(sounds[name]) == 0:
continue
manage_type_queue(name, type_channels, sounds)
time.sleep(1) time.sleep(1)
types, sounds = load_sounds(mixer) types, sounds = load_sounds(mixer)
except KeyboardInterrupt: except KeyboardInterrupt: