// [END speech_transcribe_async] // [START speech_transcribe_async_gcs] static object AsyncRecognizeGcs(string storageUri) { var speech = SpeechClient.Create(); var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromStorageUri(storageUri)); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine($"Transcript: { alternative.Transcript}"); } } return(0); }
public async Task <string> LongRecognize(string googleLink, string language, int rate, Action <int> onProgress = null) { var speech = SpeechClient.Create(); var request = await speech.LongRunningRecognizeAsync(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = rate, LanguageCode = language, }, RecognitionAudio.FromStorageUri(googleLink)); do { await Task.Delay(TimeSpan.FromSeconds(5)); request = await request.PollOnceAsync(); onProgress?.Invoke(request.Metadata.ProgressPercent); } while (!request.IsCompleted); return(request.Result.Results.SelectMany(R => R.Alternatives) .Select(A => A.Transcript) .ConcatStrings(" ")); }
// [END speech_sync_recognize_gcs] // [START speech_async_recognize] static object LongRunningRecognize(string filePath) { var speech = SpeechClient.Create(); var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile(filePath)); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); } } return(0); }
// [END speech_transcribe_auto_punctuation] // [START speech_transcribe_model_selection] static object SyncRecognizeModelSelection(string filePath, string model) { var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", // The `model` value must be one of the following: // "video", "phone_call", "command_and_search", "default" Model = model }, RecognitionAudio.FromFile(filePath)); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); } } return(0); }
public async Task StartRecording() { _speechClient = SpeechClient.Create(); _streamingRecognizeStream = _speechClient.StreamingRecognize(); // Write the initial request with the config. await _streamingRecognizeStream.WriteAsync( new StreamingRecognizeRequest() { StreamingConfig = new StreamingRecognitionConfig() { Config = new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "ro" }, InterimResults = false, SingleUtterance = false } }); _sendResponses = Task.Run(async() => { var responseStream = _streamingRecognizeStream.GetResponseStream(); while (await responseStream.MoveNextAsync()) { StreamingRecognizeResponse response = responseStream.Current; foreach (StreamingRecognitionResult result in response.Results) { foreach (SpeechRecognitionAlternative alternative in result.Alternatives) { await Clients.All.SendAsync("ReceiveMessage", alternative.Transcript); } } } }); }
/// <summary> /// Sends a speech recognition request to the speech service /// </summary> /// <param name="audioFile">The audio file.</param> /// <param name="locale">The locale.</param> /// <param name="serviceUrl">The service URL.</param> /// <param name="subscriptionKey">The subscription key.</param> /// <returns> /// A task /// </returns> public async Task Run(string audioFile, string locale, Uri serviceUrl, string subscriptionKey) { // create the preferences object var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey)); // Create a a speech client using (var speechClient = new SpeechClient(preferences)) { speechClient.SubscribeToPartialResult(this.OnPartialResult); speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult); // create an audio content and pass it a stream. using (var audio = new FileStream(audioFile, FileMode.Open, FileAccess.Read)) { var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600"); var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0"); var requestMetadata = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService"); await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false); } } }
public ActionResult <IEnumerable <string> > Post(IFormFile content) { List <string> returnResult = new List <string>(); if (content == null) { throw new Exception("Dosya Bulunamadı!"); } try { Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path); var speech = SpeechClient.Create(); if (content.Length > 0) { var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 8000, LanguageCode = "tr-TR", }, RecognitionAudio.FromStream(content.OpenReadStream())); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { returnResult.Add(alternative.Transcript); } } } return(returnResult); } catch (Exception e) { throw; } }
public async Task <LongRunningRecognizeResponse> Recognize(string storageUri, RecognizeConfiguration configuration = null) { configuration ??= new RecognizeConfiguration(); var speech = SpeechClient.Create(); var audio = RecognitionAudio.FromStorageUri(storageUri); var longOperation = await speech.LongRunningRecognizeAsync(new RecognitionConfig() { Encoding = configuration.AudioEncoding, SampleRateHertz = configuration.SampleRateHertz, LanguageCode = configuration.LanguageCode, DiarizationConfig = new SpeakerDiarizationConfig() { EnableSpeakerDiarization = true, MinSpeakerCount = configuration.MinSpeakerCount, MaxSpeakerCount = configuration.MaxSpeakerCount }, Metadata = new RecognitionMetadata() { OriginalMediaType = RecognitionMetadata.Types.OriginalMediaType.Video } //}, RecognitionAudio.FromStorageUri(storageUri)); }, audio); //}, RecognitionAudio.FetchFromUri("https://www.youtube.com/watch?v=5Btbdt7ksko&fbclid=IwAR2FQ5KlTzxHH7UdYDTx4Vcnk6TfFfFtWpMJw-jH1UOMAbodsnY8mS1bNlI")); longOperation = await longOperation.PollUntilCompletedAsync(); LongRunningRecognizeResponse response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine($"Transcript: { alternative.Transcript}"); } } return(response); }
public async Task <IActionResult> InsertAudio(int caminhaoId, IFormFile file) { try { MemoryStream msAudio = new MemoryStream(); file.CopyTo(msAudio); /* Grava no S3*/ var link = await _serv.SalvaAudioNoS3(msAudio); /* Transcreve audio */ string AudioTranscrito = String.Empty; var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.EncodingUnspecified, LanguageCode = "pt-br", }, await RecognitionAudio.FromStreamAsync(file.OpenReadStream())); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { AudioTranscrito = alternative.Transcript; } } await _serv.SalvaMensagemBanco(link, AudioTranscrito, 1, caminhaoId, "you"); return(Ok(file)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void CreateClients() { try { if (settingsService.settings.googleSettings.ServiceAccountKey != "") { //Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", settings.googleSettings.ServiceAccountKey); var credential = GoogleCredential.FromFile(settingsService.settings.googleSettings.ServiceAccountKey) .CreateScoped(TextToSpeechClient.DefaultScopes); var channel = new Channel(TextToSpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials()); toSpeechClient = TextToSpeechClient.Create(channel); credential = GoogleCredential.FromFile(settingsService.settings.googleSettings.ServiceAccountKey) .CreateScoped(SpeechClient.DefaultScopes); channel = new Channel(SpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials()); toTextClient = SpeechClient.Create(channel); } } catch (Exception e) { Console.WriteLine($"Google credentials not set. Error: {e}"); MessageBox.Show($"Google credentials not set. Error: {e}"); } }
public async Task <RecognitionResponseModel> Recognize(byte[] file, string languageCode, CancellationToken cancellationToken = default) { var speech = SpeechClient.Create(); var response = await speech.RecognizeAsync(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.OggOpus, LanguageCode = "uz-UZ", SampleRateHertz = 48000 }, RecognitionAudio.FromBytes(file), cancellationToken); var alternative = response?.Results?.FirstOrDefault()?.Alternatives?.FirstOrDefault(); return(new RecognitionResponseModel() { Transcript = alternative?.Transcript, Confidence = alternative?.Confidence ?? 0, Words = alternative?.Words.Select(p => new Models.WordInfo { Confidence = p.Confidence, EndTime = p.EndTime.ToTimeSpan().ToString(), SpeakerTag = p.SpeakerTag, StartTime = p.StartTime.ToTimeSpan().ToString(), Word = p.Word }).ToArray() }); }
private void Recognize() { SpeechClientBuilder builder = new SpeechClientBuilder(); builder.CredentialsPath = GOOGLE_API_CREDS_PATH; SpeechClient client = builder.Build(); RecognizeRequest request = new RecognizeRequest() { Audio = RecognitionAudio.FromFile(TEMP_AUDIO_PATH), Config = new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.EncodingUnspecified, LanguageCode = "ru-RU", EnableWordTimeOffsets = false } }; RecognizeResponse response = client.Recognize(request); Result.Text = string.Join("\n", response.Results.Select( result => result.Alternatives[0].Transcript )); }
public static string SpeechToText(string file) { string fileContent = ""; var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 44100, LanguageCode = "pl-PL" }, RecognitionAudio.FromFile(file)); Console.WriteLine("Transcript of " + file + " ready."); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { //Console.WriteLine(alternative.Transcript); fileContent += alternative.Transcript; } } return(fileContent); }
void GRecognition() { System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLECREDENTIALS.json"); var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile("resampled.wav")); string Text = ""; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Text = Text + " " + alternative.Transcript; } } Console.WriteLine(Text); }
private static void RecognizeFromAudioFile(string fileName) { var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile(fileName)); Console.WriteLine("Recognized text:"); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); Console.WriteLine($"Confidence: {alternative.Confidence * 100}%"); Console.WriteLine("****"); } Console.ReadLine(); } }
public string AudioFileToText(string fileName) { string gcsUri = $"gs://catta-telegram-bot-voice-messages/{fileName}"; var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 48000, LanguageCode = "it-IT", }, RecognitionAudio.FromStorageUri(gcsUri)); string text = ""; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { text += alternative.Transcript + "\n"; } } return(text); }
/// <summary>Snippet for Recognize</summary> public void Recognize_RequestObject() { // Snippet: Recognize(RecognizeRequest,CallSettings) // Create client SpeechClient speechClient = SpeechClient.Create(); // Initialize request argument(s) RecognizeRequest request = new RecognizeRequest { Config = new RecognitionConfig { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, SampleRateHertz = 44100, LanguageCode = "en-US", }, Audio = new RecognitionAudio { Uri = "gs://bucket_name/file_name.flac", }, }; // Make the request RecognizeResponse response = speechClient.Recognize(request); // End snippet }
static object SyncRecognizeWithCredentials(string filePath, string credentialsFilePath) { SpeechClientBuilder builder = new SpeechClientBuilder { CredentialsPath = credentialsFilePath }; SpeechClient speech = builder.Build(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile(filePath)); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); } } return(0); }
/// <summary>Snippet for RecognizeAsync</summary> public async Task RecognizeAsync() { // Snippet: RecognizeAsync(RecognitionConfig,RecognitionAudio,CallSettings) // Additional: RecognizeAsync(RecognitionConfig,RecognitionAudio,CancellationToken) // Create client SpeechClient speechClient = await SpeechClient.CreateAsync(); // Initialize request argument(s) RecognitionConfig config = new RecognitionConfig { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, SampleRateHertz = 44100, LanguageCode = "en-US", }; RecognitionAudio audio = new RecognitionAudio { Uri = "gs://bucket_name/file_name.flac", }; // Make the request RecognizeResponse response = await speechClient.RecognizeAsync(config, audio); // End snippet }
public SpeechToText(String storageUri) { sttResult = new StringBuilder(); var speech = SpeechClient.Create(); var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, //SampleRateHertz = 16000, LanguageCode = "ko", }, RecognitionAudio.FromStorageUri(storageUri)); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { //Console.WriteLine($"Transcript: { alternative.Transcript}"); //콘솔확인용 sttResult.AppendLine(alternative.Transcript); } } }
// [END speech_transcribe_model_selection] // [START speech_transcribe_enhanced_model] static object SyncRecognizeEnhancedModel(string filePath) { var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 8000, LanguageCode = "en-US", UseEnhanced = true, // A model must be specified to use an enhanced model. // Currently, only 'phone_call' is supported. Model = "phone_call", }, RecognitionAudio.FromFile(filePath)); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); } } return(0); }
public void Recognize() { var audio = LoadResourceAudio("speech.raw"); // Sample: Recognize // Additional: Recognize(*,*,*) SpeechClient client = SpeechClient.Create(); RecognitionConfig config = new RecognitionConfig { Encoding = AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = LanguageCodes.English.UnitedStates, UseEnhanced = true }; RecognizeResponse response = client.Recognize(config, audio); Console.WriteLine(response); // End sample Assert.Equal( "this is a test file for the google cloud speech api", response.Results[0].Alternatives[0].Transcript, true); }
private void btnSpeechInfo_Click(object sender, EventArgs e) { btnRecordVoice.Enabled = true; btnSave.Enabled = false; btnSpeechInfo.Enabled = false; if (File.Exists("audio.raw")) { var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile("audio.raw")); textBox1.Text = ""; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { textBox1.Text = textBox1.Text + " " + alternative.Transcript; } } if (textBox1.Text.Length == 0) { textBox1.Text = "No Data "; } } else { textBox1.Text = "Audio File Missing "; } }
/// <summary> /// Sends a speech recognition request to the speech service /// </summary> /// <param name="audioFile">The audio file.</param> /// <param name="locale">The locale.</param> /// <returns> /// A task /// </returns> public async Task <string> TranslateToText(string audioFile, string locale = "en-GB") { var preferences = new Preferences(locale, _serviceUrl, new CognitiveServicesAuthorizationProvider(_subscriptionKey)); // Create a a speech client using (var speechClient = new SpeechClient(preferences)) { speechClient.SubscribeToPartialResult(this.OnPartialResult); speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult); try { // create an audio content and pass it a stream. using (var downloadStream = new WebClient()) using (var audio = new MemoryStream(downloadStream.DownloadData(audioFile))) { var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600"); var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0"); var requestMetadata = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService"); await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token) .ConfigureAwait(false); } } catch (Exception e) { if (e is PlatformNotSupportedException) { return(await TranslateToTextFallback(audioFile).ConfigureAwait(false)); //fallback for when websockets are not supported } } } return(string.Empty); }
public async Task AsyncRecognizeAsync_RequestObject() { // Snippet: AsyncRecognizeAsync(AsyncRecognizeRequest,CallSettings) // Create client SpeechClient speechClient = await SpeechClient.CreateAsync(); // Initialize request argument(s) AsyncRecognizeRequest request = new AsyncRecognizeRequest { Config = new RecognitionConfig(), Audio = new RecognitionAudio(), }; // Make the request Operation <AsyncRecognizeResponse> response = await speechClient.AsyncRecognizeAsync(request); // Poll until the returned long-running operation is complete Operation <AsyncRecognizeResponse> completedResponse = await response.PollUntilCompletedAsync(); // Retrieve the operation result AsyncRecognizeResponse result = completedResponse.Result; // Or get the name of the operation string operationName = response.Name; // This name can be stored, then the long-running operation retrieved later by name Operation <AsyncRecognizeResponse> retrievedResponse = await speechClient.PollOnceAsyncRecognizeAsync(operationName); // Check if the retrieved long-running operation has completed if (retrievedResponse.IsCompleted) { // If it has completed, then access the result AsyncRecognizeResponse retrievedResult = retrievedResponse.Result; } // End snippet }
/// <summary> /// Sends a speech recognition request to the speech service /// </summary> /// <param name="audioFile">The audio file.</param> /// <param name="locale">The locale.</param> /// <param name="serviceUrl">The service URL.</param> /// <param name="subscriptionKey">The subscription key.</param> /// <returns> /// A task /// </returns> /// public async Task Run(string audioFile, string locale, Uri serviceUrl, string subscriptionKey, string episode) { // create the preferences object var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey)); string simpleFilename = Path.GetFileNameWithoutExtension(audioFile); if (int.TryParse(episode, out int outepisode)) { episodenumber = outepisode; } else { return; } htmlOutputFilePath = Path.GetDirectoryName(audioFile) + @"\" + episodenumber.ToString() + ".html"; opmlOutputFilePath = Path.GetDirectoryName(audioFile) + @"\" + episodenumber.ToString() + ".opml"; // Create a a speech client using (var speechClient = new SpeechClient(preferences)) { //speechClient.SubscribeToPartialResult(this.OnPartialResult); speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult); // create an audio content and pass it a stream. using (var audio = new FileStream(audioFile, FileMode.Open, FileAccess.Read)) { var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600"); var applicationMetadata = new ApplicationMetadata("NA Transcribe", "1.0.0"); var requestMetadata = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "NATranscriptService"); await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false); } } }
private void Button_WOC2_Click_1(object sender, EventArgs e)///Convert to text///// { recordBtn.Enabled = true; audioSaveBtn.Enabled = false; convertBtn.Enabled = false; if (File.Exists("audio.raw")) { var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "en", }, RecognitionAudio.FromFile("audio.raw")); richTextBox1.Text = ""; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { richTextBox1.Text = richTextBox1.Text + " " + alternative.Transcript; } } if (richTextBox1.Text.Length == 0) { richTextBox1.Text = "No Data "; } } else { richTextBox1.Text = "Audio File Missing "; } }
/// <summary> /// Print confidence level for individual words in a transcription of a short audio file /// Separating different speakers in an audio file recording /// </summary> /// <param name="localFilePath">Path to local audio file, e.g. /path/audio.wav</param> public static void SampleLongRunningRecognize(string localFilePath) { SpeechClient speechClient = SpeechClient.Create(); // string localFilePath = "resources/commercial_mono.wav" LongRunningRecognizeRequest request = new LongRunningRecognizeRequest { Config = new RecognitionConfig { // If enabled, each word in the first alternative of each result will be // tagged with a speaker tag to identify the speaker. EnableSpeakerDiarization = true, // Optional. Specifies the estimated number of speakers in the conversation. DiarizationSpeakerCount = 2, // The language of the supplied audio LanguageCode = "en-US", }, Audio = new RecognitionAudio { Content = ByteString.CopyFrom(File.ReadAllBytes(localFilePath)), }, }; // Poll until the returned long-running operation is complete LongRunningRecognizeResponse response = speechClient.LongRunningRecognize(request).PollUntilCompleted().Result; foreach (var result in response.Results) { // First alternative has words tagged with speakers SpeechRecognitionAlternative alternative = result.Alternatives[0]; Console.WriteLine($"Transcript: {alternative.Transcript}"); // Print the speakerTag of each word foreach (var word in alternative.Words) { Console.WriteLine($"Word: {word.Word}"); Console.WriteLine($"Speaker tag: {word.SpeakerTag}"); } } }
/// <summary>Snippet for LongRunningRecognizeAsync</summary> public async Task LongRunningRecognizeRequestObjectAsync() { // Snippet: LongRunningRecognizeAsync(LongRunningRecognizeRequest, CallSettings) // Additional: LongRunningRecognizeAsync(LongRunningRecognizeRequest, CancellationToken) // Create client SpeechClient speechClient = await SpeechClient.CreateAsync(); // Initialize request argument(s) LongRunningRecognizeRequest request = new LongRunningRecognizeRequest { Config = new RecognitionConfig(), Audio = new RecognitionAudio(), OutputConfig = new TranscriptOutputConfig(), }; // Make the request Operation <LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = await speechClient.LongRunningRecognizeAsync(request); // Poll until the returned long-running operation is complete Operation <LongRunningRecognizeResponse, LongRunningRecognizeMetadata> completedResponse = await response.PollUntilCompletedAsync(); // Retrieve the operation result LongRunningRecognizeResponse result = completedResponse.Result; // Or get the name of the operation string operationName = response.Name; // This name can be stored, then the long-running operation retrieved later by name Operation <LongRunningRecognizeResponse, LongRunningRecognizeMetadata> retrievedResponse = await speechClient.PollOnceLongRunningRecognizeAsync(operationName); // Check if the retrieved long-running operation has completed if (retrievedResponse.IsCompleted) { // If it has completed, then access the result LongRunningRecognizeResponse retrievedResult = retrievedResponse.Result; } // End snippet }
static object Rec(string filePath, int bitRate, RecognitionConfig.Types.AudioEncoding encoding) { var speech = SpeechClient.Create(); //var response = speech.Recognize(new RecognitionConfig() //{ // Encoding = encoding, // SampleRateHertz = bitRate, // LanguageCode = "en", //}, RecognitionAudio.FromFile(filePath)); //foreach (var result in response.Results) //{ // foreach (var alternative in result.Alternatives) // { // Console.WriteLine(alternative.Transcript); // } //} var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = encoding, //RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = bitRate, LanguageCode = "en", }, RecognitionAudio.FromFile(filePath)); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine(alternative.Transcript); } } return(0); }