首页 > 解决方案 > 如何将 C# 语法翻译成 Matlab?

问题描述

我正在为项目的一部分进行文本到语音转换,Matlab 有一些功能可以引入 .NET 程序集,但文档非常有限。我无法调用一些方法来改变声音。Microsoft 的 SystemSpeech 文档暗示这应该可以工作,但是当我尝试时出现此错误:

objspeech.SelectVoice('Anna')
Message: Cannot set voice. No matching voice is installed or the voice was
disabled.
Source: System.Speech
HelpLink:

这是我的代码的最小工作版本:

NET.addAssembly('System.Speech');
objspeech = System.Speech.Synthesis.SpeechSynthesizer;
% objspeech.SelectVoice('Anna');
objspeech.Volume = 100;
SpeakAsync(objspeech, 'Hello World');

唯一的失败是 SelectVoice。我不知道如何改变它。运行 objspeech.GetInstalledVoices 返回:

ReadOnlyCollection<System*Speech*Synthesis*InstalledVoice> with properties:

    Count: 2

有谁知道为什么 SelectVoice 不起作用?

标签: c#.netmatlab

解决方案


以下是我为最终获得所有可用声音并选择不同的声音所做的工作:

resultEnumerable = NET.explicitCast(result,'System.Collections.IEnumerable');
resultEnumerator = resultEnumerable.GetEnumerator();
resultEnumerator = NET.explicitCast(resultEnumerator, 'System.Collections.IEnumerator');
while (resultEnumerator.MoveNext)
    v1=resultEnumerator.Current;
    v1.VoiceInfo.Name
end

objspeech.SelectVoice(v1)

我的 v1 在这里是“Microsoft Zira Desktop”,效果很好objspeech.SelectVoice('Microsoft Zira Desktop')。奇怪的是,在 Windows 设置中,我安装并运行了 Microsoft David、Mark 和 Zira,但只有 David 和 Zira 可以通过 .NET 访问。


推荐阅读