camp-atmo/campatmo.py

97 lines
2.7 KiB
Python
Raw Normal View History

2019-08-20 12:11:41 +02:00
#!/usr/bin/env python3
import time
import sys
import random
from glob import glob
from pygame import mixer
2019-08-20 12:41:54 +02:00
VERBOSITY = 0
2019-08-20 12:11:41 +02:00
class CampAtmo:
tracks_per_type = 2
single_track_types = ["lines"]
sound_volume = 0.5
2019-08-20 12:41:54 +02:00
def __init__(self):
mixer.init(frequency=22050 * 2)
self.load_sounds()
2019-08-20 12:41:54 +02:00
def load_sounds(self):
type_names = glob("samples/*/")
type_files = {c.split("/")[1]: glob(c + "/*.ogg") for c in type_names}
self.types = {}
i = 0
mixer.set_num_channels(
sum(
[
1 if name in self.single_track_types else self.tracks_per_type
for name in type_files
]
)
2019-08-20 13:12:59 +02:00
)
v(f"{mixer.get_num_channels()} channels set")
for name in type_files:
self.types[name] = []
for k in range(
1 if name in self.single_track_types else self.tracks_per_type
):
self.types[name].append(mixer.Channel(i))
i += 1
self.sounds = {}
for name, files in type_files.items():
self.sounds[name] = []
for f in files:
sound = mixer.Sound(f)
sound.set_volume(self.sound_volume)
self.sounds[name].append(sound)
random.shuffle(self.sounds[name])
2019-08-20 13:12:59 +02:00
def manage_type_queue(self, name, type_channels):
try:
next_sound_index = max(
[
self.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(self.sounds[name]):
random.shuffle(self.sounds[name])
next_sound_index = 0
for c in type_channels:
if c.get_queue() is not None and len(self.sounds[name]) > 1:
continue
c.queue(self.sounds[name][next_sound_index])
if c.get_queue() is None and len(self.sounds[name]) > 1:
c.queue(self.sounds[name][next_sound_index + 1])
2019-08-20 13:12:59 +02:00
def run_forever(self):
2019-08-20 12:11:41 +02:00
while True:
for name, type_channels in self.types.items():
if len(self.sounds[name]) == 0 or name in self.single_track_types:
2019-08-20 12:11:41 +02:00
continue
self.manage_type_queue(name, type_channels)
2019-08-20 12:11:41 +02:00
time.sleep(1)
self.load_sounds()
def main():
atmo = CampAtmo()
try:
atmo.run_forever()
2019-08-20 12:11:41 +02:00
except KeyboardInterrupt:
print("exit")
sys.exit(0)
2019-08-20 12:41:54 +02:00
def v(*msg):
if VERBOSITY >= 1:
print(*msg)
2019-08-20 12:11:41 +02:00
if __name__ == "__main__":
main()