30 de mayo de 2013

Google Text to Speech

Autor: Mauricio R. Molinero

Código para traducir texto a voz en español, ya que el Microsoft Speech 5.1 no tiene voces en español.

Utilizando esta simple funcion podemos oir la famosa frase. Aquí el código
=tts("Nada corre como un zorro")

FUNCTION tts()
  LPARAMETERS tctext
  *// Mauricio R. Molinero
  **
  m.http_get = "http://translate.google.com/translate_tts?tl=es&q="+urlencode(m.tctext)

  LOCAL ohttp AS winhttp.winhttprequest.5.1

  m.ohttp = CREATEOBJECT("WinHTTP.WinHTTPRequest.5.1")
  m.ohttp.OPEN("GET", m.http_get)

  m.ohttp.setrequestheader("Host","translate.google.com")
  m.ohttp.setrequestheader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)  ")
  m.ohttp.setrequestheader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  m.ohttp.setrequestheader("Accept-Language","en-us,en;q=0.5")
  m.ohttp.setrequestheader("Accept-Encoding","gzip,deflate")
  m.ohttp.setrequestheader("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  m.ohttp.setrequestheader("Keep-Alive","115")
  m.ohttp.setrequestheader("Connection","keep-alive")

  m.ohttp.SEND()

  TRY
    DELETE FILE (ADDBS(SYS(2023)) + "trans*.mp3")
  CATCH TO m.loErr

  ENDTRY

  m.lcFileName =  ADDBS(SYS(2023)) + "trans" + FORCEEXT(SYS(2015),"MP3")
  SET SAFETY OFF
  STRTOFILE(CAST(m.ohttp.responsebody AS MEMO),m.lcFileName)

  IF VARTYPE(m.oMCI) # "O"
    PUBLIC oMCI
    m.oMCI = CREATEOBJECT("MCI.MMcontrol")
  ENDIF
  m.omci.COMMAND = "Close"
  *//
  m.omci.FileName = m.lcFileName
  m.omci.AutoEnable=1
  m.omci.COMMAND = "Open"
  m.omci.COMMAND = "Play"

  RETURN
ENDFUNC


FUNCTION urlencode
  PARAMETERS tcvalue, llnoplus
  LOCAL lcresult, lcchar, lnsize, lnx

  *** Do it in VFP Code
  m.lcresult = ""

  FOR m.lnx = 1 TO LEN(m.tcvalue)
    m.lcchar = SUBSTR(m.tcvalue,m.lnx,1)
    IF ATC(m.lcchar,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") > 0
      m.lcresult = m.lcresult + m.lcchar
      LOOP
    ENDIF
    IF m.lcchar = " " AND !m.llnoplus
      m.lcresult = m.lcresult + "+"
      LOOP
    ENDIF
    *** Convert others to Hex equivalents
    m.lcresult = m.lcresult + "%" + RIGHT(TRANSFORM(ASC(m.lcchar),"@0"),2)
  ENDFOR

  RETURN m.lcresult
ENDFUNC

No hay comentarios. :

Publicar un comentario