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

59 lines
1.6 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
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
if int(volume_norm) > 2:
2020-11-30 20:47:34 +01:00
self.speaking = True
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):
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):
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")
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')
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__':
Microphone().start()