Download bgm_engine.py from VanyaJ/deck2video-skill: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/VanyaJ/deck2video-skill/resolve/main/bgm_engine.py
- Command line
-
hf download hf://VanyaJ/deck2video-skill/bgm_engine.py
-
curl -L -o bgm_engine.py https://huggingface.co/VanyaJ/deck2video-skill/resolve/main/bgm_engine.py
3.97 kB
| """ | |
| Standalone Ambient BGM Engine (`bgm_engine.py`) | |
| 1. Fetches & caches royalty-free GPL-3.0 ambient piano/focus tracks from DENO Music Library. | |
| 2. Falls back to procedural warm stereo ambient pad synthesis if offline/firewalled. | |
| Zero external workspace dependencies — 100% portable across Mac / Windows / Linux. | |
| """ | |
| from __future__ import annotations | |
| import urllib.request | |
| import wave | |
| from pathlib import Path | |
| import numpy as np | |
| DENO_PRESET_TRACKS = { | |
| "focused": ( | |
| "deno-bgm-001", | |
| "F-Major Glass Piano Soft Groove", | |
| "https://pub-5eb8d6eced784b6d934b539c312b02af.r2.dev/mp3/deno-bgm-f-major-glass-piano-soft-groove.mp3", | |
| ), | |
| "bright": ( | |
| "deno-bgm-002", | |
| "Bright Piano Chill House 03", | |
| "https://pub-5eb8d6eced784b6d934b539c312b02af.r2.dev/mp3/deno-bgm-bright-piano-chill-house-03.mp3", | |
| ), | |
| } | |
| def synth_offline_ambient_pad(out_wav: Path, duration_sec: float = 30.0, sr: int = 48000) -> Path: | |
| """Generates a warm Fmaj9 stereo ambient synth pad WAV when offline.""" | |
| out_wav.parent.mkdir(parents=True, exist_ok=True) | |
| dur = max(6.0, min(duration_sec, 90.0)) | |
| n = int(sr * dur) | |
| t = np.linspace(0.0, dur, n, endpoint=False) | |
| # Fmaj9 warm chord frequencies (F3, A3, C4, E4, G4) | |
| freqs = [174.61, 220.00, 261.63, 329.63, 392.00] | |
| amps = [0.32, 0.24, 0.22, 0.16, 0.12] | |
| left = np.zeros(n) | |
| right = np.zeros(n) | |
| for idx, (f, a) in enumerate(zip(freqs, amps)): | |
| lfo = 0.82 + 0.18 * np.sin(2.0 * np.pi * (0.09 + idx * 0.03) * t + idx) | |
| left += np.sin(2.0 * np.pi * f * t) * a * lfo | |
| right += np.sin(2.0 * np.pi * (f * 1.0012) * t + 0.4 * idx) * a * lfo | |
| fade = np.minimum(t / 1.5, 1.0) * np.minimum((dur - t) / 1.8, 1.0) | |
| left *= fade | |
| right *= fade | |
| peak = max(float(np.max(np.abs(left))), float(np.max(np.abs(right))), 1e-6) | |
| stereo = np.stack([left / peak * 0.65, right / peak * 0.65], axis=-1) | |
| pcm = np.clip(stereo * 32767.0, -32768, 32767).astype(np.int16) | |
| with wave.open(str(out_wav), "wb") as wf: | |
| wf.setnchannels(2) | |
| wf.setsampwidth(2) | |
| wf.setframerate(sr) | |
| wf.writeframes(pcm.tobytes()) | |
| return out_wav | |
| def get_or_create_bgm_source( | |
| cache_dir: Path, | |
| mood: str = "focused", | |
| duration_sec: float = 60.0, | |
| offline: bool = True, | |
| ) -> tuple[Path, str]: | |
| """Returns `(audio_path, title)` for ambient BGM using 100% local assets/synthesis by default.""" | |
| cache_dir.mkdir(parents=True, exist_ok=True) | |
| track_id, title, url = DENO_PRESET_TRACKS.get(mood.lower(), DENO_PRESET_TRACKS["focused"]) | |
| mp3_path = cache_dir / f"{track_id}.mp3" | |
| if mp3_path.exists() and mp3_path.stat().st_size > 4096: | |
| return mp3_path, title | |
| # In Zero-Egress Offline Mode (default), NEVER connect to external CDN; check any bundled local MP3/WAV first | |
| if offline: | |
| default_bundled_mp3 = cache_dir / "deno-bgm-001.mp3" | |
| if default_bundled_mp3.exists() and default_bundled_mp3.stat().st_size > 4096: | |
| return default_bundled_mp3, "F-Major Glass Piano Soft Groove (Bundled Local)" | |
| wav_fallback = cache_dir / "offline_ambient_pad.wav" | |
| if not wav_fallback.exists() or wav_fallback.stat().st_size < 4096: | |
| synth_offline_ambient_pad(wav_fallback, duration_sec=duration_sec) | |
| return wav_fallback, "Procedural Fmaj9 Ambient Pad" | |
| try: | |
| req = urllib.request.Request(url, headers={"User-Agent": "Deck2Video/4.2"}) | |
| with urllib.request.urlopen(req, timeout=15) as resp: | |
| data = resp.read() | |
| if len(data) > 4096: | |
| mp3_path.write_bytes(data) | |
| return mp3_path, title | |
| except Exception as exc: | |
| print(f"[BGM Info] Offline or CDN unreachable ({exc}); using procedural ambient pad.") | |
| wav_fallback = cache_dir / "offline_ambient_pad.wav" | |
| synth_offline_ambient_pad(wav_fallback, duration_sec=duration_sec) | |
| return wav_fallback, "Procedural Fmaj9 Ambient Pad" | |