2020-11-30 20:47:34 +01:00
|
|
|
import numpy as np
|
|
|
|
import sounddevice as sd
|
|
|
|
from time import sleep
|
2020-11-30 21:32:17 +01:00
|
|
|
from tkinter import Tk, Canvas, Label, PhotoImage
|
2020-11-30 20:47:34 +01:00
|
|
|
|
|
|
|
class Microphone:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.speaking = False
|
|
|
|
|
|
|
|
def audio_callback(self, indata, frames, time, status):
|
|
|
|
volume_norm = np.linalg.norm(indata) * 10
|
2020-11-30 22:22:43 +01:00
|
|
|
if int(volume_norm) > 2:
|
2020-11-30 20:47:34 +01:00
|
|
|
self.speaking = True
|
2020-11-30 22:22:43 +01:00
|
|
|
elif int(volume_norm) < 2:
|
2020-11-30 20:47:34 +01:00
|
|
|
self.speaking = False
|
|
|
|
|
|
|
|
def get_status_speaking(self):
|
|
|
|
return self.speaking
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
with sd.InputStream(callback = self.audio_callback):
|
2020-11-30 22:22:43 +01:00
|
|
|
Affichage().start()
|
2020-11-30 20:47:34 +01:00
|
|
|
|
|
|
|
class Affichage:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.buffer = 500 # milliseconds
|
2020-11-30 21:03:14 +01:00
|
|
|
self.speaking = "speaking.png"
|
|
|
|
self.notspeaking = "not_speaking.png"
|
2020-11-30 20:47:34 +01:00
|
|
|
|
|
|
|
def refresh(self):
|
2020-11-30 22:22:43 +01:00
|
|
|
self.get_image()
|
|
|
|
self.label.config(image = self.img)
|
2020-11-30 20:47:34 +01:00
|
|
|
self.fenetre.update_idletasks()
|
|
|
|
self.fenetre.after(self.buffer, self.refresh)
|
2020-11-30 21:32:17 +01:00
|
|
|
|
|
|
|
def get_image(self):
|
|
|
|
if Microphone().get_status_speaking():
|
2020-11-30 21:39:25 +01:00
|
|
|
print("parle")
|
2020-11-30 22:22:43 +01:00
|
|
|
self.img = PhotoImage(file = self.speaking)
|
2020-11-30 21:32:17 +01:00
|
|
|
else:
|
2020-11-30 22:22:43 +01:00
|
|
|
self.img = PhotoImage(file = self.notspeaking)
|
2020-11-30 20:47:34 +01:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.fenetre = Tk()
|
|
|
|
self.fenetre.title('Microphone')
|
2020-11-30 22:22:43 +01:00
|
|
|
self.fenetre.geometry("357x697")
|
|
|
|
self.get_image()
|
|
|
|
self.label = Label(self.fenetre, image = self.img)
|
|
|
|
self.label.pack(fill = "both", expand = "yes")
|
2020-11-30 20:47:34 +01:00
|
|
|
|
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
self.fenetre.mainloop()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-11-30 22:22:43 +01:00
|
|
|
Microphone().start()
|