#!/usr/bin/env python3 """효과음을 직접 합성합니다(외부 음원 없음, 저작권 걱정 없음). sfx/*.ogg 로 저장합니다. python3 tools/sfx.py """ import os import subprocess import wave import numpy as np SR = 44100 OUT = os.path.join(os.path.dirname(__file__), "..", "sfx") rng = np.random.default_rng(7) def t(sec): return np.arange(int(SR * sec)) / SR def env(n, attack=0.005, decay=0.3): x = np.arange(n) / SR a = np.clip(x / attack, 0, 1) return a * np.exp(-x / decay) def bell(freq, sec, decay): x = t(sec) # 맑은 종소리: 기본음 + 배음(조금 어긋난 비율) s = (np.sin(2 * np.pi * freq * x) + 0.45 * np.sin(2 * np.pi * freq * 2.01 * x) + 0.2 * np.sin(2 * np.pi * freq * 3.03 * x)) return s * env(len(x), 0.002, decay) def place(buf, sig, at): i = int(at * SR) end = min(len(buf), i + len(sig)) buf[i:end] += sig[: end - i] def coin(): buf = np.zeros(int(SR * 0.6)) place(buf, bell(1318.5, 0.6, 0.09), 0.0) # E6 place(buf, bell(1975.5, 0.55, 0.18), 0.07) # B6 return buf def buy(): # 도-미-솔-높은도 빠른 아르페지오 + 반짝임 buf = np.zeros(int(SR * 1.0)) for i, f in enumerate([523.25, 659.25, 783.99, 1046.5]): place(buf, bell(f, 0.7, 0.22), i * 0.075) sparkle = rng.standard_normal(int(SR * 0.4)) sparkle = np.convolve(sparkle, np.ones(3) / 3, "same") * env(len(sparkle), 0.01, 0.12) * 0.08 place(buf, sparkle * np.sin(2 * np.pi * 6000 * t(0.4)), 0.3) return buf def levelup(): # 확장 축하: 두 박자 팡파레(사각파 느낌을 배음으로) buf = np.zeros(int(SR * 1.6)) notes = [(523.25, 0.0, 0.15), (659.25, 0.15, 0.15), (783.99, 0.3, 0.15), (1046.5, 0.45, 0.6)] for f, at, d in notes: x = t(d + 0.4) s = sum(np.sin(2 * np.pi * f * k * x) / k for k in (1, 3, 5)) * 0.6 s += 0.5 * np.sin(2 * np.pi * f * 2 * x) e = env(len(x), 0.01, 0.35 if d > 0.3 else 0.12) place(buf, s * e, at) return buf def sizzle(sec=3.0): n = int(SR * sec) noise = rng.standard_normal(n) # 고역 위주(지글지글): 1차 고역 통과 hp = np.empty(n) a = 0.92 prev_x = prev_y = 0.0 for i in range(n): y = a * (prev_y + noise[i] - prev_x) hp[i] = y prev_x, prev_y = noise[i], y # 천천히 출렁이는 세기 x = t(sec) mod = 0.55 + 0.25 * np.sin(2 * np.pi * 0.7 * x) + 0.2 * np.sin(2 * np.pi * 1.9 * x + 1) s = hp * mod * 0.25 # 기름 튀는 소리(짧은 딱딱) for at in rng.uniform(0, sec, 70): click = rng.standard_normal(int(SR * 0.012)) * env(int(SR * 0.012), 0.0005, 0.003) place(s, click * rng.uniform(0.3, 0.9), at) # 이음매 없이 반복되게 앞뒤를 겹칩니다. fade = int(SR * 0.3) w = np.linspace(0, 1, fade) s[:fade] = s[:fade] * w + s[-fade:] * (1 - w) return s[:-fade] def pop(): # 요리 화면 톡톡: 통통 튀는 짧은 소리(높은 음에서 빠르게 내려감) x = t(0.12) f = 900 * np.exp(-x * 18) + 380 phase = 2 * np.pi * np.cumsum(f) / SR return np.sin(phase) * env(len(x), 0.001, 0.035) def save(name, sig): sig = sig / max(1e-9, np.max(np.abs(sig))) * 0.85 pcm = (sig * 32767).astype(np.int16) wav = os.path.join(OUT, name + ".wav") with wave.open(wav, "wb") as f: f.setnchannels(1) f.setsampwidth(2) f.setframerate(SR) f.writeframes(pcm.tobytes()) ogg = os.path.join(OUT, name + ".ogg") subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-i", wav, "-c:a", "libvorbis", "-q:a", "5", ogg], check=True) os.remove(wav) print(ogg) def main(): os.makedirs(OUT, exist_ok=True) save("coin", coin()) save("buy", buy()) save("levelup", levelup()) save("sizzle", sizzle()) save("pop", pop()) if __name__ == "__main__": main()