File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ You can go through the tutorial https://www.roytuts.com/python-voice-recording-through-microphone-for-arbitrary-time-using-pyaudio/
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments