namin72's picture
Upload 719 files
9817056 verified
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=== ๋ชจ๋“  ๋…น์Œ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ===")