;;; uko-speak.el -- speech output

;; Copyright (C) 2003 Sasa Hasan, Universität Koblenz-Landau

;; Author: Sasa Hasan  <sasa.hasan@uni-koblenz.de>
;; Maintainer: Michael Kuehn <kuehn@uni-koblenz.de>
;; Created: March 2003
;; 
;; $Id: uko-speak.el,v 1.4 2004/02/06 14:29:47 rriipp Exp $





(defvar uko-speak-host "localhost"
  "Name or address of the host running the speech server.")

(defvar uko-speak-port 9987
  "Port of the socket used to communicate with the speech server.")

(defvar uko-speak-process-name "uko-speak"
  "Name of the process associated with the speech server.")

(defvar uko-speak-buffer-name "*UKO Speak*"
  "Name of the buffer where the output from the speech server is stored.")




(defun uko-speak-connect ()
  "Connect to the speech server."
  (interactive)
  (when (null (get-process uko-speak-process-name))
    (open-network-stream uko-speak-process-name uko-speak-buffer-name uko-speak-host uko-speak-port)
    ) )

(defun uko-speak-disconnect ()
  "Disconnect from the speech server."
  (interactive)
  (delete-process uko-speak-process-name)
  )

(defun uko-speak-send-buffer (&optional buffer-name)
  "Send the content of buffer with name 'buffer-name' to the speech server."
  (interactive "bBuffer: ")
  (save-excursion
    (when buffer-name
      (set-buffer (get-buffer buffer-name))
      )
    (uko-speak-clear)
    (uko-speak-send-string (buffer-string))
    ) )

(defun uko-speak-send-current-buffer ()
  "Send the content of the current buffer to the speech server."
  (interactive)
  (uko-speak-clear)
  (uko-speak-send-string (buffer-string))
  )

(defun uko-speak-add-region ()
  "Add the currently selected region to the active message of the speech server."
  (interactive)
  (when (region-exists-p)
    (uko-speak-send-string (buffer-substring (region-beginning) (region-end)))
    ) )

(defun uko-speak-clear ()
  "Clear the content in the speech synthesis message window of the server."
  (interactive)
  (uko-speak-send-string "/clear")
  )

(defun uko-speak-start ()
  "Start the speech synthesis."
  (interactive)
  (uko-speak-send-string "/speak")
  )

(defun uko-speak-stop ()
  "Stop the current speech synthesis."
  (interactive)
  (uko-speak-send-string "/stop")
  )

(defun uko-speak-pause ()
  "Pause the current speech synthesis."
  (interactive)
  (uko-speak-send-string "/pause")
  )

(defun uko-speak-resume ()
  "Resume the current speech synthesis."
  (interactive)
  (uko-speak-send-string "/resume")
  )

(defun uko-speak-get-voice-names ()
  "Return a list with available mode names (speakers)."
  (interactive)
  (uko-speak-send-string "/getvoicelist")
  )

(defun uko-speak-get-output-formats ()
  "Return a list with available output formats (e.g. 44KHz 16bit Mono)."
  (interactive)
  (uko-speak-send-string "/getoutputlist")
  )

(defun uko-speak-get-current-voice ()
  "Return the current voice ID."
  (interactive)
  (uko-speak-send-string "/getvoice")
  )

(defun uko-speak-get-current-output ()
  "Return the current voice ID."
  (interactive)
  (uko-speak-send-string "/getoutput")
  )

(defun uko-speak-set-voice (voice)
  "Select the speaker of mode 'mode' (number or mode substring)."
  (interactive "sVoice name or number: ")
  (if (or (> (string-to-number voice) 0) (equal voice "0"))
      (uko-speak-send-string (concat "/voice " voice))
    (uko-speak-send-string (concat "/voicename " voice "#"))
    ) )

(defun uko-speak-set-output (mode)
  "Select the output format of mode 'mode' (number or mode substring)."
  (interactive "sOutput format name or number: ")
  (if (> (string-to-number mode) 0)
      (uko-speak-send-string (concat "/output " mode))
    (uko-speak-send-string (concat "/outputname " mode "#"))
    ) )

(defun uko-speak-set-rate (rate)
  "Set the synthesis rate."
  (interactive "sRate: ")
  (uko-speak-send-string (concat "/rate " rate))
  )

(defun uko-speak-set-volume (vol)
  "Set the volume."
  (interactive "sVolume: ")
  (uko-speak-send-string (concat "/volume " vol))
  )

(defun uko-speak-set-default-output-format ()
  "Set the output format to default (good quality: 44KHz 16bit mono)."
  (interactive)
  (uko-speak-send-string "/outputname 44khz 16 bit mono#")
  )

(defun uko-speak-set-voice-german-male ()
  "Set the speaker to default German male speaker."
  (interactive)
  (uko-speak-send-string "/voicename Thomas 16Khz#")
  )

(defun uko-speak-set-voice-german-female ()
  "Set the speaker to default German female speaker."
  (interactive)
  (uko-speak-send-string "/voicename Dagmar 16Khz#")
  )

(defun uko-speak-set-voice-english-male ()
  "Set the speaker to default English male speaker."
  (interactive)
  (uko-speak-send-string "/voicename Gordon 16Khz#")
  )

(defun uko-speak-set-voice-english-female ()
  "Set the speaker to default English female speaker."
  (interactive)
  (uko-speak-send-string "/voicename Vicky 16Khz#")
  )

(defun uko-speak-set-voice-french-male ()
  "Set the speaker to default French male speaker."
  (interactive)
  (uko-speak-send-string "/voicename Robert 16Khz#")
  )

(defun uko-speak-set-voice-french-female ()
  "Set the speaker to default French female speaker."
  (interactive)
  (uko-speak-send-string "/voicename Cathy 16Khz#")
  )

(defun uko-speak-send-string (message)
  "Send a string to the speech server."
  (interactive "sString: ")
  (process-send-string uko-speak-process-name message)
  )

(defun uko-speak-is-connected ()
  "Test if a connection is open."
  (interactive)
  (get-process uko-speak-process-name)
)

(defun uko-speak-init-logox ()
  "Initialize the Logox4 Engine with a license key."
  (interactive)
  (uko-speak-connect)
  (uko-speak-set-default-output-format)
  (uko-speak-set-voice "Christiane") ;; Logox4
  (uko-speak-set-rate "0")
  (uko-speak-clear)
  (uko-speak-send-string "<LICENSE=COMPUTERLINGUISTIK,UNI-KOBLENZ,TIM-STEFFENS@RPXNN-DPMWV-SLQSJ-9DVFX>")
  (uko-speak-start)
  ;;(sleep-for 2)
  (uko-speak-disconnect)
)
