forked from python-geeks/Automation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_recorder.py
64 lines (53 loc) · 1.63 KB
/
audio_recorder.py
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
import threading
import tkinter as tk
import wave
import pyaudio
class App:
chunk = 1024
sample_format = pyaudio.paInt16
channels = 2
fs = 44100
frames = []
def __init__(self, master):
self.isrecording = False
self.button1 = tk.Button(
main, text="Start", width=6, command=self.startrecording
)
self.button2 = tk.Button(main, text="Stop", width=6, command=self.stoprecording)
self.button1.pack()
self.button2.pack()
def startrecording(self):
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format=self.sample_format,
channels=self.channels,
rate=self.fs,
frames_per_buffer=self.chunk,
input=True,
)
self.isrecording = True
self.button1["state"] = "disable"
print("Recording")
t = threading.Thread(target=self.record)
t.start()
def stoprecording(self):
self.isrecording = False
print("Recording Completed")
self.filename = input("Name the file?")
self.filename = self.filename + ".wav"
wf = wave.open(self.filename, "wb")
wf.setnchannels(self.channels)
wf.setsampwidth(self.p.get_sample_size(self.sample_format))
wf.setframerate(self.fs)
wf.writeframes(b"".join(self.frames))
wf.close()
main.destroy()
def record(self):
while self.isrecording:
data = self.stream.read(self.chunk)
self.frames.append(data)
main = tk.Tk()
main.title("Audio Recorder")
main.minsize(width=250, height=70)
app = App(main)
main.mainloop()