Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
106
Tools/audio/eleven.py
Normal file
106
Tools/audio/eleven.py
Normal file
@ -0,0 +1,106 @@
|
||||
"""ElevenLabs sound/music fetcher for WRAP PARTY.
|
||||
|
||||
The API key is read at runtime from the ELEVEN_KEY_FILE env var (kept outside the project).
|
||||
Raw candidates are saved to Tools/audio/eleven_raw/<name>_<k>.wav (44.1 kHz PCM).
|
||||
|
||||
Usage:
|
||||
python eleven.py sfx <name> <duration> <prompt influence> "<prompt>" [count]
|
||||
python eleven.py music <name> <length_ms> "<prompt>"
|
||||
python eleven.py tts <name> <voice_id> "<text with v3 audio tags>" [count]
|
||||
python eleven.py credits
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import wave
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
RAW = os.path.join(HERE, "eleven_raw")
|
||||
API = "https://api.elevenlabs.io/v1"
|
||||
|
||||
|
||||
def key():
|
||||
with open(os.environ["ELEVEN_KEY_FILE"], encoding="utf-8") as f:
|
||||
return f.read().strip()
|
||||
|
||||
|
||||
def post(path, payload, accept="audio/*"):
|
||||
req = urllib.request.Request(API + path, data=json.dumps(payload).encode(), method="POST",
|
||||
headers={"xi-api-key": key(), "Content-Type": "application/json", "Accept": accept})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=600) as r:
|
||||
return r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
raise SystemExit(f"HTTP {e.code}: {e.read()[:600].decode(errors='replace')}")
|
||||
|
||||
|
||||
def credits():
|
||||
req = urllib.request.Request(API + "/user/subscription", headers={"xi-api-key": key()})
|
||||
with urllib.request.urlopen(req, timeout=60) as r:
|
||||
d = json.load(r)
|
||||
return d["character_limit"] - d["character_count"]
|
||||
|
||||
|
||||
def save_pcm(path, pcm, channels=1, rate=44100):
|
||||
with wave.open(path, "wb") as w:
|
||||
w.setnchannels(channels)
|
||||
w.setsampwidth(2)
|
||||
w.setframerate(rate)
|
||||
w.writeframes(pcm)
|
||||
|
||||
|
||||
def sfx(name, duration, influence, prompt, count=1):
|
||||
os.makedirs(RAW, exist_ok=True)
|
||||
for k in range(count):
|
||||
out = os.path.join(RAW, f"{name}_{k}.wav")
|
||||
if os.path.exists(out):
|
||||
print("skip", out)
|
||||
continue
|
||||
payload = {"text": prompt, "prompt_influence": influence}
|
||||
if duration > 0:
|
||||
payload["duration_seconds"] = duration
|
||||
pcm = post("/sound-generation?output_format=pcm_44100", payload)
|
||||
save_pcm(out, pcm, channels=2) # pcm_44100 comes back as interleaved stereo
|
||||
print("saved", out, f"{len(pcm) / 2 / 44100:.2f}s")
|
||||
|
||||
|
||||
def tts(name, voice, text, count=1):
|
||||
"""Speech with the v3 model, or ELEVEN_MODEL (it reads [angry], [shouting], [pause]... tags). Mono 24 kHz."""
|
||||
os.makedirs(RAW, exist_ok=True)
|
||||
for k in range(count):
|
||||
out = os.path.join(RAW, f"{name}_{k}.wav")
|
||||
if os.path.exists(out):
|
||||
print("skip", out)
|
||||
continue
|
||||
# 44.1 kHz PCM is a Pro-tier format; 24 kHz is plenty for a voice on a phone
|
||||
pcm = post(f"/text-to-speech/{voice}?output_format=pcm_24000",
|
||||
{"text": text, "model_id": os.environ.get("ELEVEN_MODEL", "eleven_v3"), "voice_settings": {"stability": 0.5}})
|
||||
save_pcm(out, pcm, channels=1, rate=24000)
|
||||
print("saved", out, f"{len(pcm) / 2 / 24000:.2f}s")
|
||||
|
||||
|
||||
def music(name, length_ms, prompt):
|
||||
os.makedirs(RAW, exist_ok=True)
|
||||
out = os.path.join(RAW, f"{name}.mp3")
|
||||
if os.path.exists(out):
|
||||
print("skip", out)
|
||||
return
|
||||
data = post("/music?output_format=mp3_44100_192",
|
||||
{"prompt": prompt, "music_length_ms": length_ms, "force_instrumental": True}, accept="audio/mpeg")
|
||||
with open(out, "wb") as f:
|
||||
f.write(data)
|
||||
print("saved", out, len(data), "bytes")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cmd = sys.argv[1]
|
||||
if cmd == "credits":
|
||||
print("credits left:", credits())
|
||||
elif cmd == "sfx":
|
||||
sfx(sys.argv[2], float(sys.argv[3]), float(sys.argv[4]), sys.argv[5], int(sys.argv[6]) if len(sys.argv) > 6 else 1)
|
||||
elif cmd == "tts":
|
||||
tts(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 1)
|
||||
elif cmd == "music":
|
||||
music(sys.argv[2], int(sys.argv[3]), sys.argv[4])
|
||||
Reference in New Issue
Block a user