Refactor campatmo into own CampAtmo class

This commit is contained in:
Sebastian Lohff 2019-08-20 13:30:25 +02:00
parent 172f31223c
commit d4765556fc
1 changed files with 64 additions and 58 deletions

View File

@ -6,74 +6,80 @@ 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
class CampAtmo:
tracks_per_type = 2
single_track_types = ["lines"]
sound_volume = 0.5
def __init__(self):
mixer.init(frequency=22050 * 2)
self.load_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
]
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
]
)
)
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])
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])
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])
def run_forever(self):
while True:
for name, type_channels in self.types.items():
if len(self.sounds[name]) == 0:
continue
self.manage_type_queue(name, type_channels)
time.sleep(1)
self.load_sounds()
def main():
mixer.init(frequency=22050 * 2)
types, sounds = load_sounds(mixer)
atmo = CampAtmo()
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)
atmo.run_forever()
except KeyboardInterrupt:
print("exit")
sys.exit(0)