Пример #1
0
        public async ValueTask <ISpeechResult> TranslateTextAsync(AWSSpeechRequest request)
        {
            var result = new AWSSpeechResult();

            var chunks = request.Content.Chunk(30).ToArray();

            var textIndex = 0;

            for (int i = 0; i < chunks.Length; i++)
            {
                if (i > 0)
                {
                    await Task.Delay(60000);
                }

                var tasks = new List <Task>();

                foreach (var chunk in chunks[i])
                {
                    tasks.Add(QueueText(chunk, textIndex, request, result));
                    textIndex++;
                }

                await Task.WhenAll(tasks);
            }

            return(result);
        }
Пример #2
0
        private async Task QueueText(string text, int index, AWSSpeechRequest request, AWSSpeechResult result)
        {
            var synthesizeSpeechRequest = new StartSpeechSynthesisTaskRequest
            {
                OutputFormat       = request.OutputFormat,
                VoiceId            = request.VoiceId,
                Text               = text,
                OutputS3BucketName = _translationOptions.Bucket,
                OutputS3KeyPrefix  = Guid.NewGuid().ToString()
            };

            var response = await _pollyClient.StartSpeechSynthesisTaskAsync(synthesizeSpeechRequest);

            using (var waitHandle = new AutoResetEvent(false))
                using (new Timer(
                           callback: async(e) => { await CheckSynthesisTask(response.SynthesisTask.TaskId, waitHandle); },
                           state: null,
                           dueTime: TimeSpan.Zero,
                           period: TimeSpan.FromSeconds(_translationOptions.PollRate)
                           )
                       )
                {
                    waitHandle.WaitOne();
                }

            var task = await _pollyClient.GetSpeechSynthesisTaskAsync(new GetSpeechSynthesisTaskRequest { TaskId = response.SynthesisTask.TaskId });

            if (task.SynthesisTask.TaskStatus == Amazon.Polly.TaskStatus.Failed)
            {
                throw new InvalidOperationException();
            }

            var key        = task.SynthesisTask.OutputUri.Split('/').Last();
            var fileResult = await _fileClient.DownloadAsync(key, "");

            using (var stream = await fileResult.OpenStreamAsync())
            {
                await result.DigestBytes(index, stream);
            }

            if (request.CallBack != null)
            {
                await request.CallBack(text.Length);
            }

            await _fileClient.DeleteAsync(key, "");
        }