0

I cannot figure out the Google Cloud speech API to transcribe a .wav file to a .txt file using vb.net.

The authentication is working:

AuthExplicit2("AudioTranscription", "C:\AudioTranscription\GoogleAuthentication\GoogleTranscriptionAuthentication.json")

I have tried finding code examples to use as a template but i just can't figure out the code. Can anyone help me with this?

    Try
        Dim fileStream As FileStream = File.OpenRead("C:\AudioTranscription\Audio\dictation-partial.flac")
        'Dim memoryStream As MemoryStream = File.OpenWrite("C:\AudioTranscription\Audio\dictation-partial.flac")
        'Dim memoryStream As MemoryStream = New MemoryStream("C:\AudioTranscription\Audio\dictation-partial.txt")
        memoryStream.SetLength(fileStream.Length)
        fileStream.Read(memoryStream.GetBuffer(), 0, CInt(fileStream.Length))
        Dim BA_AudioFile As Byte() = memoryStream.GetBuffer()
        Dim _HWR_SpeechToText As HttpWebRequest = Nothing
        '_HWR_SpeechToText = CType(HttpWebRequest.Create("https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE"), HttpWebRequest)
        _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials
        _HWR_SpeechToText.Method = "POST"
        _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100"
        _HWR_SpeechToText.ContentLength = BA_AudioFile.Length
        Dim stream As Stream = _HWR_SpeechToText.GetRequestStream()
        stream.Write(BA_AudioFile, 0, BA_AudioFile.Length)
        stream.Close()
        Dim HWR_Response As HttpWebResponse = CType(_HWR_SpeechToText.GetResponse(), HttpWebResponse)

        If HWR_Response.StatusCode = HttpStatusCode.OK Then
            Dim SR_Response As StreamReader = New StreamReader(HWR_Response.GetResponseStream())
            Console.WriteLine(SR_Response.ReadToEnd())
        End If
    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try

    Console.ReadLine()
    imgAudioFile.Visible = True
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
Darrell
  • 49
  • 8

1 Answers1

0

The code within the public documentation requires either client libraries or making a post request. As you're using the post request, you should upload your audio file to Cloud Storage to use the url within your request according to the documentation instead of using your local file. Regarding your POST request, you could send the JSON body on your post request based on this post.

F10
  • 2,843
  • 2
  • 12
  • 18