Skip to content

Commit

Permalink
encapsule logic for broker decision
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp2310 committed Apr 18, 2022
1 parent c90a675 commit 74901d3
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions core/server/AudioServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import time
import uuid
import wave
from typing import Dict, Optional
from typing import Dict, Optional, Union
# noinspection PyUnresolvedReferences
from webrtcvad import Vad

Expand Down Expand Up @@ -119,6 +119,12 @@ def recordFrame(self, deviceUid: str, frame: bytes):

self._waves[deviceUid].writeframes(frame)

def publishToListener(self, topic: str, payload: Union[dict, str] = None, qos: int = 0, retain: bool = False):
if self._broadcastLocal:
self.MqttManager.localPublish(topic=topic, payload=payload)
else:
self.MqttManager.publish(topic=topic, payload=payload, qos=qos, retain=retain)


def publishAudio(self):
"""
Expand Down Expand Up @@ -151,12 +157,12 @@ def publishAudio(self):
if not speech and speechFrames < minSpeechFrames:
speechFrames += 1
elif speechFrames >= minSpeechFrames:
self.publishToListener(
topic=constants.TOPIC_VAD_UP.format(self.ConfigManager.getAliceConfigByName('uuid')),
payload={
'siteId': self.ConfigManager.getAliceConfigByName('uuid')
})
speech = True
self.MqttManager.localPublish(
topic=constants.TOPIC_VAD_UP.format(self.ConfigManager.getAliceConfigByName('uuid')),
payload={
'siteId': self.ConfigManager.getAliceConfigByName('uuid')
})
silence = self.SAMPLERATE / self.FRAMES_PER_BUFFER
speechFrames = 0
else:
Expand All @@ -165,11 +171,11 @@ def publishAudio(self):
silence -= 1
else:
speech = False
self.MqttManager.localPublish(
topic=constants.TOPIC_VAD_DOWN.format(self.ConfigManager.getAliceConfigByName('uuid')),
payload={
'siteId': self.ConfigManager.getAliceConfigByName('uuid')
})
self.publishToListener(
topic=constants.TOPIC_VAD_DOWN.format(self.ConfigManager.getAliceConfigByName('uuid')),
payload={
'siteId': self.ConfigManager.getAliceConfigByName('uuid')
})
else:
speechFrames = 0

Expand All @@ -192,10 +198,8 @@ def publishAudioFrames(self, frames: bytes):
wav.writeframes(frames)

audioFrames = buffer.getvalue()
if self._broadcastLocal:
self.MqttManager.localPublish(topic=constants.TOPIC_AUDIO_FRAME.format(self.ConfigManager.getAliceConfigByName('uuid')), payload=bytearray(audioFrames))
else:
self.MqttManager.publish(topic=constants.TOPIC_AUDIO_FRAME.format(self.ConfigManager.getAliceConfigByName('uuid')), payload=bytearray(audioFrames))

self.publishToListener(topic=constants.TOPIC_AUDIO_FRAME.format(self.ConfigManager.getAliceConfigByName('uuid')), payload=bytearray(audioFrames))


def onPlayBytes(self, payload: bytearray, deviceUid: str, sessionId: str = None, requestId: str = None):
Expand Down

0 comments on commit 74901d3

Please sign in to comment.