При использовании Microsoft Azure Text To Speech with Unity в начале и конце воспроизводимого звука будут прерывистые звуки. - PullRequest
2 голосов
/ 27 апреля 2020

Я использую Microsoft Azure Text To Speech с Unity. Но в начале и в конце воспроизводимого звука будут прерывистые звуки. Это нормально, или результат. 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();
    }

1 Ответ

1 голос
/ 27 апреля 2020

Аудиоформат по умолчанию - Riff16Khz16BitMonoPcm, заголовок которого находится в начале result.AudioData. Если вы передадите audioData в audioClip, он будет воспроизводить заголовок, а затем вы услышите некоторый шум.

Вы можете установить необработанный формат без заголовка с помощью speechConfig.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm);, см. этот пример для деталей.

...