Transcribe Audio file to text file using google cloud speech API in vb.net

Shimshai

New member
Joined
Mar 22, 2018
Messages
2
Programming Experience
10+
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 with line: 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?
VB.NET:
    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
 
Firstly, please don't post code as HTML because it makes it harder to read. Post it as plain text in appropriate code formatting tags, i.e.

[xcode=vb]your code here[/xcode]

which will add syntax highlighting automatically or, if you need to add specific formatting yourself, e.g. bold or colour a specific line:

[code]your code here[/code]

As for the code, you haven't actually described what's wrong. Where exactly in the code does the behaviour differ from your expectation and how? If you don't know, you need to debug to find out. If you don't know how to debug properly, i.e. set breakpoints and step through the code, then you can start learning here.
 
Back
Top