supression canvas, ajout label, lancement de microphone au lieu de affichage, micro op, affichage op

This commit is contained in:
Mylloon 2020-11-30 22:22:43 +01:00
parent d1da2216a7
commit bc1f6efe5f

29
soui.py
View file

@ -6,27 +6,21 @@ from tkinter import Tk, Canvas, Label, PhotoImage
class Microphone: class Microphone:
def __init__(self): def __init__(self):
self.buffer = 0.1 # seconds
self.speaking = False self.speaking = False
def audio_callback(self, indata, frames, time, status): def audio_callback(self, indata, frames, time, status):
volume_norm = np.linalg.norm(indata) * 10 volume_norm = np.linalg.norm(indata) * 10
if int(volume_norm) > 2 and not self.speaking: if int(volume_norm) > 2:
self.speaking = True self.speaking = True
elif int(volume_norm) < 2 and self.speaking: elif int(volume_norm) < 2:
self.speaking = False self.speaking = False
def get_status_speaking(self): def get_status_speaking(self):
return self.speaking return self.speaking
def repeat(self):
print(self.speaking)
sleep(self.buffer)
return self.repeat()
def start(self): def start(self):
with sd.InputStream(callback = self.audio_callback): with sd.InputStream(callback = self.audio_callback):
self.repeat() Affichage().start()
class Affichage: class Affichage:
@ -36,28 +30,29 @@ class Affichage:
self.notspeaking = "not_speaking.png" self.notspeaking = "not_speaking.png"
def refresh(self): def refresh(self):
self.canvas.itemconfig(self.canvas_image, image = PhotoImage(master = self.fenetre, file = self.get_image())) self.get_image()
self.label.config(image = self.img)
self.fenetre.update_idletasks() self.fenetre.update_idletasks()
self.fenetre.after(self.buffer, self.refresh) self.fenetre.after(self.buffer, self.refresh)
def get_image(self): def get_image(self):
if Microphone().get_status_speaking(): if Microphone().get_status_speaking():
print("parle") print("parle")
return self.speaking self.img = PhotoImage(file = self.speaking)
else: else:
print("parle pas") self.img = PhotoImage(file = self.notspeaking)
return self.notspeaking
def start(self): def start(self):
self.fenetre = Tk() self.fenetre = Tk()
self.fenetre.title('Microphone') self.fenetre.title('Microphone')
self.canvas = Canvas(self.fenetre) self.fenetre.geometry("357x697")
self.canvas.configure(width = 1080, height = 2140) self.get_image()
self.canvas_image = self.canvas.create_image(540, 1070, image = PhotoImage(master = self.fenetre, file = self.get_image())) self.label = Label(self.fenetre, image = self.img)
self.label.pack(fill = "both", expand = "yes")
self.refresh() self.refresh()
self.fenetre.mainloop() self.fenetre.mainloop()
if __name__ == '__main__': if __name__ == '__main__':
Affichage().start() Microphone().start()