Skip to content

Commit b60659a

Browse files
committed
add voice recorder gui app tutorial
1 parent efdbb34 commit b60659a

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
219219
- [How to Record a Specific Window in Python](https://www.thepythoncode.com/article/record-a-specific-window-in-python). ([code](python-for-multimedia/record-specific-window))
220220
- [How to Add Audio to Video in Python](https://www.thepythoncode.com/article/add-audio-to-video-in-python). ([code](python-for-multimedia/add-audio-to-video))
221221
- [How to Compress Images in Python](https://www.thepythoncode.com/article/compress-images-in-python). ([code](python-for-multimedia/compress-image))
222+
- [How to Build a GUI Voice Recorder App in Python](https://www.thepythoncode.com/article/make-a-gui-voice-recorder-python). ([code](python-for-multimedia/voice-recorder-app))
222223

223224
- ### [Web Programming](https://www.thepythoncode.com/topic/web-programming)
224225
- [Detecting Fraudulent Transactions in a Streaming Application using Kafka in Python](https://www.thepythoncode.com/article/detect-fraudulent-transactions-with-apache-kafka-in-python). ([code](general/detect-fraudulent-transactions))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Build a GUI Voice Recorder App in Python](https://www.thepythoncode.com/article/make-a-gui-voice-recorder-python)
23.2 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scipy
2+
sounddevice
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# importing everything from tkinter
2+
from tkinter import *
3+
# importing the styling module ttk from tkinter
4+
from tkinter import ttk
5+
# importing the message boxes from tkinter
6+
from tkinter.messagebox import showinfo, showerror, askokcancel
7+
# this is for recording the actual voice
8+
import sounddevice
9+
# this is for saving the recorded file to wav file format
10+
from scipy.io.wavfile import write
11+
# threading will help the app's tasks run concurrently
12+
import threading
13+
# importing datetime from datetime to handle dates
14+
from datetime import datetime
15+
# this will handle time
16+
import time
17+
# os module will be used for renaming files
18+
import os
19+
20+
21+
# the function for closing the main window
22+
def close_window():
23+
# here we are checking if the value of askokcancel is True
24+
if askokcancel(title='Close Voice Recorder', message='Are you sure you want to close the Voice Recorder?'):
25+
# this kills the window
26+
window.destroy()
27+
28+
29+
30+
# function for recording sound
31+
def record_voice():
32+
# the try statement is for
33+
try:
34+
# this is the frequence at which the record will happen
35+
freq = 44100
36+
# getting the recording duration from the entry
37+
duration = int(duration_entry.get())
38+
# calling the recorder via the rec() function
39+
recording = sounddevice.rec(duration*freq, samplerate=freq, channels=2)
40+
# declaring the counter
41+
counter = 0
42+
# the loop is for displaying the recording progress
43+
while counter < duration:
44+
# updating the window
45+
window.update()
46+
# this will help update the window after every 1 second
47+
time.sleep(1)
48+
# incrementing the counter by 1
49+
counter += 1
50+
# displaying the recording duration
51+
progress_label.config(text=str(counter))
52+
# this records audio for the specified duration
53+
sounddevice.wait()
54+
# writing the audio data to recording.wav
55+
write('recording.wav', freq, recording)
56+
# looping through all the files in the current folder
57+
for file in os.listdir():
58+
# checking if the file name is recording.wav
59+
if file == 'recording.wav':
60+
# spliting the base and the extension
61+
base, ext = os.path.splitext(file)
62+
# getting current time
63+
current_time = datetime.now()
64+
# creating a new name for the recorded file
65+
new_name = 'recording-' + str(current_time.hour) + '.' + str(current_time.minute) + '.' + str(current_time.second) + ext
66+
# renaming the file
67+
os.rename(file, new_name)
68+
# display a message when recording is done
69+
showinfo('Recording complete', 'Your recording is complete')
70+
# function for catching all errors
71+
except:
72+
# display a message when an error is caught
73+
showerror(title='Error', message='An error occurred' \
74+
'\nThe following could ' \
75+
'be the causes:\n->Bad duration value\n->An empty entry field\n' \
76+
'Do not leave the entry empty and make sure to enter a valid duration value')
77+
78+
# the function to run record_voice as a thread
79+
def recording_thread():
80+
# creating the thread whose target is record_voice()
81+
t1 = threading.Thread(target=record_voice)
82+
# starting the thread
83+
t1.start()
84+
85+
86+
87+
88+
# creates the window using Tk() fucntion
89+
window = Tk()
90+
91+
# this will listen to the close window event
92+
window.protocol('WM_DELETE_WINDOW', close_window)
93+
94+
# creates title for the window
95+
window.title('Voice Recorder')
96+
97+
# the icon for the application, this replaces the default icon
98+
window.iconbitmap(window, 'voice_recorder_icon.ico')
99+
100+
# dimensions and position of the window
101+
window.geometry('500x450+440+180')
102+
# makes the window non-resizable
103+
window.resizable(height=FALSE, width=FALSE)
104+
105+
106+
"""Styles for the widgets"""
107+
# style for the label
108+
label_style = ttk.Style()
109+
label_style.configure('TLabel', foreground='#000000', font=('OCR A Extended', 15))
110+
111+
# style for the entry
112+
entry_style = ttk.Style()
113+
entry_style.configure('TEntry', font=('Dotum', 15))
114+
115+
# style for the button
116+
button_style = ttk.Style()
117+
button_style.configure('TButton', foreground='#000000', font='DotumChe')
118+
119+
120+
# creates the canvas for containing all the widgets
121+
canvas = Canvas(window, width=500, height=400)
122+
canvas.pack()
123+
124+
# loading the logo
125+
logo = PhotoImage(file='recorder.png')
126+
# creates dimensions of the logo
127+
logo = logo.subsample(2, 2)
128+
# adding the logo to the canvas
129+
canvas.create_image(240, 135, image=logo)
130+
131+
132+
# creating a ttk label
133+
duration_label = ttk.Label(window, text='Enter Recording Duration in Seconds:', style='TLabel')
134+
# creating a ttk entry
135+
duration_entry = ttk.Entry(window, width=76, style='TEntry')
136+
137+
# adding the label to the canvas
138+
canvas.create_window(235, 290, window=duration_label)
139+
# adding the entry to the canvas
140+
canvas.create_window(250, 315, window=duration_entry)
141+
142+
143+
# creating the empty label for displaying download progress
144+
progress_label = ttk.Label(window, text='')
145+
# creating the button
146+
record_button = ttk.Button(window, text='Record', style='TButton', command=recording_thread)
147+
148+
# adding the label to the canvas
149+
canvas.create_window(240, 365, window=progress_label)
150+
# adding the button to the canvas
151+
canvas.create_window(240, 410, window=record_button)
152+
153+
154+
# this will run the main window infinetly
155+
window.mainloop()
Binary file not shown.

0 commit comments

Comments
 (0)