forked from MaheepTulsian/Video_Conference_Automation_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_audio.py
63 lines (48 loc) · 1.93 KB
/
record_audio.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
import pyaudio
import wave
import keyboard
import threading
def record_audio(output_file="recording.wav", sample_rate=44100, chunk=1024, channels=2):
"""
Records audio and saves it as a WAV file. Recording stops when the Enter key is pressed.
Parameters:
- output_file (str): The name of the output WAV file.
- sample_rate (int): The sample rate of the recording.
- chunk (int): The number of frames per buffer.
- channels (int): The number of audio channels (1 for mono, 2 for stereo).
"""
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Open a new stream for recording
stream = audio.open(format=pyaudio.paInt16, channels=channels,
rate=sample_rate, input=True,
frames_per_buffer=chunk)
print("Recording started. Press Enter to stop...")
# Store the recorded frames in a list
frames = []
# Function to record audio until Enter is pressed
def record():
while not keyboard.is_pressed('enter'):
data = stream.read(chunk)
frames.append(data)
# Start recording in a separate thread to allow key press detection
recording_thread = threading.Thread(target=record)
recording_thread.start()
# Wait until the recording thread finishes (Enter key is pressed)
recording_thread.join()
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
audio.terminate()
print("Recording finished.")
# Save the recorded data as a WAV file
with wave.open(output_file, 'wb') as wf:
wf.setnchannels(channels)
wf.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
wf.setframerate(sample_rate)
wf.writeframes(b''.join(frames))
print(f"Audio saved as {output_file}")
# Example usage
if __name__ == "__main__":
record_audio()