Spaces:
Running
Running
Surf Studio
Surf Studio
Surf AI
Surf Studio
Surf AI
Surf Studio
Squash history to free LFS storage (1 GB Space limit); current tree kept: sets 5/6 + set 7 manifest/source
8b1c056 Download source/studio/audio.py from Ethscriptions/POLS: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/spaces/Ethscriptions/POLS/resolve/main/source/studio/audio.py
- Command line
-
hf download hf://spaces/Ethscriptions/POLS/source/studio/audio.py
-
curl -L -o audio.py https://huggingface.co/spaces/Ethscriptions/POLS/resolve/main/source/studio/audio.py
5.03 kB
| """Sound design + TTS helpers (48 kHz stereo float32).""" | |
| import asyncio, os, subprocess, hashlib | |
| import numpy as np | |
| SR = 48000 | |
| CACHE = '/tmp/work/tts' | |
| os.makedirs(CACHE, exist_ok=True) | |
| def tts(text, voice, rate='+0%', pitch='+0Hz'): | |
| """Return trimmed mono float32 voice clip.""" | |
| import edge_tts | |
| tag = hashlib.md5(f"{text}|{voice}|{rate}|{pitch}".encode()).hexdigest()[:12] | |
| mp3 = f"{CACHE}/{tag}.mp3" | |
| if not os.path.exists(mp3) or os.path.getsize(mp3) < 2000: | |
| for _ in range(4): | |
| try: | |
| asyncio.run(edge_tts.Communicate(text, voice, rate=rate, pitch=pitch).save(mp3)) | |
| if os.path.getsize(mp3) > 2000: | |
| break | |
| except Exception as e: # network hiccup | |
| print('tts retry', e) | |
| raw = subprocess.run(['ffmpeg', '-v', 'error', '-i', mp3, '-f', 'f32le', '-ac', '1', '-ar', str(SR), '-'], | |
| check=True, capture_output=True).stdout | |
| x = np.frombuffer(raw, dtype=np.float32).astype(np.float64) | |
| env = np.convolve(np.abs(x), np.ones(480) / 480, mode='same') | |
| idx = np.where(env > 0.012)[0] | |
| if len(idx): | |
| a = max(0, idx[0] - 720) | |
| b = min(len(x), idx[-1] + 2400) | |
| x = x[a:b] | |
| f = min(480, len(x) // 4) | |
| x[:f] *= np.linspace(0, 1, f) | |
| x[-f:] *= np.linspace(1, 0, f) | |
| return x / (np.abs(x).max() + 1e-6) * 0.92 | |
| def _t(d): | |
| return np.arange(int(SR * d)) / SR | |
| def _onepole_lp(x, fc): | |
| """Time-varying one-pole low-pass (vectorised in blocks).""" | |
| out = np.empty_like(x) | |
| y = 0.0 | |
| blk = 64 | |
| for s in range(0, len(x), blk): | |
| a = np.exp(-2 * np.pi * fc[s] / SR) | |
| seg = x[s:s + blk] | |
| for i, v in enumerate(seg): | |
| y = (1 - a) * v + a * y | |
| out[s + i] = y | |
| return out | |
| def whoosh(d=0.45): | |
| t = _t(d) | |
| rng = np.random.default_rng(3) | |
| n = rng.standard_normal(len(t)) | |
| fc_hi = 500 + 6500 * (t / d) ** 1.4 | |
| fc_lo = 150 + 1800 * (t / d) ** 1.4 | |
| band = _onepole_lp(n, fc_hi) - _onepole_lp(n, fc_lo) | |
| env = np.sin(np.pi * np.clip(t / d, 0, 1) ** 0.7) ** 2 | |
| out = band * env | |
| return out / (np.abs(out).max() + 1e-6) * 0.42 | |
| def tick(i): | |
| """Plucky marimba-ish tick; pitch climbs a pentatonic scale across the word.""" | |
| scale = [0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24, 26, 28, 31, 33, 36] | |
| f = 587.33 * 2 ** (scale[min(i, len(scale) - 1)] / 12) | |
| t = _t(0.18) | |
| s = np.sin(2 * np.pi * f * t) * 0.8 + np.sin(2 * np.pi * f * 4 * t) * 0.15 * np.exp(-t * 70) | |
| s *= np.exp(-t * 26) * (1 - np.exp(-t * 2000)) | |
| return s * 0.30 | |
| def chime(): | |
| out = np.zeros(int(SR * 1.4)) | |
| for k, (f, dt) in enumerate([(1046.5, 0.0), (1318.5, 0.055), (1568.0, 0.11), (2093.0, 0.165)]): | |
| t = _t(1.4 - dt) | |
| tone = np.sin(2 * np.pi * f * t) + 0.35 * np.sin(2 * np.pi * f * 2.76 * t) * np.exp(-t * 9) \ | |
| + 0.15 * np.sin(2 * np.pi * f * 5.4 * t) * np.exp(-t * 16) | |
| tone *= np.exp(-t * 4.0) * (1 - np.exp(-t * 900)) | |
| s = int(SR * dt) | |
| out[s:s + len(tone)] += tone * (0.9 - k * 0.12) | |
| return out / np.abs(out).max() * 0.26 | |
| def pop(): | |
| t = _t(0.12) | |
| f = 1100 * np.exp(-t * 22) + 260 | |
| ph = 2 * np.pi * np.cumsum(f) / SR | |
| return np.sin(ph) * np.exp(-t * 32) * (1 - np.exp(-t * 3000)) * 0.32 | |
| def shimmer(d=1.0): | |
| rng = np.random.default_rng(11) | |
| out = np.zeros(int(SR * d)) | |
| for _ in range(30): | |
| st = rng.uniform(0, d * 0.55) | |
| f = rng.uniform(2800, 6800) | |
| t = _t(0.2) | |
| g = np.sin(2 * np.pi * f * t) * np.exp(-t * 28) * rng.uniform(0.2, 0.5) | |
| s = int(st * SR) | |
| n = min(len(g), len(out) - s) | |
| out[s:s + n] += g[:n] | |
| return out * np.exp(-_t(d) * 2.4) * 0.16 | |
| class Mixer: | |
| def __init__(self, dur): | |
| self.buf = np.zeros((int(SR * (dur + 2)), 2), dtype=np.float64) | |
| def add(self, clip, at, gain=1.0, pan=0.0): | |
| s = int(round(at * SR)) | |
| n = min(len(clip), len(self.buf) - s) | |
| if n <= 0: | |
| return | |
| l = np.cos((pan + 1) * np.pi / 4) * np.sqrt(2) | |
| r = np.sin((pan + 1) * np.pi / 4) * np.sqrt(2) | |
| self.buf[s:s + n, 0] += clip[:n] * gain * l | |
| self.buf[s:s + n, 1] += clip[:n] * gain * r | |
| def render(self, dur, path): | |
| b = self.buf[:int(round(SR * dur))].copy() | |
| b = np.tanh(b * 1.1) / np.tanh(1.1) | |
| fade = int(SR * 0.06) | |
| b[-fade:] *= np.linspace(1, 0, fade)[:, None] | |
| b[:240] *= np.linspace(0, 1, 240)[:, None] | |
| raw = path + '.f32' | |
| b.astype('<f4').tofile(raw) | |
| subprocess.run(['ffmpeg', '-v', 'error', '-y', '-f', 'f32le', '-ar', str(SR), '-ac', '2', '-i', raw, | |
| '-c:a', 'pcm_s16le', path], check=True) | |
| os.remove(raw) | |
| def envelope(clip, fps=30): | |
| hop = SR // fps | |
| n = len(clip) // hop + 1 | |
| e = np.array([np.sqrt(np.mean(clip[i * hop:(i + 1) * hop] ** 2) + 1e-12) if len(clip[i * hop:(i + 1) * hop]) else 0 | |
| for i in range(n)]) | |
| return np.clip(e / (e.max() + 1e-9), 0, 1) | |