omegastripes

text_to_speech_google_api_simultaneously_mproc.vbs

May 5th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option explicit
  2. dim mproc
  3. set mproc = new multiprocess
  4. mproc.launch "base", "run", ""
  5.  
  6. ' main programm section
  7.  
  8. class base
  9.    
  10.     public sndfolder
  11.     private message
  12.    
  13.     public sub run()
  14.         dim elt, wmpqty
  15.         sndfolder = createobject("Shell.Application").namespace(&h0).self.path & "\tts\session" & left(createobject("Scriptlet.TypeLib").guid, 38) & "\"
  16.         smartcreatefolder sndfolder
  17.         wmpqty = inputbox("Number of talking uids")
  18.         message = wmpqty & " talking UIDs" & vbcrlf & vbcrlf
  19.         for elt = 0 to wmpqty - 1
  20.             host.createproc "playspeech", "run", ""
  21.         next
  22.         host.joint "playspeech", 25, 0
  23.         for each elt in host.gethost("playspeech")
  24.             host.hosts(elt).process.wmp.controls.play
  25.         next
  26.         wscript.echo message
  27.         for each elt in host.gethost("playspeech")
  28.             do while host.hosts(elt).process.wmp.playstate = 3
  29.                 wscript.sleep 10
  30.             loop
  31.         next
  32.         host.free host.id
  33.     end sub
  34.    
  35.     public sub playspeech_oninitialized(src)
  36.         src.permit = true
  37.     end sub
  38.    
  39.     public sub pushmessage(uid)
  40.         message = message & uid & vbcrlf
  41.     end sub
  42.    
  43.     private sub smartcreatefolder(strfolder)
  44.         with createobject("Scripting.FileSystemObject")
  45.             if not .folderexists(strfolder) then
  46.                 smartcreatefolder(.getparentfoldername(strfolder))
  47.                 .createfolder(strfolder)
  48.             end if
  49.         end with
  50.     end sub
  51.    
  52. end class
  53.  
  54. class playspeech
  55.    
  56.     public wmp
  57.    
  58.     public sub run()
  59.         dim uid, path
  60.         uid = left(createobject("Scriptlet.TypeLib").guid, 38)
  61.         scene.base.pushmessage uid
  62.         path = scene.base.sndfolder & uid & ".mp3"
  63.         filedownload "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=" & encodeuricomponent(uid), path
  64.         set wmp = createobject("WMPlayer.OCX")
  65.         wmp.url = path
  66.         wmp.controls.stop
  67.         host.state = 25
  68.         host.joint "base", 24, 0
  69.         host.free host.id
  70.     end sub
  71.    
  72.     private sub filedownload(url, path)
  73.         dim body
  74.         on error resume next
  75.         do
  76.             with createobject("Microsoft.XMLHTTP")
  77.                 .open "GET", url, false
  78.                 .send
  79.                 body = .responsebody
  80.             end with
  81.             if err = 0 then exit do
  82.             err.clear
  83.         loop
  84.         with createobject("Adodb.Stream")
  85.             .type = 1
  86.             .open
  87.             .write body
  88.             .savetofile path, 2
  89.             .close
  90.         end with
  91.     end sub
  92.  
  93.    
  94.     private function encodeuricomponent(text)
  95.         with createobject("htmlfile")
  96.             with .parentwindow
  97.                 .execscript ";", "jscript"
  98.                 encodeuricomponent = .encodeuricomponent(text)
  99.             end with
  100.         end with
  101.     end function
  102.    
  103. end class
  104.  
  105. ' do not modify service class section
  106.  
  107. class multiprocess
  108.    
  109.     public primary, ancestor, parent, process, err
  110.     public names, execs, hosts
  111.     public id, aid, isprimary
  112.     public classname, methodname, aliasname
  113.     public found, lost, active
  114.     public state, permit, release
  115.     private container, signature, wshshell
  116.    
  117.     public sub launch(startclassname, startmethodname, startaliasname)
  118.         permit = false
  119.         release = false
  120.         executeglobal "dim scene, host, ancestor, process"
  121.         if not isempty(host) then exit sub
  122.         set host = me
  123.         executeglobal "set host.err = err"
  124.         executeglobal "function getroot: set getroot = me: end function"
  125.         set parent = getroot
  126.         isprimary = not wscript.arguments.named.exists("task")
  127.         if isprimary then
  128.             dim sample
  129.             state = 24
  130.             randomize
  131.             signature = ""
  132.             do
  133.                 signature = signature & hex(rnd * 16)
  134.             loop while len(signature) < 16
  135.             aid = empty
  136.             id = 0
  137.             found = 0
  138.             lost = 0
  139.             set wshshell = createobject("WScript.Shell")
  140.             set primary = host
  141.             set ancestor = nothing
  142.             set process = nothing
  143.             set scene = parent
  144.             set parent.ancestor = nothing
  145.             set parent.process = nothing
  146.             set hosts = createobject("Scripting.Dictionary")
  147.             set execs = createobject("Scripting.Dictionary")
  148.             set names = createobject("Scripting.Dictionary")
  149.             classname = empty
  150.             methodname = empty
  151.             aliasname = empty
  152.             set hosts(0) = host
  153.             set container = getobject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
  154.             container.putproperty signature, parent
  155.             startproc startclassname, startmethodname, startaliasname
  156.             on error resume next
  157.             do
  158.                 for each sample in execs.keys
  159.                     if release or active = 0 then exit do
  160.                     if not (execs(sample) is nothing) then
  161.                         if execs(sample).status > 0 then
  162.                             abolish sample
  163.                         end if
  164.                     end if
  165.                     wscript.sleep 1
  166.                 next
  167.             loop
  168.             release = true
  169.             state = 28
  170.             scenequit
  171.         else
  172.             dim job
  173.             job = split(wscript.arguments.named("task"), ";")
  174.             signature = cstr(job(0))
  175.             do
  176.                 for each container in createobject("Shell.Application").windows
  177.                     if isobject(container.getproperty(signature)) then
  178.                         exit do
  179.                     end if
  180.                 next
  181.                 wscript.sleep 1
  182.             loop
  183.             aid = clng(job(1))
  184.             id = clng(job(2))
  185.             found = null
  186.             lost = null
  187.             set scene = container.getproperty(signature)
  188.             set primary = scene.host
  189.             set hosts = primary.hosts
  190.             set ancestor = hosts(aid)
  191.             if isresponsive(aid) then
  192.                 set parent.ancestor = ancestor.parent.process
  193.             else
  194.                 set parent.ancestor = nothing
  195.             end if
  196.             classname = cstr(job(3))
  197.             methodname = cstr(job(4))
  198.             aliasname = cstr(job(5))
  199.             state = 4
  200.             primary.implicate id, aliasname, host
  201.             executeglobal "set process = new " & classname
  202.             executeglobal "set host.process = process"
  203.             executeglobal "set scene." & aliasname & " = process"
  204.             if isresponsive(aid) then
  205.                 executeglobal "set host.ancestor.parent." & aliasname & " = process"
  206.             end if
  207.             state = 8
  208.             primary.staff host
  209.             ancestorevent "oninitialized"
  210.             state = 12
  211.             if methodname <> "" then
  212.                 do until permit
  213.                     wscript.sleep 10
  214.                 loop
  215.                 state = 16
  216.                 executeglobal "process." & methodname
  217.             end if
  218.             state = 20
  219.             ancestorevent "oncompleted"
  220.             state = 24
  221.             do until release
  222.                 wscript.sleep 10
  223.             loop
  224.             state = 28
  225.         end if
  226.     end sub
  227.    
  228.     public default function startproc(classname, methodname, aliasname)
  229.         set startproc = start(createproc(classname, methodname, aliasname))
  230.     end function
  231.    
  232.     public function createproc(classname, methodname, aliasname)
  233.         if aliasname = "" then aliasname = classname
  234.         newvar aliasname
  235.         scene.host.newvar aliasname
  236.         createproc = primary.spawn(id, classname, methodname, aliasname)
  237.     end function
  238.    
  239.     public function spawn(issuer, classname, methodname, aliasname)
  240.         if not release then
  241.             found = found + 1
  242.             spawn = found
  243.             active = found - lost
  244.             names(spawn) = aliasname
  245.             set hosts(spawn) = nothing
  246.             if not hosts.exists(aliasname) then
  247.                 hosts.add aliasname, createobject("Scripting.Dictionary")
  248.             end if
  249.             set hosts(aliasname)(spawn) = nothing
  250.             execs.add spawn, wshshell.exec("""" & wscript.fullname & """ """ & wscript.scriptfullname & """ ""/task:" & join(array(signature, issuer, spawn, classname, methodname, aliasname), ";") & """")
  251.         end if
  252.     end function
  253.    
  254.     public function start(target)
  255.         select case outline(target)
  256.         case "Nothing", "multiprocess"
  257.             do while getstate(target) < 12
  258.                 wscript.sleep 10
  259.             loop
  260.             if isresponsive(target) then
  261.                 set start = hosts(target).process
  262.                 hosts(target).permit = true
  263.             else
  264.                 set start = nothing
  265.             end if
  266.         case "Dictionary"
  267.             dim elt
  268.             set start = hosts(target)
  269.             for each elt in start.keys
  270.                 do while getstate(elt) < 12
  271.                     wscript.sleep 10
  272.                 loop
  273.             next
  274.             for each elt in start.keys
  275.                 if isresponsive(elt) then
  276.                     hosts(elt).permit = true
  277.                 end if
  278.             next
  279.         case else
  280.             set start = nothing
  281.         end select
  282.     end function
  283.    
  284.     public sub implicate(id, aliasname, host)
  285.         set hosts(aliasname)(id) = host
  286.         set hosts(id) = host
  287.     end sub
  288.    
  289.     public sub staff(host)
  290.         set hosts(host.process) = host
  291.     end sub
  292.    
  293.     public sub abolish(id)
  294.         if hosts.exists(names(id)) then
  295.             hosts(names(id))(id) = empty
  296.         end if
  297.         names(id) = empty
  298.         if isresponsive(id) then
  299.             hosts(hosts(id).process) = empty
  300.         end if
  301.         hosts(id) = empty
  302.         set execs(id) = nothing
  303.         lost = lost + 1
  304.         active = found - lost
  305.     end sub
  306.    
  307.     private sub ancestorevent(eventname)
  308.         if aid > 0 then
  309.             on error resume next
  310.             executeglobal "ancestor." & aliasname & "_" & eventname & " host.hosts(" & id & ")"
  311.             if err.number = 424 or err.number = 438 then err.clear
  312.         end if
  313.     end sub
  314.    
  315.     public sub assignhandler(handlername, byval varsqty)
  316.         dim vars
  317.         vars = ""
  318.         if varsqty > 0 then
  319.             do
  320.                 vars = vars & "param" & varsqty
  321.                 varsqty = varsqty - 1
  322.                 if varsqty = 0 then exit do
  323.                 vars = vars & ", "
  324.             loop
  325.         end if
  326.         executeglobal "sub " & handlername & "(" & vars & "): process." & handlername & " " & vars & ": end sub"
  327.     end sub
  328.    
  329.     public sub newvar(varname)
  330.         executecommand "dim " & varname
  331.     end sub
  332.    
  333.     public sub executecommand(command)
  334.         executeglobal command
  335.     end sub
  336.    
  337.     public function getstate(target)
  338.         select case outline(target)
  339.         case "multiprocess"
  340.             on error resume next
  341.             getstate = hosts(target).state
  342.             if err.number <> 0 then
  343.                 err.clear
  344.                 getstate = 64
  345.             end if
  346.         case "Nothing"
  347.             getstate = 1
  348.         case "Dictionary"
  349.             getstate = null
  350.         case empty
  351.             getstate = 0
  352.         case else
  353.             getstate = 64
  354.         end select
  355.     end function
  356.    
  357.     private function outline(target)
  358.         on error resume next
  359.         if hosts.exists(target) then
  360.             outline = typename(hosts(target))
  361.             if err.number <> 0 then
  362.                 err.clear
  363.                 outline = "Object"
  364.             end if
  365.         else
  366.             outline = empty
  367.         end if
  368.     end function
  369.    
  370.     public function isresponsive(target)
  371.         isresponsive = cbool(getstate(target) and 28)
  372.     end function
  373.    
  374.     public function getid(target)
  375.         on error resume next
  376.         if isobject(target) then
  377.             if isresponsive(target) then
  378.                 getid = hosts(target).id
  379.                 if err.number = 0 then exit function
  380.                 err.clear
  381.             end if
  382.         elseif primary.execs.exists(target) then
  383.             getid = target
  384.             exit function
  385.         end if
  386.         getid = null
  387.     end function
  388.    
  389.     public function gethost(target)
  390.         on error resume next
  391.         if hosts.exists(target) then
  392.             set gethost = hosts(target)
  393.             if err.number = 0 then exit function
  394.             err.clear
  395.         end if
  396.         set gethost = nothing
  397.     end function
  398.    
  399.     public sub free(target)
  400.         select case outline(target)
  401.         case "multiprocess"
  402.             on error resume next
  403.             gethost(target).release = true
  404.             err.clear
  405.         case "Dictionary"
  406.             dim elt
  407.             for each elt in gethost(target)
  408.                 free(elt)
  409.             next
  410.         end select
  411.     end sub
  412.    
  413.     public function joint(target, state, timeout)
  414.         dim reftime
  415.         reftime = timer
  416.         select case outline(target)
  417.         case "multiprocess", "Nothing"
  418.             do while getstate(target) < state
  419.                 if timeisout(timeout, reftime) then
  420.                     joint = false
  421.                     exit function
  422.                 end if
  423.                 wscript.sleep 10
  424.             loop
  425.         case "Dictionary"
  426.             dim elt
  427.             for each elt in gethost(target)
  428.                 do while getstate(elt) < state
  429.                     if timeisout(timeout, reftime) then
  430.                         joint = false
  431.                         exit function
  432.                     end if
  433.                     wscript.sleep 10
  434.                 loop
  435.             next
  436.         end select
  437.         joint = true
  438.     end function
  439.    
  440.     private function timeisout(timeout, reftime)
  441.         if timeout > 0 then
  442.             dim delta
  443.             delta = timer - reftime
  444.             if delta < 0 then delta = delta + 86400
  445.             if delta > timeout then
  446.                 timeisout = true
  447.             end if
  448.         else
  449.             timeisout = false
  450.         end if
  451.     end function
  452.    
  453.     public sub interrupt(target, timeout)
  454.         select case outline(target)
  455.         case "multiprocess"
  456.             on error resume next
  457.             with gethost(target).parent
  458.                 .wscript.timeout = timeout
  459.                 .wscript.quit
  460.             end with
  461.             err.clear
  462.         case "Dictionary"
  463.             dim elt
  464.             for each elt in gethost(target)
  465.                 interrupt elt, timeout
  466.             next
  467.         end select
  468.     end sub
  469.    
  470.     public sub kickout(target)
  471.         if primary.execs.exists(target) then
  472.             if getstate(target) < 64 then
  473.                 on error resume next
  474.                 primary.execs(target).terminate
  475.                 err.clear
  476.             end if
  477.         else
  478.             select case outline(target)
  479.             case "multiprocess"
  480.                 kickout getid(target)
  481.             case "Dictionary"
  482.                 dim elt
  483.                 for each elt in gethost(target)
  484.                     kickout(elt)
  485.                 next
  486.             end select
  487.         end if
  488.     end sub
  489.    
  490.     public sub terminate(target)
  491.         interrupt target, 1
  492.         if not joint(target, 64, 2) then kickout target
  493.     end sub
  494.    
  495.     public sub push(name, value)
  496.         container.putproperty name, value
  497.     end sub
  498.    
  499.     public function pop(name)
  500.         on error resume next
  501.         if isobject(container.getproperty(name)) then
  502.             set pop = container.getproperty(name)
  503.         else
  504.             pop = container.getproperty(name)
  505.         end if
  506.     end function
  507.    
  508.     private sub scenequit
  509.         if isprimary then
  510.             dim col, i, status
  511.             col = execs.keys
  512.             for i = ubound(col) to 0 step -1
  513.                 interrupt col(i), 1
  514.             next
  515.             wscript.sleep 2000
  516.             on error resume next
  517.             for i = ubound(col) to 0 step -1
  518.                 status = execs(col(i)).status
  519.                 if err.number = 0 and status = 0 then execs(col(i)).terminate
  520.                 err.clear
  521.             next
  522.             container.quit
  523.         end if
  524.     end sub
  525.    
  526.     private sub class_terminate()
  527.         if state < 28 and isprimary then scenequit
  528.     end sub
  529.    
  530. end class
Add Comment
Please, Sign In to add comment