How to do it
I am not quite the wizzard when it comes to HTTP, so me having figured out how to apply the Google Speech API in R is either sheer luck or good work by Google. It took me half a day mainly fiddling around with API keys and quality specs of the audio file (all is described clearly in the online documentation - as I found out at the end) . But now that it works I can really appreciate the power and precision of this tool. And it handles Dutch too!
library(httr)
library(RJSONIO)
library(base64enc)
# The API currently only accepts base64encoded data
audio_enc <- base64encode("bomans1.flac")
# Use the browser key from the Google Speech API (in this case hidden in the .Renviron file)
key <- Sys.getenv("SPEECH_API")
url <- paste0("https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=", key)
r <-
POST(url = url
,
body = toJSON(list(
config = c(
encoding = "FLAC",
sample_rate = 22050,
languageCode = "nl-NL"
),
audio = c(content = audio_enc)
))
)
content(r)$results[[1]]$alternatives[[1]]$transcript
## [1] "Ik zit mij bij het vensterraam dadelijk te vervelen Ik wou dat ik twee hondjes was dan konden we samen spelen"
You have to wait for around 20 seconds to receive the return json file from the Speech Engine.