-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_mic.py
More file actions
96 lines (76 loc) · 3.55 KB
/
record_mic.py
File metadata and controls
96 lines (76 loc) · 3.55 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pyaudio
import wave
# (constants) -------------------------
MONO_CHANNEL = 1;
STEREO_CHANNEL = 2;
FRAMES_PER_BUFFER = 3200;
FORMAT_T = pyaudio.paInt16;
CHANNELS = MONO_CHANNEL;
RATE = 16000;
FILE_PATH = "./assets/"
FILE_NAME = "ouput";
FILE_FMT = ".wav";
# --------------------------------------
def InitPyAudio() -> pyaudio.PyAudio:
p: pyaudio.PyAudio = pyaudio.PyAudio(); # Initialize PyAudio object
return p;
def InitAudioStream(
p: pyaudio.PyAudio, # PyAudio Object
fmt: int, # Bytes per frame
audio_channels: int, # Channel of audio
framrate: int, # Number of sample frames per second
audio_input: bool, # Speaker/Microphone .. input state
audio_fbuffer: int # Frame chunks per buffer to be read
) -> pyaudio.PyAudio.Stream:
# Initializing stream object
stream: pyaudio.PyAudio.Stream = p.open(
format = fmt,
channels = audio_channels,
rate = framrate,
input = audio_input,
frames_per_buffer = audio_fbuffer
);
return stream;
def StopPyAudio(p: pyaudio.PyAudio) -> None:
p.terminate();
def StopStream(stream: pyaudio.PyAudio.Stream) -> None:
stream.stop_stream();
stream.close();
def StartRecording(
p: pyaudio.PyAudio, # PyAudio Object
stream: pyaudio.PyAudio.Stream, # PyAudio Stream Object
framerate: int, # Number of sample frames per second
frame_per_buffer: int, # Frames chunks per buffer
rec_ts: int # Record time in seconds
) -> list[bytes]:
frames: list[bytes] = [];
# (framerate/frame_per_buffer) -> audio time wrt framerate
# audio time * rec_ts -> loops rec_ts times the audio time
for i in range(0, int((framerate/frame_per_buffer)*rec_ts)):
chunks: bytes = stream.read(frame_per_buffer); # frames_per_buffer bytes read from input stream
frames.append(chunks);
StopStream(stream);
StopPyAudio(p);
return frames;
def CreateWav(
save_path: str, # Output wave file path
p: pyaudio.PyAudio, # PyAudio Object
channels: int, # wave audio channels
fmt: int, # bytes per frame
framerate: int, # Number of sample frames per second
frames: list[bytes] # list of frames to write
) -> None:
fwav = wave.open(save_path, "wb"); # Open wave audio file for writing in binary mode
fwav.setnchannels(channels); # Setting the audio channels
fwav.setsampwidth(p.get_sample_size(fmt)); # Setting up the bytes per frame is taking
fwav.setframerate(framerate); # Number of sample frames in one second
fwav.writeframes(b"".join(frames)); # Writing the frames in binary
fwav.close(); # Closing the wave file
def main() -> None:
p: pyaudio.PyAudio = InitPyAudio();
stream: pyaudio.PyAudio.Stream = InitAudioStream(p, FORMAT_T, CHANNELS, RATE, True, FRAMES_PER_BUFFER);
print("Recording...");
frames: list[bytes] = StartRecording(p, stream, RATE, FRAMES_PER_BUFFER, 5);
CreateWav(FILE_PATH+FILE_NAME+FILE_FMT, p, CHANNELS, FORMAT_T, RATE, frames);
if ("__main__" == __name__):
main();