/// <summary> /// Convert text to speech to an audio file then save it to a S3 bucket /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <ConvertTextResponse> AddTextToSpeechFileToBucket(ConvertTextRequest request) { if (string.IsNullOrEmpty(request.Content)) { throw new ArgumentNullException("Content cannot be empty"); } _logger.Info($"Content requested to convert: {request.Content}"); var pollyRequest = new SynthesizeSpeechRequest { OutputFormat = OutputFormat.Mp3, Text = request.Content, TextType = "text", VoiceId = VoiceId.Amy, // https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Polly/TVoiceId.html LanguageCode = LanguageCode.EnUS // https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Polly/TLanguageCode.html }; // Send request to Amazon Polly to convert text to audio. var synthesizeSpeechResponse = await _pollyClient.SynthesizeSpeechAsync(pollyRequest); if (synthesizeSpeechResponse == null || synthesizeSpeechResponse.HttpStatusCode != (HttpStatusCode)200) { throw new Exception("Text could not be converted to audio."); } _logger.Info($"Requested characters was: {synthesizeSpeechResponse.RequestCharacters}"); // Stream the audio from Amazon Polly to a S3 bucket using the filename provided by the request PutObjectResponse s3Response; using (var memoryStream = new MemoryStream()) { synthesizeSpeechResponse.AudioStream.CopyTo(memoryStream); s3Response = await _s3Client.PutObjectAsync(new PutObjectRequest { BucketName = _s3BucketName, Key = request.FileName, InputStream = memoryStream }); } if (s3Response == null || s3Response.HttpStatusCode != (HttpStatusCode)200) { throw new Exception("Unable to save audio file to s3"); } // Publish to a SNS topic that a new audio file has been saved. var publishRequest = new PublishRequest { TopicArn = _notificationSnsTopic, Message = "An audio file was saved", Subject = "New MP3 File" }; await _snsClient.PublishAsync(publishRequest); return(new ConvertTextResponse { FileName = request.FileName }); }
/// <summary> /// Incoming request from API Gateway that is redirect to a Logic class for text to audio conversion /// </summary> /// <see cref="Logic.AddTextToSpeechFileToBucket(ConvertTextRequest)">Forwards request to Logic class</see> public async Task <ConvertTextResponse> AddTextToSpeechFileToBucket(ConvertTextRequest request) => await _logic.AddTextToSpeechFileToBucket(request);