{"name":"Polly Pantheon","key":"ppa","version":"1.0.1","instructions":"Enter the text to be read in the text area. Choose a voice and hear it read","showatto":"1","showplayers":"0","requirecss":"","requirejs":"","shim":"","defaults":"format=\"text|ssml\"","amd":"1","body":"<audio id=\"@@AUTOID@@_audioplayer\" class=\"nomediaplugin\" controls></audio><br>\n<textarea id=\"@@AUTOID@@_textbox\" cols=\"60\" rows=\"5\"></textarea><br>\n<select id=\"@@AUTOID@@_language\">\n</select>\n<select id=\"@@AUTOID@@_voice\">\n</select><br>\n<a id=\"@@AUTOID@@_playbutton\" class=\"btn btn-success\"  width=\"400px\">PLAY</a><br>\n<input id=\"@@AUTOID@@_filename\" type=\"text\" length=\"250\" value=\"filename\"></input>\n<a id=\"@@AUTOID@@_downbutton\" class=\"btn btn-secondary\" href=\"#\" width=\"200px\" download=\"audiofile\">Download</a>\n<!-- @@CLOUDPOODLLTOKEN@@ -->","bodyend":"","script":"var langselect = $('#' + @@AUTOID@@ + '_language');\nvar voiceselect = $('#' + @@AUTOID@@ + '_voice');\nvar poodllfilelib = M.cfg.wwwroot + '/filter/poodll/poodllfilelib.php?datatype=speaktext&paramone=';\nvar thetextarea = $('#' + @@AUTOID@@+ '_textbox');\nvar thefilename = $('#' + @@AUTOID@@+ '_filename');\nvar theplaybutton = $('#' + @@AUTOID@@+ '_playbutton');\nvar thedownloadbutton = $('#' + @@AUTOID@@+ '_downbutton');\nvar theplayer = $('#' + @@AUTOID@@ + '_audioplayer');\nvar langoptiontemplate ='<option value=\"@lang@\">@lang@</option>';\nvar voiceoptiontemplate ='<option value=\"@voice@\">@voice@(@mf@)</option>';\n\nvar langvoices=[\n {\"lang\": \"English(US)\", \"voices\":  [{name: 'Joey', mf: 'm'},{name: 'Kendra', mf: 'f'}]},\n{\"lang\": \"English(GB)\", \"voices\":  [{name: 'Brian', mf: 'm'},{name: 'Amy', mf: 'f'}]}, \n{\"lang\": \"English(AU)\", \"voices\": [{name: 'Russell', mf: 'm'},{name: 'Nicole', mf: 'f'}]},   \n{\"lang\": \"English(IN)\", \"voices\":  [{name: 'Aditi', mf: 'm'},{name: 'Raveena', mf: 'f'}]}, \n{\"lang\": \"English(WELSH)\", \"voices\":  [{name: 'Geraint', mf: 'm'}]},\n{\"lang\": \"Danish\", \"voices\":  [{name: 'Mads', mf: 'm'},{name: 'Naja', mf: 'f'}]},  \n{\"lang\": \"Dutch\", \"voices\":  [{name: 'Ruben', mf: 'm'},{name: 'Lotte', mf: 'f'}]}, \n{\"lang\": \"French(FR)\", \"voices\":  [{name: 'Mathieu', mf: 'm'},{name: 'Celine', mf: 'f'}]},  \n{\"lang\":  \"French(CA)\", \"voices\":  [{name: 'Chantal', mf: 'm'}]},\n{\"lang\": \"German\", \"voices\":  [{name: 'Hans', mf: 'm'},{name: 'Marlene', mf: 'f'}]},\n{\"lang\": \"Icelandic\", \"voices\":  [{name: 'Karl', mf: 'm'},{name: 'Dora', mf: 'f'}]},\n{\"lang\": \"Italian\", \"voices\":  [{name: 'Carla', mf: 'f'},{name: 'Bianca', mf: 'f'},{name: 'Giorgio', mf: 'm'}]}, \n{\"lang\": \"Japanese\", \"voices\":  [{name: 'Takumi', mf: 'm'},{name: 'Mizuki', mf: 'f'}]},   \n{\"lang\": \"Korean\", \"voices\":  [{name: 'Seoyan', mf: 'f'}]},\n{\"lang\": \"Norwegian\", \"voices\":  [{name: 'Liv', mf: 'f'}]},\n{\"lang\": \"Polish\", \"voices\":  [{name: 'Jacek', mf: 'm'},{name: 'Ewa', mf: 'f'}]}, \n{\"lang\": \"Portugese(BR)\", \"voices\":  [{name: 'Ricardo', mf: 'm'},{name: 'Vitoria', mf: 'f'}]},   \n{\"lang\": \"Portugese(PT)\", \"voices\":  [{name: 'Cristiano', mf: 'm'},{name: 'Ines', mf: 'f'}]}, \n{\"lang\": \"Romanian\", \"voices\":  [{name: 'Carmen', mf: 'f'}]},\n{\"lang\": \"Russian\", \"voices\":  [{name: 'Maxim', mf: 'm'},{name: 'Tatyana', mf: 'f'}]},  \n{\"lang\": \"Spanish(ES)\", \"voices\":  [{name: 'Enrique', mf: 'm'},{name: 'Conchita', mf: 'f'}]},  \n{\"lang\": \"Spanish(US)\", \"voices\":  [{name: 'Miguel', mf: 'm'},{name: 'Penelope', mf: 'f'}]},  \n{\"lang\": \"Swedish\", \"voices\":  [{name: 'Astrid', mf: 'f'}]},\n{\"lang\": \"Turkish\", \"voices\":  [{name: 'Filiz', mf: 'f'}]},\n{\"lang\": \"Welsh\", \"voices\":  [{name: 'Gwyneth', mf: 'f'}]}, \n];\n\n//init drop downs\n$.each(langvoices, function(index,voiceset){\n   langselect.append(langoptiontemplate.replace(/@lang@/gi,voiceset.lang)); \n});\n\n//init events\n//play button event\ntheplaybutton.on('click',function(){\n   var usetext = thetextarea.val();\n   var usevoice = voiceselect.val();\n   fetch_polly_url(usetext,usevoice,function(audiourl){ \n       play_audio(audiourl);\n   });\n  \n});\n\n//download button event\nthedownloadbutton.on('click',function(){\n   var usetext = thetextarea.val();\n   var uselang = langselect.val();\n   var usevoice = voiceselect.val();\n   fetch_polly_url(usetext,usevoice,function(audiourl){\n        var filename=thefilename.val() + '_' + uselang + '_' + usevoice;\n        thedownloadbutton.attr('download',filename);\n        thedownloadbutton.attr('href',audiourl);\n   });\n  \n});\n\n//lang change event\nlangselect.on('change',function(){\n    voiceselect.find('option').remove();\n   var thelang =this.value;\n   $.each(langvoices, function(index,voiceset){\n      if(voiceset.lang==thelang){\n      \n         $.each(voiceset.voices, function(index,voice){\n              var newoption =voiceoptiontemplate.replace(/@voice@/gi,voice.name);\n              newoption = newoption.replace(/@mf@/gi,voice.mf);\n              voiceselect.append(newoption); \n         });\n       } \n   });\n});\n\n//set first item in lang to kick it all off\nlangselect.trigger(\"change\")\n\n//fetch audio url\nfunction fetch_polly_url(speaktext, voice, callback) {\n\n                //The REST API we are calling\n                var functionname = 'local_cpapi_fetch_polly_url';\n\n                //fetch the Posturl. We need this.\n                //set up our ajax request\n                var xhr = new XMLHttpRequest();\n                var that = this;\n\n                //set up our handler for the response\n                xhr.onreadystatechange = function (e) {\n                    if (this.readyState === 4) {\n                        if (xhr.status == 200) {\n\n                            //get a yes or forgetit or tryagain\n                            var payload = xhr.responseText;\n                            var payloadobject = JSON.parse(payload);\n                            if (payloadobject) {\n                                //returnCode > 0  indicates an error\n                                if (payloadobject.returnCode > 0) {\n                                    console.log(payloadobject.returnMessage);\n                                    return false;\n                                    //if all good, then lets do the embed\n                                } else if (payloadobject.returnCode === 0){\n                                    var pollyurl = payloadobject.returnMessage;\n                                    callback(pollyurl);\n                                } else {\n                                    console.log('Polly Signed URL Request failed:');\n                                    console.log(payloadobject);\n                                }\n                            } else {\n                                console.log('Polly Signed URL Request something bad happened');\n                            }\n                        } else {\n                            console.log('Polly Signed URL Request Not 200 response:' + xhr.status);\n                        }\n                    }\n                };\n\n                //make our request\n                var xhrparams = \"wstoken=\" + @@CLOUDPOODLLTOKEN@@\n                + \"&wsfunction=\" + functionname\n                + \"&moodlewsrestformat=\" + 'json'\n                + \"&text=\" + encodeURIComponent(speaktext)\n                + '&texttype=text'\n                + '&voice=' + voice\n                + '&appid=' + 'filter_poodll'\n                + '&owner=poodll'\n                + '&region=useast1';\n\n                var serverurl = 'https://cloud.poodll.com' + \"/webservice/rest/server.php\";\n                xhr.open(\"POST\", serverurl, true);\n                xhr.setRequestHeader(\"Cache-Control\", \"no-cache\");\n                xhr.setRequestHeader(\"Content-Type\", \"application/x-www-form-urlencoded\");\n                xhr.send(xhrparams);\n }\n\n//play whatever\nfunction play_audio(audiourl){\n  theplayer.attr('src',audiourl);\n  theplayer[0].play();\n}","style":"","dataset":"","datasetvars":"","alternate":"","alternateend":""}