Example #1
0
    def process_pending_commands(self):
        """Process all pending commands.

        Available commands:
          stop() - stop processing and exit the process
          flush() - flush input buffers.
            Now it only flushes the input connection.
            It is not able flush data already send to the sound card.

        Return True if the process should terminate.
        """

        if self.commands.poll():
            command = self.commands.recv()
            if self.cfg['AudioIO']['debug']:
                self.cfg['Logging']['system_logger'].debug(command)

            if isinstance(command, Command):
                if command.parsed['__name__'] == 'stop':
                    # Discard all data in play buffer.
                    while self.audio_play.poll():
                        self.audio_play.recv()

                    return True

                if command.parsed['__name__'] == 'flush':
                    # Discard all data in play buffer.
                    while self.audio_play.poll():
                        self.audio_play.recv()

                    return False

                if command.parsed['__name__'] == 'flush_out':
                    self.audio_to_send = ""
                    while self.audio_play.poll():
                        self.audio_play.recv()

                    msg = AlexToClient()
                    msg.type = AlexToClient.FLUSH_OUT_AUDIO
                    msg.priority = -1
                    msg.seq = self.curr_seq
                    self.send_to_client(msg.SerializeToString())

            elif isinstance(command, ASRHyp):
                hyp = command.hyp
                asr_hyp = hyp.get_best()

                msg = AlexToClient()
                msg.type = AlexToClient.ASR_RESULT
                msg.asr_result = unicode(asr_hyp).lower()
                msg.priority = -1
                msg.seq = self.curr_seq
                self.send_to_client(msg.SerializeToString())
            elif isinstance(command, TTSText):
                txt = command.text

                msg = AlexToClient()
                msg.type = AlexToClient.SYSTEM_PROMPT
                msg.priority = -1
                msg.seq = self.curr_seq
                msg.system_prompt = unicode(txt)
                self.send_to_client(msg.SerializeToString())

            self.process_command(command)

        return False
Example #2
0
    def read_write_audio(self):
        """Send some of the available data to the output.
        It should be a non-blocking operation.

        Therefore:
          1) do not send more then play_buffer_frames
          2) send only if stream.get_write_available() is more then the frame size
        """

        if self.audio_play.poll():
            data_play = self.audio_play.recv()
            if isinstance(data_play, Frame):
                buffer = data_play.payload
                self.audio_to_send += buffer
                self.n_played_frames += len(buffer) / 2

            elif isinstance(data_play, Command):
                if data_play.parsed['__name__'] == 'utterance_start':
                    msg = AlexToClient()
                    msg.seq = self.curr_seq
                    msg.type = AlexToClient.SPEECH_BEGIN
                    msg.utterance_id = self.utterance_id
                    self.send_to_client(msg.SerializeToString())

                if data_play.parsed['__name__'] == 'utterance_end':
                    msg = AlexToClient()
                    msg.seq = self.curr_seq
                    msg.type = AlexToClient.SPEECH_END
                    msg.utterance_id = self.utterance_id
                    self.send_to_client(msg.SerializeToString())

                    self.utterance_id += 1

            self.process_command(data_play)

        while len(self.audio_to_send) > WSIO.AUDIO_FRAMES_PER_MSG:
            buffer = self.audio_to_send[:WSIO.AUDIO_FRAMES_PER_MSG]
            self.audio_to_send = self.audio_to_send[WSIO.AUDIO_FRAMES_PER_MSG:]

            msg = AlexToClient()
            msg.type = AlexToClient.SPEECH
            msg.speech = buffer
            msg.seq = self.curr_seq
            self.send_to_client(msg.SerializeToString())