I want to make a voice recorder with sounddevice and tkinter module in python.
I want to make my app to start recording when the "Start Recording" button is pressed. After that, I want that sounddevice module records while the stoprec variable is False; and once the stoprec variable equals to True, I want sounddevice to stop recording. When I click "Start Recording" button, the window freezes. Can anyone help me?
here is my source code:
from tkinter import *
import speech_recognition as sr
import tkinter.messagebox as mb
import sounddevice as sd
from scipy.io.wavfile import write
from googletrans import Translator
from lingua import LanguageDetectorBuilder
win = Tk()
win.title("Voice Recording")
win.geometry('400x500')
win.resizable(False, False)
win.iconbitmap('Logo.ico')
stoprec = False
time = 0
def start(startbtn):
freq = 44100
startbtn.config(state = 'disabled')
lst = []
def stop():
global stoprec
stoprec = True
btnstop.config(state = 'disabled')
btnstop.config(command = stop)
def count():
global stoprec
global time
if stoprec == False:
win.after(1000, count)
time += 1
lst.append(time)
count()
def record():
while stoprec == False:
rec = sd.rec(lst[-1] * freq, freq, 2)
write('recording.wav', freq, rec)
record()
img = PhotoImage(file = 'Microphone.png')
lbl = Label(win, image = img)
lbl.pack()
startbtn = Button(win, text = 'Start Recording', font = ('times new roman', 15), cursor = 'hand2', command = lambda : start(startbtn))
startbtn.pack(pady = 10)
btnstop = Button(win, text = 'Stop Recording', font = ('times new roman', 15), cursor = 'hand2')
btnstop.pack()
win.mainloop()
Thanks.