public static async Task Main()
        {
            var client = new AmazonPollyClient();

            var allVoicesRequest  = new DescribeVoicesRequest();
            var enUsVoicesRequest = new DescribeVoicesRequest()
            {
                LanguageCode = "en-US",
            };

            try
            {
                string nextToken;
                do
                {
                    var allVoicesResponse = await client.DescribeVoicesAsync(allVoicesRequest);

                    nextToken = allVoicesResponse.NextToken;
                    allVoicesRequest.NextToken = nextToken;

                    Console.WriteLine("\nAll voices: ");
                    allVoicesResponse.Voices.ForEach(voice =>
                    {
                        DisplayVoiceInfo(voice);
                    });
                }while (nextToken is not null);

                do
                {
                    var enUsVoicesResponse = await client.DescribeVoicesAsync(enUsVoicesRequest);

                    nextToken = enUsVoicesResponse.NextToken;
                    enUsVoicesRequest.NextToken = nextToken;

                    Console.WriteLine("\nen-US voices: ");
                    enUsVoicesResponse.Voices.ForEach(voice =>
                    {
                        DisplayVoiceInfo(voice);
                    });
                }while (nextToken is not null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught: " + ex.Message);
            }
        }
        public async Task <IList <IVoice> > GetVoicesAsync()
        {
            var voices = await _client.DescribeVoicesAsync(new DescribeVoicesRequest());

            return(voices.Voices.Select(voice =>
            {
                var result = new AmazonPollyVoice()
                {
                    Language = voice.LanguageName,
                    Name = voice.Name,
                    Gender = (Gender)Enum.Parse(typeof(Gender), voice.Gender.ToString()),
                    VoiceId = voice.Id
                };

                return result;
            }).Cast <IVoice>().ToList());
        }
Ejemplo n.º 3
0
        private async Task GetAllVoices()
        {
            var client = new AmazonPollyClient(RegionEndpoint.USEast1);
            var describeVoiceResponse = await client.DescribeVoicesAsync(new DescribeVoicesRequest());

            pollyTreeView.Root.SortOrder = SortOrder.Ascending;
            var voiceManager = new VoiceManager(ClsId);

            allVoices = describeVoiceResponse.Voices;
            foreach (var voice in allVoices)
            {
                TreeNodeAdv languageNode = null;

                foreach (TreeNodeAdv node in pollyTreeView.Nodes)
                {
                    if (node.Text.Equals(voice.LanguageName))
                    {
                        languageNode = node;
                        break;
                    }
                }

                if (languageNode == null)
                {
                    var treeNodeAdv = new TreeNodeAdv(voice.LanguageName);
                    var nodeIndex   = pollyTreeView.Root.Nodes.Add(treeNodeAdv);
                    languageNode = pollyTreeView.Root.Nodes[nodeIndex];
                    languageNode.CheckStateChanged += CheckStateChanged;
                }

                TreeNodeAdv genderNode = null;
                foreach (TreeNodeAdv node in languageNode.Nodes)
                {
                    if (!node.Text.Equals(voice.Gender))
                    {
                        continue;
                    }
                    genderNode = node;
                    break;
                }

                if (genderNode == null)
                {
                    var treeNodeAdv = new TreeNodeAdv(voice.Gender);
                    var nodeIndex   = languageNode.Nodes.Add(treeNodeAdv);
                    genderNode = languageNode.Nodes[nodeIndex];
                }

                genderNode.CheckStateChanged += CheckStateChanged;
                if (voiceManager.IsVoiceInstalled(voice))
                {
                    installedVoices.Add(voice.Id);
                }
                var voiceNodeIndex = genderNode.Nodes.Add(new TreeNodeAdv()
                {
                    Text    = voice.Name,
                    Tag     = voice,
                    Checked = voiceManager.IsVoiceInstalled(voice)
                });
                genderNode.SortOrder = SortOrder.Ascending;
                genderNode.Nodes[voiceNodeIndex].CheckStateChanged += CheckStateChanged;
            }

            pollyTreeView.Root.Sort();
        }
Ejemplo n.º 4
0
        private async void ExecuteGetVoicesCommand()
        {
            if (string.IsNullOrEmpty(this.Config.AccessKey) ||
                string.IsNullOrEmpty(this.Config.SecretKey))
            {
                ModernMessageBox.ShowDialog(
                    "Enter your access key and secret key.",
                    "ACT.Hojoring");

                return;
            }

            var endpoint = this.Config.Endpoint;
            var chain    = new CredentialProfileStoreChain();

            var hash        = (this.Config.Region + this.Config.AccessKey + this.Config.SecretKey).GetHashCode().ToString("X4");
            var profileName = $"polly_profile_{hash}";

            AWSCredentials awsCredentials;

            if (!chain.TryGetAWSCredentials(
                    profileName,
                    out awsCredentials))
            {
                var options = new CredentialProfileOptions
                {
                    AccessKey = this.Config.AccessKey,
                    SecretKey = this.Config.SecretKey,
                };

                var profile = new CredentialProfile(profileName, options);
                profile.Region = endpoint;

                chain.RegisterProfile(profile);

                chain.TryGetAWSCredentials(
                    profileName,
                    out awsCredentials);
            }

            if (awsCredentials == null)
            {
                return;
            }

            var voice = this.Config.Voice;

            using (var pc = new AmazonPollyClient(
                       awsCredentials,
                       endpoint))
            {
                var res = await pc.DescribeVoicesAsync(new DescribeVoicesRequest());

                if (res == null ||
                    res.HttpStatusCode != HttpStatusCode.OK)
                {
                    ModernMessageBox.ShowDialog(
                        "Voices update is failed.",
                        "ACT.Hojoring");

                    return;
                }

                this.Voices.Clear();
                this.Config.Voice = string.Empty;

                foreach (var v in
                         from x in res.Voices
                         orderby
                         x.LanguageCode.ToString(),
                         x.Gender.ToString(),
                         x.Name
                         select
                         x)
                {
                    this.Voices.Add(
                        new PollyConfigs.PollyVoice {
                        Name = $"{v.Id.Value} ({v.LanguageCode}, {v.Gender})", Value = v.Id
                    });
                }
            }

            if (this.Voices.Any(x => x.Value == voice))
            {
                this.Config.Voice = voice;
            }

            Settings.Default.Save();

            ModernMessageBox.ShowDialog(
                "Voices update is completed.",
                "ACT.Hojoring");
        }
Ejemplo n.º 5
0
        public override async Task ProcessMessageAsync(Message message, ILambdaContext context)
        {
            if (message.Iterations < 0)
            {
                return;
            }
            var suffix = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

            // Initiate describe voices request.
            var describeVoiceResponse = await _pollyClient.DescribeVoicesAsync(new DescribeVoicesRequest {
                // LanguageCode = "en-US"
            });

            // LogInfo(JsonConvert.SerializeObject(describeVoiceResponse.Voices));
            var randomIndex = _rand.Next(describeVoiceResponse.Voices.Count);
            var randomVoice = describeVoiceResponse.Voices[randomIndex];

            LogInfo($"Selected random voice '{randomVoice.Name}' in {randomVoice.LanguageName}");

            // Initiate speech synthesis request.
            var synthesizeResponse = await _pollyClient.SynthesizeSpeechAsync(new SynthesizeSpeechRequest {
                VoiceId      = randomVoice.Id,
                OutputFormat = OutputFormat.Mp3,
                Text         = message.Text
            });

            var audioStream = new MemoryStream();
            await synthesizeResponse.AudioStream.CopyToAsync(audioStream);

            audioStream.Position = 0;

            // Ensure audio file is in an s3 bucket for Transcribe to consume.
            await _s3Client.PutObjectAsync(new PutObjectRequest {
                BucketName  = _audioBucket,
                Key         = $"polly_{suffix}.mp3",
                InputStream = audioStream
            });

            // Initiate Transcription Job
            var transcriptionName     = $"transcribe_{suffix}";
            var mediaUri              = $"https://s3-us-east-1.amazonaws.com/{_audioBucket}/polly_{suffix}.mp3";
            var transcriptionResponse = await _transcribeClient.StartTranscriptionJobAsync(new StartTranscriptionJobRequest {
                LanguageCode = "en-US",
                MediaFormat  = MediaFormat.Mp3,
                Media        = new Media {
                    MediaFileUri = mediaUri
                },
                OutputBucketName     = _textBucket,
                TranscriptionJobName = transcriptionName
            });

            // Wait for transcription job to complete
            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(3));

                var transcriptionJobStatusReponse = await _transcribeClient.GetTranscriptionJobAsync(new GetTranscriptionJobRequest {
                    TranscriptionJobName = transcriptionName
                });

                if (transcriptionJobStatusReponse.TranscriptionJob.TranscriptionJobStatus == TranscriptionJobStatus.FAILED)
                {
                    LogWarn("Transcribe job failed");
                    return;
                }
                if (transcriptionJobStatusReponse.TranscriptionJob.TranscriptionJobStatus == TranscriptionJobStatus.COMPLETED)
                {
                    LogInfo("Job Completed!!");
                    break;
                }
                LogInfo("Checking job status... again.");
            }

            // Retrieve text file from s3 and extract the text
            var s3Response = await _s3Client.GetObjectAsync(new GetObjectRequest {
                BucketName = _textBucket,
                Key        = $"{transcriptionName}.json"
            });

            var text = await new StreamReader(s3Response.ResponseStream).ReadToEndAsync();

            LogInfo(text);
            var json          = JObject.Parse(text);
            var transcription = json["results"]["transcripts"][0]["transcript"].ToString();

            LogInfo(transcription);

            if (message.Iterations >= 0 && transcription.Length > 0)
            {
                await _snsClient.PublishAsync(new PublishRequest {
                    TopicArn = _topic,
                    Message  = JsonConvert.SerializeObject(new Message {
                        Iterations = message.Iterations - 1,
                        Text       = transcription,
                        Link       = mediaUri
                    })
                });
            }

            // :)
        }