Exemple #1
0
        /// <summary>
        /// 转换语音
        /// </summary>
        /// <param name="text">合成语语音所用文本</param>
        /// <param name="filename">"输出音频文件名"</param>
        /// <param name="voicename">声音人物选择</param>
        /// <param name="voicespeed">语速</param>
        public void ProcessVoice(string text, string filename, VoiceName voicename = VoiceName.xiaoyan, int voicespeed = 5)
        {
            string param       = string.Format(" vcn={0}, spd ={1}, vol = 50, bgs=0, aue=speex-wb, smk = 3", voicename, voicespeed);
            string login_param = " appid = 58d376fc,work_dir =   .  ";

            Begin_ProcessVoice(text, filename, param, login_param);
        }
Exemple #2
0
        /// <summary>
        /// Generates SSML.
        /// </summary>
        /// <param name="locale">The locale.</param>
        /// <param name="gender">The gender.</param>
        /// <param name="name">The voice name.</param>
        /// <param name="text">The text input.</param>
        private string GenerateSsml(string locale, string gender, VoiceName voicename, string text, int pitchdelta)
        {
            string voice = ConvertVoiceNametoString(voicename);

            XNamespace xmlns   = "http://www.w3.org/2001/10/synthesis";
            var        ssmlDoc = new XDocument(
                new XElement(xmlns + "speak",
                             new XAttribute("version", "1.0"),
                             new XAttribute(XNamespace.Xml + "lang", locale),      // was locked to "en-US"
                             new XElement("voice",
                                          new XAttribute(XNamespace.Xml + "lang", locale),
                                          new XAttribute(XNamespace.Xml + "gender", gender),
                                          new XAttribute("name", voice),
                                          new XElement("prosody",
                                                       new XAttribute("pitch", pitchdelta.ToString() + "Hz"),
                                                       text))));

            //var ssmlDoc = new XDocument(
            //      new XElement(xmlns + "speak",
            //          new XAttribute("version", "1.0"),
            //          new XAttribute(XNamespace.Xml + "lang", locale), // was locked to "en-US"
            //          new XElement("voice",
            //              new XAttribute(XNamespace.Xml + "lang", locale),
            //              new XAttribute(XNamespace.Xml + "gender", gender),
            //              new XAttribute("name", voice),
            //                        text)));

            return(ssmlDoc.ToString());
        }
Exemple #3
0
 public virtual Stream TextToSpeech(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
 {
     return(PolicyService.ExecuteRetryAndCapture400Errors(
                "SpeechService.TextToSpeech",
                ApiKeys.SpeechRetryInSeconds,
                () =>
     {
         var result = SpeechRepository.TextToSpeech(text, locale, voiceName, voiceType, outputFormat);
         return result;
     },
                null));
 }
Exemple #4
0
        public virtual Stream TextToSpeech(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            var response = RepositoryClient.GetAudioStream(
                ApiKeys.SpeechEndpoint,
                text,
                locale,
                voiceName,
                voiceType,
                outputFormat,
                GetSpeechToken());

            return(response);
        }
Exemple #5
0
        /// <summary>
        /// Generates SSML.
        /// </summary>
        /// <param name="locale">The locale.</param>
        /// <param name="gender">The gender.</param>
        /// <param name="name">The voice name.</param>
        /// <param name="text">The text input.</param>
        private string GenerateSsml(string locale, string gender, VoiceName voicename, string text)
        {
            string voice = ConvertVoiceNametoString(voicename);

            var ssmlDoc = new XDocument(
                new XElement("speak",
                             new XAttribute("version", "1.0"),
                             new XAttribute(XNamespace.Xml + "lang", locale),      // was locked to "en-US"
                             new XElement("voice",
                                          new XAttribute(XNamespace.Xml + "lang", locale),
                                          new XAttribute(XNamespace.Xml + "gender", gender),
                                          new XAttribute("name", voice),
                                          text)));

            return(ssmlDoc.ToString());
        }
Exemple #6
0
 public string GetVoiceLocale(VoiceName voicename)
 {
     return(ConvertVoiceNametoString(voicename).Substring(46, 5));
 }
Exemple #7
0
        /// <summary>
        /// Converts a specific VoioceName enum option into its string counterpart as expected
        /// by the API when building the SSML string that is sent to Cognitive Services.
        /// Make sure that each option in the enum is included in the switch below.
        /// </summary>
        /// <param name="voicename"></param>
        /// <returns></returns>
        public string ConvertVoiceNametoString(VoiceName voicename)
        {
            switch (voicename)
            {
            case VoiceName.enAUCatherine:
                return("Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)");

            case VoiceName.enAUHayleyRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)");

            case VoiceName.enCALinda:
                return("Microsoft Server Speech Text to Speech Voice (en-CA, Linda)");

            case VoiceName.enCAHeatherRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-CA, HeatherRUS)");

            case VoiceName.enGBSusanApollo:
                return("Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)");

            case VoiceName.enGBHazelRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-GB, HazelRUS)");

            case VoiceName.enGBGeorgeApollo:
                return("Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)");

            case VoiceName.enIESean:
                return("Microsoft Server Speech Text to Speech Voice (en-IE, Sean)");

            case VoiceName.enINHeeraApollo:
                return("Microsoft Server Speech Text to Speech Voice (en-IN, Heera, Apollo)");

            case VoiceName.enINPriyaRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-IN, PriyaRUS)");

            case VoiceName.enINRaviApollo:
                return("Microsoft Server Speech Text to Speech Voice (en-IN, Ravi, Apollo)");

            case VoiceName.enUSZiraRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)");

            case VoiceName.enUSJessaRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)");

            case VoiceName.enUSBenjaminRUS:
                return("Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)");

            case VoiceName.deATMichael:
                return("Microsoft Server Speech Text to Speech Voice (de-AT, Michael)");

            case VoiceName.deCHKarsten:
                return("Microsoft Server Speech Text to Speech Voice (de-CH, Karsten)");

            case VoiceName.deDEHedda:
                return("Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)");

            case VoiceName.deDEHeddaRUS:
                return("Microsoft Server Speech Text to Speech Voice (de-DE, HeddaRUS)");

            case VoiceName.deDEStefanApollo:
                return("Microsoft Server Speech Text to Speech Voice (de-DE, Stefan, Apollo)");

            case VoiceName.esESHelenaRUS:
                return("Microsoft Server Speech Text to Speech Voice (es-ES, HelenaRUS)");

            case VoiceName.esESLauraApollo:
                return("Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)");

            case VoiceName.esESPabloApollo:
                return("Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)");

            case VoiceName.esMXHildaRUS:
                return("Microsoft Server Speech Text to Speech Voice (es-MX, HildaRUS)");

            case VoiceName.esMXRaulApollo:
                return("Microsoft Server Speech Text to Speech Voice (es-MX, Raul, Apollo)");

            case VoiceName.frCACaroline:
                return("Microsoft Server Speech Text to Speech Voice (fr-CA, Caroline)");

            case VoiceName.frCAHarmonieRUS:
                return("Microsoft Server Speech Text to Speech Voice (fr-CA, HarmonieRUS)");

            case VoiceName.frCHGuillaume:
                return("Microsoft Server Speech Text to Speech Voice (fr-CH, Guillaume)");

            case VoiceName.frFRJulieApollo:
                return("Microsoft Server Speech Text to Speech Voice (fr-FR, Julie, Apollo)");

            case VoiceName.frFRHortenseRUS:
                return("Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)");

            default:
                return("Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)");
            }
        }
        public virtual async Task <Stream> GetAudioStreamAsync(string url, string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat, string token)
        {
            HttpClientHandler handler = new HttpClientHandler()
            {
                CookieContainer = new CookieContainer(), UseProxy = false
            };
            HttpClient client = new HttpClient(handler);

            client.DefaultRequestHeaders.Clear();

            var Headers = GetAudioHeaders(token, outputFormat);

            foreach (var header in Headers)
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
            }

            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = new StringContent(GetAudioContent(text, locale, voiceName, voiceType))
            };

            var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None);

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception($"({response.StatusCode}) {response.ReasonPhrase}");
            }

            return(await response.Content.ReadAsStreamAsync());
        }
        public virtual Stream GetAudioStream(string url, string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat, string token)
        {
            // Construct HTTP request to get the logo
            HttpWebRequest httpRequest = (HttpWebRequest)
                                         WebRequest.Create(url);

            httpRequest.Method = WebRequestMethods.Http.Post;

            var headers = GetAudioHeaders(token, outputFormat);

            httpRequest.Headers.Add("X-Microsoft-OutputFormat", outputFormat.MemberAttrValue());
            httpRequest.Headers.Add("Authorization", $"Bearer {token}");
            httpRequest.ContentType = "application/ssml+xml";
            httpRequest.UserAgent   = "TTSClient";

            byte[] reqData = Encoding.UTF8.GetBytes(GetAudioContent(text, locale, voiceName, voiceType));
            httpRequest.ContentLength = (long)reqData.Length;
            Stream requestStream = httpRequest.GetRequestStream();

            requestStream.Write(reqData, 0, reqData.Length);
            requestStream.Close();

            // Get back the HTTP response for web server
            HttpWebResponse httpResponse       = (HttpWebResponse)httpRequest.GetResponse();
            Stream          httpResponseStream = httpResponse.GetResponseStream();

            var newStream = new MemoryStream();

            httpResponseStream.CopyTo(newStream);
            httpResponse.Close();
            newStream.Position = 0;

            return(newStream);
        }
        public virtual string GetAudioContent(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType)
        {
            var langValue     = locale.MemberAttrValue();
            var namespaceAttr = $"xmlns=\"http://www.w3.org/2001/10/synthesis\"";
            var versionAttr   = $"version=\"1.0\"";
            var langAttr      = $"xml:lang=\"{langValue}\"";
            var genderAttr    = $"xmlns:gender=\"{voiceType}\"";
            var nameAttr      = $"name=\"Microsoft Server Speech Text to Speech Voice {voiceName.MemberAttrValue()}\"";
            var speakTag      = $"<speak {versionAttr} {namespaceAttr} {langAttr}><voice {nameAttr}>{text}</voice></speak>";

            return(speakTag);
        }
Exemple #11
0
        public virtual bool TextToFile(string text, string filePath, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            if (File.Exists(filePath))
            {
                return(true);
            }

            //can only send 1000 characters at a time
            //speech xml request = 178 characters
            var textLimit = 822;

            var textParts = (text.Length > textLimit)
                ? SplitToLength(text, textLimit)
                : new List <string> {
                text
            };

            using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Write))
            {
                foreach (var t in textParts)
                {
                    using (var dataStream = TextToSpeech(t, locale, voiceName, voiceType, outputFormat))
                    {
                        if (dataStream != null)
                        {
                            dataStream.CopyTo(fileStream);
                        }
                    }
                }
                fileStream.Close();
            }

            return(true);
        }