-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemo_GUI.py
More file actions
75 lines (61 loc) · 2 KB
/
Copy pathmemo_GUI.py
File metadata and controls
75 lines (61 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import wave
from dataclasses import dataclass
import pyaudio
import time
import threading
import tkinter as tk
class VoiceRecorder:
def __init__(self):
self.root = tk.Tk()
# self.root.title("Voice Recorder")
# self.root.geometry("300x100")
self.root.resizable(False, False)
# self.record_button = tk.Button(self.root, text="Record", command=self.record)
# self.record_button.pack(pady=10)
self.button = tk.Button(self.root, text="Record", font=("Arial", 120, "bold"), command=self.click_handler)
self.button.pack()
self.label = tk.Label(text="00:00:00", font=("Arial", 20))
self.label.pack()
self.recording = False
self.frames = []
self.root.mainloop()
def click_handler(self):
if self.recording:
self.recording = False
self.button.config(fg="black")
else:
self.recording = True
self.button.config(fg="red")
threading.Thread(target=self.record).start()
def record(self):
audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
frames = []
start_time = time.time()
while self.recording:
data = stream.read(1024)
frames.append(data)
# self.label.config(text=time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time)))
passed = time.time() - start_time
secs = passed % 60
mins = passed // 60
hours = mins // 60
self.label.config(text=f"{int(hours):02d}:{int(mins):02d}:{int(secs):02d}")
stream.stop_stream()
stream.close()
audio.terminate()
exists = True
i = 1
while exists:
if os.path.exists(f"input/recording{i}.wav"):
i += 1
else:
exists = False
sound_file = wave.open(f"input/recording{i}.wav", "wb")
sound_file.setnchannels(1)
sound_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
sound_file.setframerate(44100)
sound_file.writeframes(b"".join(frames))
sound_file.close()
VoiceRecorder()