2 * Copyright 1998-2001 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
8 package com.sun.speech.engine.synthesis;
10 import java.util.List;
12 import javax.speech.synthesis.SynthesizerModeDesc;
13 import javax.speech.synthesis.Voice;
16 * Maintains a list of JSAPI 1.0 <code>Voices</code>.
18 public class VoiceList {
21 * The list of <code>Voices</code>.
23 protected final List voiceList;
29 voiceList = new java.util.ArrayList();
33 * Constructs from the voice list in <code>desc</code>.
34 * Requires that all voices in mode desc be instances of
35 * <code>BaseVoice</code>.
37 * @param desc the <code>SynthesizerModeDesc</code> to get voices from
39 public VoiceList(SynthesizerModeDesc desc) {
40 voiceList = new java.util.ArrayList();
42 Voice[] v = desc.getVoices();
45 for (int i = 0; i < v.length; i++) {
46 addVoice((BaseVoice)(v[i]));
52 * Adds a voice to the list.
54 * @param voice the voice to add
58 public void addVoice(BaseVoice voice) {
59 if (!voiceList.contains(voice)) {
65 * Removes a voice from the list.
67 * @param voice the voice to remove
71 public void removeVoice(BaseVoice voice) {
72 voiceList.remove(voice);
76 * Gets a voice by its identifier.
78 * @param id the voice id
80 * @return the voice if it exists; otherwise <code>null</code>
82 * @see BaseVoice#getId
84 public BaseVoice getVoiceById(String id) {
85 for (int i = 0; i < voiceList.size(); i++) {
86 BaseVoice bv = (BaseVoice)(voiceList.get(i));
87 if (bv.getId().equals(id)) {
95 * Gets the id of a voice.
97 * @param voice the voice
98 * @param variant the voice variant
100 * @return the id of the voice
102 public String getVoiceId(Voice voice, int variant) {
103 // If voice is a BaseVoice simply return its id.
105 if (voice instanceof BaseVoice) {
106 BaseVoice bv = (BaseVoice)voice;
107 String id = bv.getId();
108 if (id != null && id.length() > 0) {
113 // Build a list of indicies of all matching voices.
114 // If variant is <= 0 return with first match
116 int indexes[] = new int[voiceList.size()];
119 for (int i=0; i<voiceList.size(); i++) {
120 BaseVoice bv = (BaseVoice)(voiceList.get(i));
121 if (bv.match(voice)) {
125 indexes[count++] = i;
129 // If no matches, return "".
134 // Apply modulo to select variant in list
135 variant = (variant - 1) % count;
137 // Return the selected voice id.
138 BaseVoice bv = (BaseVoice)(voiceList.get(indexes[variant]));
143 * Gets id for voice based on parameters provided in JSML.
144 * Priority to voice name. Then try to match age and gender plus
147 * @param name the voice name
148 * @param gender the gender
150 * @param variant the variant
152 * @return the voice id
157 public String getVoiceId(String name, int gender, int age, int variant) {
160 // Is there a match by voice name? If yes, return it.
161 // Otherwise, ignore name.
162 if (name != null && name.length() > 0) {
163 id = getVoiceId(new Voice(name,
164 Voice.GENDER_DONT_CARE,
168 if (id != null && id.length() > 0) {
174 // Try to match gender and age
175 id = getVoiceId(new Voice(null,
180 if (id != null && id.length() > 0) {
184 // Try to match gender and adjoining ages
185 int looseAge = age | (age << 1) | (age >> 1);
186 id = getVoiceId(new Voice(null,
191 if (id != null && id.length() > 0) {
195 // Try to match just gender
196 id = getVoiceId(new Voice(null,
201 if (id != null && id.length() > 0) {