def test_audio_play_bytes(): """Test AudioPlayBytes.""" assert AudioPlayBytes.is_topic( AudioPlayBytes.topic(site_id=site_id, request_id=request_id)) assert (AudioPlayBytes.get_site_id( AudioPlayBytes.topic(site_id=site_id, request_id=request_id)) == site_id) assert (AudioPlayBytes.get_request_id( AudioPlayBytes.topic(site_id=site_id, request_id=request_id)) == request_id)
def test_topics(self): """Check get_ methods for topics""" siteId = "testSiteId" requestId = "testRequestId" intentName = "testIntent" wakewordId = "testWakeWord" # AudioFrame self.assertTrue(AudioFrame.is_topic(AudioFrame.topic(siteId=siteId))) self.assertEqual( AudioFrame.get_siteId(AudioFrame.topic(siteId=siteId)), siteId) # AudioPlayBytes self.assertTrue( AudioPlayBytes.is_topic( AudioPlayBytes.topic(siteId=siteId, requestId=requestId))) self.assertEqual( AudioPlayBytes.get_siteId( AudioPlayBytes.topic(siteId=siteId, requestId=requestId)), siteId, ) self.assertEqual( AudioPlayBytes.get_requestId( AudioPlayBytes.topic(siteId=siteId, requestId=requestId)), requestId, ) # AudioPlayFinished self.assertTrue( AudioPlayFinished.is_topic(AudioPlayFinished.topic(siteId=siteId))) self.assertEqual( AudioPlayFinished.get_siteId( AudioPlayFinished.topic(siteId=siteId)), siteId) # NluIntent self.assertTrue( NluIntent.is_topic(NluIntent.topic(intentName=intentName))) self.assertEqual( NluIntent.get_intentName(NluIntent.topic(intentName=intentName)), intentName) # HotwordDetected self.assertTrue( HotwordDetected.is_topic( HotwordDetected.topic(wakewordId=wakewordId))) self.assertEqual( HotwordDetected.get_wakewordId( HotwordDetected.topic(wakewordId=wakewordId)), wakewordId, )
def test_http_mqtt_text_to_speech(self): """Test text-to-speech HTTP endpoint""" text = "This is a test." self.client.subscribe(TtsSay.topic()) self.client.subscribe(AudioPlayBytes.topic(site_id=self.site_id)) self.client.subscribe(TtsSayFinished.topic()) response = requests.post( self.api_url("text-to-speech"), data=text, params={ "siteId": self.site_id, "sessionId": self.session_id }, ) self.check_status(response) wav_data = response.content self.assertGreater(len(wav_data), 0) # Check tts/say tts_say_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(TtsSay.is_topic(tts_say_msg.topic)) tts_say = TtsSay.from_dict(json.loads(tts_say_msg.payload)) self.assertEqual(tts_say.site_id, self.site_id) self.assertEqual(tts_say.session_id, self.session_id) self.assertEqual(tts_say.text, text) # Check audioServer/playBytes play_bytes_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(AudioPlayBytes.is_topic(play_bytes_msg.topic)) self.assertEqual(AudioPlayBytes.get_site_id(play_bytes_msg.topic), self.site_id) self.assertEqual(play_bytes_msg.payload, wav_data) # Check tts/sayFinished tts_finished_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(TtsSayFinished.is_topic(tts_finished_msg.topic)) tts_finished = TtsSayFinished.from_dict( json.loads(tts_finished_msg.payload)) self.assertEqual(tts_finished.session_id, self.session_id) # Ask for repeat response = requests.post(self.api_url("text-to-speech"), params={"repeat": "true"}) self.check_status(response) self.assertEqual(wav_data, response.content)
def handle_say(self, say: TtsSay): """Run TTS system and publish WAV data.""" wav_bytes: typing.Optional[bytes] = None try: say_command = shlex.split( self.tts_command.format(lang=say.lang)) + [say.text] _LOGGER.debug(say_command) wav_bytes = subprocess.check_output(say_command) _LOGGER.debug("Got %s byte(s) of WAV data", len(wav_bytes)) except Exception: _LOGGER.exception("tts_command") finally: self.publish(TtsSayFinished(id=say.id, sessionId=say.sessionId)) if wav_bytes: # Play WAV if self.play_command: try: # Play locally play_command = shlex.split( self.play_command.format(lang=say.lang)) _LOGGER.debug(play_command) subprocess.run(play_command, input=wav_bytes, check=True) except Exception: _LOGGER.exception("play_command") else: # Publish playBytes request_id = say.id or str(uuid4()) self.client.publish( AudioPlayBytes.topic(site_id=self.siteId, request_id=request_id), wav_bytes, )
def test_no_play(self): """Test text-to-speech HTTP endpoint with play=false""" text = "This is a test." self.client.subscribe(TtsSay.topic()) self.client.subscribe(AudioPlayBytes.topic(site_id=self.site_id)) self.client.subscribe(TtsSayFinished.topic()) self.client.subscribe(AudioToggleOff.topic()) self.client.subscribe(AudioToggleOn.topic()) response = requests.post( self.api_url("text-to-speech"), data=text, params={ "siteId": self.site_id, "sessionId": self.session_id, "play": "false", }, ) self.check_status(response) wav_data = response.content self.assertGreater(len(wav_data), 0) # Check audioServer/toggleOff audio_off_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(AudioToggleOff.is_topic(audio_off_msg.topic)) audio_off = AudioToggleOff.from_dict(json.loads(audio_off_msg.payload)) self.assertEqual(audio_off.site_id, self.site_id) # Check tts/say tts_say_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(TtsSay.is_topic(tts_say_msg.topic)) tts_say = TtsSay.from_dict(json.loads(tts_say_msg.payload)) self.assertEqual(tts_say.site_id, self.site_id) self.assertEqual(tts_say.session_id, self.session_id) self.assertEqual(tts_say.text, text) # Check audioServer/playBytes (will be ignored by audio output system) play_bytes_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(AudioPlayBytes.is_topic(play_bytes_msg.topic)) self.assertEqual(AudioPlayBytes.get_site_id(play_bytes_msg.topic), self.site_id) self.assertEqual(play_bytes_msg.payload, wav_data) # Check tts/sayFinished tts_finished_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(TtsSayFinished.is_topic(tts_finished_msg.topic)) tts_finished = TtsSayFinished.from_dict( json.loads(tts_finished_msg.payload)) self.assertEqual(tts_finished.site_id, self.site_id) self.assertEqual(tts_finished.session_id, self.session_id) # Check audioServer/toggleOn audio_on_msg = self.mqtt_messages.get(timeout=5) self.assertTrue(AudioToggleOn.is_topic(audio_on_msg.topic)) audio_on = AudioToggleOn.from_dict(json.loads(audio_on_msg.payload)) self.assertEqual(audio_on.site_id, self.site_id)