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,32 +42,36 @@ def load_sounds(mixer):
return types, sounds 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(): def main():
mixer.init(frequency=22050 * 2) mixer.init(frequency=22050 * 2)
types, sounds = load_sounds(mixer) types, sounds = load_sounds(mixer)
try: try:
while True: while True:
for name, type_ in types.items(): for name, type_channels in types.items():
if len(sounds[name]) == 0: if len(sounds[name]) == 0:
continue continue
try: manage_type_queue(name, type_channels, sounds)
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])
time.sleep(1) time.sleep(1)
types, sounds = load_sounds(mixer) types, sounds = load_sounds(mixer)
except KeyboardInterrupt: except KeyboardInterrupt: