81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
import argparse
|
|
import datetime
|
|
import os
|
|
import sys
|
|
|
|
try:
|
|
import sounddevice as sd
|
|
import numpy as np
|
|
from scipy.io.wavfile import write
|
|
except ImportError:
|
|
print("오류: sounddevice, numpy, scipy 패키지가 필요합니다.")
|
|
print("uv pip install sounddevice numpy scipy 를 실행해주세요.")
|
|
sys.exit(1)
|
|
|
|
def list_devices():
|
|
"""시스템에 연결된 오디오 입력 디바이스의 목록을 출력합니다."""
|
|
print("=== 시스템 오디오 디바이스 목록 ===")
|
|
print(sd.query_devices())
|
|
print("================================")
|
|
print("녹음에 사용할 입력 디바이스의 인덱스 번호를 확인하세요.")
|
|
|
|
def record_audio(device_index, duration=None, samplerate=16000):
|
|
"""
|
|
지정된 디바이스 인덱스에서 오디오를 캡처하여 WAV 파일로 저장합니다.
|
|
디바이스는 주로 USB 사운드카드 (Line-In)를 타겟팅합니다.
|
|
"""
|
|
try:
|
|
device_info = sd.query_devices(device_index, 'input')
|
|
except Exception as e:
|
|
print(f"디바이스 초기화 오류: {e}")
|
|
return
|
|
|
|
print(f"[{device_info['name']}] 디바이스에서 녹음을 시작합니다.")
|
|
os.makedirs("data/samples", exist_ok=True)
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
filename = f"data/samples/sample_{timestamp}.wav"
|
|
|
|
if duration:
|
|
print(f"{duration}초 동안 녹음합니다...")
|
|
recording = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, device=device_index, dtype='int16')
|
|
sd.wait()
|
|
write(filename, samplerate, recording)
|
|
print(f"저장 완료: {filename}")
|
|
else:
|
|
print("무제한 녹음 모드입니다. 종료하려면 Ctrl+C 키를 누르세요.")
|
|
audio_data = []
|
|
|
|
def callback(indata, frames, time, status):
|
|
if status:
|
|
print(status, file=sys.stderr)
|
|
audio_data.append(indata.copy())
|
|
|
|
try:
|
|
with sd.InputStream(samplerate=samplerate, channels=1, device=device_index, dtype='int16', callback=callback):
|
|
while True:
|
|
sd.sleep(1000)
|
|
except KeyboardInterrupt:
|
|
print("\n사용자에 의해 녹음이 중단되었습니다. 파일을 저장합니다...")
|
|
if audio_data:
|
|
recording = np.concatenate(audio_data, axis=0)
|
|
write(filename, samplerate, recording)
|
|
print(f"저장 완료: {filename}")
|
|
else:
|
|
print("저장할 오디오 데이터가 없습니다.")
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="HUTAMS 현장 무전기 Line-in 녹음 툴")
|
|
parser.add_argument("--list-devices", action="store_true", help="시스템에 연결된 모든 디바이스 목록 출력")
|
|
parser.add_argument("--device", type=int, help="녹음에 사용할 오디오 디바이스 인덱스", default=None)
|
|
parser.add_argument("--duration", type=int, help="녹음 길이 (초 단위, 생략시 무제한)", default=None)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.list_devices:
|
|
list_devices()
|
|
elif args.device is not None:
|
|
record_audio(args.device, args.duration)
|
|
else:
|
|
print("사용법: --device [인덱스] 를 지정하거나 --list-devices 로 디바이스 번호를 확인하세요.")
|
|
list_devices()
|