home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / examples / text2wave.sh < prev    next >
Lisp/Scheme  |  1999-09-09  |  6KB  |  167 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-*-mode:scheme-*-
  2. ;;                                                                       ;;
  3. ;;                Centre for Speech Technology Research                  ;;
  4. ;;                     University of Edinburgh, UK                       ;;
  5. ;;                       Copyright (c) 1996,1997                         ;;
  6. ;;                        All Rights Reserved.                           ;;
  7. ;;                                                                       ;;
  8. ;;  Permission is hereby granted, free of charge, to use and distribute  ;;
  9. ;;  this software and its documentation without restriction, including   ;;
  10. ;;  without limitation the rights to use, copy, modify, merge, publish,  ;;
  11. ;;  distribute, sublicense, and/or sell copies of this work, and to      ;;
  12. ;;  permit persons to whom this work is furnished to do so, subject to   ;;
  13. ;;  the following conditions:                                            ;;
  14. ;;   1. The code must retain the above copyright notice, this list of    ;;
  15. ;;      conditions and the following disclaimer.                         ;;
  16. ;;   2. Any modifications must be clearly marked as such.                ;;
  17. ;;   3. Original authors' names are not deleted.                         ;;
  18. ;;   4. The authors' names are not used to endorse or promote products   ;;
  19. ;;      derived from this software without specific prior written        ;;
  20. ;;      permission.                                                      ;;
  21. ;;                                                                       ;;
  22. ;;  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        ;;
  23. ;;  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ;;
  24. ;;  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ;;
  25. ;;  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     ;;
  26. ;;  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ;;
  27. ;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ;;
  28. ;;  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ;;
  29. ;;  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ;;
  30. ;;  THIS SOFTWARE.                                                       ;;
  31. ;;                                                                       ;;
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. ;;;           Author:  Alan W Black
  34. ;;;           Date:    November 1997
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;;
  37. ;;;  Text to a single waveform like festival_client but without
  38. ;;;  starting hte server
  39. ;;;
  40.  
  41. ;;; Because this is a --script type file I has to explicitly
  42. ;;; load the initfiles: init.scm and user's .festivalrc
  43. (load (path-append libdir "init.scm"))
  44.  
  45. ;;; Process command line arguments
  46. (define (text2wave_help)
  47.   (format t "%s\n"
  48.   "text2wave [options] textfile
  49.   Convert a textfile to a waveform
  50.   Options
  51.   -mode <string>  Explicit tts mode.
  52.   -o ofile        File to save waveform (default is stdout).
  53.   -otype <string> Output waveform type: ulaw, snd, aiff, riff, nist etc.
  54.                   (default is riff)
  55.   -F <int>        Output frequency.
  56.   -eval <string>  File or lisp s-expression to be evaluated before
  57.                   synthesis.
  58. ")
  59.   (quit))
  60.  
  61. ;;; No gc messages
  62. (gc-status nil)
  63.  
  64. ;;; Default argument values
  65. (defvar outfile "-")
  66. (defvar output_type 'riff)
  67. (defvar frequency nil)  ;; default is no frequency modification
  68. (defvar text_files '("-"))
  69. (defvar mode nil)
  70. (defvar wavefiles nil)
  71.  
  72. ;;; Get options
  73. (define (get_options)
  74.  
  75.   (let ((files nil)
  76.     (o argv))
  77.     (if (or (member_string "-h" argv)
  78.         (member_string "-help" argv)
  79.         (member_string "--help" argv)
  80.         (member_string "-?" argv))
  81.     (text2wave_help))
  82.     (while o
  83.       (begin
  84.     (cond
  85.      ((string-equal "-o" (car o))
  86.       (if (not (cdr o))
  87.           (text2wave_error "no output file specified"))
  88.       (set! outfile (car (cdr o)))
  89.       (set! o (cdr o)))
  90.      ((string-equal "-otype" (car o))
  91.       (if (not (cdr o))
  92.           (text2wave_error "no output filetype specified"))
  93.       (set! output_type (car (cdr o)))
  94.       (set! o (cdr o)))
  95.      ((or (string-equal "-f" (car o)) ;; for compatibility and memory loss
  96.           (string-equal "-F" (car o)))
  97.       (if (not (cdr o))
  98.           (text2wave_error "no frequency specified"))
  99.       (set! frequency (car (cdr o)))
  100.       (set! o (cdr o)))
  101.      ((string-equal "-mode" (car o))
  102.       (if (not (cdr o))
  103.           (text2wave_error "no mode specified"))
  104.       (set! mode (car (cdr o)))
  105.       (set! o (cdr o)))
  106.      ((string-equal "-eval" (car o))
  107.       (if (not (cdr o))
  108.           (dumpfeats_error "no file specified to load"))
  109.       (if (string-matches (car (cdr o)) "^(.*")
  110.           (eval (read-from-string (car (cdr o))))
  111.           (load (car (cdr o))))
  112.       (set! o (cdr o)))
  113.      (t
  114.       (set! files (cons (car o) files))))
  115.     (set! o (cdr o))))
  116.     (if files
  117.     (set! text_files (reverse files)))))
  118.  
  119. (define (text2wave_error message)
  120.   (format stderr "%s: %s\n" "text2wave" message)
  121.   (text2wave_help))
  122.  
  123. (define (save_record_wave utt)
  124. "Saves the waveform and records its so it can be joined into a 
  125. a single waveform at the end."
  126.   (let ((fn (make_tmp_filename)))
  127.     (utt.save.wave utt fn)
  128.     (set! wavefiles (cons fn wavefiles))
  129.     utt))
  130.  
  131. (define (combine_waves)
  132.   "Join all the waves together into the desired output file
  133. and delete the intermediate ones."
  134.   (let ((wholeutt (Utterance Text "")))
  135.     (mapcar
  136.      (lambda (d) 
  137.        (utt.import.wave wholeutt d t)
  138.        (delete-file d))
  139.      (reverse wavefiles))
  140.     (if frequency
  141.     (utt.wave.resample wholeutt (parse-number frequency)))
  142.     (utt.save.wave wholeutt outfile output_type)
  143.     ))
  144.  
  145. ;;;
  146. ;;; Redefine what happens to utterances during text to speech 
  147. ;;;
  148. (set! tts_hooks (list utt.synth save_record_wave))
  149.  
  150. (define (main)
  151.   (get_options)
  152.  
  153.   ;; do the synthesis
  154.   (mapcar
  155.    (lambda (f) 
  156.      (if mode
  157.      (tts_file f mode)
  158.      (tts_file f (tts_find_text_mode f auto-text-mode-alist))))
  159.    text_files)
  160.  
  161.   ;; Now put the waveforms together at again
  162.   (combine_waves)
  163. )
  164.  
  165. ;;;  Do the work
  166. (main)
  167.