public async ValueTask <ISpeechResult> TranslateTextAsync(GoogleSpeechRequest request)
        {
            lock (_lockObj)
            {
                if (_getSpeechTask == null)
                {
                    _getSpeechTask = Task.Run(async() => await _speechSynthesizer.TranslateTextAsync(request), _cts.Token);
                }
            }

            return(await _getSpeechTask);
        }
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(5000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : textChunks.Sum(tc => tc.Length),
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new GoogleSpeechRequest(
                                                                             voiceSelection : new VoiceSelectionParams
                {
                    Name = request.TranslationSubject
                },
                                                                             audioConfig : new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Mp3
                },
                                                                             content : textChunks,
                                                                             callback : async length =>
                {
                    Interlocked.Increment(ref version);
                    await _semaphoreSlim.WaitAsync();

                    try
                    {
                        await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                               aggregateId: request.AggregateId,
                                                               version: version,
                                                               correlationId: request.CorrelationId,
                                                               causationId: request.CausationId,
                                                               charactersProcessed: length,
                                                               userId: request.UserId
                                                               ));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        _semaphoreSlim.Release();
                    }
                }
                                                                             ));


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : ++version,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : ++version,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }
示例#3
0
        public async Task StartAsync(TranslationRequest request)
        {
            int version = request.Version + 1;

            var fileResult = await _fileClient.DownloadAsync(
                fileName : $"{request.CorrelationId.ToString()}{Path.GetExtension(request.FileName)}",
                userId : request.UserId
                );

            string text;

            using (var fileStream = await fileResult.OpenStreamAsync())
                using (var textExtractor = TextExtractor.Open(fileStream, fileResult.ContentType))
                {
                    text = await textExtractor.ExtractAsync();
                }

            var textChunks = text.ChunkWithDelimeters(100000, '.', '!', '?', ')', '"', '}', ']');

            await _eventPublisher.PublishAsync(new TranslationStarted(
                                                   aggregateId : request.AggregateId,
                                                   version : version,
                                                   correlationId : request.CorrelationId,
                                                   causationId : request.CausationId,
                                                   characterCount : text.Length,
                                                   userId : request.UserId
                                                   ));

            try
            {
                var result = await _speechSynthesizer.TranslateTextAsync(new AWSSpeechRequest {
                    OutputFormat = "",
                    Content      = textChunks,
                    VoiceId      = request.TranslationSubject,
                    CallBack     = async length =>
                    {
                        Interlocked.Increment(ref version);
                        await _semaphoreSlim.WaitAsync();

                        try
                        {
                            await _eventPublisher.PublishAsync(new TranslationCharactersProcessed(
                                                                   aggregateId: request.AggregateId,
                                                                   version: version,
                                                                   correlationId: request.CorrelationId,
                                                                   causationId: request.CausationId,
                                                                   charactersProcessed: length,
                                                                   userId: request.UserId
                                                                   ));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            _semaphoreSlim.Release();
                        }
                    }
                });;


                using (var stream = await result.OpenStreamAsync())
                {
                    await _fileClient.UploadAsync($"{request.AggregateId}.mp3", request.UserId, stream);

                    await _eventPublisher.PublishAsync(new TranslationSucceded(
                                                           aggregateId : request.AggregateId,
                                                           version : version + 1,
                                                           correlationId : request.CorrelationId,
                                                           causationId : request.CausationId,
                                                           userId : request.UserId
                                                           ));
                }
            }
            catch (Exception e)
            {
                await _eventPublisher.PublishAsync(new TranslationFailed(
                                                       aggregateId : request.AggregateId,
                                                       version : version + 1,
                                                       correlationId : request.CorrelationId,
                                                       causationId : request.CausationId,
                                                       error : e.Message,
                                                       userId : request.UserId
                                                       ));
            }
        }