Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ac1802

Browse files
authored
Add files via upload
1 parent 3249676 commit 0ac1802

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

python-record-my-voice/readme.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/python-voice-recording-through-microphone-for-arbitrary-time-using-pyaudio/

python-record-my-voice/record.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pyaudio
2+
import wave
3+
4+
CHUNK = 1024
5+
FORMAT = pyaudio.paInt16
6+
CHANNELS = 2
7+
RATE = 44100
8+
9+
def record():
10+
11+
p = pyaudio.PyAudio()
12+
13+
stream = p.open(format=FORMAT,
14+
channels=CHANNELS,
15+
rate=RATE,
16+
input=True,
17+
frames_per_buffer=CHUNK)
18+
19+
print("Start recording")
20+
21+
frames = []
22+
23+
try:
24+
while True:
25+
data = stream.read(CHUNK)
26+
frames.append(data)
27+
except KeyboardInterrupt:
28+
print("Done recording")
29+
except Exception as e:
30+
print(str(e))
31+
32+
sample_width = p.get_sample_size(FORMAT)
33+
34+
stream.stop_stream()
35+
stream.close()
36+
p.terminate()
37+
38+
return sample_width, frames
39+
40+
def record_to_file(file_path):
41+
wf = wave.open(file_path, 'wb')
42+
wf.setnchannels(CHANNELS)
43+
sample_width, frames = record()
44+
wf.setsampwidth(sample_width)
45+
wf.setframerate(RATE)
46+
wf.writeframes(b''.join(frames))
47+
wf.close()
48+
49+
if __name__ == '__main__':
50+
print('#' * 80)
51+
print("Please speak word(s) into the microphone")
52+
print('Press Ctrl+C to stop the recording')
53+
54+
record_to_file('output.wav')
55+
56+
print("Result written to output.wav")
57+
print('#' * 80)

0 commit comments

Comments
 (0)