-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecording_audio.py
More file actions
52 lines (41 loc) · 1.42 KB
/
recording_audio.py
File metadata and controls
52 lines (41 loc) · 1.42 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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 7 18:49:32 2023
@author: Jorge
"""
import pyaudio
import wave
import numpy as np
def recording(wav_output_filename):
form_1 = pyaudio.paInt16
chans = 1 # 1 channel
samp_rate = 44100
chunk = 1024
record_secs = 5
#dev_index = 2
wav_output_filename = wav_output_filename # name of .wav file
audio = pyaudio.PyAudio() # create pyaudio instantiation
print('You will have '+ str(record_secs) +' seconds to record')
print('Start speaking when you see the message recording')
# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, input = True,frames_per_buffer=chunk)
print("recording")
frames = []
# loop through stream and append audio chunks to frame array
for ii in range(0,int((samp_rate/chunk)*record_secs)):
data = stream.read(chunk)
frames.append(data)
print("finished recording")
# stop the stream, close it, and terminate the pyaudio instantiation
stream.stop_stream()
stream.close()
audio.terminate()
# save the audio frames as .wav file
wavefile = wave.open(wav_output_filename,'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()
return True
#recording('prueba.wav')