home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / lib / synthesis.scm < prev    next >
Lisp/Scheme  |  1999-06-16  |  15KB  |  439 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: Richard Caley (rjc@cstr.ed.ac.uk)             ;;
  35.  ;;                   Date: Fri Aug 15 1997                               ;;
  36.  ;; -------------------------------------------------------------------   ;;
  37.  ;; New synthesis mainline.                                               ;;
  38.  ;;                                                                       ;;
  39.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40.  
  41.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42.  ;;                                                                       ;;
  43.  ;; Hooks to add to the synthesis process.                                ;;
  44.  ;;                                                                       ;;
  45.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46.  
  47. (defvar default_before_synth_hooks nil
  48.   "default_before_synth_hooks
  49.    The default list of functions to be run on all synthesized utterances
  50.    before synthesis starts.")
  51.  
  52. (defvar before_synth_hooks default_before_synth_hooks
  53.   "before_synth_hooks
  54.    List of functions to be run on synthesised utterances before synthesis
  55.    starts.")
  56.  
  57. (defvar default_after_analysis_hooks nil
  58.   "default_after_analysis_hooks
  59.    The default list of functions to be run on all synthesized utterances
  60.    after analysis but before synthesis.")
  61.  
  62. (defvar after_analysis_hooks default_after_analysis_hooks
  63.   "after_analysis_hooks
  64.    List of functions to be applied after analysis and before synthesis.")
  65.  
  66. (defvar default_after_synth_hooks nil
  67.   "default_after_synth_hooks
  68.    The default list of functions to be run on all synthesized utterances
  69.    after Wave_Synth.  This will normally be nil but if for some reason you
  70.    need to change the gain or rescale *all* waveforms you could set the
  71.    function here, in your siteinit.scm.")
  72.  
  73. (defvar after_synth_hooks default_after_synth_hooks
  74.   "after_synth_hooks
  75.    List of functions to be applied after all synthesis modules have been
  76.    applied.  This is primarily designed to allow waveform manipulation,
  77.    particularly resampling and volume changes.")
  78.  
  79. (defvar default_access_strategy 'ondemand
  80.   "default_access_strategy
  81.    How to access units from databases.")
  82.  
  83.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84.  ;;                                                                       ;;
  85.  ;; Macro to define utterance types.                                      ;;
  86.  ;;                                                                       ;;
  87.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  88.  
  89. (defmac (defUttType form)
  90.   (list 'defUttType_real 
  91.     (list 'quote (cadr form))
  92.     (list 'quote (cddr form))))
  93.  
  94. (defvar UttTypes nil
  95.   "UttTypes
  96.    List of types and functions used by the utt.synth function to call 
  97.    appropriate methods.")
  98.  
  99. (define (defUttType_real type form)
  100.   "(defUttType TYPE . BODY)
  101.    Define a new utterance type.  TYPE is an atomic type that is specified
  102.    as the first argument to the function Utterance.  BODY is evaluated
  103.    with argument utt, when utt.synth is called with an utterance of type
  104.    TYPE.  You almost always require the function Initialize first.
  105.    [see Utterance types]"
  106.   ;;; Yes I am cheating a bit with the macro/function name.
  107.   ;;; should check about redefining and the syntax of the forms
  108.   (set! UttTypes
  109.     (cons 
  110.      (cons type form)
  111.      UttTypes))
  112.   type)
  113.  
  114.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  115.  ;;                                                                       ;;
  116.  ;; Macro to define synthesis types.                                      ;;
  117.  ;;                                                                       ;;
  118.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  119.  
  120. (defmac (defSynthType form)
  121.   (list 'defSynthType_real 
  122.     (list 'quote (cadr form))
  123.     (list 'quote (cddr form))))
  124.  
  125. (defvar SynthTypes nil
  126.   "SynthTypes
  127.    List of synthesis types and functions used by the utt.synth function to
  128.    call appropriate methods for wave synthesis.")
  129.  
  130. (define (defSynthType_real type form)
  131.   "(defSynthType TYPE . BODY)
  132.    Define a new wave synthesis type.  TYPE is an atomic type that
  133.    identifies the type of synthesis. BODY is evaluated with argument
  134.    utt, when utt.synth is called with an utterance of type TYPE.
  135.    [see Utterance types]"
  136.  
  137.   (set! SynthTypes
  138.     (cons 
  139.      (cons type form)
  140.      SynthTypes))
  141.   type)
  142.  
  143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  144. ;;;
  145. ;;;  Some actual Utterance type definitions
  146. ;;;
  147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  148.  
  149. (defUttType Words
  150.   (Initialize utt)
  151.   (POS utt)
  152.   (Phrasify utt)
  153.   (Word utt)
  154.   (Pauses utt)
  155.   (Intonation utt)
  156.   (PostLex utt)
  157.   (Duration utt)
  158.   (Int_Targets utt)
  159.   (Wave_Synth utt)
  160.   )
  161.  
  162. (defUttType Text
  163.   (Initialize utt)
  164.   (Text utt)
  165.   (Token_POS utt)
  166.   (Token utt)
  167.   (POS utt)
  168.   (Phrasify utt)
  169.   (Word utt)
  170.   (Pauses utt)
  171.   (Intonation utt)
  172.   (PostLex utt)
  173.   (Duration utt)
  174.   (Int_Targets utt)
  175.   (Wave_Synth utt)
  176.   )
  177.  
  178. (defUttType Tokens   ;; This is used in tts_file, Tokens will be preloaded
  179.   (Token_POS utt)    ;; when utt.synth is called
  180.   (Token utt)        
  181.   (POS utt)
  182.   (Phrasify utt)
  183.   (Word utt)
  184.   (Pauses utt)
  185.   (Intonation utt)
  186.   (PostLex utt)
  187.   (Duration utt)
  188.   (Int_Targets utt)
  189.   (Wave_Synth utt)
  190.   )
  191.  
  192. (defUttType Concept  ;; rather gradious name for when information has
  193.   (POS utt)          ;; been preloaded (probably XML) to give a word
  194.   (Phrasify utt)     ;; relation (SOLE uses this)
  195.   (Word utt)
  196.   (Pauses utt)
  197.   (Intonation utt)
  198.   (PostLex utt)
  199.   (Duration utt)
  200.   (Int_Targets utt)
  201.   (Wave_Synth utt)
  202.   )
  203.  
  204. (defUttType Phrase
  205.   (Initialize utt)
  206.   (Token_POS utt)
  207.   (Token utt)        
  208.   (POS utt)
  209.   (Phrasify utt)
  210.   (Word utt)
  211.   (Pauses utt)
  212.   (Intonation utt)
  213.   (PostLex utt)
  214.   (Duration utt)
  215.   (Int_Targets utt)
  216.   (Wave_Synth utt)
  217.   )
  218.  
  219. (defUttType Segments
  220.   (Initialize utt)
  221.   (Wave_Synth utt)
  222.   )
  223.  
  224. (defUttType Phones
  225.   (Initialize utt)
  226.   (Fixed_Prosody utt)
  227.   (Wave_Synth utt)
  228.   )
  229.  
  230. (defUttType SegF0
  231.   (Wave_Synth utt)
  232.   )
  233.  
  234. (defUttType Wave
  235.   (Initialize utt))
  236.  
  237.  
  238.  
  239.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240.  ;;                                                                       ;;
  241.  ;; And some synthesis types.                                             ;;
  242.  ;;                                                                       ;;
  243.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  244.  
  245. (defSynthType Taylor
  246.   (Taylor_Synthesize utt)
  247.   )
  248.  
  249. (defSynthType UniSyn
  250.   (defvar UniSyn_module_hooks nil)
  251.   (Param.def "unisyn.window_name" "hanning")
  252.   (Param.def "unisyn.window_factor" 1.0)
  253.   (Parameter.def 'us_sigpr 'lpc)
  254.  
  255.   (apply_hooks UniSyn_module_hooks utt)  ;; for processing of diphone names
  256.   (us_get_diphones utt)
  257.   (us_unit_concat utt)
  258.   
  259.   (if (not (member 'f0 (utt.relationnames utt)))
  260.       (targets_to_f0 utt))
  261. ;; temporary fix
  262.   (if (utt.relation.last utt 'Segment)
  263.       (set! pm_end (+ (item.feat (utt.relation.last utt 'Segment) "end") 0.02))
  264.       (set! pm_end 0.02))
  265.  
  266.   (us_f0_to_pitchmarks  utt 'f0 'TargetCoef pm_end)
  267.   (us_mapping utt 'segment_single)
  268.   (cond
  269.    ((string-equal "td_psola" (Parameter.get 'us_sigpr))
  270.     ;; Not in standard distribution, so has to be separate function
  271.     (us_tdpsola_synthesis utt 'analysis_period))
  272.    (t
  273.     ;; All the rest 
  274.     (us_generate_wave utt (Parameter.get 'us_sigpr)
  275.               'analysis_period)))
  276. )
  277.  
  278. (defSynthType None
  279.   ;; do nothing
  280.   utt
  281.   )
  282.  
  283. (defSynthType Standard
  284.   (print "synth method: Standard")
  285.   
  286.   (let ((select (Parameter.get 'SelectionMethod)))
  287.     (if select
  288.     (progn
  289.      (print "select")
  290.      (apply select (list utt))
  291.      )
  292.     )
  293.     )
  294.  
  295.   (let ((join (Parameter.get 'JoiningMethod)))
  296.     (if join
  297.     (progn
  298.      (print "join")
  299.      (apply join (list utt))
  300.      )
  301.     )
  302.     )
  303.  
  304.   (let ((impose (Parameter.get 'ImposeMethod)))
  305.     (if impose
  306.     (progn
  307.      (print "impose")
  308.      (apply impose (list utt))
  309.      )
  310.     )
  311.     )
  312.  
  313.   (let ((power (Parameter.get 'PowerSmoothMethod)))
  314.     (if power
  315.     (progn
  316.      (print "power")
  317.      (apply power (list utt))
  318.      )
  319.     )
  320.     )
  321.  
  322.   (let ((wavesynthesis (Parameter.get 'WaveSynthesisMethod)))
  323.     (if wavesynthesis
  324.     (progn
  325.      (print "synthesis")
  326.      (apply wavesynthesis (list utt))
  327.      )
  328.     )
  329.     )
  330.   )
  331.  
  332. (defSynthType Minimal
  333.   (print "synth method: Minimal")
  334.   
  335.   (let ((select (Parameter.get 'SelectionMethod)))
  336.     (if select
  337.     (progn
  338.      (print "select")
  339.      (apply select (list utt))
  340.      )
  341.     )
  342.     )
  343.  
  344.   (let ((wavesynthesis (Parameter.get 'WaveSynthesisMethod)))
  345.     (if wavesynthesis
  346.     (progn
  347.      (print "synthesis")
  348.      (apply wavesynthesis (list utt "Unit" "Join" "Wave"))
  349.      )
  350.     )
  351.     )
  352.   )
  353.  
  354.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  355.  ;;                                                                       ;;
  356.  ;; Finally the actual driver function.                                   ;;
  357.  ;;                                                                       ;;
  358.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  359.  
  360. (define (utt.synth utt)
  361.  
  362.   "(utt.synth UTT) 
  363.     The main synthesis function.  Given UTT it will apply the
  364.     functions specified for UTT's type, as defined with deffUttType
  365.     and then those demanded by the voice.  After modules have been
  366.     applied synth_hooks are applied to allow extra manipulation.
  367.     [see Utterance types]"
  368.  
  369.   (apply_hooks before_synth_hooks utt)
  370.  
  371.   (let ((type (utt.type utt)))
  372.     (let ((definition (assoc type UttTypes)))
  373.       (if (null? definition)
  374.       (error "Unknown utterance type" type)
  375.       (let ((body (eval (cons 'lambda 
  376.                   (cons '(utt) (cdr definition))))))
  377.         (body utt)))))
  378.  
  379.   (apply_hooks after_synth_hooks utt)
  380.   utt)
  381.  
  382.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  383.  ;;                                                                       ;;
  384.  ;; And a couple of utility expressions.                                  ;;
  385.  ;;                                                                       ;;
  386.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  387.  
  388. (define (SayText text)
  389. "(SayText TEXT)
  390. TEXT, a string, is rendered as speech."
  391.    (utt.play (utt.synth (eval (list 'Utterance 'Text text)))))
  392.  
  393. (define (SayPhones phones)
  394. "(SayPhones PHONES)
  395. PHONES is a list of phonemes.  This uses the Phones type utterance
  396. to synthesize and play the given phones.  Fixed duration specified in
  397. FP_duration and fixed monotone duration (FP_F0) are used to generate
  398. prosody."
  399.    (utt.play (utt.synth (eval (list 'Utterance 'Phones phones)))))
  400.  
  401.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  402.  ;;                                                                       ;;
  403.  ;; This is the standard synthesis function.  The Wave Synthesis may be   ;;
  404.  ;; more than a simple module                                             ;;
  405.  ;;                                                                       ;;
  406.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  407.  
  408.  
  409. (define (Wave_Synth utt)
  410. "(Wave_Synth UTT)
  411.   Generate waveform from information in UTT, at least a Segment stream
  412.   must exist.  The actual form of synthesis used depends on the Parameter
  413.   Synth_Method.   If it is a function that is applied.  If it is atom it
  414.   should be a SynthType as defined by defSynthType
  415.   [see Utterance types]"
  416.   (apply_hooks after_analysis_hooks utt)
  417.   (let ((method_val (Parameter.get 'Synth_Method)))
  418.     (cond
  419.      ((null method_val)
  420.       (error "Undefined Synth_Method"))
  421.      ((and (symbol? method_val) (symbol-bound? method_val))
  422.       ;; Wish there was a function? 
  423.       (apply (symbol-value method_val) (list utt)))
  424.      ((member (typeof method_val) '(subr closure))
  425.       (apply method_val (list utt)))
  426.      (t  ;; its a defined synthesis type
  427.       (let ((synthesis_modules (assoc_string method_val SynthTypes)))
  428.     (if (null? synthesis_modules)
  429.         (error (format nil "Undefined SynthType %s\n" method_val))
  430.         (let ((body (eval (cons 'lambda 
  431.                     (cons '(utt) (cdr synthesis_modules))))))
  432.           (body utt)))))))
  433.   utt)
  434.  
  435. (provide 'synthesis)
  436.  
  437.  
  438.  
  439.