-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_audio.py
More file actions
29 lines (22 loc) · 1009 Bytes
/
Copy pathrecord_audio.py
File metadata and controls
29 lines (22 loc) · 1009 Bytes
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
#record_audio.py
import sounddevice as sd
import scipy.io.wavfile as wav
import numpy as np
import time
import datetime
import sys
def record_audio(duration, filename="cpu_leakage.wav", samplerate=44100):
"""Records audio for the specified duration and saves it to a file."""
print(f"[{datetime.datetime.now()}] Recording started: {filename}")
recording = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='float32')
sd.wait() # Block until recording finishes
# Save as 16-bit PCM WAV
wav.write(filename, samplerate, (recording * 32767).astype(np.int16))
print(f"[{datetime.datetime.now()}] Recording saved as {filename}")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python3 record_audio.py <duration> <filename>")
sys.exit(1)
duration = int(sys.argv[1]) # Get duration from command-line argument
filename = sys.argv[2] # Get filename from command-line argument
record_audio(duration, filename)