0

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.

  • The official example application [rec_gui.py](https://github.com/spatialaudio/python-sounddevice/blob/master/examples/rec_gui.py) provides a `tkinter`-based GUI with a "record" and "stop" button that does exactly what you describe. – Matthias Jul 17 '22 at 08:21

1 Answers1

0

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

Recording

Recording started

Stoped Recording

Recording Stoped

output File .wav

File saved

Ramesh
  • 504
  • 5
  • 9