camp-atmo/campatmo.py

72 lines
2.0 KiB
Python
Executable File

#!/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"]
def load_files():
type_names = glob("samples/*/")
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 = {}
i = 0
mixer.set_num_channels(
sum(
[
1 if name in single_track_types else tracks_per_type
for name in type_files
]
)
)
print(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:
sounds[name].append(mixer.Sound(f))
random.shuffle(sounds[name])
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_]
)
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)
except KeyboardInterrupt:
print("exit")
sys.exit(0)
if __name__ == "__main__":
main()