upstream version 1.2.2
[debian/freetts] / com / sun / speech / engine / synthesis / BaseSynthesizerProperties.java
1 /**
2  * Copyright 1998-2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.engine.synthesis;
9
10 import java.beans.PropertyVetoException;
11
12 import javax.speech.SpeechError;
13
14 import javax.speech.synthesis.Voice;
15 import javax.speech.synthesis.SynthesizerProperties;
16
17 import com.sun.speech.engine.BaseEngineProperties;
18
19 /**
20  * Supports the JSAPI 1.0 <code>SynthesizerProperties</code> 
21  * interface.  The properties of a <code>Synthesizer</code> are:
22  *
23  * <UL>
24  *   <LI>Speaking voice,
25  *   <LI>Baseline pitch,
26  *   <LI>Pitch range,
27  *   <LI>Speaking rate,
28  *   <LI>Volume.
29  * </UL>
30  */
31 public class BaseSynthesizerProperties extends BaseEngineProperties
32     implements SynthesizerProperties {
33
34     /**
35      * The default voice.
36      */
37     protected Voice defaultVoice;
38
39     /**
40      * The default pitch.
41      */
42     protected float defaultPitch;
43
44     /**
45      * The default pitch range.
46      */
47     protected float defaultPitchRange;
48
49     /**
50      * The default specking rate.
51      */
52     protected float defaultSpeakingRate;
53
54     /**
55      * The default volume.
56      */
57     protected float defaultVolume;
58     
59     /**
60      * The current voice.
61      */
62     protected Voice currentVoice;
63     
64     /**
65      * The current pitch.
66      */
67     protected float currentPitch;
68     
69     /**
70      * The current pitch range.
71      */
72     protected float currentPitchRange;
73     
74     /**
75      * The current speaking rate.
76      */
77     protected float currentSpeakingRate;
78     
79     /**
80      * The current volume.
81      */
82     protected float currentVolume;
83
84     /**
85      * Class constructor.
86      */
87     public BaseSynthesizerProperties() {
88         this.defaultVoice = null;
89         this.defaultPitch = 0.0f;
90         this.defaultPitchRange = 0.0f;
91         this.defaultSpeakingRate = 0.0f;
92         this.defaultVolume = 0.0f;
93     }
94     
95     /**
96      * Creates a new <code>BaseSynthesizerProperties</code> with the
97      * given default values.
98      *
99      * @param defaultVoice the default voice
100      * @param defaultPitch the default pitch
101      * @param defaultPitchRange the default pitch range
102      * @param defaultSpeakingRate the default speaking rate
103      * @param defaultVolume the default volume
104      */
105     public BaseSynthesizerProperties(Voice defaultVoice,
106                                      float defaultPitch,
107                                      float defaultPitchRange,
108                                      float defaultSpeakingRate,
109                                      float defaultVolume) {
110         if (defaultVoice != null) {
111             this.defaultVoice = (Voice)(defaultVoice.clone());
112         } else {
113             this.defaultVoice = null;
114         }
115         
116         this.defaultPitch = defaultPitch;
117         this.defaultPitchRange = defaultPitchRange;
118         this.defaultSpeakingRate = defaultSpeakingRate;
119         this.defaultVolume = defaultVolume;
120
121         if (defaultVoice != null) {
122             currentVoice = (Voice)(defaultVoice.clone());
123         } else {
124             currentVoice = null;
125         }
126     
127         currentPitch = defaultPitch;
128         currentPitchRange = defaultPitchRange;
129         currentSpeakingRate = defaultSpeakingRate;
130         currentVolume = defaultVolume;
131     }
132
133     /**
134      * Resets all properties to their default values.
135      */
136     public void reset() {
137         try {
138             setVoice(defaultVoice);
139             setVolume(defaultVolume);
140             setPitch(defaultPitch);
141             setPitchRange(defaultPitchRange);
142             setSpeakingRate(defaultSpeakingRate);
143         } catch (PropertyVetoException e) {
144             throw new SpeechError("Inconsistent default properties");
145         }
146     }
147
148     /**
149      * Gets the current synthesizer voice.
150      *
151      * @return the current synthesizer voice.
152      *
153      * @see #setVoice
154      */
155     public Voice getVoice() {
156         return (Voice)(currentVoice.clone());
157     }
158
159     /**
160      * Sets the current synthesizer voice.
161      *
162      * @param voice the new voice
163      *
164      * @see #getVoice
165      *
166      * @throws PropertyVetoException if the voice cannot be set to
167      *   the given value
168      */
169     public void setVoice(Voice voice)
170         throws PropertyVetoException {
171         // [[[TODO: Need to check that the voice is legal.]]]
172         Voice oldVoice = currentVoice;
173         currentVoice = (Voice)(voice.clone());
174         postPropertyChangeEvent("Voice", oldVoice, voice);
175     }
176
177     /**
178      * Gets the baseline pitch for synthesis.
179      *
180      * @return the baseline pitch in Hertz
181      *
182      * @see #setPitch
183      */
184     public float getPitch() {
185         return currentPitch;
186     }
187
188     /**
189      * Sets the baseline pitch for the current synthesis voice.
190      *
191      * @param hertz the new baseline pitch in Hertz
192      *
193      * @see #getPitch
194      *
195      * @throws PropertyVetoException if the baseline pitch cannot be
196      *   set to the given value       
197      */
198     public void setPitch(float hertz)
199         throws PropertyVetoException {
200         float oldPitch = currentPitch;
201         currentPitch = hertz;
202         postPropertyChangeEvent("Pitch", oldPitch, hertz);
203     }
204
205     /**
206      * Gets the pitch range for synthesis.
207      *
208      * @return the current pitch range in Hertz
209      *
210      * @see #setPitchRange
211      *
212      */
213     public float getPitchRange() {
214         return currentPitchRange;
215     }
216
217     /**
218      * Sets the pitch range for the current synthesis voice.
219      *
220      * @param hertz the new range in Hertz
221      *
222      * @see #getPitchRange
223      *
224      * @throws PropertyVetoException if the pitch range cannot be set
225      *   to the given value
226      */
227     public void setPitchRange(float hertz)
228         throws PropertyVetoException {
229         float oldRange = currentPitchRange;
230         currentPitchRange = hertz;
231         postPropertyChangeEvent("PitchRange", oldRange, hertz);
232     }
233
234     /**
235      * Gets the current target speaking rate in words per minute.
236      *
237      * @return the current target speaking rate in words per minute.
238      *
239      * @see #getSpeakingRate
240      */
241     public float getSpeakingRate() {
242         return currentSpeakingRate;
243     }
244
245     /**
246      * Sets the target speaking rate in words per minute.
247      *
248      * @param wpm the new speaking rate in words per minute
249      *
250      * @throws PropertyVetoException if the speaking rate cannot be
251      *   set to the given value
252      *
253      * @see #getSpeakingRate
254      */
255     public void setSpeakingRate(float wpm)
256         throws PropertyVetoException {
257         float oldRate = currentSpeakingRate;
258         currentSpeakingRate = wpm;
259
260         postPropertyChangeEvent("SpeakingRate", oldRate, wpm);
261     }
262
263     /**
264      * Gets the current volume.
265      *
266      * @return the current volume expressed as a <code>float</code>
267      *   0.0 and 1.0, inclusive
268      *
269      * @see #setVolume
270      */
271     public float getVolume() {
272         return currentVolume;
273     }
274
275     /**
276      * Sets the volume.
277      *
278      * @param volume the new volume expressed as a <code>float</code>
279      *   0.0 and 1.0, inclusive
280      *
281      * @see #getVolume
282      *
283      * @throws PropertyVetoException if the volume cannot be
284      *   set to the given value
285      */
286     public void setVolume(float volume)
287         throws PropertyVetoException {
288         if (volume > 1.0f)
289             volume = 1.0f;
290         else if (volume < 0.0f)
291             volume = 0.0f;
292     
293         float oldVolume = currentVolume;
294         currentVolume = volume;
295
296         postPropertyChangeEvent("Volume", oldVolume, volume);
297     }
298 }