private List <Voice> GetAllVoices(TtsSetting ttsSetting)
        {
            var            region = ttsSetting.EndPoint.Substring(8, ttsSetting.EndPoint.IndexOf('.') - 8);
            Authentication auth   = new Authentication(ttsSetting.EndPoint, ttsSetting.Key);

            using (HttpClient client = new HttpClient())
            {
                var req = $"https://{region}.tts.speech.microsoft.com/cognitiveservices/voices/list";
                using (HttpRequestMessage request = new HttpRequestMessage())
                {
                    string accessToken;
                    try
                    {
                        accessToken = auth.GetAccessToken();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Failed to obtain an access token: {ex}");
                        return(null);
                    }

                    request.Method     = HttpMethod.Get;
                    request.RequestUri = new Uri(req);
                    request.Headers.Add("Authorization", "Bearer " + accessToken);
                    using (HttpResponseMessage response = client.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult())
                    {
                        response.EnsureSuccessStatusCode();
                        var res = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                        return(JsonConvert.DeserializeObject <List <Voice> >(res));
                    }
                }
            }
        }
        private void SaveTtsSetting(TtsSetting ttsSetting)
        {
            var ser = JsonConvert.SerializeObject(ttsSetting);

            using (StreamWriter sw = new StreamWriter(Server.MapPath(TtsConfigFile)))
            {
                sw.Write(ser);
            }
        }
        // GET: Tts/Edit
        public ActionResult Edit()
        {
            TtsSetting ttsSetting = new TtsSetting();

            if (System.IO.File.Exists(Server.MapPath(TtsConfigFile)))
            {
                ttsSetting = LoadTtsSetting(Server.MapPath(TtsConfigFile));
                PrepareViewBag(ttsSetting);
            }
            return(View(ttsSetting));
        }
 private void PrepareViewBag(TtsSetting ttsSetting)
 {
     try
     {
         // The langauges
         ViewBag.Languages = new SelectList(CultureInfo.GetCultures(CultureTypes.NeutralCultures), nameof(CultureInfo.Name), nameof(CultureInfo.DisplayName));
         var voices = GetAllVoices(ttsSetting);
         // The voices
         ViewBag.Voices = new SelectList(voices, nameof(Voice.ShortName), nameof(Voice.Name));
     }
     catch
     {
         // Do nothing, bag will be empty
     }
 }
        public ActionResult Edit(TtsSetting ttsSetting)
        {
            try
            {
                if (ttsSetting != null)
                {
                    SaveTtsSetting(ttsSetting);
                }

                // Check if all is valid
                try
                {
                    Authentication auth        = new Authentication(ttsSetting.EndPoint, ttsSetting.Key);
                    var            accessToken = auth.GetAccessToken();
                }
                catch (Exception ex)
                {
                    ViewBag.LastMessage = "Error validating your keys";
                    PrepareViewBag(ttsSetting);
                    return(View());
                }

                // Check if we have a preferred selection, if not, then return to the view
                if (string.IsNullOrEmpty(ttsSetting.PrefferedShortName))
                {
                    ViewBag.LastMessage = "Please select a preferred voice";
                    PrepareViewBag(ttsSetting);
                    return(View());
                }
                else
                {
                    // Find and save the voice details
                    ttsSetting.PrefferedVoice = GetAllVoices(ttsSetting).Where(m => m.ShortName == ttsSetting.PrefferedShortName).FirstOrDefault();
                    SaveTtsSetting(ttsSetting);
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ViewBag.LastMessage = $"An error occured, please try again: {ex}";
                PrepareViewBag(ttsSetting);
                return(View());
            }
        }