jayaspjacob Claude Opus 4.8 (1M context) commited on
Commit
d07d1a1
·
1 Parent(s): 60cefb1

Use real public-domain music loops for the sound beds

Browse files

Add four FreePD (Kevin MacLeod, CC0/public-domain) loops as the background
beds — Ambient Drift (Ambient C Motion), Lo-fi Pulse (Chill Beat),
Cinematic Rise (Wonder Flow), Newsroom Bed (Action Investigation) — fetched
via scripts/build_music_loops.py and stored as compact mono FLAC (LFS).
tts/music.py now loads/loops the real track (crossfaded to episode length)
when present and falls back to the procedural numpy bed otherwise. Credits
added to the README.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

README.md CHANGED
@@ -68,3 +68,11 @@ huggingface-cli login
68
  huggingface-cli upload <user>/podify . --repo-type=space
69
  # or: git push to the Space remote (preset .wav files tracked via Git LFS)
70
  ```
 
 
 
 
 
 
 
 
 
68
  huggingface-cli upload <user>/podify . --repo-type=space
69
  # or: git push to the Space remote (preset .wav files tracked via Git LFS)
70
  ```
71
+
72
+ ## Credits / assets
73
+
74
+ - **Voice samples** (`tts/voices/`): derived from [CMU ARCTIC](http://festvox.org/cmu_arctic/)
75
+ (free for research and commercial use). Rebuild with `scripts/build_voice_samples.py`.
76
+ - **Background-music loops** (`tts/music_loops/`): [FreePD](https://freepd.com/) by Kevin
77
+ MacLeod — 100% public domain (CC0). Rebuild with `scripts/build_music_loops.py`.
78
+ A procedural numpy fallback in `tts/music.py` is used if the loops are absent.
scripts/build_music_loops.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Build background-music loops from FreePD (public-domain / CC0).
2
+
3
+ FreePD (Kevin MacLeod) releases its catalogue as 100% public-domain music, mirrored on the
4
+ Internet Archive (item `freepd`). We pull four mood tracks, trim each to a ~60s segment,
5
+ normalise, fade, and save a compact mono OGG loop used by tts/music.py. No attribution is
6
+ required (public domain), but FreePD is credited in the README.
7
+
8
+ Run from the repo root: python scripts/build_music_loops.py
9
+ OGG files are tracked via Git LFS (see .gitattributes).
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import io
15
+ import os
16
+ import urllib.parse
17
+ import urllib.request
18
+
19
+ import numpy as np
20
+ import soundfile as sf
21
+
22
+ IA_BASE = "https://archive.org/download/freepd"
23
+ TARGET_SR = 44100
24
+ SEG_START = 8.0 # skip any intro
25
+ SEG_LEN = 60.0
26
+ OUT_DIR = os.path.join("tts", "music_loops")
27
+
28
+ # bed id -> FreePD track (path within the IA item)
29
+ TRACKS = {
30
+ "ambient_drift": "Page2/Ambient C Motion.mp3",
31
+ "lofi_pulse": "Page2/Chill Beat.mp3",
32
+ "cinematic_rise": "Page2/Wonder Flow.mp3",
33
+ "newsroom_bed": "Page2/Action Investigation.mp3",
34
+ }
35
+
36
+
37
+ def _fetch(track: str) -> bytes:
38
+ url = IA_BASE + "/" + urllib.parse.quote(track)
39
+ req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
40
+ with urllib.request.urlopen(req, timeout=120) as r:
41
+ return r.read()
42
+
43
+
44
+ def _resample(x: np.ndarray, sr: int, target: int) -> np.ndarray:
45
+ if sr == target:
46
+ return x
47
+ n = int(round(len(x) * target / sr))
48
+ xp = np.linspace(0, 1, len(x), dtype=np.float64)
49
+ fp = np.linspace(0, 1, n, dtype=np.float64)
50
+ return np.interp(fp, xp, x).astype(np.float32)
51
+
52
+
53
+ def build():
54
+ os.makedirs(OUT_DIR, exist_ok=True)
55
+ for bed_id, track in TRACKS.items():
56
+ audio, sr = sf.read(io.BytesIO(_fetch(track)), dtype="float32")
57
+ if audio.ndim > 1:
58
+ audio = audio.mean(axis=1)
59
+ audio = _resample(audio, sr, TARGET_SR)
60
+
61
+ start = int(SEG_START * TARGET_SR)
62
+ if start + int(5 * TARGET_SR) >= len(audio):
63
+ start = 0
64
+ seg = audio[start:start + int(SEG_LEN * TARGET_SR)]
65
+
66
+ peak = float(np.abs(seg).max()) or 1.0
67
+ seg = (seg / peak * 0.9).astype(np.float32)
68
+
69
+ f = int(1.0 * TARGET_SR)
70
+ if len(seg) > 2 * f:
71
+ seg[:f] *= np.linspace(0, 1, f, dtype=np.float32)
72
+ seg[-f:] *= np.linspace(1, 0, f, dtype=np.float32)
73
+
74
+ # FLAC: stable libsndfile write + lossless/compact. Loops are read server-side
75
+ # only (mixed into the speech), so browser codec support is irrelevant.
76
+ out = os.path.join(OUT_DIR, f"{bed_id}.flac")
77
+ sf.write(out, seg, TARGET_SR, format="FLAC", subtype="PCM_16")
78
+ print(f" {bed_id:16s} <- {track} {len(seg)/TARGET_SR:.1f}s {os.path.getsize(out)//1024}KB")
79
+
80
+
81
+ if __name__ == "__main__":
82
+ build()
83
+ print("done.")
tts/music.py CHANGED
@@ -8,11 +8,65 @@ kick pulses. Each ``render_bed`` returns a mono float32 track at the speech samp
8
 
9
  from __future__ import annotations
10
 
 
 
 
11
  import numpy as np
12
 
13
  # Linear gain applied to the (peak-normalised) bed when mixed under speech.
14
  MUSIC_GAIN = 0.14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def _sine(freq: float, n: int, sr: int, phase: float = 0.0) -> np.ndarray:
18
  t = np.arange(n, dtype=np.float32) / sr
@@ -88,12 +142,25 @@ def render_bed(name: str, duration_sec: float, sr: int) -> np.ndarray:
88
  return (m / peak * amp).astype(np.float32)
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  def mix(speech: np.ndarray, name: str, sr: int, gain: float = MUSIC_GAIN) -> np.ndarray:
92
  """Mix the named bed under the speech. Returns float32 in [-1, 1]."""
93
  speech = np.asarray(speech, dtype=np.float32).reshape(-1)
94
  if not name or name.strip().lower().startswith("no music"):
95
  return speech
96
- bed = render_bed(name, len(speech) / sr, sr)
97
  if len(bed) < len(speech):
98
  bed = np.pad(bed, (0, len(speech) - len(bed)))
99
  else:
 
8
 
9
  from __future__ import annotations
10
 
11
+ import functools
12
+ import os
13
+
14
  import numpy as np
15
 
16
  # Linear gain applied to the (peak-normalised) bed when mixed under speech.
17
  MUSIC_GAIN = 0.14
18
 
19
+ # Real public-domain (FreePD/CC0) loops, used when present; else procedural fallback.
20
+ _LOOPS_DIR = os.path.join(os.path.dirname(__file__), "music_loops")
21
+ _LOOP_FILES = {
22
+ "ambient drift": "ambient_drift.flac",
23
+ "lo-fi pulse": "lofi_pulse.flac",
24
+ "cinematic rise": "cinematic_rise.flac",
25
+ "newsroom bed": "newsroom_bed.flac",
26
+ }
27
+
28
+
29
+ @functools.lru_cache(maxsize=8)
30
+ def _load_loop(filename: str):
31
+ """Load a loop file as (mono float32, sr), cached. Returns None if unavailable."""
32
+ path = os.path.join(_LOOPS_DIR, filename)
33
+ if not os.path.isfile(path):
34
+ return None
35
+ try:
36
+ import soundfile as sf
37
+
38
+ audio, sr = sf.read(path, dtype="float32")
39
+ if audio.ndim > 1:
40
+ audio = audio.mean(axis=1)
41
+ return audio.astype(np.float32), int(sr)
42
+ except Exception:
43
+ return None
44
+
45
+
46
+ def _resample(x: np.ndarray, sr: int, target: int) -> np.ndarray:
47
+ if sr == target or len(x) == 0:
48
+ return x
49
+ n = int(round(len(x) * target / sr))
50
+ return np.interp(np.linspace(0, 1, n), np.linspace(0, 1, len(x)), x).astype(np.float32)
51
+
52
+
53
+ def _tile_to(x: np.ndarray, n: int, sr: int) -> np.ndarray:
54
+ """Repeat a loop to length n with a short equal-power crossfade at each seam."""
55
+ if len(x) >= n:
56
+ return x[:n]
57
+ xf = min(int(0.5 * sr), len(x) // 4)
58
+ if xf <= 0:
59
+ reps = int(np.ceil(n / len(x)))
60
+ return np.tile(x, reps)[:n]
61
+ fade_in = np.linspace(0, 1, xf, dtype=np.float32)
62
+ fade_out = fade_in[::-1]
63
+ out = np.array(x, dtype=np.float32)
64
+ while len(out) < n:
65
+ tail = out[-xf:] * fade_out
66
+ head = x[:xf] * fade_in
67
+ out = np.concatenate([out[:-xf], tail + head, x[xf:]])
68
+ return out[:n]
69
+
70
 
71
  def _sine(freq: float, n: int, sr: int, phase: float = 0.0) -> np.ndarray:
72
  t = np.arange(n, dtype=np.float32) / sr
 
142
  return (m / peak * amp).astype(np.float32)
143
 
144
 
145
+ def _bed_for(name: str, n: int, sr: int) -> np.ndarray:
146
+ """Length-n bed at sr: a real FreePD loop if bundled, else procedural synthesis."""
147
+ key = (name or "").strip().lower()
148
+ loaded = _load_loop(_LOOP_FILES.get(key, ""))
149
+ if loaded is not None:
150
+ loop, lsr = loaded
151
+ loop = _resample(loop, lsr, sr)
152
+ bed = _tile_to(loop, n, sr)
153
+ peak = float(np.abs(bed).max()) or 1.0
154
+ return (bed / peak * 0.9).astype(np.float32)
155
+ return render_bed(name, n / sr, sr)
156
+
157
+
158
  def mix(speech: np.ndarray, name: str, sr: int, gain: float = MUSIC_GAIN) -> np.ndarray:
159
  """Mix the named bed under the speech. Returns float32 in [-1, 1]."""
160
  speech = np.asarray(speech, dtype=np.float32).reshape(-1)
161
  if not name or name.strip().lower().startswith("no music"):
162
  return speech
163
+ bed = _bed_for(name, len(speech), sr)
164
  if len(bed) < len(speech):
165
  bed = np.pad(bed, (0, len(speech) - len(bed)))
166
  else:
tts/music_loops/ambient_drift.flac ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c3173137804483ef766e03ad11319ff564853298b3faebf3befaba6ce328af3
3
+ size 2530419
tts/music_loops/cinematic_rise.flac ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6426727da3d68f3c7495a35efad3b2368fd8406a42931be7b9bea41a8d0b0a0c
3
+ size 3088641
tts/music_loops/lofi_pulse.flac ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b09dd6ba6be923d2db951d0cf6e785089b54eddf59d9a8f8061bcd22a427bba
3
+ size 3048935
tts/music_loops/newsroom_bed.flac ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b254438fb0bb4e0a4ac2feeefc6aa6ce0ac39a42026a4d1e2dc2c9c46f4d000a
3
+ size 1909197