Reload samples every main loop step

This commit is contained in:
Jakob 2019-08-20 12:41:54 +02:00
parent 795cc25b31
commit eaccc76c29
1 changed files with 23 additions and 10 deletions

View File

@ -8,17 +8,13 @@ from pygame import mixer
tracks_per_type = 2 tracks_per_type = 2
single_track_types = ["lines"] single_track_types = ["lines"]
sound_volume = 0.5
VERBOSITY = 0
def load_files(): def load_sounds(mixer):
type_names = glob("samples/*/") type_names = glob("samples/*/")
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names} 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 = {} types = {}
i = 0 i = 0
mixer.set_num_channels( 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: for name in type_files:
types[name] = [] types[name] = []
for k in range(1 if name in single_track_types else tracks_per_type): 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(): for name, files in type_files.items():
sounds[name] = [] sounds[name] = []
for f in files: 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]) random.shuffle(sounds[name])
return types, sounds
def main():
mixer.init(frequency=22050 * 2)
types, sounds = load_sounds(mixer)
try: try:
while True: while True:
for name, type_ in types.items(): for name, type_ in types.items():
@ -48,7 +52,10 @@ def main():
continue continue
try: try:
next_sound_index = max( 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: except ValueError:
next_sound_index = 0 next_sound_index = 0
@ -62,10 +69,16 @@ def main():
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])
time.sleep(1) time.sleep(1)
types, sounds = load_sounds(mixer)
except KeyboardInterrupt: except KeyboardInterrupt:
print("exit") print("exit")
sys.exit(0) sys.exit(0)
def v(*msg):
if VERBOSITY >= 1:
print(*msg)
if __name__ == "__main__": if __name__ == "__main__":
main() main()