You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB

#!/usr/bin/env python3
import time
import sys
import random
from glob import glob
from pygame import mixer
tracks_per_type = 2
single_track_types = ["lines"]
sound_volume = 0.5
VERBOSITY = 0
def load_sounds(mixer):
type_names = glob("samples/*/")
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
types = {}
i = 0
mixer.set_num_channels(
sum(
[
1 if name in single_track_types else tracks_per_type
for name in type_files
]
)
)
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):
types[name].append(mixer.Channel(i))
i += 1
sounds = {}
for name, files in type_files.items():
sounds[name] = []
for f in files:
sound = mixer.Sound(f)
sound.set_volume(sound_volume)
sounds[name].append(sound)
random.shuffle(sounds[name])
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_channels in types.items():
if len(sounds[name]) == 0:
continue
manage_type_queue(name, type_channels, sounds)
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()