home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / examples / durmeanstd.sh < prev    next >
Lisp/Scheme  |  1999-09-09  |  6KB  |  179 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:    December 1997
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;;
  37. ;;;  Find the means and standard deviations of the durations of all
  38. ;;;  phones in the given utterances.
  39. ;;;
  40.  
  41. ;;; Because this is a --script type file it has to explicitly
  42. ;;; load the initfiles: init.scm and user's .festivalrc
  43. (load (path-append libdir "init.scm"))
  44.  
  45. (define (durmeanstd_help)
  46.   (format t "%s\n"
  47.   "durmeanstd [options] festival/utts/*.utts
  48.   Find means and standard deviation of phone durations in utterances
  49.   Options
  50.   -output <ofile>
  51.              File to save output in
  52.   -log 
  53.              Take log of durations first
  54. ")
  55.   (quit))
  56.  
  57. ;;; Default options values
  58. (defvar utt_files nil)
  59. (defvar outfile "durs.meanstd")
  60. (defvar log_domain nil)
  61.  
  62. ;;; Get options
  63. (define (get_options)
  64.   (let ((files nil)
  65.     (o argv))
  66.     (if (or (member_string "-h" argv)
  67.         (member_string "-help" argv)
  68.         (member_string "--help" argv)
  69.         (member_string "-?" argv))
  70.     (durmeanstd_help))
  71.     (while o
  72.       (begin
  73.     (cond
  74.      ((string-equal "-output" (car o))
  75.       (if (not (cdr o))
  76.           (durmeanstd_error "no output file specified"))
  77.       (set! outfile (car (cdr o)))
  78.       (set! o (cdr o)))
  79.      ((string-equal "-log" (car o))
  80.       (set! log_domain t))
  81.      (t
  82.       (set! files (cons (car o) files))))
  83.     (set! o (cdr o))))
  84.     (if files
  85.     (set! utt_files (reverse files)))))
  86.  
  87. (define (durmeanstd_error message)
  88.   (format stderr "%s: %s\n" "durmeanstd" message)
  89.   (durmeanstd_help))
  90.  
  91. ;;; No gc messages
  92. (gc-status nil)
  93.  
  94. ;;;  A simple sufficient statistics class
  95. (define (suffstats.new)
  96.   (list
  97.    0    ;; n
  98.    0    ;; sum
  99.    0    ;; sumx
  100.    ))
  101.  
  102. (define (suffstats.set_n x n)
  103.   (set-car! x n))
  104. (define (suffstats.set_sum x sum)
  105.   (set-car! (cdr x) sum))
  106. (define (suffstats.set_sumx x sumx)
  107.   (set-car! (cdr (cdr x)) sumx))
  108. (define (suffstats.n x)
  109.   (car x))
  110. (define (suffstats.sum x)
  111.   (car (cdr x)))
  112. (define (suffstats.sumx x)
  113.   (car (cdr (cdr x))))
  114. (define (suffstats.reset x)
  115.   (suffstats.set_n x 0)
  116.   (suffstats.set_sum x 0)
  117.   (suffstats.set_sumx x 0))
  118. (define (suffstats.add x d)
  119.   (suffstats.set_n x (+ (suffstats.n x) 1))
  120.   (suffstats.set_sum x (+ (suffstats.sum x) d))
  121.   (suffstats.set_sumx x (+ (suffstats.sumx x) (* d d)))
  122. )
  123.  
  124. (define (suffstats.mean x)
  125.   (/ (suffstats.sum x) (suffstats.n x)))
  126. (define (suffstats.variance x)
  127.   (/ (- (* (suffstats.n x) (suffstats.sumx x))
  128.     (* (suffstats.sum x) (suffstats.sum x)))
  129.      (* (suffstats.n x) (- (suffstats.n x) 1))))
  130. (define (suffstats.stddev x)
  131.   (sqrt (suffstats.variance x)))
  132.  
  133. ;;; Index for each phone
  134. (defvar phonelist nil) ;; index of phone to suffstats
  135. (define (get_phone_data phone)
  136.   (let ((a (car (cdr (assoc phone phonelist)))))
  137.     (if a
  138.     a
  139.     (begin ;; first time for this phone
  140.       (set! phonelist
  141.         (cons
  142.          (list phone (suffstats.new))
  143.          phonelist))
  144.       (car (cdr (assoc phone phonelist)))))))
  145.  
  146. (define (cummulate_seg_durs utt_name)
  147.   (let ((utt (utt.load nil utt_name)))
  148.     (mapcar
  149.      (lambda (s)
  150.        (suffstats.add 
  151.     (get_phone_data (item.name s))
  152.     (if log_domain
  153.         (log (item.feat s "segment_duration"))
  154.         (item.feat s "segment_duration"))))
  155.      (utt.relation.items utt 'Segment))))
  156.  
  157. (define (output_dur_data data outfile)
  158.   (let ((fd (fopen outfile "w")))
  159.     (mapcar
  160.      (lambda (d)
  161.        (format fd "(%s %f %f)\n"
  162.            (car d)
  163.            (suffstats.mean (car (cdr d)))
  164.            (suffstats.stddev (car (cdr d)))))
  165.      data)
  166.     (fclose fd)))
  167.  
  168. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  169. ;;;   The main work
  170. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  171. (define (main)
  172.   (get_options)
  173.  
  174.   (mapcar cummulate_seg_durs utt_files)
  175.   (output_dur_data phonelist outfile)
  176. )
  177.  
  178. (main)
  179.