0

What is the code for implementing the Google Speech API in my C# based application? I found out that it is possible to create an audio file and sent it to http://slides.html5rocks.com/#speech-input and receive it as text. Could you please explain how to do this or provide me with the code if you have attempted this before? Been stuck here for a while now

Much appreciated.

Code So far:

    SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
    SpeechSynthesizer dummy = new SpeechSynthesizer();


    public Form1()
    {
        InitializeComponent();


        Choices searching = new Choices("Porsche");
        GrammarBuilder searchService = new GrammarBuilder("Search");

        searchService.Append(searching);


        // Create a Grammar object from the GrammarBuilder and load it to the  recognizer.
        Grammar googleGrammar = new Grammar(searchService); ;
        rec.RequestRecognizerUpdate();
        rec.LoadGrammar(googleGrammar);

        // Add a handler for the speech recognized event.
        rec.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);

        // Configure the input to the speech recognizer.
        rec.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous speech recognition.
        rec.RecognizeAsync(RecognizeMode.Multiple);
    }




    private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        try
        {
            FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
            BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
            byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
            FS_Audiofile.Close();
            BR_Audiofile.Close();

            HttpWebRequest _HWR_SpeechToText = null;

            _HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");

            _HWR_SpeechToText.Method = "POST";
            _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
            _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
            _HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);

            HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
            if (HWR_Response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                textBox1.Text = SR_Response.ToString();

            }

        }
        catch (Exception ex)
        {

        }  
    }

This does not return any value from Google.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
user3306938
  • 37
  • 2
  • 8
  • 1
    stuck where? post your attempts and where its not working for you, SO is not a free code service, we are not here to code for you, but to help you getting your code to work. – Prix Feb 16 '14 at 06:57
  • 1
    Welcome to Stack Overflow! Please do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Feb 16 '14 at 09:15
  • @user3306938 you forgot to state what is wrong with your code and not working for you. – Prix Feb 16 '14 at 10:04
  • @Prix It's there below the code. basically a string value of my recording doesnt get printed – user3306938 Feb 16 '14 at 11:18
  • google has shut down the api url that u try to use... https://www.google.com/intl/en/chrome/demos/speech.html check the link and check the chromium source that backs the link. try following thread : http://mikepultz.com/2013/07/google-speech-api-full-duplex-php-version/ – Robert Rowntree Feb 16 '14 at 23:43
  • its not shut down according to the end of that last link. you may need to supply your api key and your client IP addr as stated in the post. – Robert Rowntree Feb 16 '14 at 23:49

2 Answers2

2

the following works in curl as long as the file sent is not too long... under 5 seconds.

curl -X POST -H "Content-Type: audio/x-flac; rate=16000" \ -T seg_1.flac "https://www.google.com/speech-api/v1/recognize? \ xjerr=1&client=speech2text&maxresults=1&lang=en-US&key=...48593"

{"status":0,"id":"","hypotheses":[{"utterance":"now it was the favorite pastime","confidence":0.95148802}]}

So, encode to speechX or flac

include a parm with your sample rate from the recording

include your key

keep the file short in duration ( you will have to split files prior to API access )

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
1

The FS_Audiofile filestream you are sending to Google is empty, that's why you are not receiving anything back.

You are missing this call:

recognizedAudio.WriteToAudioStream(FS_Audiofile);
bre_dev
  • 562
  • 3
  • 21