From 0a8c7dd0d07bd15d5c0f32079508ab819a63a1de Mon Sep 17 00:00:00 2001 From: misygauziya Date: Sun, 9 Jul 2023 02:03:45 +0900 Subject: [PATCH] Check Point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit このままだと動かない --- window_config.py | 50 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/window_config.py b/window_config.py index 0a80b333..66d7a840 100644 --- a/window_config.py +++ b/window_config.py @@ -1,4 +1,6 @@ +from time import sleep from os import path as os_path +from threading import Thread from tkinter import DoubleVar, IntVar from tkinter import font as tk_font import customtkinter @@ -7,11 +9,11 @@ from flashtext import KeywordProcessor from utils import save_json, print_textbox from audio_utils import get_input_device_list, get_output_device_list +from audio_recorder import SelectedMicRecorder, SelectedSpeakerRecorder from languages import translation_lang, transcription_lang class ToplevelWindowConfig(CTkToplevel): - MAX_MIC_ENERGY_THRESHOLD = 2000 - MAX_SPEAKER_ENERGY_THRESHOLD = 2000 + def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) @@ -24,6 +26,12 @@ class ToplevelWindowConfig(CTkToplevel): self.after(200, lambda: self.iconbitmap(os_path.join(os_path.dirname(__file__), "img", "app.ico"))) self.title("Config") + # init parameter + self.MAX_MIC_ENERGY_THRESHOLD = 2000 + self.MAX_SPEAKER_ENERGY_THRESHOLD = 4000 + self.FLAG_LOOP_MIC = True + self.FLAG_LOOP_SPEAKER = True + # tabwiew config self.tabview_config = CTkTabview(self) self.tabview_config.grid(row=0, column=0, padx=5, pady=5, sticky="nsew") @@ -276,6 +284,9 @@ class ToplevelWindowConfig(CTkToplevel): corner_radius=0 ) self.progressBar_input_mic_energy_threshold.grid(row=row, column=1, columnspan=1, padx=5, pady=(5,0), sticky="nsew") + self.th_progressBar_input_mic_energy_threshold_recorder = Thread(target=self.progressBar_input_mic_energy_threshold_recorder, daemon=True) + self.th_progressBar_input_mic_energy_threshold_recorder.start() + sleep(2) row +=1 self.slider_input_mic_energy_threshold = customtkinter.CTkSlider( @@ -435,7 +446,9 @@ class ToplevelWindowConfig(CTkToplevel): corner_radius=0 ) self.progressBar_input_speaker_energy_threshold.grid(row=row, column=1, columnspan=1, padx=5, pady=(5,0), sticky="nsew") - + self.th_progressBar_input_speaker_energy_threshold_recorder = Thread(target=self.progressBar_input_speaker_energy_threshold_recorder, daemon=True) + self.th_progressBar_input_speaker_energy_threshold_recorder.start() + row +=1 self.slider_input_speaker_energy_threshold = customtkinter.CTkSlider( self.tabview_config.tab("Transcription"), @@ -762,6 +775,19 @@ class ToplevelWindowConfig(CTkToplevel): self.parent.INPUT_MIC_VOICE_LANGUAGE = choice save_json(self.parent.PATH_CONFIG, "INPUT_MIC_VOICE_LANGUAGE", self.parent.INPUT_MIC_VOICE_LANGUAGE) + def progressBar_input_mic_energy_threshold_recorder(self): + while self.FLAG_LOOP_MIC: + mic_device_name = self.parent.CHOICE_MIC_DEVICE + mic_device = [device for device in get_input_device_list() if device["name"] == mic_device_name][0] + re = SelectedMicRecorder(mic_device, energy_threshold=0, dynamic_energy_threshold=False, record_timeout=0) + while self.FLAG_LOOP_MIC: + if mic_device_name != self.parent.CHOICE_MIC_DEVICE: + break + with re.source as source: + energy = re.recorder.listen_energy(source) + self.progressBar_input_mic_energy_threshold.set(energy/self.MAX_MIC_ENERGY_THRESHOLD) + sleep(2) + def slider_input_mic_energy_threshold_callback(self, value): self.parent.INPUT_MIC_ENERGY_THRESHOLD = int(value) save_json(self.parent.PATH_CONFIG, "INPUT_MIC_ENERGY_THRESHOLD", self.parent.INPUT_MIC_ENERGY_THRESHOLD) @@ -798,6 +824,19 @@ class ToplevelWindowConfig(CTkToplevel): self.parent.INPUT_SPEAKER_VOICE_LANGUAGE = choice save_json(self.parent.PATH_CONFIG, "INPUT_SPEAKER_VOICE_LANGUAGE", self.parent.INPUT_SPEAKER_VOICE_LANGUAGE) + def progressBar_input_speaker_energy_threshold_recorder(self): + while self.FLAG_LOOP_SPEAKER: + speaker_device_name = self.parent.CHOICE_SPEAKER_DEVICE + speaker_device = [device for device in get_output_device_list() if device["name"] == speaker_device_name][0] + re = SelectedSpeakerRecorder(speaker_device, energy_threshold=0, dynamic_energy_threshold=False, record_timeout=0) + while self.FLAG_LOOP_SPEAKER: + if speaker_device_name != self.parent.CHOICE_SPEAKER_DEVICE: + break + with re.source as source: + energy = re.recorder.listen_energy(source) + self.progressBar_input_speaker_energy_threshold.set(energy/self.MAX_SPEAKER_ENERGY_THRESHOLD) + sleep(2) + def slider_input_speaker_energy_threshold_callback(self, value): self.parent.INPUT_SPEAKER_ENERGY_THRESHOLD = int(value) save_json(self.parent.PATH_CONFIG, "INPUT_SPEAKER_ENERGY_THRESHOLD", self.parent.INPUT_SPEAKER_ENERGY_THRESHOLD) @@ -839,6 +878,11 @@ class ToplevelWindowConfig(CTkToplevel): pass def delete_window(self): + self.FLAG_LOOP_MIC = False + self.FLAG_LOOP_SPEAKER = False + self.th_progressBar_input_mic_energy_threshold_recorder.join() + self.th_progressBar_input_speaker_energy_threshold_recorder.join() + sleep(1) self.parent.checkbox_translation.configure(state="normal") self.parent.checkbox_transcription_send.configure(state="normal") self.parent.checkbox_transcription_receive.configure(state="normal")