def _smart_listen(self, interrupt_check, callback): r = sr.Recognizer(self.own.record_callback, self.cfg.gt('listener', 'silent_multiplier')) adata, vad_hwd = None, None stream_hwd = issubclass(self.cfg.detector.DETECTOR, StreamDetector) chrome_mode = self.cfg.gts('chrome_mode') vad_name = self.cfg.gt('listener', 'vad_chrome') if chrome_mode else None vad_name = vad_name or self.cfg.gt('listener', 'vad_mode', '') while not interrupt_check(): try: with sr.Microphone(device_index=self.own.mic_index) as source: __vad, noising = self._get_vad_detector(source, vad_name) if not isinstance(__vad, SnowboyHWD): vad_hwd = self._get_hw_detector( source.SAMPLE_WIDTH, source.SAMPLE_RATE, __vad) else: vad_hwd = __vad del __vad self._revert_message(vad_hwd) try: model_id, frames, elapsed_time = sr.wait_detection( source, vad_hwd, interrupt_check, noising) except sr.Interrupted: continue except RuntimeError: return if stream_hwd: model_name, phrase, msg = vad_hwd.model_info else: model_name, phrase, msg = self.cfg.models.model_info_by_id( model_id) if chrome_mode: if not phrase: # модель без триггера? self._detected_sr(msg or 'unset trigger', model_name, None, None) continue if self.cfg.gts('chrome_choke'): self.own.full_quiet() if stream_hwd: msg_ = 'In stream voice activation {}: {}, HWD: {}' self.log( msg_.format(model_name, phrase, self.cfg.detector.NAME)) try: adata = self._listen(r, source, vad_hwd, frames, elapsed_time) except (sr.WaitTimeoutError, RuntimeError): continue if chrome_mode: self._adata_parse(adata, model_name, phrase, vad_hwd, callback) else: vad_hwd.reset() self._detected(model_name, phrase, msg, callback) finally: vad_hwd and vad_hwd.die()
def _voice_record(self, hello: str or None, save_to: str, convert_rate, convert_width, limit): lvl = 5 # Включаем монопольный режим if hello is not None: self.own.say(self.own.tts(hello)(), lvl, True, is_file=True, blocking=120) else: self.own.set_lvl(lvl) r = sr.Recognizer(record_callback=self.own.record_callback) mic = sr.Microphone(device_index=self.get_mic_index()) vad = self.own.get_vad_detector(mic) self.own.play(self.cfg.path['ding'], lvl, blocking=3) with mic as source: record_time = time.time() try: adata = r.listen1(source=source, vad=vad, timeout=5, phrase_time_limit=limit) except sr.WaitTimeoutError as e: return str(e) if time.time() - record_time < 0.5: return F('Во время записи произошел сбой, это нужно исправить') try: os.makedirs(os.path.dirname(save_to), exist_ok=True) with open(save_to, "wb") as f: f.write(adata.get_wav_data(convert_rate, convert_width)) except IOError as err: return str(err) else: return None
def _block_listen(self, hello, lvl, file_path, self_call=False): r = sr.Recognizer(self.own.record_callback, self.cfg.gt('listener', 'silent_multiplier')) mic = sr.Microphone(device_index=self.get_mic_index()) alarm = self.cfg.gts('alarmtts') and not hello if alarm or file_path: self.own.say_callback(True) try: if alarm: self.own.play(self.cfg.path['dong'], lvl, blocking=2) vad = self.own.get_vad_detector(mic) if file_path: self.own.play(file_path, lvl, blocking=120) finally: if alarm or file_path: self.own.say_callback(False) audio, record_time, energy_threshold, rms = self.own.listener_listen(r, mic, vad) if record_time < 0.5 and not self_call: # Если от инициализации микрофона до записи прошло больше 20-35 сек, то запись ломается # Игнорируем полученную запись и запускаем новую, без приветствий self.log('Long ask fix!', logger.DEBUG) return self._block_listen(hello=True, lvl=lvl, file_path=None, self_call=True) else: return audio, record_time, energy_threshold, rms
def callback(_, mic, detector): with mic as source: try: sr.Recognizer().listen1(source=source, vad=detector, timeout=0.8, phrase_time_limit=0.5) except sr.WaitTimeoutError: pass return None
def listen(self, r=None, mic=None, vad=None): r = r or sr.Recognizer(self.own.record_callback, self.cfg.gt('listener', 'silent_multiplier')) mic = mic or sr.Microphone(device_index=self.own.mic_index) with mic as source: vad = vad or self.get_vad_detector(source) record_time = time.time() try: adata = self._listen(r, source, vad) except (sr.WaitTimeoutError, RuntimeError): adata = None record_time = time.time() - record_time return adata, record_time, vad.energy_threshold, vad.rms()
def callback(interrupt_check, mic, detector): r = sr.Recognizer(silent_multiplier=self.cfg.gt('listener', 'silent_multiplier')) adata = None while not interrupt_check(): with mic as source: try: adata = self._listen(r, source, detector) except sr.WaitTimeoutError: continue except RuntimeError: adata = None break return adata