// PB General rules for vowels:
// Short vowels
// ACC: Short "pille" [p?el@-], "andre" [AndRV]
// AC[V]: Short "piler" [p?ilV] - verbs, not nouns, which is a problem
// A[N]: Short "bange" [b?AN@-]
// Long vowels
// A + @- OR V: Long "ae" "aer" [&:@-]
// AC + @- OR V: Long "pile" [pi:l@-]
// AC[i]: Long "smidig" [smi:Di]

// Change the length of short vowels (?+vowel)
procedure ShortVowelLength
// "endelig" - Short initial vowel sounds too short
IF thisPh(isWordStart) THEN
	length 160
	RETURN
ENDIF
// "slutte" t/d + @- makes the u too long
IF next2PhW(@-) THEN
  IF nextPhW(t) OR nextPhW(d) THEN
	//length 100
	  LengthAdd -50
	  // Don't shorten it further if it comes after an "r" sound
	  // Exit the procedure
	  RETURN
  ENDIF
ENDIF
// "bygget" - consonant + [@-D] makes the vowel too long
IF next2PhW(@-) THEN
  IF next3PhW(t) OR next3PhW(d) OR next3PhW(D) THEN
	  LengthAdd -50
	  // Don't shorten it further if it comes after an "r" sound ("brygget")
	  // Exit the procedure
	  RETURN
  ENDIF
ENDIF
// "bygger" - consonant + [V] makes the vowel too long
IF nextPhW(isNotVowel) AND next2PhW(V) THEN
	  LengthAdd -50
	  // Don't shorten it further if it comes after an "r" sound ("brygger")
	  // Exit the procedure
	  RETURN
ENDIF
// "rigtigt", "fred", "frem", "centralen" - R makes the vowel too long
  IF prevPhW(R) OR prevPhW(3-) OR prevPhW(r) THEN
//     length 100
	LengthAdd -50
  ENDIF
// "ring", "ringe", "fængsel"
  IF nextPhW(N) THEN
//    length 100
	LengthAdd -10
  ENDIF
// "sigte" t/d + @- makes the vowel too long
  IF nextPhW(isNotVowel) AND next2PhW(t) OR next2PhW(d) THEN
	IF next3PhW(@-) THEN
//		length 100
		LengthAdd -50
    ENDIF	
  ENDIF
endprocedure

// Change the length of normal vowels (without ? in front of them)
procedure LongVowelLength
// "alene" - Short initial vowel sounds too short at length 140
IF thisPh(isWordStart) THEN
	length 160
	RETURN
ENDIF
// "forlade" - [D@-] makes the vowel too long
IF nextPhW(D) AND next2PhW(@-) THEN
	length 180
	RETURN
ENDIF
// PB long vowel followed by [@-] or [V](vowel+vowel) - "pigen" [p'i@-n]
  IF nextPhW(@-) OR nextPhW(V) THEN
	length 225
  ENDIF
// PB "enig", "enige", "evig", "stædig" - vowel+consolant+[i]
  IF nextPhW(isNotVowel) AND next2PhW(i) THEN
	length 225
  ENDIF
// "ræve", "dele", "mene", "røve", "møve"
IF nextPhW(isNotVowel) AND next2PhW(@-) OR next2PhW(3) OR next3PhW(@-) THEN
	length 225
ENDIF
// "vilje", "nedladende" [n'eDl&D@-n@-], "delte" - short followed by 2 consonants
  IF nextPhW(isNotVowel) AND next2PhW(isNotVowel) THEN
  // Don't make "møve" [m'Ww_!@-_!] short
    IF NOT next2PhW(_!) THEN
	  length 140
    ENDIF
  ENDIF
// "bryde", "bryder", "strålen", "henrivende" R makes the vowel too long
  IF prevPhW(R) OR prevPhW(3-) OR prevPhW(r) THEN
    IF next2PhW(@-) OR next2PhW(V) THEN
//      length 180
		LengthAdd -70
	ENDIF
  ENDIF
  // "syste", "sylte" t/d/D + @- makes the vowel too long
  // "international", - added [V] TEST
  IF nextPhW(isNotVowel) AND next2PhW(t) OR next2PhW(d) OR next2PhW(D) THEN
	IF next3PhW(@-) OR next3PhW(V) THEN
		length 110
    ENDIF	
  ENDIF
// "glimrende" [l/3] makes the following vowel too long
// LengthAdd doesn't work here. The length could be 225 or 140 - 30
  IF prevPhW(l/3) THEN
//	LengthAdd -30
	length 110
  ENDIF
// "længe" - short - 2 consonants => 1 consonant (ng => [N])
IF nextPhW(N) THEN
	length 140
ENDIF
endprocedure

// A bit longer than [@-]
phoneme @
  vowel starttype #@ endtype #@
  unstressed
  length 60
  FMT(vowel/@_3)
endphoneme

phoneme @-   //  very short schwa
  vowel starttype #@  endtype #@
  unstressed nonsyllabic
  ipa ə
  IF nextPhW(*) OR nextPhW(r) THEN
    ipa NULL   // @-* is used to make 'r'
  ENDIF
  length 50
// "femten", "manden" - only a short "n" sound
  IF nextPhW(n) THEN
	length 15
  ENDIF
IF prevPhW(isNotVowel) AND thisPh(isWordEnd) THEN
	length 15
ENDIF
// "lige" [li@-]
IF prevPhW(i) AND thisPh(isWordEnd) THEN
	length 15
ENDIF
  FMT(vowel/@-)
endphoneme

// ToDo: change [@] to [a#] if adjacent to [r] or [R]
phoneme 3
  vowel starttype #a endtype #a
  unstressed
  length 40
  FMT(vowel/a#_3)
endphoneme

// PB sort of schwa [ɐ] (0250+032F)
// "spurgt" [sp'o3-d] – ipa [spˈoɐ̯d]
phoneme 3-    // used for 'r' after a vowel (to create a diphthong)
  liquid
  lengthmod 7
  ipa ɐ̯
    FMT(r/a_)
endphoneme

phoneme i
  vowel starttype #i endtype #i
  length 140
// Long vowel followed by consonant and @- "gide", "pile"
IF nextPhW(isNotVowel) AND next2PhW(@-) THEN
//	length 225
ENDIF
// "gider", vrider"
IF nextPhW(D) AND next2PhW(V) OR next2PhW(?V) THEN
	length 140
ENDIF
//"skider" short
IF nextPhW(D) AND next2PhW(V) THEN
    length 140
ENDIF
//"skideren" long
IF nextPhW(D) AND next2PhW(V) AND next3PhW(V) THEN
    length 225
ENDIF
CALL LongVowelLength
  FMT(vowel/i_4)
endphoneme

// PB short i
// sviret vs. svirret
phoneme ?i
  vowel starttype #i endtype #i
  length 140
  CALL ShortVowelLength
  IfNextVowelAppend(;)
  FMT(vowel/i_4)
endphoneme

// PB English i - Tim
phoneme I
  vowel  starttype #i  endtype #i
  length 130
  IfNextVowelAppend(;)
  FMT(vowel/ii_2)
endphoneme

phoneme e
  vowel starttype #e endtype #e
  length 140 
  CALL LongVowelLength
  CALL ShortVowelLength
  FMT(vowel/e)
endphoneme

// PB short e
// "skille" vs. "skele", "pille" vs. "pile"
phoneme ?e
  vowel starttype #e endtype #e
  length 140
  CALL ShortVowelLength
  FMT(vowel/e)
endphoneme

phoneme E
  vowel starttype #e endtype #e
  length 140 
// no link with next vowel
IF thisPh(isWordEnd) THEN
  IfNextVowelAppend(_!)
ENDIF
// "ære", "kærester", "ærefrygt" - vowel + vowel
IF nextPhW(V) THEN
	length 225
ENDIF
// "dræber" TEST shortened by -70 in procedure because of the "r" sound
IF prevPhW(R) OR prevPhW(r) OR prevPhW(3-) AND next2PhW(V) THEN
	length 225
ENDIF
  CALL LongVowelLength
// "værelse"  [v'E3-Vls@_!]
IF nextPhW(3-) AND next2PhW(V) THEN
	length 100
ENDIF
  FMT(vowel/e_mid2)
endphoneme

// PB short E
// "læsse" vs. "læse"
phoneme ?E
  vowel starttype #e endtype #e
  ipa ε
  length 140
  CALL ShortVowelLength
  FMT(vowel/e_mid2)
endphoneme

phoneme &
  vowel starttype #e endtype #e
  ipa æ
  length 140
// "same", "sale", "bade" - consonant + @-: long
// but NOT "hinanden"
IF nextPhW(isNotVowel) AND next2PhW(@-) OR next2PhW(@) THEN
  IF NOT next3PhW(n) THEN
  	length 225
  ENDIF
ENDIF
// ThisPh + V or @-
// "ae", "aer" vowel + vowel: extra length
IF nextPhW(@-) OR nextPhW(V) THEN
	length 260
ENDIF
CALL LongVowelLength
  FMT(vowel/ee_2)
endphoneme

// Short &
// e.g. the last a in "staldkarl"
// "sale" vs. "sal"
phoneme ?&
  vowel starttype #e endtype #e
   length 140
   FMT(vowel/ee_2)
endphoneme

// Added for the æ in "dræbt"
phoneme &#
  vowel starttype  #e endtype #e
    length 140
	ipa a
	CALL LongVowelLength
//	CALL ShortVowelLength
	FMT(vowel/&)
endphoneme

// PB short &#
// "revl" vs. "tremme"
phoneme ?&#
  vowel starttype  #e endtype #e
    length 140
	CALL ShortVowelLength
		FMT(vowel/&)
endphoneme

phoneme A // PB changed to a_8
  vowel starttype #a endtype #a
  length 140 
CALL LongVowelLength
// "fare" [f'A:A]
IF nextPhW(A) THEN
	length 225
ENDIF
  FMT(vowel/a_8)
endphoneme

// PB short A
// "krabbe" vs. "drabelig"
phoneme ?A
  vowel starttype #a endtype #a
  length 140
CALL ShortVowelLength
  FMT(vowel/a_8)
endphoneme

phoneme u
  vowel starttype #u endtype #u
  length 140 
// "suge", "uge", "bluse", "julegave"
IF nextPhW(@-) OR nextPhW(V) OR next2PhW(@-) OR next2PhW(V)THEN
	length 225
ENDIF
CALL LongVowelLength
  FMT(vowel/u_bck)
endphoneme

// Short u
// "tude" vs. "tuden" - [tuD3] [t?uD@n]
phoneme ?u
  vowel starttype #u endtype #u
  length 140
  CALL ShortVowelLength
  FMT(vowel/u_bck)
endphoneme

phoneme o
  vowel starttype #o endtype #o
  length 140 
// "bore", "borer" [boV] o + V
IF nextPhW(V) OR nextPhW(@-) THEN
	length 225
ENDIF
// "kone", "koner" o + consonant + V or @-
IF next2PhW(V) OR next2PhW(@-) THEN
	length 225
ENDIF
CALL LongVowelLength
  FMT(vowel/o_2)
endphoneme

// PB Short o
// "patron" vs. "kone"
phoneme ?o
  vowel starttype #o endtype #o
  length 140
  FMT(vowel/o_2)
endphoneme

phoneme O
  vowel starttype #o endtype #o
  length 140
  ipa ɒ // changed from ɔ - Den Danske Ordbog: ɒ
// "sove", "sover" - consonant + @ or V: long
IF next2PhW(@-) OR next2PhW(V) THEN
	length 225
ENDIF
// PB "gået" - ThisPh + V or @-
IF nextPhW(V) OR nextPhW(@-) THEN
	length 225
ENDIF
// "såre", "sårede"  [s'O:?OD@-]
IF nextPhW(O) OR nextPhW(?O) THEN
	length 225
ENDIF
CALL LongVowelLength
  FMT(vowel/o_5)
endphoneme

// Short O
// "toget" vs. "tåget"
phoneme ?O
  vowel starttype #o endtype #o
  length 140
  FMT(vowel/o_5)
endphoneme

phoneme V
  vowel starttype #@ endtype #@
  length 140
CALL LongVowelLength
  FMT(vowel/V_4)
endphoneme

// PB Short V
// "forstår" vs. "kåre"
phoneme ?V
  vowel starttype #@ endtype #@
  length 140
  FMT(vowel/V_4)
endphoneme

phoneme 0
  vowel starttype #o endtype #o
  length 140 
  ipa ɔ
  FMT(vowel/oo_2)
endphoneme

// Short 0 "sukker"
phoneme ?0
  vowel starttype #o endtype #o
  length 140
  ipa ɔ
  FMT(vowel/oo_2)
endphoneme

phoneme y
  vowel starttype #i endtype #i
  length 140
// PB long vowel followed by consonant and certain vowels
// "gyde", "gyder"
IF nextPhW(isNotVowel) AND next2PhW(@-) OR next2PhW(V) THEN
	// not "gebyret" TEST
	IF NOT nextPhW(3-) AND NOT nextPhW(R) AND NOT nextPhW(r) THEN
		length 225
	ENDIF
ENDIF
// "syge" [sy@-] vowel + vowel: extra length, but NOT "fyret" [fyVD]
IF nextPhW(@-) OR nextPhW(3) AND NOT next2PhW(D) THEN
	length 225
ENDIF
// "tyve"
IF nextPhW(w) AND next2PhW(@-) THEN
	length 225
ENDIF
CALL LongVowelLength
  FMT(vowel/y)
endphoneme

// PB Short y
// "kylling" vs. "kyle", "krybbe" vs. "krybe"
phoneme ?y
  vowel starttype #i endtype #i
  length 140
  CALL ShortVowelLength
  FMT(vowel/y)
endphoneme

phoneme Y
  vowel starttype #i endtype #i
  length 140
IF nextPhW(V) OR nextPhW(@-) THEN
	length 225
ENDIF
  FMT(vowel/yy)
endphoneme

phoneme W
  vowel starttype #@ endtype #@
  length 140 
// "røveri" but NOT "surfer" [sWfV] TEST
IF next2PhW(V) OR next2PhW(?V) AND NOT next2PhW(isFinalVowel) THEN
	length 140
ENDIF
// "børnebog" - short
IF nextPhW(r) AND next2PhW(n) AND next3PhW(@-) THEN
	length 140
ENDIF
CALL LongVowelLength
  FMT(vowel/oe)
endphoneme

// Short W
// "prøv" vs. "prøve"
phoneme ?W
  vowel starttype #@ endtype #@
  length 140
  CALL ShortVowelLength
  FMT(vowel/oe)
endphoneme

// Added for the ø in "røv", "røg", "øje" instead of [V3]
phoneme W#
  vowel starttype #@ endtype #@
  length 140 //225
// PB long vowel followed by consonant and certain vowels
IF nextPhW(isNotVowel) AND next2PhW(@-) OR next2PhW(@) OR next2PhW(3) OR next2PhW(V) OR next2PhW(?V) THEN
	length 225
ENDIF
  FMT(vowel/V)
endphoneme

// Short W#
// "rømme"
phoneme ?W#
  vowel starttype #@ endtype #@
  length 140
  CALL ShortVowelLength
  FMT(vowel/V)
endphoneme

phoneme aI
  vowel starttype #a endtype #i
  length 300
IF NOT next2Ph(3) AND NOT next2Ph(V) AND NOT next2Ph(@-) AND nextPhW(isNotVowel) THEN
   glstop
   length 225
ENDIF
// "dreje" [dR'aI@-_!]
IF nextPhW(3) OR nextPhW(V) OR nextPhW(@) OR nextPhW(@-) THEN
	length 280
ENDIF
// PB "drej" vs. "dreje"
IF thisPh(isWordEnd) THEN
	length 140
ENDIF
// "lejde" [l'aId@-_!], "lejder" [l'aIdV_!]
IF nextPhW(isNotVowel) AND next2PhW(@-) OR next2PhW(V) THEN
	length 190
ENDIF
  FMT(vdiph/ai)
endphoneme


// CONSONANTS

// PB added l/3
phoneme l
  liquid
  lengthmod 7
// [ll] => [-l] - avoid double l
IF nextPhW(l) THEN
//    ChangePhoneme(-)
ENDIF
  ChangePhoneme(l/3)
//  CALL base/l
endphoneme

phoneme l/3    // Replacement for [l/]
  liquid
  lengthmod 7
  FMT(l/l_)
endphoneme

phoneme v        // approximant, not fricative
  import_phoneme base/v#
  voicingswitch f
endphoneme

// PB Actually a kind of schwa = [ɐ]? - "byder" [bˈyðɐʌ]
phoneme r    // used for 'r' after a vowel (to create a diphthong)
  liquid
  lengthmod 7
  ipa ɐ̯
  IF nextPhW(isVowel) AND NOT nextPhW(?V) AND NOT nextPhW(V) AND NOT nextPhW(@-) THEN
    ChangePhoneme(R)
  ENDIF
  FMT(r/a_)
endphoneme

phoneme R
  lengthmod 6
  liquid
  ipa ʁ
  FMT(r/aa)
endphoneme

// don't weaken consonants at end of word or before a stop

phoneme s
  vls alv frc sibilant
  voicingswitch z
  lengthmod 3
  Vowelin  f1=0  f2=1700 -300 300  f3=-100 80
  Vowelout f1=0  f2=1700 -300 250  f3=-100 80  rms=20

  IF nextPh(p) OR nextPh(t) OR nextPh(k) THEN
    WAV(ufric/s!)
  ENDIF
  WAV(ufric/s)
endphoneme

phoneme p
  vls blb stop
  voicingswitch b
  lengthmod 7
  Vowelin  f1=0  f2=1000 -50 -100  f3=-200 80 amp=11
  Vowelout f1=0  f2=1000 -500 -350  f3=-300 80 rms=22

  IF nextPh(isPause2) THEN
    WAV(ustop/p_unasp)
  ELIF nextPh(r) THEN
    WAV(ustop/pr, 70)
  ELIF nextPh(R) OR nextPh(R2) THEN
    WAV(ustop/pr)
  ELIF nextPh(@-) THEN
    WAV(ustop/p_unasp)
  ELIF nextPh(l) THEN
    WAV(ustop/pl)
  ENDIF
  WAV(ustop/p)
endphoneme

phoneme t
  vls alv stop
  voicingswitch d
  lengthmod 7
  Vowelin f1=0  f2=1700 -300 300  f3=-100 80
  Vowelout f1=0 f2=1700 -300 250  f3=-100 80  rms=20

  IF nextPh(r) OR nextPh(R) OR nextPh(R2) THEN
    WAV(ustop/tr)
  ENDIF
// "respekt"  [REsp'Egd]
  IF thisPh(isWordEnd) THEN
//	ChangePhoneme(d)
  ENDIF
  WAV(ustop/t, 90)
endphoneme

phoneme j
  liquid palatal
  lengthmod 7
// no link with next vowel
IF thisPh(isWordEnd) THEN
  IfNextVowelAppend(_!)
ENDIF
  IF nextPhW(isVowel) THEN
    NextVowelStarts
      VowelStart(j/j@)
      VowelStart(j/ja)
      VowelStart(j/je,-35)
      VowelStart(j/ji)
      VowelStart(j/jo,-65) // "kjole"
      VowelStart(j/ju)
    EndSwitch
Vowelout len=70
    VowelEnding(j/xj, -30)

    IF prevPh(isPause) THEN
      FMT(j/_j)
    ENDIF
  ELSE
    // no vowel follows
    Vowelout len=70
    FMT(j/j_)
  ENDIF
endphoneme