"""CPU helpers for the fixed Kokoro v1 / 24 kHz harmonic-source contract. Phase and oscillator arithmetic remain FP32. The Gaussian transform preserves the seeded reference samples while using FP64 arithmetic and parallel execution. """ from __future__ import annotations import json from collections import OrderedDict from pathlib import Path import numpy as np import torch from torch.nn import functional as F class NativeNoise: """Optional C++ source; owns a separate generator with the same seed/draws.""" def __init__(self, library, threads=8): import ctypes self.ctypes = ctypes self.library = ctypes.CDLL(str(library)) self.function = self.library.kokoro_normal self.function.argtypes = [ctypes.c_uint64, ctypes.c_int64, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] self.function.restype = ctypes.c_int self.threads = threads def __call__(self, samples, harmonics, seed): result = torch.empty(1, harmonics, samples, dtype=torch.float32) status = self.function(seed, samples, harmonics, result.data_ptr(), self.threads) if status: raise RuntimeError(f"Native Gaussian source failed: {status}") return result.transpose(1, 2) def kokoro_normal(samples, harmonics): """Vectorized FP64 Box-Muller for the reference's strided CPU normal fill. This helper is used only after resetting the CPU generator for a Kokoro source call. The sample count is even and there is no cached normal value. Uniform draws, pair order and physical output order match PyTorch 2.8's scalar normal_distribution path; the arithmetic remains FP64 until the original final FP32 rounding. """ count = samples * harmonics if count % 2: raise ValueError("The Kokoro source requires an even normal sample count") uniform = torch.rand(count // 2, 2, dtype=torch.float64) radius = torch.sqrt(-2.0 * torch.log1p(-uniform[:, 1])) theta = (2.0 * torch.pi) * uniform[:, 0] pairs = torch.empty(count // 2, 2, dtype=torch.float32) pairs[:, 0] = radius * torch.cos(theta) pairs[:, 1] = radius * torch.sin(theta) return pairs.reshape(1, harmonics, samples).transpose(1, 2) def low_rate_sine(f0, cfg, seed, *, fast_noise=False, native_noise=None): """Avoid audio-rate frequency/remainder tensors; preserve RNG and FP32 order. The reference repeats each low-rate pitch value 300 times, then samples frequency increments back down at positions 149.5, 449.5, ... . Both neighbors at those positions are identical repeats of the same pitch. The random initial phase placed at audio sample zero is never sampled, but its random draw must still be consumed before generating noise. """ if cfg["upsample_scale"] != 300 or cfg["sampling_rate"] != 24000: raise ValueError("This optimization requires Kokoro's 300x / 24 kHz source contract") pitch = torch.from_numpy(f0) if pitch.dtype != torch.float32 or pitch.ndim != 2 or pitch.shape[0] != 1 or pitch.shape[1] < 1: raise ValueError("Expected nonempty batch-one FP32 pitch [1, frames]") harmonics = cfg["harmonic_num"] + 1 with torch.inference_mode(), torch.random.fork_rng(devices=[]): torch.manual_seed(seed) frequencies = pitch[:, :, None] * torch.arange(1, harmonics + 1, dtype=torch.float32)[None, None] increments = (frequencies / cfg["sampling_rate"]) % 1 # Retain the reference generator's consumption, including fundamental. torch.rand(1, harmonics) phase = torch.cumsum(increments, dim=1) * 2 * torch.pi phase = F.interpolate(phase.transpose(1, 2) * cfg["upsample_scale"], scale_factor=cfg["upsample_scale"], mode="linear").transpose(1, 2) waves = torch.sin(phase) * cfg["sine_amp"] uv = (pitch > cfg["voiced_threshold"]).to(torch.float32).repeat_interleave( cfg["upsample_scale"], dim=1)[:, :, None] noise_amp = uv * cfg["noise_std"] + (1 - uv) * cfg["sine_amp"] / 3 if native_noise is not None: gaussian = native_noise(waves.shape[1], harmonics, seed) else: gaussian = kokoro_normal(waves.shape[1], harmonics) if fast_noise else torch.randn_like(waves) noise = noise_amp * gaussian return waves * uv + noise def optimized_sine(f0, cfg, seed): return low_rate_sine(f0, cfg, seed, fast_noise=True) class CPUHelpers: """Reusable harmonic DSP and bounded cache for exact style-affine inputs. The owning TTS worker serializes calls, as with the reference's fork_rng. Each native noise call uses private buffers and a private seeded generator. """ def __init__(self, root, *, noise_library=None, noise_threads=16, cache_styles=16): self.cfg = json.loads((Path(root) / "host_source.json").read_text()) self.native_noise = NativeNoise(noise_library, noise_threads) if noise_library else None self.noise_implementation = "native-fp64" if self.native_noise else "torch-vector-fp64" self.window = torch.hann_window(self.cfg["n_fft"], periodic=True, dtype=torch.float32) self.affine_cache = OrderedDict() self.cache_styles = cache_styles self.affine_hits = 0 def sine(self, f0, seed): return low_rate_sine(f0, self.cfg, seed, fast_noise=True, native_noise=self.native_noise) def affine(self, backend, style): exact = np.ascontiguousarray(style[:, :128], dtype=np.float32) key = exact.tobytes() if key in self.affine_cache: self.affine_hits += 1 self.affine_cache.move_to_end(key) return self.affine_cache[key] value = backend.run("host_affine", {"style": exact})[0] if self.cache_styles > 0: value.setflags(write=False) self.affine_cache[key] = value while len(self.affine_cache) > self.cache_styles: self.affine_cache.popitem(last=False) return value def stft(self, merged): with torch.inference_mode(): data = torch.from_numpy(merged).transpose(1, 2).squeeze(1) spectrum = torch.stft(data, self.cfg["n_fft"], self.cfg["hop_length"], self.cfg["n_fft"], window=self.window, return_complex=True) return torch.cat([torch.abs(spectrum), torch.angle(spectrum)], dim=1).numpy() def harmonic_features(self, f0, backend, seed): import time record = getattr(backend, "add_timing", lambda name, seconds: None) start = time.perf_counter() waves = self.sine(f0, seed) record("source_oscillator", time.perf_counter() - start) merged = backend.run("harmonic_merge", {"harmonics": waves.numpy()})[0] start = time.perf_counter() result = self.stft(merged) record("source_stft", time.perf_counter() - start) return result