public async Task <IActionResult> TextToSpeech([FromBody] JObject request) { var watch = System.Diagnostics.Stopwatch.StartNew(); string methodName = "TextToSpeech"; ResponseDTO response = new ResponseDTO(); try { Log.Write(appSettings, LogEnum.DEBUG.ToString(), label, className, methodName, $"REQUEST: {JsonConvert.SerializeObject(request)}"); TextToSpeechRequest requestBody = request.ToObject <TextToSpeechRequest>(); response.Result = await Task.Run(() => TextToSpeechService.TextToSpeech(appSettings, requestBody)); response.Success = true; watch.Stop(); Log.Write(appSettings, LogEnum.DEBUG.ToString(), label, className, methodName, $"RESULT: {JsonConvert.SerializeObject(response)} Execution Time: {watch.ElapsedMilliseconds} ms"); return(Ok(response)); } catch (Exception e) { response.Success = false; response.Msg = e.Message; watch.Stop(); Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(request)}"); Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}"); return(BadRequest(response)); } }
private async Task <TextToSpeechRequest> MakeSpeechRequestAsync() { var voices = await GetVoicesAsync().ConfigureAwait(false); var voice = (from v in voices where v.ShortName == "en-US-JessaNeural" select v) .First(); var format = AudioFormat.Audio16KHz128KbitrateMonoMP3; var audioRequest = new TextToSpeechRequest(region, resourceName, format) { Text = "Hello, world", VoiceName = voice.ShortName, Style = SpeechStyle.Cheerful, RateChange = 0.75f, PitchChange = -0.1f }; if (!cache.IsCached(audioRequest)) { audioRequest.AuthToken = await GetTokenAsync().ConfigureAwait(false); } return(audioRequest); }
private async Task <TextToSpeechRequest> MakeSpeechRequestAsync() { var voices = await GetVoicesAsync().ConfigureAwait(false); var voice = (from v in voices where v.ShortName == "en-US-JessaNeural" select v) .First(); var format = TextToSpeechStreamClient.SupportedFormats.FirstOrDefault(f => f.ContentType == MediaType.Audio_Mpeg && f.Channels == 1 && f.SampleRate == 16000 && f.BitsPerSample == 32); var audioRequest = new TextToSpeechRequest(region, resourceName, format) { Text = "Hello, world", VoiceName = voice.ShortName, Style = SpeechStyle.Cheerful, RateChange = 0.75f, PitchChange = -0.1f }; if (!cache.IsCached(audioRequest)) { audioRequest.AuthToken = await GetTokenAsync().ConfigureAwait(false); } return(audioRequest); }
private TextToSpeechResponse Request(string text, string url) { var request = new TextToSpeechRequest(text); var jsonRequest = JsonConvert.SerializeObject(request, JsonSettings); var requestContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); if (!string.IsNullOrWhiteSpace(_apiKey)) { _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}"); } var requestUrl = UseProxyIfPossible(BaseUrl + url); var result = _httpClient.PostAsync(requestUrl, requestContent).Result; var response = result.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <TextToSpeechResponse>(response)); }
/// <summary> /// Perform text-to-speech on a string Takes as input a string and a file format (mp3 or wav) and outputs a wave form in the appropriate format. /// </summary> /// <exception cref="Cloudmersive.APIClient.NET.Speech.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="reqConfig">String input request</param> /// <returns>Task of ApiResponse (Object)</returns> public async System.Threading.Tasks.Task <ApiResponse <Object> > SpeakTextToSpeechAsyncWithHttpInfo(TextToSpeechRequest reqConfig) { // verify the required parameter 'reqConfig' is set if (reqConfig == null) { throw new ApiException(400, "Missing required parameter 'reqConfig' when calling SpeakApi->SpeakTextToSpeech"); } var localVarPath = "/speech/speak/text/voice/basic/audio"; var localVarPathParams = new Dictionary <String, String>(); var localVarQueryParams = new List <KeyValuePair <String, String> >(); var localVarHeaderParams = new Dictionary <String, String>(this.Configuration.DefaultHeader); var localVarFormParams = new Dictionary <String, String>(); var localVarFileParams = new Dictionary <String, FileParameter>(); Object localVarPostBody = null; // to determine the Content-Type header String[] localVarHttpContentTypes = new String[] { "application/json", "text/json", "application/xml", "text/xml", "application/x-www-form-urlencoded" }; String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes); // to determine the Accept header String[] localVarHttpHeaderAccepts = new String[] { "application/json", "text/json", "application/xml", "text/xml" }; String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts); if (localVarHttpHeaderAccept != null) { localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept); } if (reqConfig != null && reqConfig.GetType() != typeof(byte[])) { localVarPostBody = this.Configuration.ApiClient.Serialize(reqConfig); // http body (model) parameter } else { localVarPostBody = reqConfig; // byte array } // authentication (Apikey) required if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("Apikey"))) { localVarHeaderParams["Apikey"] = this.Configuration.GetApiKeyWithPrefix("Apikey"); } // make the HTTP request IRestResponse localVarResponse = (IRestResponse)await this.Configuration.ApiClient.CallApiAsync(localVarPath, Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarPathParams, localVarHttpContentType); int localVarStatusCode = (int)localVarResponse.StatusCode; if (ExceptionFactory != null) { Exception exception = ExceptionFactory("SpeakTextToSpeech", localVarResponse); if (exception != null) { throw exception; } } return(new ApiResponse <Object>(localVarStatusCode, localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), (Object)this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(Object)))); }
/// <summary> /// Perform text-to-speech on a string Takes as input a string and a file format (mp3 or wav) and outputs a wave form in the appropriate format. /// </summary> /// <exception cref="Cloudmersive.APIClient.NET.Speech.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="reqConfig">String input request</param> /// <returns>Task of Object</returns> public async System.Threading.Tasks.Task <Object> SpeakTextToSpeechAsync(TextToSpeechRequest reqConfig) { ApiResponse <Object> localVarResponse = await SpeakTextToSpeechAsyncWithHttpInfo(reqConfig); return(localVarResponse.Data); }
/// <summary> /// Perform text-to-speech on a string Takes as input a string and a file format (mp3 or wav) and outputs a wave form in the appropriate format. /// </summary> /// <exception cref="Cloudmersive.APIClient.NET.Speech.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="reqConfig">String input request</param> /// <returns>Object</returns> public Object SpeakTextToSpeech(TextToSpeechRequest reqConfig) { ApiResponse <Object> localVarResponse = SpeakTextToSpeechWithHttpInfo(reqConfig); return(localVarResponse.Data); }