neutts_synth.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. """Standalone NeuTTS synthesis helper.
  3. Called by tts_tool via subprocess so the ~500MB TTS model lives in a process that exits
  4. after synthesis. Usage:
  5. python -m tools.neutts_synth --text "Hello" --out out.wav --ref-audio jo.wav --ref-text jo.txt
  6. Run ``hermes setup tts`` and choose NeuTTS; espeak-ng is also required (apt/brew).
  7. """
  8. import argparse
  9. import struct
  10. import sys
  11. from pathlib import Path
  12. def _write_wav(path: str, samples, sample_rate: int = 24000) -> None:
  13. """Write a WAV file from float32 samples (no soundfile dependency)."""
  14. import numpy as np
  15. if not isinstance(samples, np.ndarray):
  16. samples = np.array(samples, dtype=np.float32)
  17. pcm = (np.clip(samples.flatten(), -1.0, 1.0) * 32767).astype(np.int16)
  18. data_size = len(pcm) * 2 # 16-bit mono
  19. with open(path, "wb") as f:
  20. f.write(b"RIFF" + struct.pack("<I", 36 + data_size) + b"WAVEfmt ")
  21. f.write(struct.pack("<IHHIIHH", 16, 1, 1, sample_rate, sample_rate * 2, 2, 16))
  22. f.write(b"data" + struct.pack("<I", data_size) + pcm.tobytes())
  23. def main():
  24. parser = argparse.ArgumentParser(description="NeuTTS synthesis helper")
  25. parser.add_argument("--text", required=True, help="Text to synthesize")
  26. parser.add_argument("--out", required=True, help="Output WAV path")
  27. parser.add_argument("--ref-audio", required=True, help="Reference voice audio path")
  28. parser.add_argument("--ref-text", required=True, help="Reference voice transcript path")
  29. parser.add_argument("--model", default="neuphonic/neutts-air-q4-gguf",
  30. help="HuggingFace backbone model repo")
  31. parser.add_argument("--device", default="cpu", help="Device (cpu/cuda/mps)")
  32. args = parser.parse_args()
  33. ref_audio = Path(args.ref_audio).expanduser()
  34. ref_text_path = Path(args.ref_text).expanduser()
  35. if not ref_audio.exists():
  36. print(f"Error: reference audio not found: {ref_audio}", file=sys.stderr)
  37. sys.exit(1)
  38. if not ref_text_path.exists():
  39. print(f"Error: reference text not found: {ref_text_path}", file=sys.stderr)
  40. sys.exit(1)
  41. ref_text = ref_text_path.read_text(encoding="utf-8-sig").strip()
  42. try:
  43. from neutts import NeuTTS
  44. except ImportError:
  45. print("Error: neutts not installed. Run hermes setup tts and choose NeuTTS.", file=sys.stderr)
  46. sys.exit(1)
  47. # llama_cpp (backbone) offloads to GPU only for the literal string "gpu";
  48. # torch (codec) only accepts "cuda". A single --device value can't satisfy
  49. # both — "cuda" silently no-ops on the backbone, leaving it on CPU.
  50. tts = NeuTTS(
  51. backbone_repo=args.model,
  52. backbone_device="gpu" if args.device == "cuda" else args.device,
  53. codec_repo="neuphonic/neucodec",
  54. codec_device=args.device)
  55. wav = tts.infer(args.text, tts.encode_reference(str(ref_audio)), ref_text)
  56. out_path = Path(args.out)
  57. out_path.parent.mkdir(parents=True, exist_ok=True)
  58. try:
  59. import soundfile as sf
  60. sf.write(str(out_path), wav, 24000)
  61. except ImportError:
  62. _write_wav(str(out_path), wav, 24000)
  63. print(f"OK: {out_path}", file=sys.stderr)
  64. if __name__ == "__main__":
  65. main()