gpt4 book ai didi

azure - 使用 Microsoft Azure Text To Speech with Unity 时,播放声音的开头和结尾会出现断音

转载 作者:行者123 更新时间:2023-12-03 00:03:08 25 4
gpt4 key购买 nike

我正在使用 Microsoft Azure Text To Speech with Unity。但在播放声音的开头和结尾时会出现断音。这是正常的吗,还是 result.AudioData 损坏了。下面是代码。

    public AudioSource audioSource;
void Start()
{
SynthesisToSpeaker("你好世界");
}
public void SynthesisToSpeaker(string text)
{
var config = SpeechConfig.FromSubscription("[redacted]", "southeastasia");
config.SpeechSynthesisLanguage = "zh-CN";
config.SpeechSynthesisVoiceName = "zh-CN-XiaoxiaoNeural";

// Creates a speech synthesizer.
// Make sure to dispose the synthesizer after use!
SpeechSynthesizer synthesizer = new SpeechSynthesizer(config, null);
Task<SpeechSynthesisResult> task = synthesizer.SpeakTextAsync(text);
StartCoroutine(CheckSynthesizer(task, config, synthesizer));
}
private IEnumerator CheckSynthesizer(Task<SpeechSynthesisResult> task,
SpeechConfig config,
SpeechSynthesizer synthesizer)
{
yield return new WaitUntil(() => task.IsCompleted);
var result = task.Result;
// Checks result.
string newMessage = string.Empty;
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
var sampleCount = result.AudioData.Length / 2;
var audioData = new float[sampleCount];
for (var i = 0; i < sampleCount; ++i)
{
audioData[i] = (short)(result.AudioData[i * 2 + 1] << 8
| result.AudioData[i * 2]) / 32768.0F;
}
// The default output audio format is 16K 16bit mono
var audioClip = AudioClip.Create("SynthesizedAudio", sampleCount,
1, 16000, false);
audioClip.SetData(audioData, 0);
audioSource.clip = audioClip;
audioSource.Play();

}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
}
synthesizer.Dispose();
}

最佳答案

默认音频格式为Riff16Khz16BitMonoPcm ,其开头有一个 riff header result.AudioData 。如果您将audioData传递给audioClip,它将播放标题,然后您会听到一些噪音。

您可以通过 speechConfig.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm); 将格式设置为不带标题的原始格式,参见this sample了解详情。

关于azure - 使用 Microsoft Azure Text To Speech with Unity 时,播放声音的开头和结尾会出现断音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61453371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com