首页 > 解决方案 > 无法在 c# 中使用 Google.Cloud.TextToSpeech.V1 指定音频配置文件

问题描述

我正在尝试使用 Google.Cloud.TextToSpeech.V1 将文本转换为音频。它工作正常,但我不知道如何在我在 Node.js 和 python 中找到代码时使用 c# 指定要使用的音频配置文件但在 c# 中没有任何内容这是我的代码

  static void Main(string[] args)
        {
           
            List<Word> lst = IntialData();

            System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\Users\Admin\TextToSpeechApiDemo\key.json");


            var client = TextToSpeechClient.Create();
           
            // The input to be synthesized, can be provided as text or SSML.

            foreach (Word item in lst)
            {
                var input = new SynthesisInput
                {
                    Text = item.Name,
                    
                  
                };
          
                // Build the voice request.
                var voiceSelection = new VoiceSelectionParams
                {
                    LanguageCode = "ar",
                    //SsmlGender = SsmlVoiceGender.Female,
                
                    Name = "ar-XA-Wavenet-A"
                };

                // Specify the type of audio file.
                var audioConfig = new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Linear16,
                    
                    




                }; 
          

                    // Perform the text-to-speech request.
                    var response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);

                // Write the response to the output file.
                using (var output = File.Create(@"E:\Noursound\sim\ar-XA-Wavenet-A\" + item.Id.ToString() + ".wav"))
                {

                    response.AudioContent.WriteTo(output);

                }

            }
            
           
        }

我在 python 中找到了这段代码,他设置了 effects_profile_id

audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.MP3,
        effects_profile_id=[effects_profile_id],

我怎么能用c#做到这一点

标签: c#google-text-to-speech

解决方案


问题出在我使用的 NuGet 包的版本中 1.0.0-beta01 ,它没有 EffectsProfileId 属性,但在将其更新为 Google.Cloud.TextToSpeech.V1 版本 2.3.0 后,我找到了该属性。

using Google.Cloud.TextToSpeech.V1;

class Program
{
    static void Main(string[] args)
    {
        var config = new AudioConfig
        {
            AudioEncoding = AudioEncoding.Mp3,
            EffectsProfileId = { "your profile ID" }
        };
    }
}

我在 github 上为此创建了 git issue 这是一个链接


推荐阅读