;; ===================================================================================
;; Italian FESTIVAL Modules(IFM)
;; Copyright (C) 2001-2005 by the IFM Development Team
;; at "ISTC-SPFD CNR" and at "ITC-Irst".
;; ===================================================================================
;;	ISTC-SPFD CNR
;;		Istituto di Scienze e Tecnologie della Cognizione
;;		Sezione di Padova "Fonetica e Dialettologia"
;;		Consiglio Nazionale delle Ricerche
;;		Via G. Anghinoni, 10 - 35121 Padova
;;		tel (+39) 049 8274418 - fax (+39) 049 8274416
;;		e-mail: segreteria@pd.istc.cnr.it 
;; 
;;	ITC-irst
;;		Istituto Trentino di Cultura
;;		Centro per la ricerca scientifica e tecnologica 
;;		Via Santa Croce 77 - 38100 Trento ITALIA
;;		tel (+39) 0461-210111 - fax (+39) 0461-980436
;;		e-mail: info@itc.it 
;; ===================================================================================
;; This file is part of IFM.
;; 
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;; ===================================================================================
;; Authors:	Piero COSI, ISTC-SPFD CNR, (cosi@pd.istc.cnr.it)
;; 		Carlo DRIOLI
;; 		Graziano TISATO
;; 		Roberto GRETTER, ITC-irst (SSI/MPA), (gretter@itc.it) 
;; 		Fabio TESSER
;; ===================================================================================
;;		WEB:  http://www.pd.istc.cnr.it/TTS/ItalianFESTIVAL
;; ===================================================================================
;;
;;  Set up lp_ogi_diphone using the OGI FESTIVAL diphone synthesizer
;;  Loredana Panato diphones: female Italian collected February 2000 at IRST
;;
;; ===================================================================================
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  Note this requires OGI FESTIVAL modules which have their own licence
;;;  different from the one above
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Add the directory contains general italian stuff to load-path
(defvar lp_ogi_diphone_dir (cdr (assoc 'lp_ogi_diphone voice-locations)))
(set! load-path (cons (path-append lp_ogi_diphone_dir "festvox/") load-path))
;; for common italian modules
(set! load-path (cons (path-append libdir "italian_scm/") load-path))

;;; other files we need
(require 'italian_phoneset)
(require 'italian_duration)
(require 'ifd_diph_ds_dur)
(require 'italian_intonation)
(require 'italian_lts)
(require 'italian_lexicon)
(require 'italian_phrasing)
(require 'italian_token)
(require 'italian_oredatetel)
(require 'italian_gpos)
(require 'italian_module)
(require 'ita_map)

(require 'ogi_unitsel)
(require 'ogi_synthesis)

;;; Use the grouped or ungrupped file by default ?
(defvar lp_ogi_di_grouped_or_ungrouped 'grouped)

;;VOICE ResEt....
(define (italian_voice_reset)
  "(italian_voice_reset)
Reset global variables back to previous voice."
  (set! token.prepunctuation italian_previous_tok_prepunc)
  (set! token.punctuation italian_previous_tok_punc) ;F_MOD per l'italiano devo toglier l'accento ' dalla punteggiatura e non dalla prepunteggiatura
  (load (path-append libdir "synthesis.scm"));;per togliere i set dei moduli da noi utilizzati
  
)

;;;  Full voice definition 
(define (voice_lp_ogi_diphone)
"(voice_italian_lp)
Set up synthesis for Female Italian speaker: Loredana Panato"
  (voice_reset)
  
  (Parameter.set 'Language 'italian)
  ;; Phone set
  (Parameter.set 'PhoneSet 'italian)
  (PhoneSet.select 'italian)

  ;; numeric expansion
  (Parameter.set 'Token_Method 'Token_Any)
  (set! token_to_words italian_token_to_words)

  ;; Because of use of ' for accents remove it from prepunctuation
  (set! italian_previous_tok_prepunc token.prepunctuation)
  (set! italian_previous_tok_punc token.punctuation)
  (set! token.prepunctuation "\"'`({[")  ;)F_MOD ;default "\"'`({[")
  (set! token.punctuation "\".,:;!?(){}[]") ;F_MOD ;default "\"'`.,:;!?(){}[]" ....fabio ho tolto '`
  (set! token.whitespace " \t\n\r") ;fabio ha tolto il meno da whithe space per i numeri
   
  ;;; Postlexical rules
  (set! postlex_vowel_reduce_cart_tree  nil)
  (set! postlex_rules_hooks nil)

  ;; No pos prediction (get it from lexicon)
  (set! pos_lex_name nil)
  ;; Phrase break prediction by punctuation
  (set! pos_supported nil) ;; well not real pos anyhow
  ;; Phrasing
  (set! phrase_cart_tree italian_phrase_cart_tree)
  (Parameter.set 'Phrase_Method 'cart_tree)
  
 ;; Lexicon selection
 ;(lex.select "italian")
 (lex.select "otherlex")


;;;;;;; F0 prediction ;;;;;;;;;;;;;;;
;  (set! f0_lr_start f2b_f0_lr_start)
;  (set! f0_lr_mid f2b_f0_lr_mid)
;  (set! f0_lr_end f2b_f0_lr_end)
;  (Parameter.set 'Int_Method Intonation_Tree)

;;;;;;;;GENERAL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
  ;; Accent and tone prediction
 (set! int_accent_cart_tree italian_accent_cart_tree)
 (Parameter.set 'Int_Target_Method 'Simple)
 (Parameter.set 'Int_Method 'General)
  
  ;;INTONAZIONE F_W+Question
  (set! int_general_params (list (list 'targ_func targ_func_fw_q_fem)))
  
;;;;;;;;;;;;;;;;;;;;;;FINE GENERAL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Part of speech Function Word
 (set! guess_pos italian_guess_pos)

;; Pauses prediction
 (Parameter.set 'Pause_Method Classic_Pauses_mB)

;;;;;;;;;;;; Duration prediction ;;;;;;;;;;;;;;
;; David Sassoli ;;
 (set! duration_cart_tree ifd_diph_ds::zdurtree)
 (set! duration_ph_info ifd_diph_ds::phone_durs)
  (Parameter.set 'Duration_Method 'Tree_ZScores)
  
  ;; Waveform synthesizer: diphones
  
  ;;;;;;  OGI  ;;;;;
  ;; diphone unit selection fallbacks (none)
  ;(set! ogi_di_alt_L '((m= (m))  ))
  ;(set! ogi_di_alt_R '((m= (m))  ))
  
  (set! ogi_di_alt_L '( ))
  (set! ogi_di_alt_R '( ))
  (set! ogi_di_default "#-#")
  
  (Parameter.set 'Synth_Method 'OGIdiphone)
  (OGIdbase.activate "lp_ogi_diphone")      
  (OGIresLPC.init lp_ogi_di_OGI_syn_params)  
  
  (require 'italian_module_flush)
  ;; set callback to restore some original values changed by the italian voice
  (set! current_voice_reset italian_voice_reset)
  (set! current-voice 'lp_ogi_diphone)
)

;; Context-dependent smoothing:
;;   (feat val int) for each
;;   Can put any feat,val pair here, as long as feat is defined in phoneset definition.
;;   Smoother setup algorithm iterates through list until feat, val is matched, then uses int as 
;;     smoothing window length.  This could be improved.
(set! lp_ogi_di_smooth_spectra   ;;; max number of frames to use in smoothing LPC at joins
      '(spectra_smooth
        ((vc + 10)   ; vowel
         (ctype s 0) ; 0 stop
         (ctype f 2) ;2 fricative
         (ctype a 0) ; 0 affricate
         (ctype n 5) ; 5 nasal
         (ctype l 5) ; 5 lateral
         (ctype r 5) ; 5 approximant
         )))

(set! lp_ogi_di_smooth_power   ;;; max number of frames to use in smoothing power at joins
      '(power_smooth
        ((vc + 10)   ; vowel
         (ctype s 0) ; 0 stop
         (ctype f 2) ; 2 fricative
         (ctype a 0) ; 0 affricate
         (ctype n 5) ; 5 nasal
         (ctype l 5) ; 5 lateral
         (ctype r 5) ; 5 approximant 
         )))
;;
;;  Residual LPC synthesizer parameters - used by OGIresLPC.init
;;
(set! lp_ogi_di_OGI_syn_params  
 (list
  '(F0_default 50.0)      ;; default Fo used if can't find any Fo targets
  '(T0_UV_thresh 0.020)   ;; used to make V/UV decision 
  '(T0_UV_pm 0.010)       ;; used to place UV pmarks
  '(post_gain 1.0)        ;; adjust final loudness
  '(deemphasis 0.94)      ;; 0.94 opposite of preemphasis (lowpass filter)
  '(mod_method "direct")  ;; method for realizing prosodic targets
  '(beta_smooth 5)        ;; 3 smoother len for pitch mod factor in "soft" mod method
  '(window_type "trapezoid") 
  '(smooth_cross_ph_join "Y") ;; smooth across joins at phone boundaries?
  '(spectra_match_or_replace "match") ;; for join smoothing
  '(power_match_or_replace   "match") ;; for join smoothing
  lp_ogi_di_smooth_spectra
  lp_ogi_di_smooth_power
  ))
  

(define (init_lp_ogi_diphone)
"(init_lp_ogi_diphone)
  Initialise the PC_OGI diphone database."
  
(if (equal? lp_ogi_di_grouped_or_ungrouped 'ungrouped)
    ;;  ungrouped
    (OGIdbase.init
     (list
      '(dbname lp_ogi_diphone)
      (list 'unitdic_file (path-append lp_ogi_diphone_dir "festvox" "lp_diph_ogi.msec"))
      ;(list 'gain_file (path-append lp_ogi_diphone_dir "festvox" "lp_ogi_gain.dat"))
      '(phoneset "italian")  
      '(base_dir "/dev/h/Sintesi/Data/LoredanaPanato/reduced/")
      '(lpc_dir "lpc_ogi/")
      '(lpc_ext ".lsf")
      '(exc_dir "lpc_ogi/")
      '(exc_ext ".res")
      '(pm_dir "pm/")
      '(pm_ext ".pmv")
      '(data_type "resLPC")
      '(access_mode "ondemand")
      '(samp_freq 16000)
      '(sig_band 0.010)
      '(isCompressed "Y") ;; if "Y", compress when saving group file
      '(preemph 0.96)  ;0.96
      ))

    ;;  grouped -- parameters set here override those in groupfile
    (OGIdbase.init   
     (list
      '(dbname lp_ogi_diphone)
      (list 'groupfile (path-append lp_ogi_diphone_dir "group" "lp_ogi_diphone.group"))
      '(access_mode "ondemand")
      ))
    t)
)

(init_lp_ogi_diphone)

(proclaim_voice
 'lp_ogi_diphone
 '((language italian)
   (gender female)
   (dialect none)
   (description
    "This voice provides a Italian female voice using a
     residual excited LPC diphone synthesis method.  The lexicon
     is provived by a set of letter to sound rules producing pronunciation
     accents and syllabification.  The durations, intonation and
     prosodic phrasing are minimal but are acceptable for simple
     examples.")))

(provide 'lp_ogi_diphone)