Sub Class_Globals
Private mBaseLink As String = "https://speech.googleapis.com/v1/speech:recognize?key="
Private Const GOOGLE_SPEECH_API_KEY As String = "xxxxxx..."
End Sub
Public Sub Initialize
End Sub
Private Sub getDataBytesFromAudio( dir As String, filename As String) As String
Dim su As StringUtils
Dim s As String = su.EncodeBase64(File.ReadBytes(dir,filename))
Return s
End Sub
Private Sub recognizeAudio(cbk As Object, event As String, dir As String, fileName As String) As ResumableSub
Dim resultMap As Map = CreateMap("Success":False, "Transcription":"", "Confidence": 0.0f, "ErrorCause":"Other" )
Dim link As String = mBaseLink & GOOGLE_SPEECH_API_KEY
Dim data As String = $"{"audio": {"content":"${getDataBytesFromAudio(dir,fileName)}"},"config": {"languageCode": "en-US"}}"$
Log(data)
Dim j As HttpJob
j.Initialize("recognize", Me)
j.PostString(link, data)
j.GetRequest.SetHeader("Content-Type", "application/json")
j.GetRequest.SetContentType("application/json")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim res As String = j.GetString
Log("Received answer: "&res)
Dim JSON As JSONParser
JSON.Initialize(res)
If res.StartsWith("{") Then
Dim m As Map = JSON.NextObject
Dim resultList As List = m.Get("results")
If resultList<>Null And resultList.IsInitialized Then
Dim m2 As Map = resultList.Get(0)
Dim l2 As List = m2.GetDefault("alternatives",Null)
If l2<>Null And l2.IsInitialized Then
Dim m3 As Map = l2.Get(0)
Dim transcription As String = m3.GetDefault("transcript","Not found")
Dim confidence As Float = m3.GetDefault("confidence", 0)
resultMap.Put("Success", True)
resultMap.Put("Transcription", transcription)
resultMap.Put("Confidence", confidence)
resultMap.Put("ErrorCause","")
End If
End If
Else
resultMap.Put("ErrorCause", "Could not parse result: "&res)
End If
Else
resultMap.Put("ErrorCause", "HTTP Job did not succeed")
End If
If event.Length>0 Then
CallSubDelayed2(cbk, event&"_finished", resultMap)
End If
Return resultMap
End Sub
Public Sub recognizeAudioSYNC( dir As String, filename As String) As ResumableSub
wait for (recognizeAudio(Null, "", dir, filename)) Complete (res As Map)
Return res
End Sub
Public Sub recognizeAudioASYNC(callBack As Object, event As String, dir As String, fileName As String)
recognizeAudio(callBack, event, dir, fileName)
End Sub