简体   繁体   English

sounddevice 冻结 tkinter GUI

[英]sounddevice freezes the tkinter GUI

I want to make a voice recorder with sounddevice and tkinter module in python.我想用 python 中的 sounddevice 和 tkinter 模块制作一个录音机。

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;之后,我希望 sounddevice 模块在 stoprec 变量为 False 时记录; and once the stoprec variable equals to True, I want sounddevice to stop recording.一旦 stoprec 变量等于 True,我希望 sounddevice 停止录制。 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.谢谢。

you need to check if recording stoped then start recording again您需要检查录制是否停止然后重新开始录制

if stoprec:
        record()
        btnstop.config(state='normal')
        startbtn.config(state='disabled')

like this also change button state as normal for record again像这样也可以正常更改按钮状态以再次记录

here is complete code这是完整的代码

from tkinter import *
import sounddevice as sd
from scipy.io.wavfile import write

win = Tk()
win.title("Voice Recording")
win.geometry('400x500')
win.resizable(False, False)

stoprec = False
time = 0


def start(startbtn):
    freq = 44100
    startbtn.config(state='disabled')
    lst = []

    def stop():
        global stoprec
        stoprec = True
        btnstop.config(state='disabled')
        startbtn.config(state='normal')

    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)

    if stoprec:
        record()
        btnstop.config(state='normal')
        startbtn.config(state='disabled')


lbl = Label(win, text="start")
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()

here is output of UI这是 UI 的输出

Recording记录

开始录制

Stoped Recording停止录制

录制停止

output File .wav输出文件.wav

文件已保存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM