Views
No views yet
MaiLinh.mlpackage - CoreML cho app iOS/macOS native (fp32, 22050Hz).Mai Linh/mailinh250626.onnx + .onnx.json — ONNX chạy bằng onnxruntime trên PC/Mac.quangdung/Piper_checkpoint| File | Mô tả |
|---|---|
MaiLinh.mlpackage | Model CoreML (fp32, L=256 phoneme, M=500 ≈ 5.8s/câu) |
MaiLinhTTS.swift | Code Swift tích hợp iOS: load + chạy + cắt + fade + phát |
Mai Linh/mailinh250626.onnx + .onnx.json | Model ONNX + config (chứa phoneme_id_map) |
Mai Linh/mailinh_7BKaqqkh.mp3 | Audio mẫu giọng Mai Linh |
| Câu | ONNX CPU (4-thread) | CoreML CPU-only | CoreML GPU |
|---|---|---|---|
| Ngắn (~1.8s) | 28 ms | 46 ms | 26 ms |
| Dài (~5.0s) | 104 ms | 46 ms | 26 ms |
mean|diff| ≈ 3.7e-7 (fp32). Số trên iPhone sẽ khác.1pip install piper-tts
2echo "Xin chào các bạn" | piper \
3 --model "Mai Linh/mailinh250626.onnx" \
4 --config "Mai Linh/mailinh250626.onnx.json" \
5 --output_file out.wavOMP_NUM_THREADS=4) cho ~52x realtime.1import wave, numpy as np, coremltools as ct
2from piper import PiperVoice # pip install piper-tts coremltools
3
4L, SR, HOP = 256, 22050, 256
5v = PiperVoice.load("Mai Linh/mailinh250626.onnx", "Mai Linh/mailinh250626.onnx.json") # chỉ để phonemize
6ml = ct.models.MLModel("MaiLinh.mlpackage")
7
8ids = v.phonemes_to_ids(v.phonemize("Xin chào, tôi là Mai Linh.")[0])
9n = min(len(ids), L)
10arr = np.zeros((1, L), np.int32); arr[0, :n] = ids[:n]
11
12out = ml.predict({"input": arr,
13 "input_lengths": np.array([n], np.int32),
14 "scales": np.array([0.667, 1.0, 0.8], np.float32)})
15audio = np.asarray(out["audio"]).reshape(-1)
16nf = int(np.asarray(out["n_frames"]).reshape(-1)[0])
17audio = audio[:nf * HOP].copy() # cắt đúng độ dài thật
18f = min(int(0.008 * SR), len(audio)) # fade-out 8ms (bỏ tiếng "ụp")
19audio[-f:] *= 0.5 * (1 + np.cos(np.linspace(0, np.pi, f)))
20
21with wave.open("out.wav", "wb") as w:
22 w.setnchannels(1); w.setsampwidth(2); w.setframerate(SR)
23 w.writeframes((np.clip(audio, -1, 1) * 32767).astype(np.int16).tobytes())input : [1, 256] Int32 - phoneme ids, pad 0 cho đủ 256.input_lengths : [1] Int32 - số phoneme id thật (≤256).scales : [3] Float32 - [noise=0.667, length=1.0, noise_w=0.8]; length>1 đọc chậm hơn.audio : Float32 mono 22050Hz, độ dài cố định (M·256 mẫu).n_frames : Int32 - số frame thật. Cắt audio tại n_frames*256 rồi fade-out ngắn, cắt đươc noise ở cuối audio.MaiLinh.mlpackage vào Xcode (tự biên dịch ra .mlmodelc khi build).MaiLinhTTS.swift đã làm sẵn load model, dựng input, chạy, cắt theo n_frames, fade-out, phát qua AVAudioEngine.espeak-ng (voice vi) build cho iOS (arm64) đổi text → IPA, rồi map qua phoneme_id_map trong mailinh250626.onnx.json, chèn pad id 0 giữa các phoneme (logic phonemes_to_ids của piper).ANE compile fail nên model chạy GPU/CPU (vẫn CoreML native), đủ nhanh (~26ms/câu trên Mac).