Jump to content

Sneak Preview!


Tom Wellige
 Share


Recommended Posts

There will be two new Open ECR Extensions coming to the forum soon (with the release of SwyxWare 14.10).

 

Maybe you can figure from the code of one of the two upcoming extensions what they will be about ;) 

 


...

---------------------------------------------------------------------
--
-- GetAccessToken
--
-- Gets fresh access token from azure portal.
--
-- Parameter:
--
-- Returns:
--
---------------------------------------------------------------------
function AzureTTSClass:GetAccessToken()
    PBXScript.OutputTrace ("-------> AzureTTS:GetAccessToken()")

    local sURL, oWebRequest, nResponseCode, sResponseBody

    sURL = AZURETTS_URL_TOKEN
    sURL = StringReplace(sURL, "#REGION#", self.m_sRegion)
    PBXScript.OutputTrace ("sURL = "  .. sURL)

    local webreq = require "PBXWebRequest"
    if (webreq ~= nil) then

        local hSession, nResponseCode, sResponseBody    

        hSession = webreq.new()

        hSession:HttpAuth  (PBXHttpAuthNone)
        hSession:HttpVerb  (PBXHttpVerbPost)
        hSession:URL       (sURL)
        hSession:AddHeader ("Ocp-Apim-Subscription-Key: " .. self.m_sSubscriptionKey)

        nResponseCode = hSession:Execute()
        sResponseBody = hSession:ResponseBody()
        PBXScript.OutputTrace ("nResponseCode = " .. nResponseCode)


        if (nResponseCode == 200) then
            self.m_sAccessToken    = sResponseBody
        else
            self.m_sResult         = AZURETTS_RETURN_FAILED
            self.m_nLastError      = AZURETTS_ERROR_WEBREQUEST
            self.m_sLastErrorDescr = "Azure web request failed! (" .. sResponseBody .. ")"
            PBXScript.OutputTrace (self.m_sLastErrorDescr)
        end

        self.m_nLatestResponseCode = nResponseCode
    end

    PBXScript.OutputTrace ("<------- AzureTTS:GetAccessToken()")
end


---------------------------------------------------------------------
--
-- RunTTS
--
-- Runs request against cognitive services to return generated wav audio file.
--
-- Parameter:
--
-- Returns:
--
---------------------------------------------------------------------
function AzureTTSClass:RunTTS()
    PBXScript.OutputTrace ("-------> AzureTTSClass:RunTTS()")

    local sURL, sHost, sRequestBody

    sURL = AZURETTS_URL_TTS
    sURL = StringReplace(sURL, "#REGION#",   self.m_sRegion)
    PBXScript.OutputTrace ("sURL = "    .. sURL)

    sHost = AZURETTS_HEADER_HOST
    sHost = StringReplace(sHost, "#REGION#", self.m_sRegion)
    PBXScript.OutputTrace ("sHost = "   .. sHost)

    if (self.m_sOwnSSML ~= "") then
        sRequestBody = self.m_sOwnSSML
    else
        sRequestBody = AZURETTS_REQUEST_BODY
        sRequestBody = StringReplace(sRequestBody, "#LNG#",    self.m_sLanguage)
        sRequestBody = StringReplace(sRequestBody, "#GENDER#", self.m_sGender)
        sRequestBody = StringReplace(sRequestBody, "#VOICE#",  self.m_sVoice)
        sRequestBody = StringReplace(sRequestBody, "#TEXT#",   self.m_sText)
    end
    PBXScript.OutputTrace ("sRequestBody = " .. sRequestBody)


    local webreq = require "PBXWebRequest"
    if (webreq ~= nil) then

        local hSession, nResponseCode, sResponseBody

        hSession = webreq.new()

        hSession:HttpAuth   (PBXHttpAuthBearer)
        hSession:AuthPass   (self.m_sAccessToken)
        hSession:HttpVerb   (PBXHttpVerbPost)
        hSession:RequestBody(sRequestBody)
        hSession:URL        (sURL)
        hSession:OutputFile (self.m_sWavFile)
        hSession:AddHeader  ("X-Microsoft-OutputFormat: " .. AZURETTS_HEADER_OUTPUT_FORMAT)
        hSession:AddHeader  ("Content-Type: " .. AZURETTS_HEADER_CONTENT_TYPE)
        hSession:AddHeader  ("Host: " .. sHost)
        hSession:AddHeader  ("User-Agent: " .. AZURETTS_HEADER_USER_AGENT)

        nResponseCode = hSession:Execute()
        sResponseBody = hSession:ResponseBody()
        PBXScript.OutputTrace ("nResponseCode = " .. nResponseCode)

        if (nResponseCode == 200) then
            self.m_sWavFile           = hSession:OutputFile()
            g_sAzureTTS_LatestWavFile = self.m_sWavFile
            PBXScript.OutputTrace ("g_sAzureTTS_LatestWavFile = " .. g_sAzureTTS_LatestWavFile)
        else
            self.m_sResult         = AZURETTS_RETURN_FAILED
            self.m_nLastError      = AZURETTS_ERROR_WEBREQUEST
            self.m_sLastErrorDescr = "Azure web request failed! (" .. sResponseBody .. ")"
            PBXScript.OutputTrace (self.m_sLastErrorDescr)
        end

        self.m_nLatestResponseCode = nResponseCode

    end

    PBXScript.OutputTrace ("<------- AzureTTSClass:RunTTS()")
end


---------------------------------------------------------------------
--
-- PlayWavFile
--
-- Plays the previously received wav file.
--
-- Parameter:
--
-- Returns:
--
---------------------------------------------------------------------
function AzureTTSClass:PlayWavFile()
    PBXScript.OutputTrace ("-------> AzureTTS:PlayWavFile()")

        PlaySound(self.m_sWavFile)

    PBXScript.OutputTrace ("<------- AzureTTS:PlayWavFile()")
end

...

 

 

If you are not too much into programing I have another hint for you...

 

image.png.0c59989532127ed541827d08bd678a76.png

 

 

 

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and have taken note of our Privacy Policy.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.