32 lines
942 B
Python
32 lines
942 B
Python
import sounddevice as sd
|
|
import numpy as np
|
|
import wave, os, time
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
def record_audio(duration, filename):
|
|
# Sampling frequency
|
|
fs = 44100
|
|
|
|
# Start recording
|
|
print("Recording...")
|
|
recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=np.int16)
|
|
sd.wait() # Wait until recording is finished
|
|
print("Recording finished")
|
|
|
|
# Save as WAV file
|
|
with wave.open(filename, 'wb') as wf:
|
|
wf.setnchannels(2)
|
|
wf.setsampwidth(2)
|
|
wf.setframerate(fs)
|
|
wf.writeframes(recording.tobytes())
|
|
|
|
if __name__ == "__main__":
|
|
ROOT_DIR = os.getenv('ROOT_DIR', './')
|
|
for i in range(1, 5):
|
|
print(f"Recording {i}...")
|
|
duration = 5 # seconds
|
|
filename = os.path.join(ROOT_DIR, 'llm_media/recording{i}.wav')
|
|
record_audio(duration, filename)
|
|
print(f"Saved as {filename}")
|
|
print("Break for 3 seconds...")
|
|
time.sleep(3) |