This repository has been archived on 2021-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
microphonePopcat/soui.py

73 lines
2.1 KiB
Python
Raw Normal View History

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
from PIL import Image
2020-11-30 20:47:34 +01:00
2020-11-30 22:52:36 +01:00
speaking = False
2020-11-30 20:47:34 +01:00
2020-11-30 22:52:36 +01:00
class Microphone:
2020-11-30 20:47:34 +01:00
def audio_callback(self, indata, frames, time, status):
2020-11-30 22:52:36 +01:00
global speaking
2020-11-30 20:47:34 +01:00
volume_norm = np.linalg.norm(indata) * 10
2020-11-30 22:52:36 +01:00
if int(volume_norm) > 2 and speaking == False:
speaking = True
elif int(volume_norm) < 2 and speaking == True:
speaking = False
2020-11-30 20:47:34 +01:00
def get_status_speaking(self):
2020-11-30 22:52:36 +01:00
return speaking
2020-11-30 20:47:34 +01:00
def start(self):
with sd.InputStream(callback = self.audio_callback):
Affichage().start()
2020-11-30 20:47:34 +01:00
class Affichage:
def __init__(self):
2020-11-30 22:52:36 +01:00
self.buffer = 100 # 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 get_ratio_img(self):
image = Image.open(self.speaking)
image2 = Image.open(self.notspeaking)
width, height = image.size
width2, height2 = image2.size
if width != width2 or height != height2:
return None
else:
return f"{width}x{height}"
2020-11-30 20:47:34 +01:00
def refresh(self):
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():
self.img = PhotoImage(file = self.speaking)
2020-11-30 21:32:17 +01:00
else:
self.img = PhotoImage(file = self.notspeaking)
2020-11-30 20:47:34 +01:00
def start(self):
self.fenetre = Tk()
self.fenetre.title('Microphone')
geo = self.get_ratio_img()
if not geo:
raise NameError("Sorry, images speaking and not_speaking don't have same ratio")
self.fenetre.geometry(geo)
self.fenetre.resizable(0, 0)
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__':
Microphone().start()