File size: 3,784 Bytes
9817056 |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
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=== ๋ชจ๋ ๋
น์์ด ์๋ฃ๋์์ต๋๋ค! ๊ณ ์ํ์
จ์ต๋๋ค. ===") |