示例#1
0
    def run(self):
        try:
            set_proc_name("Alex_VAD")
            self.session_logger.cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process(
                    ).name
                    return

                if not self.local_audio_in:
                    time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # Process all pending commands.
                if self.process_pending_commands():
                    return

                # FIXME: Make the following test work.
                # # Wait until a session has started.
                # if self.session_logger.is_open:
                # Process audio data.
                try:
                    for i in range(self.cfg['VAD']['n_rwa']):
                        # process at least n_rwa frames
                        self.read_write_audio()
                except SessionClosedException as e:
                    self.system_logger.exception(
                        'VAD:read_write_audio: {ex!s}'.format(ex=e))

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.100:
                    print "VAD t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0],
                                                                   c=d[1])
        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process(
            ).name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception(
                'Uncaught exception in the VAD process.')
            self.close_event.set()
            raise
示例#2
0
    def run(self):
        try:
            set_proc_name("Alex_ASR")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process(
                    ).name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # Process all pending commands.
                if self.process_pending_commands():
                    return

                # Process audio data.
                for i in range(self.cfg['ASR']['n_rawa']):
                    self.read_audio_write_asr_hypotheses()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: ASR t = {t:0.4f} c = {c:0.4f}\n".format(
                        t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process(
            ).name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception(
                'Uncaught exception in ASR process.')
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process(
        ).name
        self.close_event.set()
示例#3
0
    def run(self):
        try:
            set_proc_name("Alex_VAD")
            self.session_logger.cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print "Received close event in: %s" % multiprocessing.current_process().name
                    return

                if not self.local_audio_in:
                    time.sleep(self.cfg["Hub"]["main_loop_sleep_time"])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # Process all pending commands.
                if self.process_pending_commands():
                    return

                # FIXME: Make the following test work.
                # # Wait until a session has started.
                # if self.session_logger.is_open:
                # Process audio data.
                try:
                    for i in range(self.cfg["VAD"]["n_rwa"]):
                        # process at least n_rwa frames
                        self.read_write_audio()
                except SessionClosedException as e:
                    self.system_logger.exception("VAD:read_write_audio: {ex!s}".format(ex=e))

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.100:
                    print "VAD t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])
        except KeyboardInterrupt:
            print "KeyboardInterrupt exception in: %s" % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            self.cfg["Logging"]["system_logger"].exception("Uncaught exception in the VAD process.")
            self.close_event.set()
            raise
示例#4
0
文件: dm.py 项目: tkraut/alex
    def run(self):
        try:
            set_proc_name("Alex_DM")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process(
                    ).name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                # process all pending commands
                if self.process_pending_commands():
                    return

                # process the incoming SLU hypothesis
                self.read_slu_hypotheses_write_dialogue_act()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: DM t = {t:0.4f} c = {c:0.4f}\n".format(
                        t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process(
            ).name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception(
                'Uncaught exception in the DM process.')
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process(
        ).name
        self.close_event.set()
示例#5
0
文件: asr.py 项目: AoJ/alex
    def run(self):
        try:
            set_proc_name("Alex_ASR")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process().name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # Process all pending commands.
                if self.process_pending_commands():
                    return

                # Process audio data.
                for i in range(self.cfg['ASR']['n_rawa']):
                    self.read_audio_write_asr_hypotheses()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: ASR t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception('Uncaught exception in ASR process.')
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process().name
        self.close_event.set()
示例#6
0
文件: dm.py 项目: saikrishnar/alex
    def run(self):
        try:
            set_proc_name("Alex_DM")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process().name
                    return

                select.select([self.commands, self.slu_hypotheses_in], [], [], 1)

                s = (time.time(), time.clock())

                # process all pending commands
                if self.process_pending_commands():
                    return

                # process the incoming SLU hypothesis
                self.read_slu_hypotheses_write_dialogue_act()

                # Print out the execution time if it took longer than the threshold.
                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: DM t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception('Uncaught exception in the DM process.')
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process().name
        self.close_event.set()
示例#7
0
文件: nlg.py 项目: UFAL-DSG/alex
    def run(self):
        try:
            set_proc_name("Alex_NLG")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process().name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                # process all pending commands
                if self.process_pending_commands():
                    return

                # process the incoming DM dialogue acts
                self.read_dialogue_act_write_text()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: NLG t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            self.cfg['Logging']['system_logger'].exception('Uncaught exception in NLG process.')
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process().name
        self.close_event.set()
示例#8
0
    def run(self):
        try:
            set_proc_name("Alex_VIO")
            self.cfg["Logging"]["session_logger"].cancel_join_thread()

            global logger
            logger = self.cfg["Logging"]["system_logger"]

            # Create library instance
            self.lib = pj.Lib()

            # Init library with default config with some customization.
            ua_cfg = pj.UAConfig()
            ua_cfg.max_calls = 1

            log_cfg = pj.LogConfig()
            log_cfg.level = self.cfg["VoipIO"]["pjsip_log_level"]
            log_cfg.callback = log_cb

            media_cfg = pj.MediaConfig()
            media_cfg.clock_rate = self.cfg["Audio"]["sample_rate"]
            media_cfg.audio_frame_ptime = int(
                1000 * self.cfg["Audio"]["samples_per_frame"] / self.cfg["Audio"]["sample_rate"]
            )
            media_cfg.no_vad = True
            media_cfg.enable_ice = False

            self.lib.init(ua_cfg, log_cfg, media_cfg)
            self.lib.set_null_snd_dev()

            # disable all codecs except for PCM?
            for c in self.lib.enum_codecs():
                if c.name not in ["PCMU/8000/1", "PCMA/8000/1"]:
                    self.lib.set_codec_priority(c.name, 0)
            m = []
            m.append("Enabled codecs:")
            for c in self.lib.enum_codecs():
                if c.priority:
                    m.append("  Codec: %s priority: %d" % (c.name, c.priority))
            self.cfg["Logging"]["system_logger"].info("\n".join(m))

            # Create UDP transport which listens to any available port
            self.transport = self.lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(0))
            self.cfg["Logging"]["system_logger"].info(
                "Listening on %s port %s" % (self.transport.info().host, self.transport.info().port)
            )

            # Start the library
            self.lib.start()

            acc_cfg = pj.AccountConfig(
                self.cfg["VoipIO"]["domain"], self.cfg["VoipIO"]["user"], self.cfg["VoipIO"]["password"]
            )
            acc_cfg.allow_contact_rewrite = False

            self.acc = self.lib.create_account(acc_cfg)

            self.acc_cb = AccountCallback(self.cfg, self.acc, self)
            self.acc.set_callback(self.acc_cb)
            self.acc_cb.wait()

            self.cfg["Logging"]["system_logger"].info(
                "Registration complete, status = %s (%s)" % (self.acc.info().reg_status, self.acc.info().reg_reason)
            )

            my_sip_uri = "sip:" + self.transport.info().host + ":" + unicode(self.transport.info().port)

            # Create memory player
            self.mem_player = pj.MemPlayer(pj.Lib.instance(), self.cfg["Audio"]["sample_rate"])
            self.mem_player.create()

            # Create memory capture
            self.mem_capture = pj.MemCapture(pj.Lib.instance(), self.cfg["Audio"]["sample_rate"])
            self.mem_capture.create()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print "Received close event in: %s" % multiprocessing.current_process().name
                    return

                time.sleep(self.cfg["Hub"]["main_loop_sleep_time"])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # process all pending commands
                if self.process_pending_commands():
                    return

                # send all pending messages which has to be synchronized with played frames
                self.send_pending_messages()

                # process audio data
                for i in range(self.cfg["VoipIO"]["n_rwa"]):
                    # process at least n_rwa frames
                    self.read_write_audio()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: VIO t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])

            # Shutdown the library
            self.transport = None
            self.acc.delete()
            self.acc = None
            self.lib.destroy()
            self.lib = None

        except KeyboardInterrupt:
            self.lib.destroy()
            self.lib = None

            print "KeyboardInterrupt exception in: %s" % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            self.lib.destroy()
            self.lib = None

            self.cfg["Logging"]["system_logger"].exception("Uncaught exception in the VIO process.")
            self.close_event.set()
            raise
示例#9
0
    def run(self):
        try:
            set_proc_name("Alex_VIO")
            self.cfg['Logging']['session_logger'].cancel_join_thread()

            global logger
            logger = self.cfg['Logging']['system_logger']

            # Create library instance
            self.lib = pj.Lib()

            # Init library with default config with some customization.
            ua_cfg = pj.UAConfig()
            ua_cfg.max_calls = 1

            log_cfg = pj.LogConfig()
            log_cfg.level = self.cfg['VoipIO']['pjsip_log_level']
            log_cfg.callback = log_cb

            media_cfg = pj.MediaConfig()
            media_cfg.clock_rate = self.cfg['Audio']['sample_rate']
            media_cfg.audio_frame_ptime = int(
                1000 * self.cfg['Audio']['samples_per_frame'] /
                self.cfg['Audio']['sample_rate'])
            media_cfg.no_vad = True
            media_cfg.enable_ice = False

            self.lib.init(ua_cfg, log_cfg, media_cfg)
            self.lib.set_null_snd_dev()

            # disable all codecs except for PCM?
            for c in self.lib.enum_codecs():
                if c.name not in ["PCMU/8000/1", "PCMA/8000/1"]:
                    self.lib.set_codec_priority(c.name, 0)
            m = []
            m.append("Enabled codecs:")
            for c in self.lib.enum_codecs():
                if c.priority:
                    m.append("  Codec: %s priority: %d" % (c.name, c.priority))
            self.cfg['Logging']['system_logger'].info('\n'.join(m))

            # Create UDP transport which listens to any available port
            self.transport = self.lib.create_transport(pj.TransportType.UDP,
                                                       pj.TransportConfig(0))
            self.cfg['Logging']['system_logger'].info(
                "Listening on %s port %s" %
                (self.transport.info().host, self.transport.info().port))

            # Start the library
            self.lib.start()

            acc_cfg = pj.AccountConfig(self.cfg['VoipIO']['domain'],
                                       self.cfg['VoipIO']['user'],
                                       self.cfg['VoipIO']['password'])
            acc_cfg.allow_contact_rewrite = False

            self.acc = self.lib.create_account(acc_cfg)

            self.acc_cb = AccountCallback(self.cfg, self.acc, self)
            self.acc.set_callback(self.acc_cb)
            self.acc_cb.wait()

            self.cfg['Logging']['system_logger'].info(
                "Registration complete, status = %s (%s)" %
                (self.acc.info().reg_status, self.acc.info().reg_reason))

            my_sip_uri = "sip:" + self.transport.info().host + ":" + unicode(
                self.transport.info().port)

            # Create memory player
            self.mem_player = pj.MemPlayer(pj.Lib.instance(),
                                           self.cfg['Audio']['sample_rate'])
            self.mem_player.create()

            # Create memory capture
            self.mem_capture = pj.MemCapture(pj.Lib.instance(),
                                             self.cfg['Audio']['sample_rate'])
            self.mem_capture.create()

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process(
                    ).name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                self.recv_input_locally()

                # process all pending commands
                if self.process_pending_commands():
                    return

                # send all pending messages which has to be synchronized with played frames
                self.send_pending_messages()

                # process audio data
                for i in range(self.cfg['VoipIO']['n_rwa']):
                    # process at least n_rwa frames
                    self.read_write_audio()

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: VIO t = {t:0.4f} c = {c:0.4f}\n".format(
                        t=d[0], c=d[1])

            # Shutdown the library
            self.transport = None
            self.acc.delete()
            self.acc = None
            self.lib.destroy()
            self.lib = None

        except KeyboardInterrupt:
            self.lib.destroy()
            self.lib = None

            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process(
            ).name
            self.close_event.set()
            return
        except:
            self.lib.destroy()
            self.lib = None

            self.cfg['Logging']['system_logger'].exception(
                'Uncaught exception in the VIO process.')
            self.close_event.set()
            raise
示例#10
0
    def run(self):
        try:
            set_proc_name("Alex_SessionLogger")
            last_session_start_time = 0
            last_session_end_time = 0

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process().name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                while not self.queue.empty():
                    self._queue.append(self.queue.get())

                if len(self._queue):
                    cmd, args, kw, cmd_time = self._queue.popleft()

                    attr = '_'+cmd
                    try:
                        if cmd == 'session_start':
                            last_session_start_time = time.time()
                        elif cmd == 'session_end':
                            last_session_start_time = time.time()


                        if not self._is_open and cmd != 'session_start':
                            session_start_found = False
                            while time.time() - cmd_time < 3.0 and not session_start_found:
                                # these are probably commands for the new un-opened session
                                for i, (_cmd, _args, _kw, _cmd_time) in enumerate(self._queue):
                                    if _cmd == 'session_start':
                                        print "SessionLogger: finally found session start"
                                        self._session_start(*_args,**_kw)
                                        del self._queue[i]
                                        session_start_found = True
                                        break
                                else:
                                    time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                            if not session_start_found and (last_session_end_time - cmd_time < 2.0):
                                # just silently ignore because these are likely the be commands for the already
                                # closed session

                                # print "SessionLogger: should be silent"
                                # print "SessionLogger: calling method", cmd, "when the session is not open"
                                # print '             ', [a for a in args if isinstance(a, basestring) and len(a) < 80]
                                continue


                            if not session_start_found:
                                print "SessionLogger: no session start found"
                                print "SessionLogger: calling method", cmd, "when the session is not open"
                                print '             ', [a for a in args if isinstance(a, basestring) and len(a) < 80]
                                continue

                        cf = SessionLogger.__dict__[attr]
                        cf(self, *args, **kw)
                    except AttributeError:
                        print "SessionLogger: unknown method", cmd
                        self.close_event.set()
                        raise
                    except SessionLoggerException as e:
                        if cmd == 'rec_write':
                            print "Exception when logging:", cmd
                            print e
                        else:
                            print "Exception when logging:", cmd, args, kw
                            print e
                    except SessionClosedException:
                        print "Exception when logging:", cmd, args, kw
                        print e

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: SessionLogger t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process().name
            self.close_event.set()
            return
        except:
            print 'Uncaught exception in the SessionLogger process.'
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process().name
        self.close_event.set()
示例#11
0
    def run(self):
        try:
            set_proc_name("Alex_SessionLogger")
            last_session_start_time = 0
            last_session_end_time = 0

            while 1:
                # Check the close event.
                if self.close_event.is_set():
                    print 'Received close event in: %s' % multiprocessing.current_process(
                    ).name
                    return

                time.sleep(self.cfg['Hub']['main_loop_sleep_time'])

                s = (time.time(), time.clock())

                while not self.queue.empty():
                    self._queue.append(self.queue.get())

                if len(self._queue):
                    cmd, args, kw, cmd_time = self._queue.popleft()

                    attr = '_' + cmd
                    try:
                        if cmd == 'session_start':
                            last_session_start_time = time.time()
                        elif cmd == 'session_end':
                            last_session_start_time = time.time()

                        if not self._is_open and cmd != 'session_start':
                            session_start_found = False
                            while time.time(
                            ) - cmd_time < 3.0 and not session_start_found:
                                # these are probably commands for the new un-opened session
                                for i, (_cmd, _args, _kw,
                                        _cmd_time) in enumerate(self._queue):
                                    if _cmd == 'session_start':
                                        print "SessionLogger: finally found session start"
                                        self._session_start(*_args, **_kw)
                                        del self._queue[i]
                                        session_start_found = True
                                        break
                                else:
                                    time.sleep(self.cfg['Hub']
                                               ['main_loop_sleep_time'])

                            if not session_start_found and (
                                    last_session_end_time - cmd_time < 2.0):
                                # just silently ignore because these are likely the be commands for the already
                                # closed session

                                # print "SessionLogger: should be silent"
                                # print "SessionLogger: calling method", cmd, "when the session is not open"
                                # print '             ', [a for a in args if isinstance(a, basestring) and len(a) < 80]
                                continue

                            if not session_start_found:
                                print "SessionLogger: no session start found"
                                print "SessionLogger: calling method", cmd, "when the session is not open"
                                print '             ', [
                                    a for a in args if
                                    isinstance(a, basestring) and len(a) < 80
                                ]
                                continue

                        cf = SessionLogger.__dict__[attr]
                        cf(self, *args, **kw)
                    except AttributeError:
                        print "SessionLogger: unknown method", cmd
                        self.close_event.set()
                        raise
                    except SessionLoggerException as e:
                        if cmd == 'rec_write':
                            print "Exception when logging:", cmd
                            print e
                        else:
                            print "Exception when logging:", cmd, args, kw
                            print e
                    except SessionClosedException:
                        print "Exception when logging:", cmd, args, kw
                        print e

                d = (time.time() - s[0], time.clock() - s[1])
                if d[0] > 0.200:
                    print "EXEC Time inner loop: SessionLogger t = {t:0.4f} c = {c:0.4f}\n".format(
                        t=d[0], c=d[1])

        except KeyboardInterrupt:
            print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process(
            ).name
            self.close_event.set()
            return
        except:
            print 'Uncaught exception in the SessionLogger process.'
            self.close_event.set()
            raise

        print 'Exiting: %s. Setting close event' % multiprocessing.current_process(
        ).name
        self.close_event.set()