Unity使用sherpa-onnx实现离线语音合成

ops/2024/10/25 17:27:18/

sherpa-onnx https://github.com/k2-fsa/sherpa-onnx
相关dll和lib库拷进Unity,官方示例代码稍作修改
在这里插入图片描述

using SherpaOnnx;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;public class TTS : MonoBehaviour
{public string Model = "vits-zh-aishell3/vits-aishell3.onnx";public string Lexicon = "vits-zh-aishell3/lexicon.txt";public string Tokens = "vits-zh-aishell3/tokens.txt";public string DataDir = "";public string DictDir = "";public string RuleFsts = "vits-zh-aishell3/rule.far";public int SpeakerId = 99;// Start is called before the first frame updatevoid Start(){try{OfflineTtsConfig config = new OfflineTtsConfig();config.Model.Vits.Model = Path.Combine(Application.streamingAssetsPath, Model);config.Model.Vits.Lexicon = Path.Combine(Application.streamingAssetsPath, Lexicon);config.Model.Vits.Tokens = Path.Combine(Application.streamingAssetsPath, Tokens);//config.Model.Vits.DataDir = Path.Combine(Application.streamingAssetsPath, DataDir);//config.Model.Vits.DictDir = Path.Combine(Application.streamingAssetsPath, DictDir);config.Model.Vits.NoiseScale = 0.667f;config.Model.Vits.NoiseScaleW = 0.8f;config.Model.Vits.LengthScale = 1f;config.Model.NumThreads = 1;config.Model.Debug = 0;config.Model.Provider = "cpu";config.RuleFsts = Application.streamingAssetsPath + "/vits-zh-aishell3/phone.fst" + ","+ Application.streamingAssetsPath + "/vits-zh-aishell3/date.fst" + ","+ Application.streamingAssetsPath + "/vits-zh-aishell3/number.fst";config.MaxNumSentences = 1;OfflineTts tts = new OfflineTts(config);OfflineTtsCallback callback = new OfflineTtsCallback(MyCallback);OfflineTtsGeneratedAudio audio = tts.GenerateWithCallback("这是一个语音合成测试", 1, SpeakerId, callback);bool ok = audio.SaveToWaveFile(Application.streamingAssetsPath + "/test.wav");if (ok){Debug.Log("succeeded!");}else{Debug.Log("Failed");}}catch (Exception e){Debug.LogError(e);}}void MyCallback(IntPtr samples, int n){float[] data = new float[n];Marshal.Copy(samples, data, 0, n);//Debug.Log("n:" + n);}// Update is called once per framevoid Update(){}
}

在这里插入图片描述
在这里插入图片描述
不知为何生成的音频是8000HZ的……
24.5.15添加了方便测试的UI
在这里插入图片描述

最后,案发现场在https://gitee.com/awnuxcvbn/usherpa-onnx-tts.git


http://www.ppmy.cn/ops/41563.html

相关文章

mac中launchctl使用教程

launchctl是macOS系统中用于管理和控制系统服务和守护进程的命令行工具。下面是一些常见的launchctl命令和用法示例&#xff1a; 启动一个服务&#xff1a; launchctl start <service_name>停止一个服务&#xff1a; launchctl stop <service_name>加载一个plis…

DiskGenius帮你恢复系统无法识别的U盘数据

场景还原 前两天早上U盘复制文件卡死后&#xff0c;强行断开U盘&#xff0c;再次使用直接无法访问&#xff0c;心拔凉拔凉&#xff01;&#xff01; 使用驱动器G:中的光盘之前需要将其格式化 位置不可用-无法访问U盘 常规科普 一、U盘无法识别 1、检查U盘是否插入正确&…

Vue Excel 文件流导出乱码快速解决方案

今日在开发一个导出功能&#xff0c;原本一个非常简单的功能&#xff0c;却没想里面藏了陷阱&#xff01; 背景 前端导出的文件流乱码&#xff0c;此时确定非后端问题&#xff08;可以在postman导出是否正常来判断&#xff09;。 前端导出&#xff1a; 后端正常数据&#xf…

量化交易:Dual Thrust策略

哈喽&#xff0c;大家好&#xff0c;我是木头左&#xff01; Dual Thrust策略起源于20世纪80年代&#xff0c;由美国著名交易员和金融作家Larry Williams首次提出。这一策略的核心思想是通过捕捉市场中的短期波动来实现盈利。Larry Williams通过多年的研究和实践&#xff0c;发…

Alembic 的使用(配合一款免费云数据库MemfireDB)

已经使用 Go 开发好一段时间了&#xff0c;最近因为工作原因又重操旧业搞起了 Python&#xff0c;基于 FastAPI 进行接口开发&#xff0c;然后去找了一下相关的脚手架&#xff0c;发现这其中挺多都用到了 Alembic&#xff0c;之前没使用过&#xff0c;于是学习了一下&#xff0…

20240514基于深度学习的弹性超材料色散关系预测与结构逆设计

论文&#xff1a;Dispersion relation prediction and structure inverse design of elastic metamaterials via deep learning DOI&#xff1a;https://doi.org/10.1016/j.mtphys.2022.100616 1、摘要 精心设计的超材料结构给予前所未有的性能&#xff0c;保证了各种各样的具…

Linux监听某个进程,自动重启

文章目录 前言一、使用 bash 脚本编写监控程序二、使用 systemd总结 前言 在 Linux 下监听某个进程&#xff0c;当进程异常退出后自动重启&#xff0c;可以使用bash脚本编写监控程序&#xff0c;也可以使用系统工具如 systemd 或 supervisor。 一、使用 bash 脚本编写监控程序…

训练集、测试集与验证集:机器学习模型评估的基石

在机器学习中&#xff0c;为了评估模型的性能&#xff0c;我们通常会将数据集划分为训练集&#xff08;Training Set&#xff09;、验证集&#xff08;Validation Set&#xff09;和测试集&#xff08;Test Set&#xff09;。这种划分有助于我们更好地理解模型在不同数据上的表…