feat: cleanup and rework audio system
This commit is contained in:
@@ -1,23 +1,30 @@
|
||||
import sched
|
||||
import time
|
||||
from threading import Thread
|
||||
from multiprocessing import Queue
|
||||
from queue import Empty, Full
|
||||
import json
|
||||
import logging
|
||||
|
||||
from redis import Redis
|
||||
|
||||
from control.walle import WallE
|
||||
from control.audio import AudioSystem
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
INTERVAL_TICK = 0.025 # 40/s
|
||||
|
||||
sound_action_queue = Queue()
|
||||
|
||||
class EventListener(Thread):
|
||||
|
||||
class RedisConnection(Thread):
|
||||
def __init__(self):
|
||||
super(EventListener, self).__init__()
|
||||
super(RedisConnection, self).__init__()
|
||||
self.keep_running = True
|
||||
self.dbcon = Redis()
|
||||
self.pubsub = self.dbcon.pubsub()
|
||||
self.pubsub.subscribe(['move', 'look'])
|
||||
self.pubsub.subscribe(['move', 'look', 'sound'])
|
||||
|
||||
def run(self) -> None:
|
||||
while self.keep_running:
|
||||
@@ -45,9 +52,17 @@ class EventListener(Thread):
|
||||
walle.set_movement(data_dict['angle'], data_dict['force'])
|
||||
elif channel == 'look':
|
||||
walle.set_eye_velocity(data_dict['angle'], data_dict['force'])
|
||||
elif channel == 'sound':
|
||||
try:
|
||||
sound_action_queue.put_nowait(data_dict)
|
||||
except Full:
|
||||
print(f'Queue was full, skipping sound')
|
||||
else:
|
||||
print(f'Unknown channel {channel}')
|
||||
|
||||
def store_audio(self, sounds):
|
||||
self.dbcon.set('sound_data', json.dumps(sounds))
|
||||
|
||||
|
||||
scheduler = sched.scheduler()
|
||||
audio = AudioSystem(scheduler, 'sounds.json')
|
||||
@@ -58,6 +73,13 @@ walle.setup()
|
||||
def walle_tick():
|
||||
scheduler.enter(INTERVAL_TICK, 3, walle_tick)
|
||||
walle.tick()
|
||||
while True:
|
||||
try:
|
||||
item = sound_action_queue.get_nowait()
|
||||
except Empty:
|
||||
break
|
||||
if item['action'] == 'play':
|
||||
audio.queue_sound(item['name'])
|
||||
|
||||
|
||||
scheduler.enter(INTERVAL_TICK, 3, walle_tick)
|
||||
@@ -66,10 +88,12 @@ audio.queue_sound('walle.boot')
|
||||
|
||||
if __name__ == '__main__':
|
||||
redisdb = Redis()
|
||||
event_listener = EventListener()
|
||||
event_listener.start()
|
||||
redis_connection = RedisConnection()
|
||||
redis_connection.store_audio([{'name': sound['name'], 'title': sound.get('title', sound['name'])}
|
||||
for sound in audio.sound_data['sounds']])
|
||||
redis_connection.start()
|
||||
try:
|
||||
scheduler.run()
|
||||
except KeyboardInterrupt:
|
||||
event_listener.stop()
|
||||
redis_connection.stop()
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user