public string ConvertSpeechToText(byte[] bytes) { System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Speech-206301277cf5.json"); var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, LanguageCode = "en", }, RecognitionAudio.FromBytes(bytes)); string text = ""; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { text += alternative.Transcript; } } return(text); }
object AsyncRecognizeGcs(string storageUri) { try { var speech = SpeechClient.Create(); var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, Model = voiceModelDropdown.SelectedItem.ToString(), EnableAutomaticPunctuation = enableAutoPunctuationDropdown.SelectedItem.ToString() == "Yes" ? true : false, //true if yes selected // SampleRateHertz = processor.getSampleRate(filePath), //another customisable option LanguageCode = languageCodeBox.Text.ToString(), }, RecognitionAudio.FromStorageUri(storageUri)); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { richTextBox1.Text = alternative.Transcript; richTextBox1.BeginInvoke((MethodInvoker) delegate { richTextBox1.AppendText(alternative.Transcript + "\n"); }); //delegate to write to a control from different thread transcribedText += alternative.Transcript + "\n"; Console.WriteLine($"Transcript: { alternative.Transcript}"); } } processingLabel.Visible = false; saveTranscriptBtn.Enabled = true; } catch (Exception ex) { MessageBox.Show("An error occured, see the cloud error reporting page for details"); cloudErrors.report(ex); } return(0); }
private void transcribeBtn_Click(object sender, EventArgs e) { processingLabel.Visible = true; //need to add some sort of loading screen cause this takes a bit of time if (filePath.Contains(".mp3")) { //convert mp3 to wav - not required currently } newFilePath = filePath + "m.wav"; //have to create new file, but it will be deleted after it's used if (URIString == "") { if (audioProcessor.getDuration(filePath) <= new TimeSpan(0, 1, 0)) { audioProcessor.ConvertStereoToMono(filePath, newFilePath); //usually files are recorded stereo, we need mono here var speech = SpeechClient.Create(); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, Model = voiceModelDropdown.SelectedItem.ToString(), EnableAutomaticPunctuation = enableAutoPunctuationDropdown.SelectedItem.ToString() == "Yes" ? true : false, //true if yes selected // SampleRateHertz = processor.getSampleRate(filePath), //another customisable option LanguageCode = languageCodeBox.Text.ToString(), //maxAlternatives, profanityFilter, speechContext }, RecognitionAudio.FromFile(newFilePath)); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { richTextBox1.Text = alternative.Transcript; transcribedText += alternative.Transcript; } } saveTranscriptBtn.Enabled = true; processingLabel.Visible = false; } else //if file is longer than 1 minute { MessageBox.Show("File is longer than 1 minute, please upload the file to Google Cloud and use an URI to access it"); transcribeBtn.Enabled = false; processingLabel.Visible = false; } } else //if URI string has been provided { try { // asyncTranscribe(URIString); AsyncRecognizeGcs(URIString); //"gs://s2t-test-bucket1/speech.mp3m.wav" } catch (UriFormatException UriException) { //write to an error log maybe MessageBox.Show("Invalid URI string"); cloudErrors.report(UriException); } } File.Delete(newFilePath); }