This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Amazon Polly
#8
(02.05.2017, 06:10)admin Wrote: Here's an example on how to access Amazon Polly. Make sure you set region, accesskey, secretkey and voice variables correctly.

This has been tested on the latest RC1 firmware, but you also need to install package updates to have support for sha256 function:
Standard LM: https://dl.openrb.com/misc/luaencdec_20170426_mxs.ipk
LM power version: https://dl.openrb.com/misc/luaencdec_20170426_imx6.ipk

Code:
function getpollyurl(text)  -- CONFIG VARIABLES  local region = 'eu-west-1' -- your AWS region  local accesskey = 'xxx'  local secretkey = 'yyy'  local voice = 'Joey' -- list of voices: https://aws.amazon.com/polly/details/  -- same as JS encodeURIComponent  local function encode(str)    return (str:gsub("[^%w%-_%.%!%~%*%'%(%)]", function(c)      return string.format("%%%02X", c:byte(1,1))    end))  end  function toquerystring(items)    local res = {}    for _, item in ipairs(items) do      res[ #res + 1 ] = item[ 1 ] .. '=' .. encode(item[ 2 ])    end    return table.concat(res, '&')  end  function getkey(key, datestamp, regionname, servicename)    local kdate = encdec.hmacsha256(datestamp, 'AWS4' .. key, true)    local kregion = encdec.hmacsha256(regionname, kdate, true)    local kservice = encdec.hmacsha256(servicename, kregion, true)    return encdec.hmacsha256('aws4_request', kservice, true)  end  local encdec = require('encdec')  local method = 'GET'  local service = 'polly'  local host = service .. '.' .. region .. '.amazonaws.com'  local api = '/v1/speech'  local endpoint = 'https://' .. host .. api  local amzdate = os.date('!%Y%m%dT%H%M%SZ')  local datestamp = os.date('!%Y%m%d')  local algorithm = 'AWS4-HMAC-SHA256'  local canonicaluri = api  local canonicalheaders = 'host:' .. host  local signedheaders = 'host'  local credentialscope = datestamp .. '/' .. region .. '/' .. service .. '/' .. 'aws4_request'  local canonicalquerystring = toquerystring({    { 'OutputFormat', 'mp3' },    { 'Text', text },    { 'TextType', 'text' },    { 'VoiceId', voice },    { 'X-Amz-Algorithm', algorithm },    { 'X-Amz-Credential', accesskey .. '/' .. credentialscope },    { 'X-Amz-Date', amzdate },    { 'X-Amz-SignedHeaders', signedheaders },  })  local payloadhash = encdec.sha256('')  local canonicalrequest = table.concat({    method,    api,    canonicalquerystring,    canonicalheaders,    '',    signedheaders,    payloadhash  }, '\n')  local stringtosign = table.concat({    algorithm,    amzdate,    credentialscope,    encdec.sha256(canonicalrequest)  }, '\n')  local signingkey = getkey(secretkey, datestamp, region, service)  local signature = encdec.hmacsha256(stringtosign, signingkey)  return endpoint .. '?' .. canonicalquerystring .. '&X-Amz-Signature=' .. signature end url = getpollyurl('testing polly from logic machine') require('ssl.https') res, status = ssl.https.request(url) if res then  -- res contains the mp3 file else  alert('Polly request failed: ' .. tostring(status)) end

Hi admin,
Thanks a lot! I can confirm that it works as expected on RC1...
BR, Bart
Reply


Messages In This Thread
Amazon Polly - by bmodeco - 25.04.2017, 20:43
RE: Amazon Polly - by gjniewenhuijse - 26.04.2017, 07:27
RE: Amazon Polly - by bmodeco - 26.04.2017, 09:38
RE: Amazon Polly - by bmodeco - 26.04.2017, 18:16
RE: Amazon Polly - by admin - 27.04.2017, 05:53
RE: Amazon Polly - by bmodeco - 27.04.2017, 13:31
RE: Amazon Polly - by admin - 02.05.2017, 06:10
RE: Amazon Polly - by bmodeco - 02.05.2017, 20:14

Forum Jump: