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.

85 lines
2.3 KiB

5 years ago
#!/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
5 years ago
def load_sounds(mixer):
5 years ago
type_names = glob("samples/*/")
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
5 years ago
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")
5 years ago
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)
5 years ago
random.shuffle(sounds[name])
return types, sounds
def main():
mixer.init(frequency=22050 * 2)
types, sounds = load_sounds(mixer)
5 years ago
try:
while True:
for name, type_ 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_
]
5 years ago
)
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)
types, sounds = load_sounds(mixer)
5 years ago
except KeyboardInterrupt:
print("exit")
sys.exit(0)
def v(*msg):
if VERBOSITY >= 1:
print(*msg)
5 years ago
if __name__ == "__main__":
main()