static StreamingRecognitionConfig CreateStreamingRecognitionConfig(
            uint sampleRate,
            string audioEncoding,
            uint countChannel,
            uint maxAlternatives,
            bool disableAutomaticPunctuation,
            bool enableInterimResults,
            bool singleUtterance
            )
        {
            var streamingRecognitionConfig = new StreamingRecognitionConfig();

            streamingRecognitionConfig.Config = CreateRecognitionConfig(
                sampleRate,
                audioEncoding,
                countChannel,
                maxAlternatives,
                disableAutomaticPunctuation
                );

            streamingRecognitionConfig.InterimResultsConfig = new InterimResultsConfig
            {
                EnableInterimResults = enableInterimResults,
            };

            streamingRecognitionConfig.SingleUtterance = singleUtterance;

            return(streamingRecognitionConfig);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Construct an InfiniteSpeechStream without starting it.
        /// </summary>
        /// <param name="wasapiCapture">A WasapiCapture that must be in stopped state. It will be disposed with this stream.</param>
        /// <param name="video">If use video enhanced model.</param>
        /// <param name="sampleRate">The sample rate of data sent to google.</param>
        public InfiniteSpeechStream([NotNull] WasapiCapture wasapiCapture,
                                    bool video = false,
                                    [NonNegativeValue] int sampleRate = DefaultReSampleRate,
                                    [NotNull] string language         = LanguageCodes.English.UnitedStates)
        {
            _wasapiCapture          = wasapiCapture;
            GoogleCloudSpeechClient = SpeechClient.Create();
            if (_wasapiCapture.CaptureState != CaptureState.Stopped)
            {
                throw new ArgumentException("WasapiCapture should not in capture state.", nameof(wasapiCapture));
            }
            _reSamplerWaveProvider = new BufferedWaveProvider(wasapiCapture.WaveFormat)
            {
                ReadFully = false
            };
            _reSampler       = new MediaFoundationResampler(_reSamplerWaveProvider, new WaveFormat(sampleRate, 16, 1));
            _convertedBuffer = new byte[_reSampler.WaveFormat.AverageBytesPerSecond * 10];
            bool   supportEnhanced = language == LanguageCodes.English.UnitedStates;
            string model           = supportEnhanced ? (video ? "video" : "phone_call") : "default";

            _recognitionConfig = new StreamingRecognitionConfig()
            {
                Config = new RecognitionConfig()
                {
                    Encoding                   = RecognitionConfig.Types.AudioEncoding.Linear16,
                    SampleRateHertz            = sampleRate,
                    LanguageCode               = language,
                    UseEnhanced                = supportEnhanced,
                    EnableAutomaticPunctuation = true,
                    Model = model
                },
                InterimResults = true,
            };
        }
Ejemplo n.º 3
0
        static void gRPc_connect(string path)
        {
            // создаем канал для обмена сообщениями с сервером
            // параметр - адрес сервера gRPC



            // создаем клиента

            //  var client = new Greeter.GreeterClient(channel);
            // var mp3 = new MP3File(@"C:\Users\IlinKS\source\repos\JWT_gRPC_console\JWT_gRPC_console\2020.10.12_21.15.41_79214430425_incoming_mixed_79098420960.mp3","testik");
            // string path = @"C:\Users\IlinKS\source\repos\JWT_gRPC_console\JWT_gRPC_console\2020.10.12_21.15.41_79214430425_incoming_mixed_79098420960.mp3";
            // string path = @"c:\Users\ilinks\source\repos\JWT_gRPC_console\JWT_gRPC_console\6650322009_89136872782_2020-01-24_08_24_12.wav";

            var audio = TagLib.File.Create(path);

            var vkc = new VoiceKitClient("bnZ6cWRvcWJncWNud2dqcGxtZ21ndXVvZXdjaWpueHk=m.rogencovfilbert", "YXlzdXBvaWhrdmZzZmtvYXZtb3plaHZqeGlrcGZ1d2c=");

            // первый запрос
            StreamingRecognitionConfig streaming_config = new StreamingRecognitionConfig();

            streaming_config.Config = new RecognitionConfig();

            streaming_config.Config.SampleRateHertz = (uint)audio.Properties.AudioSampleRate;
            streaming_config.Config.NumChannels     = (uint)audio.Properties.AudioChannels;

            audio.Dispose();

//           using FileStream fstream =  File.Open(path, FileMode.Open);

            switch (Path.GetExtension(path))
            {
            case ".mp3":
                streaming_config.Config.Encoding = AudioEncoding.MpegAudio;
                vkc.StreamingRecognize(streaming_config, File.Open(path, FileMode.Open)).GetAwaiter().GetResult();
                break;

            case ".wav":
                streaming_config.Config.Encoding = AudioEncoding.Linear16;
                vkc.StreamingRecognizeWAV(streaming_config, WavToPcmConvert(path)).GetAwaiter().GetResult();
                break;
            }



            return;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 邊錄音, 邊發到Google轉文字
        /// </summary>
        public Task <SpeechData> StartStream()
        {
            return(Task.Run(() =>
            {
                GCP.Share.Init();
                var StreamingConfig = new StreamingRecognitionConfig()
                {
                    Config = new RecognitionConfig()
                    {
                        Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz = 16000,
                        LanguageCode = Language,
                    },
                    InterimResults = true,
                };
                var Streaming = GCP.Share.Client.StreamingRecognize();
                Streaming.WriteAsync(new StreamingRecognizeRequest {
                    StreamingConfig = StreamingConfig
                }).Wait();

                Microphone.Share.OnReceive = (args) =>
                {
                    if (Streaming != null)
                    {
                        try
                        {
                            Streaming.WriteAsync(
                                new StreamingRecognizeRequest()
                            {
                                AudioContent = Google.Protobuf.ByteString.CopyFrom(args.Buffer, 0, args.BytesRecorded)
                            }).Wait();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                };

                var speechAnaysis = Task.Run <string>(async() =>
                {
                    sb = new StringBuilder();

                    try
                    {
                        while (await Streaming.ResponseStream.MoveNext(default))
Ejemplo n.º 5
0
            public async Task StreamingRecognizeWAV(StreamingRecognitionConfig config, Stream audioStream)
            {
                var streamingSTT      = _clientSTT.StreamingRecognize(GetMetadataSTT());
                var requestWithConfig = new StreamingRecognizeRequest
                {
                    StreamingConfig = config
                };
                await streamingSTT.RequestStream.WriteAsync(requestWithConfig); //передаем параметры аудио

                Task PrintResponsesTask = Task.Run(async() =>                   // Пишем ответ в консоль
                {
                    while (await streamingSTT.ResponseStream.MoveNext())
                    {
                        foreach (var result in streamingSTT.ResponseStream.Current.Results)
                        {
                            foreach (var alternative in result.RecognitionResult.Alternatives)
                            {
                                System.Console.WriteLine(alternative.Transcript);
                                text += alternative.Transcript;
                            }
                        }
                    }
                });

                var buffer = new byte[config.Config.SampleRateHertz / 10];
                int bytesRead;

                //   audioStream.Position = 44;
                while ((bytesRead = audioStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    await streamingSTT.RequestStream.WriteAsync(
                        new StreamingRecognizeRequest
                    {
                        AudioContent = Google.Protobuf
                                       .ByteString.CopyFrom(buffer, 0, bytesRead),
                    });
                }

                await streamingSTT.RequestStream.CompleteAsync();

                await PrintResponsesTask;
            }
        public async Task StreamingRecognize(StreamingRecognitionConfig config, Stream audioStream)
        {
            var streamingSTT      = _clientSTT.StreamingRecognize(GetMetadataSTT());
            var requestWithConfig = new StreamingRecognizeRequest
            {
                StreamingConfig = config
            };
            await streamingSTT.RequestStream.WriteAsync(requestWithConfig);

            Task PrintResponsesTask = Task.Run(async() =>
            {
                while (await streamingSTT.ResponseStream.MoveNext())
                {
                    foreach (var result in streamingSTT.ResponseStream.Current.Results)
                    {
                        foreach (var alternative in result.RecognitionResult.Alternatives)
                        {
                            System.Console.WriteLine(alternative.Transcript);
                        }
                    }
                }
            });

            var buffer = new byte[2 * 1024];
            int bytesRead;

            while ((bytesRead = audioStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                await streamingSTT.RequestStream.WriteAsync(
                    new StreamingRecognizeRequest
                {
                    AudioContent = Google.Protobuf
                                   .ByteString.CopyFrom(buffer, 0, bytesRead),
                });
            }

            await streamingSTT.RequestStream.CompleteAsync();

            await PrintResponsesTask;
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            // 証明書を作成
            var credential = GoogleCredential.FromJson(File.ReadAllText("SpeechTest-4db378c087bb.json"));

            credential = credential.CreateScoped("https://www.googleapis.com/auth/cloud-platform");

            // サーバに接続するためのチャンネルを作成
            var channel = new Channel("speech.googleapis.com:443", credential.ToChannelCredentials());

            // Google Speech APIを利用するためのクライアントを作成
            var client = new Speech.SpeechClient(channel);

            // ストリーミングの設定
            var streamingConfig = new StreamingRecognitionConfig
            {
                Config = new RecognitionConfig
                {
                    SampleRate   = 16000,
                    Encoding     = RecognitionConfig.Types.AudioEncoding.Linear16,
                    LanguageCode = "ja-JP",
                },
                InterimResults  = true,
                SingleUtterance = false,
            };

            // ストリーミングを開始
            using (var call = client.StreamingRecognize())
            {
                Console.WriteLine("-----------\nstart.\n");

                // Cloud Speech APIからレスポンスが返ってきた時の挙動を設定
                var responseReaderTask = Task.Run(async() =>
                {
                    // MoveNext1回につきレスポンス1回分のデータがくる
                    while (await call.ResponseStream.MoveNext())
                    {
                        var note = call.ResponseStream.Current;

                        // データがあれば、認識結果を出力する
                        if (note.Results != null && note.Results.Count > 0 &&
                            note.Results[0].Alternatives.Count > 0)
                        {
                            Console.WriteLine("result: " + note.Results[0].Alternatives[0].Transcript);
                        }
                    }
                });

                // 最初の呼び出しを行う。最初は設定データだけを送る
                var initialRequest = new StreamingRecognizeRequest
                {
                    StreamingConfig = streamingConfig,
                };
                call.RequestStream.WriteAsync(initialRequest).Wait();

                // 録音モデルの作成
                IAudioRecorder recorder = new RecordModel();

                // 録音モデルが音声データを吐いたら、それをすかさずサーバに送信する
                recorder.RecordDataAvailabled += (sender, e) =>
                {
                    if (e.Length > 0)
                    {
                        // WriteAsyncは一度に一回しか実行できないので非同期処理の時は特に注意
                        // ここではlockをかけて処理が重ならないようにしている
                        lock (recorder)
                        {
                            call.RequestStream.WriteAsync(new StreamingRecognizeRequest
                            {
                                AudioContent = RecognitionAudio.FromBytes(e.Buffer, 0, e.Length).Content,
                            }).Wait();
                        }
                    }
                };

                // 録音の開始
                recorder.Start();

                // Cloud Speech APIのストリーミングは1回60秒までなので、50秒まできたら打ち切る
                var timer = new Timer(1000 * 50);
                timer.Start();

                // 50秒経過した時、実際に打ち切るコード
                timer.Elapsed += async(sender, e) =>
                {
                    recorder.Stop();
                    await call.RequestStream.CompleteAsync();
                };

                // 待機
                responseReaderTask.Wait();

                // ここに到達した時点で、APIの呼び出しが終了したということなので、タイマーを切る
                timer.Dispose();
            }

            Console.WriteLine("\n-----------\nCompleted (Time out)");
            Console.ReadKey();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Asynchronously streams audio to the Google Cloud Speech server and receives results.
        /// </summary>
        static async Task StreamingRequest()
        {
            using (var call = m_Client.StreamingRecognize())
            {
                Task responseReaderTask = Task.Run(async() =>
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        StreamingRecognizeResponse response = call.ResponseStream.Current;
                        var responseJSON = new JSONObject();
                        if (response.Error != null)
                        {
                            var errorJSON = new JSONObject();
                            errorJSON.AddField("code", response.Error.Code);
                            errorJSON.AddField("message", response.Error.Message);
                            responseJSON.AddField("error", errorJSON);
                        }
                        if (response.Results != null && response.Results.Count > 0)
                        {
                            var resultsJSON = new JSONObject();
                            foreach (var result in response.Results)
                            {
                                var resultJSON = new JSONObject();
                                if (result.Alternatives != null && result.Alternatives.Count > 0)
                                {
                                    var alternativesJSON = new JSONObject();
                                    foreach (var alternative in result.Alternatives)
                                    {
                                        var alternativeJSON = new JSONObject();
                                        alternativeJSON.AddField("transcript", alternative.Transcript);
                                        alternativeJSON.AddField("confidence", alternative.Confidence);
                                        alternativesJSON.Add(alternativeJSON);
                                    }
                                    resultJSON.AddField("alternatives", alternativesJSON);
                                    resultJSON.AddField("is_final", result.IsFinal);
                                    resultJSON.AddField("stability", result.Stability);
                                }
                                resultsJSON.Add(resultJSON);
                            }
                            responseJSON.AddField("results", resultsJSON);
                        }
                        responseJSON.AddField("result_index", response.ResultIndex);
                        responseJSON.AddField("endpointer_type", response.EndpointerType.ToString());
                        Console.WriteLine("response: " + responseJSON);
                    }
                });

                // Send initial config request
                var configRequest = new StreamingRecognizeRequest();
                var streamingRecognitionConfig = new StreamingRecognitionConfig();
                var recognitionConfig          = new RecognitionConfig();
                recognitionConfig.Encoding   = RecognitionConfig.Types.AudioEncoding.LINEAR16;
                recognitionConfig.SampleRate = 16000;
                streamingRecognitionConfig.InterimResults  = true;
                streamingRecognitionConfig.SingleUtterance = false;
                streamingRecognitionConfig.Config          = recognitionConfig;
                configRequest.StreamingConfig = streamingRecognitionConfig;
                await call.RequestStream.WriteAsync(configRequest);

                // Send audio chunks
                Task sendChunksTask = SendChunks(call);

                await sendChunksTask;
                await call.RequestStream.CompleteAsync();

                await responseReaderTask;

                call.Dispose();
            }
        }