import sounddevice as sd import numpy as np from scipy.io.wavfile import write import keyboard import os import time # ================= 설정값 (이 부분만 바꿔서 쓰세요) ================= emotion = "SURPRISED" # 녹음할 감정 (파일명과 폴더명에 대소문자 정확히 일치해야 함) # ================================================================= # 1. 경로 설정 (보여주신 폴더 구조에 맞춤) # 현재 스크립트(recorder.py)가 있는 위치: .../senceVoice dataset/scripts/ current_dir = os.path.dirname(os.path.abspath(__file__)) # 대본 파일 경로: 같은 폴더 내 (예: .../scripts/ANGRY.txt) txt_file = os.path.join(current_dir, f"{emotion}.txt") # 저장될 폴더 경로: 상위 폴더로 한 번 나간 후 wav_dataset으로 진입 # (예: .../wav_dataset/ANGRY/) save_folder = os.path.join(current_dir, "..", "wav_dataset", emotion) # 샘플링 레이트 (SenseVoice 등 일반적인 음성 모델용) fs = 24000 # ================================================================= # 저장 폴더가 없으면 에러 방지를 위해 생성 (이미 만들어두셨으니 패스하겠지만 안전장치) if not os.path.exists(save_folder): os.makedirs(save_folder) print(f"알림: '{save_folder}' 폴더를 생성했습니다.") # 대본 파일 읽기 if not os.path.exists(txt_file): print(f"오류: 대본 파일 '{txt_file}'을 찾을 수 없습니다.") exit() with open(txt_file, 'r', encoding='utf-8') as f: lines = [line.strip() for line in f.readlines() if line.strip()] print(f"\n=== [{emotion}] 녹음 시작 (총 {len(lines)}문장) ===") print(f"저장 경로: {save_folder}") print("사용법: 'Spacebar'를 누르고 있는 동안 녹음됩니다. 떼면 저장됩니다.") print("중단하려면 'ESC'를 누르세요.\n") for i, text in enumerate(lines): # 파일명 포맷: 감정_001.wav, 감정_002.wav ... filename = f"{emotion}_{i + 1:03d}.wav" filepath = os.path.join(save_folder, filename) # 이어하기 기능: 이미 파일이 존재하면 건너뜀 if os.path.exists(filepath): print(f"[Skip] {filename} 이미 존재함 ({i + 1}/{len(lines)})") continue print(f"\n[{i + 1}/{len(lines)}] 문장 읽으세요:") print(f"👉 \033[96m{text}\033[0m") # 문장 강조 (하늘색) print("(Spacebar를 꾹 누르고 말하세요...)") # 녹음 데이터 담을 리스트 recording = [] # 키 입력 대기 루프 while True: if keyboard.is_pressed('esc'): print("\n녹음 프로그램을 종료합니다.") exit() if keyboard.is_pressed('space'): break time.sleep(0.01) # 녹음 시작 (Spacebar 누르고 있는 동안) def callback(indata, frames, time, status): if status: print(status) recording.append(indata.copy()) # Spacebar가 눌려있는 동안 계속 녹음 with sd.InputStream(samplerate=fs, channels=1, callback=callback): while keyboard.is_pressed('space'): sd.sleep(10) # 녹음 종료 및 저장 if recording: my_recording = np.concatenate(recording, axis=0) write(filepath, fs, my_recording) print(f"✅ 저장 완료: {filename}") else: print("⚠️ 녹음된 내용이 없습니다. 다시 시도하세요.") i -= 1 # 다시 하도록 인덱스 조정 (for문이라 완벽하진 않지만 다음 루프에서 처리됨) # 너무 빨리 넘어가면 실수할 수 있으니 0.5초 대기 time.sleep(0.5) print("\n=== 모든 녹음이 완료되었습니다! 고생하셨습니다. ===")