feat: detached actual sound files from code
This commit is contained in:
@@ -4,6 +4,8 @@ import wave
|
||||
import uuid
|
||||
import sched
|
||||
import logging
|
||||
import json
|
||||
import os.path
|
||||
|
||||
from pyaudio import PyAudio, Stream
|
||||
|
||||
@@ -23,19 +25,27 @@ class AudioSystem:
|
||||
audio: Optional[PyAudio]
|
||||
scheduler: sched.scheduler
|
||||
|
||||
def __init__(self, scheduler):
|
||||
def __init__(self, scheduler: sched.scheduler, sound_file: str):
|
||||
self.to_play = Queue()
|
||||
self.playing = {}
|
||||
self.audio = PyAudio()
|
||||
self.scheduler = scheduler
|
||||
self.scheduler.enter(0, 2, self.start_streams)
|
||||
self.scheduler.enter(0, 1, self.fill_streams)
|
||||
with open(sound_file) as f:
|
||||
self.sound_data = json.load(f)
|
||||
self.sound_dir = os.path.realpath(self.sound_data['sound_dir'])
|
||||
|
||||
def quit(self):
|
||||
self.audio.terminate()
|
||||
|
||||
def queue_sound(self, sound):
|
||||
self.to_play.put(sound)
|
||||
try:
|
||||
sound_entry = next(filter(lambda i: i['name'] == sound, self.sound_data['sounds']))
|
||||
except StopIteration:
|
||||
raise ValueError(f"Sound {sound!r} is not known")
|
||||
|
||||
self.to_play.put(os.path.join(self.sound_dir, sound_entry['path']))
|
||||
|
||||
def start_streams(self):
|
||||
self.scheduler.enter(INTERVAL_QUEUE, 2, self.start_streams)
|
||||
|
||||
Reference in New Issue
Block a user