当前位置:   article > 正文

Unity使用微软SDK实现文字转语音_unity文字转语音

unity文字转语音

本文将通过使用微软语音服务SDK实现文字到语音的转换

1、首先从微软官方网站现在SDK包,然后将SDK包导入新建的项目里 

2、新建项目里添加一个InputField文本输入框和一个按钮即可,然后再新建一个脚本命名为TTSDemo,挂在一个物体上即可,如图

3、TTSDemo代码如下:

  1. using Microsoft.CognitiveServices.Speech;
  2. using Microsoft.CognitiveServices.Speech.Audio;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TTSDemo : MonoBehaviour
  6. {
  7. public InputField inputField;
  8. public Button speakButton;
  9. public AudioSource audioSource;
  10. private string message;
  11. public string path;
  12. public string fileName;
  13. public string audioType;
  14. void Start()
  15. {
  16. if (inputField == null)
  17. {
  18. message = "inputField property is null! Assign a UI InputField element to it.";
  19. UnityEngine.Debug.LogError(message);
  20. }
  21. else if (speakButton == null)
  22. {
  23. message = "speakButton property is null! Assign a UI Button to it.";
  24. UnityEngine.Debug.LogError(message);
  25. }
  26. else
  27. {
  28. speakButton.onClick.AddListener(ButtonClick);
  29. }
  30. }
  31. public async void ButtonClick()
  32. {
  33. //string xmlPath = Application.dataPath + "/SpeechConfig/ZHCN.xml";
  34. var config = SpeechConfig.FromSubscription("xxxxx", "eastus");
  35. //config.SpeechSynthesisLanguage = "zh-CN";
  36. //config.SpeechSynthesisVoiceName = "zh-CN-XiaoyouNeural";
  37. var audioConfig = AudioConfig.FromWavFileInput("E:/FirstAudio.wav");
  38. using (var synthsizer = new SpeechSynthesizer(config, audioConfig))
  39. {
  40. var result = synthsizer.SpeakTextAsync(inputField.text).Result;
  41. //var xmlConfig = File.ReadAllText(Application.dataPath + "/SpeechConfig/ZHCN.xml");
  42. //var result = await synthsizer.SpeakSsmlAsync(xmlConfig);
  43. string newMessage = string.Empty;
  44. if (result.Reason == ResultReason.SynthesizingAudioCompleted)
  45. {
  46. var sampleCount = result.AudioData.Length / 2;
  47. var audioData = new float[sampleCount];
  48. for (var i = 0; i < sampleCount; ++i)
  49. {
  50. audioData[i] = (short)(result.AudioData[i * 2 + 1] << 8 | result.AudioData[i * 2]) / 32768.0F;
  51. }
  52. // The default output audio format is 16K 16bit mono
  53. var audioClip = AudioClip.Create("SynthesizedAudio", sampleCount, 1, 16000, false);
  54. audioClip.SetData(audioData, 0);
  55. audioSource.clip = audioClip;
  56. audioSource.Play();
  57. newMessage = "Speech synthesis succeeded!";
  58. Debug.Log(newMessage);
  59. }
  60. else if (result.Reason == ResultReason.Canceled)
  61. {
  62. var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
  63. //newMessage = $"CANCELED:\nReason=[{cancellation.Reason}]\nErrorDetails=[{cancellation.ErrorDetails}]\nDid you update the subscription info?";
  64. Debug.LogError(cancellation.ErrorDetails);
  65. }
  66. }
  67. }
  68. }

4、代码详解 

var config = SpeechConfig.FromSubscription("xxxxx", "eastus"); “xxxxx”就是你自己的密钥;“eastus”是区域语言(美国东部、美国西部等等), 此值代表美国东部

 如果用“eastus” InputField文本框里就只能输入英语,生成英语语音片段,不然就会报错。那么有同学会问如果要生成汉语语音怎么办:

//config.SpeechSynthesisLanguage = "zh-CN";
//config.SpeechSynthesisVoiceName = "zh-CN-XiaoyouNeural";

把这两行代码放开即可

config.SpeechSynthesisLanguage是地区,"zh-CN"代表中国

config.SpeechSynthesisVoiceName 是音色,本文用的是女声Xiaoyou

地区、音色、语音情感等参数见微软官方文档这里

using (var synthsizer = new SpeechSynthesizer(config, audioConfig)) 第1个参数是上面所解释的生成语音所需要的参数集合,第2个参数是语音存储位置和名称、类型

5、报错详览

 这种错误代表你从文字生成出来的语音是空的,记住用“eastus”就只能输入英语,不然就会报空。

这种401错误是你密钥没有验证通过,没连到SDK服务 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/116952
推荐阅读
相关标签
  

闽ICP备14008679号