def detected_callback(): if not utils.is_proper_time(): return snowboydecoder.play_audio_file(constants.getData('beep_hi.wav')) global player if player is not None and player.is_playing(): player.stop() player = None
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return Player.play(constants.getData('beep_hi.wav')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def _detected_callback(self): def start_record(): logger.info('开始录音') self._conversation.isRecording = True; if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.interrupt() Player.play(constants.getData('beep_hi.wav'), onCompleted=start_record, wait=True)
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return server.onSay("{\"action_info\": \"wake\",\"msg\": \"唤醒\"}") # self._conversation.say(random.choice(self.wakes)) Player.play(constants.getData('bee_wake.mp3')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def _detected_callback(self): print("3") if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.say('在“滴”声后,说出你想去的地方') time.sleep(4) Player.play(constants.getData('beep_hi.wav')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def _detected_callback(self): def start_record(): logger.info('开始录音') print('开始录音') self._conversation.isRecording = True; if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.interrupt() if config.get('/LED/enable', False): LED.wakeup() Player.play(constants.getData(random.choice(self._response_wav)), onCompleted=start_record, wait=True) with open("../communication/InPut.txt", "w") as file_writer: file_writer.write("true")
def _detected_callback(self): def start_record(): logger.info("开始录音") self._conversation.isRecording = True utils.setRecordable(True) if not utils.is_proper_time(): logger.warning("勿扰模式开启中") return if self._conversation.isRecording: logger.warning("正在录音中,跳过") return self._conversation.interrupt() if config.get("/LED/enable", False): LED.wakeup() utils.setRecordable(False) Player.play(constants.getData("beep_hi.wav"), onCompleted=start_record, wait=True)
def start(self, detected_callback=play_audio_file, interrupt_check=lambda: False, sleep_time=0.03, audio_recorder_callback=None, silent_count_threshold=15, recording_timeout=100): """ Start the voice detector. For every `sleep_time` second it checks the audio buffer for triggering keywords. If detected, then call corresponding function in `detected_callback`, which can be a single function (single model) or a list of callback functions (multiple models). Every loop it also calls `interrupt_check` -- if it returns True, then breaks from the loop and return. :param detected_callback: a function or list of functions. The number of items must match the number of models in `decoder_model`. :param interrupt_check: a function that returns True if the main loop needs to stop. :param float sleep_time: how much time in second every loop waits. :param audio_recorder_callback: if specified, this will be called after a keyword has been spoken and after the phrase immediately after the keyword has been recorded. The function will be passed the name of the file where the phrase was recorded. :param silent_count_threshold: indicates how long silence must be heard to mark the end of a phrase that is being recorded. :param recording_timeout: limits the maximum length of a recording. :return: None """ self._running = True def audio_callback(in_data, frame_count, time_info, status): self.ring_buffer.extend(in_data) play_data = chr(0) * len(in_data) return play_data, pyaudio.paContinue with no_alsa_error(): self.audio = pyaudio.PyAudio() self.stream_in = self.audio.open( input=True, output=False, format=self.audio.get_format_from_width( self.detector.BitsPerSample() / 8), channels=self.detector.NumChannels(), rate=self.detector.SampleRate(), frames_per_buffer=2048, stream_callback=audio_callback) if interrupt_check(): logger.debug("detect voice return") return tc = type(detected_callback) if tc is not list: detected_callback = [detected_callback] if len(detected_callback) == 1 and self.num_hotwords > 1: detected_callback *= self.num_hotwords assert self.num_hotwords == len(detected_callback), \ "Error: hotwords in your models (%d) do not match the number of " \ "callbacks (%d)" % (self.num_hotwords, len(detected_callback)) logger.debug("detecting...") state = "PASSIVE" while self._running is True: if interrupt_check(): logger.debug("detect voice break") break data = self.ring_buffer.get() if len(data) == 0: time.sleep(sleep_time) continue status = self.detector.RunDetection(data) if status == -1: logger.warning( "Error initializing streams or reading audio data") #small state machine to handle recording of phrase after keyword if state == "PASSIVE": if status > 0: #key word found self.recordedData = [] self.recordedData.append(data) silentCount = 0 recordingCount = 0 message = "Keyword " + str(status) + " detected at time: " message += time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) logger.info(message) callback = detected_callback[status - 1] if callback is not None: callback() if audio_recorder_callback is not None and status == 1 and utils.is_proper_time( ): state = "ACTIVE" continue elif state == "ACTIVE": stopRecording = False if recordingCount > recording_timeout: stopRecording = True elif status == -2: #silence found if silentCount > silent_count_threshold: stopRecording = True else: silentCount = silentCount + 1 elif status == 0: #voice found silentCount = 0 if stopRecording == True: fname = self.saveMessage() audio_recorder_callback(fname) state = "PASSIVE" continue recordingCount = recordingCount + 1 self.recordedData.append(data) logger.debug("finished.")
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return snowboydecoder.play_audio_file(constants.getData('beep_hi.wav')) self._conversation.interrupt()