home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / lib / tilt.scm < prev    next >
Lisp/Scheme  |  1999-06-17  |  31KB  |  971 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  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. ;;;
  34. ;;;          Author: Alan W Black, Kurt Dusterhoff, Janet Hitzeman
  35. ;;;          Date: April 1999
  36. ;;;
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. ;;;
  39. ;;;   Tilt intonation modules, accent/boundary preditions and F0 generation
  40. ;;;   The F0 generation is done using models as described in 
  41. ;;;   Dusterhoff, K. and Black, A. (1997). "Generating F0 contours for 
  42. ;;;   speech synthesis using the Tilt intonation theory"
  43. ;;;   (http://www.cstr.ed.ac.uk/awb/papers/esca-int97.ps) 
  44. ;;;   Proceedings of ESCA Workshop of Intonation, pp 107-110, September, 
  45. ;;;   Athens, Greece.
  46. ;;;
  47. ;;;   Intonation_Tilt assigns accents and boundaries by a CART tree
  48. ;;;   the c and sil nodes are derived directly duration creation
  49. ;;;
  50. ;;;   Int_Targets_Tilt generates the F0 using the CART trees as
  51. ;;;   described in the paper referenced above.
  52. ;;;
  53. ;;;   THIS CONTAINS *VERY* EXPERIMENTAL CODE
  54. ;;;   it requires a thoroughly clean up and probably split into
  55. ;;;   multiple files
  56.  
  57. (defvar int_tilt_params nil
  58.   "int_tilt_params
  59. Parameters for tilt intonation model.")
  60.  
  61. (Parameter.def 'tilt_method 'cart)
  62.  
  63. (define (Intonation_Tilt utt)
  64.   "(Intonation_Tilt utt)
  65. Assign accent and boundary IntEvents to each syllable, and fill in
  66. spaces with silence and connections."
  67.  (let (accent boundary)
  68.    ;; Create basic intonation relations
  69.    (utt.relation.create utt 'Intonation)
  70.    (utt.relation.create utt 'IntonationSyllable)
  71.    (mapcar
  72.     (lambda (syl)
  73.       ;; If first syllable in phrase add phrase_start
  74.       (if (string-equal "pau"
  75.        (item.feat syl "R:SylStructure.daughter1_to.Segment.p.name"))
  76.       (tilt_add_intevent utt syl 'phrase_start))
  77.  
  78.       (set! accent (wagon_predict syl tilt_a_cart_tree))
  79.       (set! boundary (wagon_predict syl tilt_b_cart_tree))
  80. ;      (format t "%s: accent %s boundary %s\n"
  81. ;          (item.feat syl "R:WordStructure.root.name")
  82. ;          accent
  83. ;          boundary)
  84.       (if (not (string-equal accent "0"))
  85.       (tilt_add_intevent utt syl accent))
  86.       (if (not (string-equal boundary "0"))
  87.       (if (and (string-equal boundary "afb")
  88.            (not (string-equal accent "0")))
  89.           (tilt_add_intevent utt syl "fb")  ;; can't have a/afb
  90.           (tilt_add_intevent utt syl boundary)))
  91.  
  92.       ;; If last syllable in phrase add phrase_end
  93.       (if (string-equal "pau"
  94.          (item.feat syl "R:SylStructure.daughtern_to.Segment.n.name"))
  95.       (tilt_add_intevent utt syl 'phrase_end)))
  96.     (utt.relation.items utt 'Syllable))
  97. ;;   (utt.relation.print utt 'Intonation)
  98.    utt))
  99.  
  100. (define (tilt_add_intevent utt syl name)
  101. "(tilt_add_intevent utt syl name)
  102. Add a new IntEvent related to syl with name."
  103.   (let (ie)
  104.     (set! ie (utt.relation.append utt 'Intonation (list name)))
  105.     (if (not (item.relation syl 'IntonationSyllable))
  106.     (utt.relation.append utt 'IntonationSyllable syl))
  107.     (item.relation.append_daughter syl 'IntonationSyllable ie)
  108.     (if (not (string-matches name "phrase_.*"))
  109.     (item.set_feat ie "int_event" 1))
  110.     ie))
  111.  
  112. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  113. ;;;
  114. ;;;  Fo generate through tilt parameters and F0 rendering
  115. ;;;
  116. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  117. (define (Int_Targets_Tilt utt)
  118.   "(Int_Targets_Tilt utt)
  119. Assign Tilt parameters to each IntEvent and then generate the 
  120. F0 contour and assign targets."
  121.   (utt.relation.set_feat utt "Intonation" "intonation_style" "tilt")
  122.   (tilt_assign_parameters utt)
  123. ;  (tilt_F0_and_targets utt)  ;; this has to be C++, sorry
  124. ;  (tilt_map_f0_range utt)
  125.   (tilt_to_f0 utt "f0")
  126.   (tilt_validate utt)
  127.   utt
  128. )
  129.  
  130. (define (tilt_validate utt)
  131.   "(tilt_validate utt)
  132. Checks that the predicted tilt parameter fall with reasonable
  133. limits and modify them where possible to be more reasonable."
  134.   (mapcar
  135.    (lambda (ie)
  136.      (cond
  137.       ((string-equal (item.name ie) "phrase_end")
  138.        ;; check previous event does overflow segments
  139.        )
  140.       (t
  141.        t))
  142.      )
  143.    (utt.relation.items utt 'Intonation))
  144. )
  145.  
  146. (define (tilt_map_f0_range utt)
  147.   "(tilt_map_f0_range utt)
  148. In order fo better trained models to be used for voices which don't
  149. have the necessary data to train models from the targets may be mapped
  150. to a different pitch range.  Note this is not optimal as pitch ranges
  151. don't map that easily, but the the results can sometimes be better than
  152. using a less sophisticated F0 generation model.  The method used
  153. is to define the mean and standard deviation of the speaker the
  154. model was trained on and the mean and standard deciation of the
  155. desired speaker.  Mapping is by converting the actual F0 value
  156. to zscores (distance from mean in number of stddev) and back into
  157. the other domain.  The variable int_tilt_params is used to find
  158. the values."
  159.   (let ((target_f0_mean (car (cdr (assoc 'target_f0_mean int_tilt_params))))
  160.     (target_f0_std (car (cdr (assoc 'target_f0_std int_tilt_params))))
  161.     (model_f0_std (car (cdr (assoc 'model_f0_std int_tilt_params))))
  162.     (model_f0_mean (car (cdr (assoc 'model_f0_mean int_tilt_params)))))
  163.     (if target_f0_mean  ;; only if one is specified
  164.      (lambda (targ)
  165.        (item.set_name
  166.         targ
  167.         (+ target_f0_mean
  168.            (* target_f0_std
  169.           (/ (- (parse-number (item.name targ))
  170.             model_f0_mean)
  171.              model_f0_std)))))
  172.      (utt.relation.leafs utt 'Target))))
  173.  
  174. (define (tilt_assign_parameters utt) 
  175.   "(tilt_assign_parameters utt)
  176. Assigned tilt parameters to IntEvents, depending on the value
  177. of the Parameter tilt_method uses wagon trees (cart) or linear
  178. regression models (lr)."
  179.   (let ((method (Parameter.get 'tilt_method)))
  180.   (cond
  181.    ((equal? method 'cart)
  182.     (tilt_assign_parameters_wagon utt))
  183.    ((equal? method 'lr)
  184.     (tilt_assign_parameters_lr utt))
  185.    (t
  186.     (error "Tilt: unknown tilt param prediction method: " tilt_method)))))
  187.  
  188. (define (tilt_assign_parameters_wagon utt)
  189.  "(tilt_assign_parameters_wagon utt)
  190. Assing parameters (start_f0, tilt, amplitude, peak_pos and duration)
  191. to each IntEvent.  Uses Wagon trees to predict values"
  192.   (mapcar
  193.    (lambda (ie)
  194.      (let ((param_trees (cdr (assoc_string (item.name ie)
  195.                        tilt_param_trees))))
  196.        (item.set_feat ie "time_path" "IntonationSyllable")
  197.        (if (string-equal "1" (item.feat ie "int_event"))
  198.        (item.set_function ie "time" "unisyn_tilt_event_position")
  199.        (item.set_function ie "time" "unisyn_tilt_phrase_position"))
  200.        (cond
  201.     ((null param_trees)  
  202.      (format stderr "Tilt: unknown Intonation type %s, ignored\n"
  203.          (item.name ie))
  204.      ;; *need* to assign default values 
  205.      (item.set_feat ie "ev.f0" 100)
  206.      (item.set_feat ie "tilt.amp" 20.0)
  207.      (item.set_feat ie "tilt.dur" 0.25)
  208.      (item.set_feat ie "tilt.tilt" -0.2)
  209.      (item.set_feat ie "rel_pos" 0.0)
  210.      )
  211.     (t
  212.      (tilt_assign_params_wagon ie param_trees)))))
  213.    (utt.relation.items utt 'Intonation)))
  214.  
  215. (define (tilt_assign_params_wagon ie trees)
  216.   "(tilt_assign_params_wagon ie trees)
  217. Assign the names parameters to ie using the trees and names in
  218. trees."
  219.   (mapcar
  220.    (lambda (tree)
  221.      (let ((val (wagon_predict ie (car (cdr tree)))))
  222.        (item.set_feat ie (car tree) val)))
  223.    trees))
  224.  
  225. (define (tilt_assign_parameters_lr utt)
  226.   "(tilt_assign_parameters_lr utt)
  227. Assing parameters (start_f0, tilt, amplitude, peak_pos and duration)
  228. to each IntEvent. Prediction by linear regression models"
  229.   (mapcar
  230.    (lambda (ie)
  231.      (let ((param_lrmodels (cdr (assoc_string (item.name ie)
  232.                     tilt_param_lrmodels))))
  233.        (cond
  234.     ((null param_lrmodels)  
  235.      (format stderr "Tilt: unknown IntEvent type %s, ignored\n"
  236.          (item.name ie))
  237.      ;; *need* to assign default values 
  238.      (item.set_feat ie "ev.f0" 100)
  239.      (item.set_feat ie "tilt.amp" 20.0)
  240.      (item.set_feat ie "tilt.dur" 0.25)
  241.      (item.set_feat ie "tilt.tilt" -0.2)
  242.      (item.set_feat ie "rel_pos" 0.0)
  243.      )
  244.     (t
  245.      (tilt_assign_params_lr ie param_lrmodels)))))
  246.    (utt.relation.items utt 'IntEvent)))
  247.  
  248. (define (tilt_assign_params_lr ie lrmodels)
  249.   "(tilt_assign_params_lr ie lrmodels)
  250. Assign the names parameters to ie using the trees and names in
  251. trees."
  252.   (mapcar
  253.    (lambda (lrm)
  254.      (let ((val (lr_predict ie (cdr lrm))))
  255.        (item.set_feat ie (car lrm) val)))
  256.    lrmodels))
  257.  
  258. (define (utt.save.tilt_events utt filename)
  259. "(utt.save.til_events UTT FILENAME)
  260. Save tilt events in UTT to FILENAME in a format suitable for
  261. ev_synth."
  262.   (let ((fd (fopen filename "w")))
  263.     (format fd "#\n")
  264.     (mapcar
  265.      (lambda (ie)
  266.        (let ((name (item.name ie)))
  267.      (cond
  268.       ((or (string-equal name "sil")
  269.            (string-equal name "c"))
  270.        (format fd "   %2.4f   100 %s; tilt: %2.6f\n" 
  271.            (item.feat ie 'end)
  272.            name 
  273.            (item.feat ie "tilt_start_f0")))
  274.       (t ;; accent or boundary
  275.        (format fd "   %2.4f   100 %s; tilt: %2.6f %2.6f %2.6f %2.6f %2.6f\n" 
  276.            (item.feat ie 'end)
  277.            name 
  278.            (item.feat ie "ev.f0")
  279.            (item.feat ie "tilt.amp")
  280.            (item.feat ie "tilt.dur")
  281.            (item.feat ie "tilt.tilt")
  282.            (item.feat ie "rel_pos"))))))
  283.      (utt.relation.items utt 'IntEvent))
  284.     (fclose fd)
  285.     utt))
  286.  
  287.  
  288. ;;;;;
  289. ;;;  Some features which should be pruned
  290. ;;;;;
  291.  
  292. (def_feature_docstring 'Syllable.lisp_time_to_next_vowel
  293.   "Syllable.lisp_time_to_next_vowel syl
  294.   The time from vowel_start to next vowel_start")
  295. (define (time_to_next_vowel syl)
  296.   "(time_to_next_vowel syl)
  297.   The time from vowel_start to next vowel_start"
  298.   (let (ttnv)
  299.     (if (string-equal "0" (item.feat syl "n.vowel_start"))
  300.     (set! ttnv 0.00)
  301.     (set! ttnv (- (item.feat syl "n.vowel_start")
  302.               (item.feat syl "vowel_start"))))
  303.     ttnv))
  304.  
  305. (def_feature_docstring 'Syllable.lisp_next_stress
  306.   "Syllable.lisp_next_stress
  307.   Number of syllables to next stressed syllable. 0 if this syllable is
  308.   stressed.  It is effectively assumed the syllable after the last syllable
  309.   is stressed.")
  310. (define (next_stress syl)
  311.   (cond 
  312.    ((null syl) 0)
  313.    ((string-equal (item.feat syl 'stress_num) "1")
  314.     0)
  315.    (t
  316.     (+ 1 (next_stress (item.relation.next syl 'Syllable))))))
  317.  
  318. (def_feature_docstring 'Syllable.lisp_last_stress
  319.   "Syllable.lisp_last_stress
  320.   Number of syllables from previous stressed syllable.  0 if this syllable
  321.   is stressed.  It is effectively assumed that the syllable before the 
  322.   first syllable is stressed.")
  323. (define (last_stress syl)
  324.   (cond 
  325.    ((null syl) 0)
  326.    ((string-equal (item.feat syl 'stress_num) "1")
  327.     0)
  328.    (t
  329.     (+ 1 (last_stress (item.relation.prev syl 'Syllable))))))
  330.  
  331.  
  332. (def_feature_docstring 'SylStructure.lisp_length_to_last_seg
  333.   "SylStructure.lisp_length_to_last_seg
  334.   Length from start of the vowel to start of last segment of syllable.")
  335. (define (length_to_last_seg syl)
  336.   (- (item.feat syl "daughtern_to.Segment.start")
  337.      (item.feat syl "vowel_start")))
  338.  
  339. (def_feature_docstring 'SylStructure.lisp_get_rhyme_length
  340.   "Syllable.lisp_get_rhyme_length
  341.   Length from start of the vowel to end of syllable.")
  342. (define (get_rhyme_length syl)
  343.   (- (item.feat syl 'end)
  344.      (item.feat syl 'vowel_start syl)))
  345.  
  346. (def_feature_docstring 'SylStructure.lisp_get_onset_length
  347.   "Syllable.lisp_get_onset_length
  348.   Length from start of syllable to start of vowel.")
  349. (define (get_onset_length syl)
  350.   (cond
  351.    ((< (- (item.feat syl 'vowel_start)
  352.       (item.feat syl 'start))
  353.        0.000)
  354.     0.000)  ;; just in case
  355.    (t
  356.     (- (item.feat syl 'vowel_start)
  357.        (item.feat syl 'start)))))
  358.  
  359. (def_feature_docstring 'Syllable.lisp_tilt_accent
  360.   "Syllable.lisp_tilt_accent
  361.   Returns \"a\" if there is a tilt accent related to this syllable, 0 
  362.   otherwise.")
  363. (define (tilt_accent syl)
  364.   (let ((events (item.relation.daughters syl 'IntonationSyllable))
  365.     (r "0"))
  366.     (mapcar
  367.      (lambda (i)
  368.        (if (member_string (item.name i) tilt_accent_list)
  369.        (set! r "a")))
  370.      events)
  371.     r))
  372.  
  373. (def_feature_docstring 'Syllable.lisp_tilt_boundary
  374.   "Syllable.lisp_tilt_boundary
  375.   Returns boundary label if there is a tilt boundary related to this 
  376. syllable, 0 otherwise.")
  377. (define (tilt_boundary syl)
  378.   (let ((events (item.relation.daughters syl 'IntonationSyllable))
  379.     (r "0"))
  380.     (mapcar
  381.      (lambda (i)
  382.        (let ((name (item.name i)))
  383.        (if (member_string name tilt_boundary_list)
  384.        (cond
  385.         ((string-matches name "a.*")
  386.          (set! r (string-after name "a")))
  387.         ((string-matches name "m.*")
  388.          (set! r (string-after name "m")))
  389.         (t
  390.          (set! r name))))))
  391.      events)
  392.     r))
  393.  
  394. (def_feature_docstring 'Syllable.lisp_tilt_accented
  395.   "Syllable.lisp_tilt_accented
  396.   Returns 1 if there is a tilt accent related to this syllable, 0 
  397.   otherwise.")
  398. (define (tilt_accented syl)
  399.   (let ((events (item.relation.daughters syl 'IntonationSyllable))
  400.     (r "0"))
  401.     (mapcar
  402.      (lambda (i)
  403.        (if (member_string (item.name i) tilt_accent_list)
  404.        (set! r "1")))
  405.      events)
  406.     r))
  407.  
  408. (def_feature_docstring 'Syllable.lisp_tilt_boundaried
  409.   "Syllable.lisp_tilt_boundaried
  410.   Returns 1 if there is a tilt boundary related to this syllable, 0 
  411.   otherwise.")
  412. (define (tilt_boundaried syl)
  413.   (let ((events (item.relation.daughters syl 'IntonationSyllable))
  414.     (r "0"))
  415.     (mapcar
  416.      (lambda (i)
  417.        (if (member_string (item.name i) tilt_boundary_list)
  418.        (set! r "1")))
  419.      events)
  420.     r))
  421.  
  422. (def_feature_docstring 'SylStructure.lisp_vowel_height
  423.   "SylStructure.lisp_vowel_height syl
  424. Classifies vowels as high, low or mid")
  425. (define (vowel_height syl)
  426.   (let ((vh (item.feat syl "daughtern.daughter1.daughter1.df.height")))
  427.     vh)
  428. )
  429.  
  430. (def_feature_docstring 'SylStructure.lisp_vowel_frontness
  431.   "SylStructure.vowel_frontness syl
  432. Classifies vowels as front, back or mid")
  433. (define (vowel_frontness syl)
  434.   (let ((vf (item.feat syl "daughtern.daughter1.daughter1.df.front")))
  435.     vf)
  436. )
  437.  
  438. (def_feature_docstring 'SylStructure.lisp_vowel_length
  439.   "SylStructure.vowel_length syl
  440. Returns the df.length feature of a syllable's vowel")
  441. (define (vowel_length syl)
  442.   (let ((vl (item.feat syl "daughtern.daughter1.daughter1.df.length")))
  443.     vl)
  444. )
  445.  
  446. (defvar sonority_vless_obst '("f" "h" "hh" "k" "p" "s" "sh" "t" "th" "ch")
  447.   "sonority_vless_obst
  448. List of voiceless obstruents for use in sonority scaling (only good w/ radio_speech)"
  449.   )
  450. (defvar sonority_v_obst '("v" "b" "g" "z" "zh" "d" "dh" "jh")
  451.   "sonority_v_obst
  452. List of voiced obstruents for use in sonority scaling (only good w/ radio_speech)"
  453.   )
  454. (defvar sonority_nas '("m" "n" "ng" "nx" "em" "en")
  455.   "sonority_nas
  456. List of nasals (only good w/ radio_speech)"
  457.   )
  458. (defvar sonority_liq '("r" "l" "er" "el" "axr")
  459.   "sonority_liq
  460. List of liquids (only good w/ radio_speech)"
  461.   )
  462. (defvar sonority_glides '("y" "w")
  463.   "sonority_glides
  464. List of glides (only good w/ radio_speech)"
  465.   )
  466.  
  467. (def_feature_docstring 'SylStructure.lisp_sonority_scale_coda
  468.   "SylStructure.sonority_scale_coda syl
  469. Returns value on sonority scale (1 -6, where 6 is most sonorous)
  470. for the coda of a syllable, based on least sonorant portion.")
  471. (define (sonority_scale_coda syl)
  472.   (let ((segs (item.daughters (item.daughtern (item.daughtern syl))))
  473.     (scale 6))
  474.     (mapcar
  475.      (lambda (seg)
  476.        (cond
  477.     ((member_string (item.name seg) sonority_vless_obst)
  478.      (if (> scale 1)
  479.          (set! scale 1)))
  480.     ((member_string (item.name seg) sonority_v_obst)
  481.      (if (> scale 2)
  482.          (set! scale 2)))
  483.     ((member_string (item.name seg) sonority_nas)
  484.      (if (> scale 3)
  485.          (set! scale 3)))
  486.     ((member_string (item.name seg) sonority_liq)
  487.      (if (> scale 4)
  488.          (set! scale 4)))
  489.     ((member_string (item.name seg) sonority_glides)
  490.      (if (> scale 5)
  491.          (set! scale 5)))
  492.     (t
  493.      (if (> scale 6)
  494.          (set! scale 6)))
  495.     )
  496.        )
  497.     segs)
  498.   scale))
  499.  
  500. (def_feature_docstring 'SylStructure.lisp_sonority_scale_onset
  501.   "SylStructure.sonority_scale_onset syl
  502. Returns value on sonority scale (1 -6, where 6 is most sonorous)
  503. for the onset of a syllable, based on least sonorant portion.")
  504. (define (sonority_scale_onset syl)
  505.   (if (string-equal "Onset" (item.feat (item.daughter1 syl) "sylval"))
  506.       (let ((segs (item.daughters (item.daughter1 syl)))
  507.         (scale 6))
  508.     (mapcar
  509.      (lambda (seg)
  510.        (cond
  511.         ((member_string (item.name seg) sonority_vless_obst)
  512.          (if (> scale 1)
  513.          (set! scale 1)))
  514.         ((member_string (item.name seg) sonority_v_obst)
  515.          (if (> scale 2)
  516.          (set! scale 2)))
  517.         ((member_string (item.name seg) sonority_nas)
  518.          (if (> scale 3)
  519.          (set! scale 3)))
  520.         ((member_string (item.name seg) sonority_liq)
  521.          (if (> scale 4)
  522.          (set! scale 4)))
  523.         ((member_string (item.name seg) sonority_glides)
  524.          (if (> scale 5)
  525.          (set! scale 5)))
  526.         (t (set! scale 6))
  527.         )
  528.        )
  529.      segs)
  530.     scale)
  531.       0))
  532.  
  533. (def_feature_docstring 'SylStructure.lisp_num_postvocalic_c
  534.   "SylStructure.lisp_num_postvocalic_c
  535. Finds the number of postvocalic consonants in a syllable.")
  536. (define (num_postvocalic_c syl)
  537.   "Finds the number of postvocalic consonants in a syllable."
  538.   (let (segs (npc 0))
  539.     (set! segs (item.daughters (item.daughtern (item.daughtern syl))))
  540.     (mapcar
  541.      (lambda (seg)
  542.        (set! npc (+ npc 1))
  543.        )
  544.      segs)
  545.     npc))
  546.  
  547.  
  548. (def_feature_docstring 'SylStructure.lisp_syl_numphones
  549.   "SylStructure.lisp_syl_numphones syl
  550. Finds the number segments in a syllable.")
  551. (define (syl_numphones syl)
  552.   (length (mt_segs_from_syl syl))
  553.   )
  554.  
  555. (def_feature_docstring 'Segment.lisp_pos_in_syl
  556.   "Segment.lisp_pos_in_syl seg
  557. Finds the position in a syllable of a segment - returns a number.")
  558. (define (pos_in_syl seg)
  559.   (let ((segments (mt_segs_from_syl 
  560.            (item.relation (item.parent_to
  561.                    (item.relation seg 'SylStructure)
  562.                    'Syllable)
  563.                   'SylStructure)))
  564.     (seg_count 1))
  565.     (mapcar
  566.      (lambda (s)
  567.        (if (not (eqv? s seg))
  568.        (set! seg_count (+ 1.0 seg_count))
  569.        nil))
  570.      segs)
  571.     seg_count))
  572.  
  573. (def_feature_docstring 'Intonation.lisp_peak_anchor_segment_type
  574.   "Intonation.peak_anchor_segment_type ie
  575. Determines whether the segment anchor for a peak
  576. is the first consonant of a syl - C0 -, the
  577. vowel of a syl - V0 -, or segments after that
  578. - C1->X,V1->X. If the segment is in a following syl,
  579. the return value will be preceded by a 1 - e.g. 1V1")
  580. (define (peak_anchor_segment_type ie)
  581.   (let ( syl peak_anchor_num numsegs peak_anchor_type)
  582.     (set! peak_anchor_num (peak_segment_anchor ie))
  583.  
  584.  
  585.     (if (> 9 peak_anchor_num)
  586.     (set! syl (item.relation
  587.            (item.parent (item.relation ie "IntonationSyllable")) 
  588.            "SylStructure")))
  589.     (if (> 9 peak_anchor_num)
  590.     (set! numsegs (item.feat syl "syl_numphones")))
  591.  
  592.     (cond
  593.      ((< 9 peak_anchor_num)
  594.       (set! peak_anchor_type "none"))
  595.      ((> 0 peak_anchor_num)
  596.       (set! peak_anchor_type
  597.         (string-append
  598.          "-1" (get_anchor_value (item.prev syl)
  599.                     (+ peak_anchor_num 
  600.                        (item.feat syl "p.syl_numphones"))))))
  601.      ((< peak_anchor_num numsegs)
  602.       (set! peak_anchor_type (get_anchor_value syl numsegs)))
  603.      ((> peak_anchor_num numsegs)
  604.       (set! peak_anchor_type
  605.         (string-append
  606.          "1" (get_anchor_value (item.next syl) (- peak_anchor_num numsegs)))))
  607.       (set! peak_anchor_type "none"))
  608. ;    (format stderr "pat: %s\n" peak_anchor_type)
  609.     peak_anchor_type))
  610.  
  611. (define (get_anchor_value sylSyl seg_num)
  612.   "Gets the c/v value of the segment within a syllable."
  613.   (let ((syl (item.relation sylSyl "SylStructure"))
  614.     (seg_val "none") segs (ccnt -1) (vcnt -1) (vpis 0))
  615.     (set! segs (mt_segs_from_syl sylSyl))
  616.     (mapcar
  617.      (lambda (seg)
  618.        (cond
  619.     ((string-equal "consonant" (item.feat seg "df.type"))
  620.        (set! vcnt (+ 1 vcnt))
  621.        (set! vpis (item.feat seg "pos_in_syl")))
  622.     (t
  623.        (set! ccnt (+ 1 ccnt))))
  624.        (cond
  625.     ((and
  626.       (eq (- seg_num 1.0) (item.feat seg "pos_in_syl"))
  627.       ( string-equal "consonant" (item.feat seg "df.type")))
  628.      (set! seg_val (string-append "V" vcnt)))
  629.     ((and
  630.       (eq (- seg_num 1.0) (item.feat seg "pos_in_syl"))
  631.       ( string-equal "vowel" (item.feat seg "df.type")))
  632.      (set! seg_val (string-append "C" (- (item.feat seg "pos_in_syl")
  633.                          vpis) "V" vcnt)))
  634.     (t nil))
  635.        )
  636.      segs)
  637.   seg_val))
  638.  
  639. (define (peak_segment_anchor ie)
  640.   "peak_segment_anchor ie
  641. Determines what segment acts as the anchor for a peak.
  642. Returns number of segments from start of accented syllable
  643. to peak."
  644. ;  (format stderr "accent: %s\n"
  645. ;      (item.name ie))
  646.   (let ((pk_pos (item.feat ie "position"))
  647.     (peak_seg_anchor 11))
  648.     (if
  649.      (or
  650.        (string-equal "phrase_start" (item.name ie))
  651.        (string-equal "phrase_end" (item.name ie))
  652.        (string-equal "pause" (item.name ie)))
  653.      (set! peak_seg_anchor 10)
  654.      (set! peak_seg_anchor (find_peak_seg_anchor ie pk_pos)))
  655.     peak_seg_anchor))
  656.  
  657. (define (find_peak_seg_anchor ie pk_pos)
  658.   "find_peak_seg_anchor ie pk_pos
  659. Part of the workings of peak_segment_anchor."
  660.   (let (( syl (item.relation
  661.         (item.parent (item.relation ie 'IntonationSyllable))
  662.         'SylStructure))
  663.     (seg_anchor 11))
  664.     (cond
  665.      ((not (eq 9.0 (segs_to_peak syl pk_pos)))
  666.       (set! seg_anchor (segs_to_peak syl pk_pos)))
  667.  
  668.      ((and (item.prev syl)
  669.        (not (eq 9.0 (segs_to_peak (item.prev syl) pk_pos))))
  670. ;      (format stderr "%s\n" (item.name (item.prev syl)))
  671.       (set! seg_anchor (* -1
  672.                  (- (+ 1 (item.feat syl "p.syl_numphones"))
  673.                  (segs_to_peak (item.prev syl) pk_pos)))))
  674.  
  675.      ((and (item.next syl)
  676.        (> pk_pos (item.feat syl "n.start")))
  677. ;      (format stderr "%s\n" (item.name (item.next syl)))
  678.       (set! seg_anchor (+ 1 
  679.               (item.feat syl "syl_numphones")
  680.               (segs_to_peak (item.next syl) pk_pos))))
  681.      (t
  682.        (format stderr "No seg anchor could be found\n")))
  683. ;    (format stderr "seg_anchor: %f\n" seg_anchor)
  684.     seg_anchor))
  685.  
  686. (define (segs_to_peak sylSyl pk_pos)
  687.   "segs_to_peak sylSyl pk_pos
  688. Determines the number of segments from the start of a syllable
  689. to an intonation peak"
  690.   (let ((syl (item.relation sylSyl "SylStructure"))
  691.     (segs_2_peak 9) segs)
  692.     (set! segs (mt_segs_from_syl syl))
  693.     (mapcar
  694.      (lambda (seg)
  695. ;    (format stderr "seg_end: %f  pk: %f\n" (item.feat seg "end")
  696. ;         pk_pos)
  697.      (if (eq 1.0 (peak_wi_seg seg pk_pos))
  698.      (set! segs_2_peak (item.feat seg "pos_in_syl")))
  699. ;     (format stderr "segs_2_peak: %f\n" segs_2_peak)
  700.      )
  701.      segs)
  702.     segs_2_peak))
  703.  
  704. (define (peak_wi_seg segment pk_pos)
  705.   "peak_wi_seg segment pk_pos
  706. Finds if a peak occurs w/i a segment"
  707.   (let ((s_start (item.feat segment "start"))
  708.     (s_end (item.feat segment "end"))
  709.     (ret 0.0))
  710.     (if (and (< s_start pk_pos)
  711.          (< pk_pos s_end))
  712.     (set! ret 1.0)
  713.     nil)
  714.     ret))
  715.  
  716. (defvar tilt_accent_list '("a" "arb" "afb" "m" "mfb" "mrb")
  717.   "tilt_accent_list
  718. List of events containing accents in tilt model.")
  719. (defvar tilt_boundary_list '("rb" "arb" "afb" "fb" "mfb" "mrb")
  720.   "tilt_boundary_list
  721. List of events containing boundaries in tilt model.")
  722.  
  723. (def_feature_docstring 'Intonation.lisp_last_tilt_accent
  724.   "Intonation.lisp_last_tilt_accent
  725.   Returns the most recent tilt accent.")
  726. (define (last_tilt_accent intev)
  727.   (let ((pie (item.relation.prev intev 'Intonation)))
  728.     (cond
  729.      ((not pie)
  730.       "0")
  731.      ((member_string (item.name pie) tilt_accent_list)
  732.       (item.name pie))
  733.      (t (last_tilt_accent pie)))))
  734.  
  735. (def_feature_docstring 'Intonation.lisp_next_tilt_accent
  736.   "Intonation.lisp_next_tilt_accent
  737.   Returns the next tilt accent.")
  738. (define (next_tilt_accent intev)
  739.   (let ((nie (item.relation.next intev 'Intonation)))
  740.     (cond
  741.      ((not nie) "0")
  742.      ((member_string (item.name nie) tilt_accent_list)
  743.       (item.name nie))
  744.      (t (next_tilt_accent nie)))))
  745.  
  746. (def_feature_docstring 'Intonation.lisp_last_tilt_boundary
  747.   "Intonation.lisp_last_tilt_boundary
  748.   Returns the most recent tilt boundary.")
  749. (define (last_tilt_boundary intev)
  750.   (let ((pie (item.relation.prev intev 'Intonation)))
  751.     (cond
  752.      ((not pie) "0")
  753.      ((member_string (item.name pie) tilt_boundary_list)
  754.       (item.name pie))
  755.      (t (last_tilt_boundary pie)))))
  756.  
  757. (def_feature_docstring 'Intonation.lisp_next_tilt_boundary
  758.   "Intonation.lisp_next_tilt_boundary
  759.   Returns the next tilt boundary.")
  760. (define (next_tilt_boundary intev)
  761.   (let ((nie (item.relation.next intev 'Intonation)))
  762.     (cond
  763.      ((not nie) "0")
  764.      ((member_string (item.name nie) tilt_boundary_list)
  765.       (item.name nie))
  766.      (t (next_tilt_boundary nie)))))
  767.  
  768. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  769. ;;;  Some basic function to metrical tree structure
  770. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  771. (define (mt_syl_from_seg seg)
  772.   (if seg
  773.       (item.root (item.relation seg 'SylStructure))
  774.       nil))
  775. (define (mt_word_from_syl syl)
  776.   (if syl
  777.       (item.root (item.relation syl 'WordStructure))
  778.       nil))
  779. (define (mt_word_from_seg seg)
  780.   (mt_word_from_syl (mt_syl_from_seg seg)))
  781.  
  782. (define (mt_segs_from_syl s)
  783.   (cond
  784.    ((null s) nil)
  785.    ((member_string 'Segment (item.relations s))
  786.     (list s))
  787.    (t
  788.     (apply
  789.      append
  790.      (mapcar mt_segs_from_syl (item.relation.daughters s 'SylStructure))))))
  791.  
  792. (define (sylmtval s)
  793.   (let ((syl (mt_syl_from_seg s)))
  794.     (if syl
  795.     (item.feat syl "MetricalValue")
  796.     "0")))
  797.  
  798. (define (sylpmtval s)
  799.   (let ((syl (mt_syl_from_seg s)))
  800.     (if syl
  801.     (item.feat syl "R:MetricalTree.parent.MetricalValue")
  802.     "0")))
  803.  
  804. (define (mt_numsyls w)
  805.   (let ((s1 (item.daughter1_to (item.relation w 'WordStructure) 'Syllable))
  806.     (sn (item.daughtern_to (item.relation w 'WordStructure) 'Syllable))
  807.     (count 1))
  808.     (while (and s1 (not (equal? s1 sn)))
  809.        (set! count (+ 1 count))
  810.        (set! s1 (item.next s1)))
  811.     (if s1
  812.     count
  813.     0)))
  814.  
  815. (define (mt_seg_numsyls s)
  816.   (let ((w (mt_word_from_seg s)))
  817.     (if w
  818.     (mt_num_syls w)
  819.     0)))
  820.  
  821.  
  822. ;;; These functions should be sort out some time
  823.     
  824. ;;; Difference between this syl and the next
  825. ;;;  number of closing brackets, number of opening brackets
  826. ;;;  difference between them
  827.  
  828. (define (mt_close n)
  829.   "(mt_close n)
  830. The number of consituents this is the end of, Effectively the
  831. number of closing brackets after this word."
  832.   (if (or (not n) (item.next n))
  833.       0
  834.       (+ 1 (mt_close (item.parent n)))))
  835.  
  836. (define (mt_open n)
  837.   "(mt_open n)
  838. The number of consituents this is the start of, Effectively the
  839. number of opening brackets before this word."
  840.   (if (or (not n) (item.prev n))
  841.       0
  842.       (+ 1 (mt_open (item.parent n)))))
  843.     
  844. (define (mt_postype syl)
  845.   "(mt_postype syl)
  846. Returns single, initial, final or middle."
  847.   (let ((w (mt_word_from_syl syl))
  848.     (psw (mt_word_from_syl (item.relation.prev syl 'Syllable)))
  849.     (nsw (mt_word_from_syl (item.relation.next syl 'Syllable))))
  850.     (cond
  851.      ((and (equal? w psw)
  852.        (equal? w nsw))
  853.       'middle)
  854.      ((and (not (equal? w psw))
  855.        (not (equal? w nsw)))
  856.       'single)
  857.      ((equal? w psw)
  858.       'final)
  859.      (t
  860.       'initial))))
  861.  
  862. (define (mt_accent syl)
  863.   "(mt_accent syl)
  864. Accent or 0 if none."
  865.   (let ((a 0))
  866.     (mapcar
  867.      (lambda (i)
  868.        (if (string-matches (item.name i) "^a.*")
  869.        (set! a "a")))
  870.      (item.relation.daughters syl 'IntonationSyllable))
  871.     a))
  872.  
  873. (define (mt_break syl)
  874.   "(mt_break syl)
  875. Break or 0 if none."
  876.   (let ((a 0))
  877.     (mapcar
  878.      (lambda (i)
  879.        (if (string-matches (item.name i) ".*b$")
  880.        (set! a (item.name i))))
  881.      (item.relation.daughters syl 'IntonationSyllable))
  882.     a))
  883.  
  884. (define (mt_ssyl_out s)
  885.   (cond
  886.    ((null s) 0)
  887.    ((not (string-equal 
  888.       "0" (item.feat s "R:WordStructure.root.lisp_word_mt_break")))
  889.     0)
  890.    ((string-equal "s" (item.feat s "MetricalValue"))
  891.     (+ 1 (mt_ssyl_out (item.relation.next s 'Syllable))))
  892.    (t
  893.     (mt_ssyl_out (item.relation.next s 'Syllable)))))
  894.  
  895. (define (mt_num_s s)
  896.   "(mt_num_s s)
  897. The number of s MetricalValues from here to a w or top."
  898.   (cond
  899.    ((null s) 0)
  900.    ((string-equal "w" (item.feat s "MetricalValue"))
  901.     0)
  902.    (t
  903.     (+ 1 (mt_num_s (item.parent s))))))
  904.  
  905. (define (mt_num_w s)
  906.   "(mt_num_w s)
  907. The number of w MetricalValues from here to a s or top."
  908.   (cond
  909.    ((null s) 0)
  910.    ((string-equal "s" (item.feat s "MetricalValue"))
  911.     0)
  912.    (t
  913.     (+ 1 (mt_num_w (item.parent s))))))
  914.  
  915. (define (mt_strong s)
  916.   "(mt_strong s)
  917. 1 if all MetricalValues a s to a word, 0 otherwise."
  918.   (cond
  919.    ((string-equal "w" (item.feat s "MetricalValue"))
  920.     "0")
  921.    ((member_string 'Word (item.relations s)) "1")
  922.    (t
  923.     (mt_strong (item.relation.parent s 'MetricalTree)))))
  924.  
  925. (define (mt_lssp s)
  926.   "(mt_lssp s)
  927. 1 if last stressed syllable in phrase, 0 otherwise."
  928.   (if (and (string-equal "s" (item.feat s "MetricalValue"))
  929.        (equal? 0 (mt_ssyl_out s)))
  930.       "1"
  931.       "0"))
  932.  
  933. (define (mt_fssw s)
  934.   "(mt_fssw s)
  935. 1 if first stressed syllable in word, 0 otherwise."
  936.   (if (and (string-equal "s" (item.feat s "MetricalValue"))
  937.        (mt_no_stress_before (item.relation.prev s 'Syllable)))
  938.       "1"
  939.       "0"))
  940.  
  941. (define (mt_nfssw s)
  942.   "(nfssw s)
  943. 1 if second or later stressed syllable in word, 0 otherwise."
  944.   (if (and (string-equal "s" (item.feat s "MetricalValue"))
  945.        (null (mt_no_stress_before (item.relation.prev s 'Syllable))))
  946.       "1"
  947.       "0"))
  948.  
  949. (define (mt_no_stress_before ss)
  950.   (cond
  951.    ((null ss) t)
  952.    ((not (string-equal 
  953.       (item.feat ss "R:WordStructure.root.addr")
  954.       (item.feat (item.next ss) "R:WordStructure.root.addr")))
  955.     t)
  956.    ((string-equal "s" (item.feat ss "MetricalValue"))
  957.     nil)
  958.    (t
  959.     (mt_no_stress_before (item.prev ss)))))
  960.  
  961. (define (word_mt_break w)
  962.   (cond
  963.    ((string-equal "1" (item.feat w "sentence_end"))
  964.     "BB")
  965.    ((string-equal "1" (item.feat w "phrase_end"))
  966.     "B")
  967.    (t
  968.     "0")))
  969.   
  970. (provide 'tilt)
  971.