def __record(self): logger.info("---recording---") self.speaking = False audio = pyaudio.PyAudio() audio_stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE) sample_size = audio.get_sample_size(FORMAT) voice_data = [] head_voice_count = 0 rear_zero_count = 0 is_start_speaking = False silence_count = RATE // CHUNK_SIZE * 0.25 i = -1 while not Speaker.is_playing(): i += 1 if i > 1000 and not is_start_speaking: logger.debug("Quit recording.") break time.sleep(0.03) chunk_data = audio_stream.read(CHUNK_SIZE) zero_cnt = chunk_data.count(0) if is_start_speaking: if zero_cnt < VOICE_THRESHOLD: rear_zero_count = 0 else: rear_zero_count += 1 if rear_zero_count > silence_count: self.speaking = False logger.debug("Stop voice, i = " + str(i)) break else: if zero_cnt < VOICE_THRESHOLD: head_voice_count += 1 else: head_voice_count = 0 if head_voice_count > 2: is_start_speaking = True self.speaking = True logger.debug("Start voice, i = " + str(i)) if zero_cnt < VOICE_THRESHOLD: voice_data.append(chunk_data) if len(voice_data) > 100: break # logger.debug("len(voice_data): %d", len(voice_data)) audio_stream.stop_stream() audio_stream.close() audio.terminate() logger.debug("Done recording, chunk size: " + str(len(voice_data))) if len(voice_data) > 0: output_filename_flac = self.write_to_flac(voice_data, sample_size) self.user_words.put(output_filename_flac)
def play_sound(self, msg, play_async=False, quieter=False): # logger.debug("play_sound: 0") now = datetime.now() if msg == self.last_kanban_msg: # logger.debug("play_sound: 1") diff = now - self.last_kanban_msg_time if diff.total_seconds() < 8: logger.warning("Skip message: " + msg) return self.last_kanban_msg = msg self.last_kanban_msg_time = now if not Speaker.is_playing(): if play_async: Speaker.play_async(msg) else: Speaker.play(msg, quieter)
def run(self): while self.running: if not Speaker.is_playing(): self.__record() time.sleep(0.1) logger.info("terminated")
def play_sound(self, msg, play_async=False): if not Speaker.is_playing(): if play_async: Speaker.play_async(msg) else: Speaker.play(msg)