static void Main(string[] args) { var sessionApi = new SessionApi(); var credentials = new AuthRequestDto("user", 261, "password"); var sessionId = sessionApi.Login(credentials).SessionId; var synthesizeApi = new SynthesizeApi(); var synthesizeText = new SynthesizeText("text/plain", "Hello world"); var synthesizeRequest = new SynthesizeRequest(synthesizeText, "Carol", "audio/wav"); var synthesizedSound = synthesizeApi.Synthesize(Guid.Parse(sessionId), synthesizeRequest).Data; var soundBytes = Convert.FromBase64String(synthesizedSound); File.WriteAllBytes("F:\\Cloud\\tts\\testWav.wav", soundBytes); }
public async Task <IActionResult> Synthesize([FromBody] SynthesizeRequest request) { if (_cache.TryGetValue(request.Text, out string filename)) { byte[] audio_data = null; // Issues with large files were reported, if this function throws an error check up on ReadAllBytesAsync audio_data = await io.File.ReadAllBytesAsync(Path.Combine(_cache_path, filename)); return(Ok(new SynthesizeResponse { Text = request.Text, Audio = audio_data })); } return(NotFound("Item was not found in cache, non cache values are currently not supported")); }
public void GenerateWAV(string speakerText, ushort voice, string outputFileName) { var request = new SynthesizeRequest() { input = new SynthesisInput() { ssml = $"<speak>{speakerText}</speak>" }, voice = new VoiceSelectionParams() { name = Settings.VoiceIds[voice], languageCode = Settings.LanguageCode }, audioConfig = new AudioConfig() { audioEncoding = AudioEncoding.LINEAR16, speakingRate = Settings.RateOfSpeech, volumeGainDb = Settings.VolumeGainDb, sampleRateHertz = Settings.SampleRateHz } }; var requestJson = JsonConvert.SerializeObject(request, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var requestPayload = new StringContent(requestJson, Encoding.UTF8, "application/json"); var serviceUrl = string.Format(SERVICE_URL_TEMPLATE, Settings.ApiKey); var response = _httpClient.PostAsync(serviceUrl, requestPayload).Result; if (response.IsSuccessStatusCode) { var responseJson = response.Content.ReadAsStringAsync().Result; var responseObj = JsonConvert.DeserializeObject <SynthesizeResponse>(responseJson); using (var fs = new FileStream(outputFileName, FileMode.Create)) { var audioBytes = Convert.FromBase64String(responseObj.audioContent); fs.Write(audioBytes, 0, audioBytes.Length); fs.Flush(); fs.Close(); } } else { throw new Exception($"HTTP response status code:{response.StatusCode}; reason: {response.ReasonPhrase}"); } }