您当前的位置: 首页 >  unity

十幺卜入

暂无认证

  • 6浏览

    0关注

    119博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3D HoloLens2 中文文字转语音即语音合成(语音提示)功能

十幺卜入 发布时间:2021-01-08 10:09:36 ,浏览量:6

文字转语音
  • UNITY_WSA平台
  • 处理过程和实现原理
  • 中文需要设置声音
  • 上代码
  • 使用方法
    • 一、部署好Unity3d HoloLens项目工程
    • 二、新建节点
    • 三、添加TTS组件并配置
    • 四、调用TTS功能
    • 五、测试、打包和运行
  • 参考地址
  • 源码地址:

UNITY_WSA平台

HoloLens2 是微软通用平台,使用的类主要是Windows.Media.SpeechSynthesis;

处理过程和实现原理

是把string内容转换成语音文件,并转换为audio clip 通过audio source组件进行播放。具体请看 代码,注释比较详细,请自行研究。

中文需要设置声音

声音如下TextToSpeechVoice枚举:

 public enum TextToSpeechVoice
    {
        /// 
        ///系统默认声音.
        /// 
        Default,

        /// 
        /// 大卫 移动
        /// 
        David,

        /// 
        ///马克 移动
        /// 
        Mark,

        /// 
        /// 兹拉 移动
        /// 
        Zira,

        /// 
        /// 瑶瑶(谐音) 中文
        /// 
        Yaoyao,

        /// 
        /// 灰灰(谐音) 中文
        /// 
        Huihui,

        /// 
        /// 康康(谐音) 中文
        /// 
        Kangkang,
    }

中文选用Yaoyao,HuiHui或者KangKang。

上代码
using System;
using UnityEngine;

#if !UNITY_EDITOR && UNITY_WSA
using Windows.Foundation;
using Windows.Media.SpeechSynthesis;
using Windows.Storage.Streams;
using System.Linq;
using System.Threading.Tasks;
#endif

namespace HoloToolkit.Unity
{
    /// 
    /// 知名的可用声音.
    /// 
    public enum TextToSpeechVoice
    {
        /// 
        ///系统默认声音.
        /// 
        Default,

        /// 
        /// 大卫 移动
        /// 
        David,

        /// 
        ///马克 移动
        /// 
        Mark,

        /// 
        /// 兹拉 移动
        /// 
        Zira,

        /// 
        /// 瑶瑶(谐音) 中文
        /// 
        Yaoyao,

        /// 
        /// 灰灰(谐音) 中文
        /// 
        Huihui,

        /// 
        /// 康康(谐音) 中文
        /// 
        Kangkang,
    }

    /// 
    /// 生成语音. 
    /// 这个类将流转换为UnityAudioClip 并使用你在inspector中提供的AudioSource播放
    /// 这可以让你在3D空间中定位声音。推荐的方法是将AudioSource放置在空对象上,
    /// 并将它设为主摄像头的子对象,将其放置在相机上方的大约0.6个单位。
    /// 这个听起来类似于Cortana在操作系统中的讲话。
    /// 
    [RequireComponent(typeof(AudioSource))]
    public class TextToSpeech : MonoBehaviour
    {
        [Tooltip("播放语音的AudioSource")]
        [SerializeField]
        private AudioSource audioSource;
        public static TextToSpeech Instance = null;
        /// 
        /// 获取或设置播放语音的AudioSource.
        /// 
        public AudioSource AudioSource { get { return audioSource; } set { audioSource = value; } }

        /// 
        ///获取或设置用于生成语音的声音.
        /// 
        public TextToSpeechVoice Voice { get { return voice; } set { voice = value; } }

        [Tooltip("生成语音的声音")]
        [SerializeField]
        private TextToSpeechVoice voice;

#if !UNITY_EDITOR && UNITY_WSA
        private SpeechSynthesizer synthesizer;
        private VoiceInformation voiceInfo;
        private bool speechTextInQueue = false;
#endif

        /// 
        /// 转换2个字节为-1至1的浮点数
        /// 
        /// 第一个字节
        /// 第二个字节
        /// 转换的值
        private static float BytesToFloat(byte firstByte, byte secondByte)
        {
            // 转换两个字节为short(从小到大)
            short s = (short)((secondByte             
关注
打赏
1663314737
查看更多评论
0.1826s