upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / relp / AudioOutput.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.relp;
12
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16 import javax.sound.sampled.AudioFormat;
17
18 import com.sun.speech.freetts.ProcessException;
19 import com.sun.speech.freetts.Utterance;
20 import com.sun.speech.freetts.UtteranceProcessor;
21 import com.sun.speech.freetts.audio.AudioPlayer;
22
23 /**
24  * Supports generating audio output from an utterance. This is an
25  * utterance processor. The primary method, <code> procesUtterance </code> 
26  * takes an utterance and hands it off to the LPCResult to be sent to the
27  * proper audio player.
28  *
29  * @see LPCResult
30  */
31 public class AudioOutput implements UtteranceProcessor {
32     /** Logger instance. */
33     private static final Logger LOGGER =
34         Logger.getLogger(AudioOutput.class.getName());
35
36     private final static AudioFormat AUDIO_8KHZ =
37         new AudioFormat(8000.0f, 16, 1, true, true);
38     private final static AudioFormat AUDIO_16KHZ =
39         new AudioFormat(16000.0f, 16, 1, true, true);
40     
41     /**
42      * Generates audio waves for the given Utterance. The audio data
43      * is decoded using the Linear Predictive Decoder
44      *
45      * @param  utterance  the utterance to generate waves
46      *
47      * @see LPCResult
48      *
49      * @throws ProcessException if an IOException is thrown during the
50      *         processing of the utterance
51      */
52     public void processUtterance(Utterance utterance) throws ProcessException {
53         LPCResult lpcResult = (LPCResult) utterance.getObject("target_lpcres");
54         SampleInfo sampleInfo = 
55             (SampleInfo) utterance.getObject(SampleInfo.UTT_NAME);
56         AudioPlayer audioPlayer = utterance.getVoice().getAudioPlayer();
57
58         audioPlayer.setAudioFormat(getAudioFormat(sampleInfo));
59         audioPlayer.setVolume(utterance.getVoice().getVolume());
60
61         if (LOGGER.isLoggable(Level.FINE)) {
62             LOGGER.fine("=== " +
63                 utterance.getString("input_text"));
64         }
65         if (!lpcResult.playWave(audioPlayer, utterance)) {
66             throw new ProcessException("Output Cancelled");
67         }
68     }
69
70
71     /**
72      * Gets the current audio format.  
73      * Given a sample info return an appropriate audio format. A cache
74      * of common audio formats is used to reduce unnecessary object
75      * creation. Note that this method always returns an AudioFormat
76      * that uses 16-bit samples.
77      *
78      * @param sampleInfo the sample info
79      *
80      * @return an audio format
81      */
82     private AudioFormat getAudioFormat(SampleInfo sampleInfo) {
83         if (sampleInfo.getSampleRate() == 8000) {
84             return AUDIO_8KHZ;
85         } else if (sampleInfo.getSampleRate() == 16000) {
86             return AUDIO_16KHZ;
87         } else {
88             return new AudioFormat(sampleInfo.getSampleRate(),
89                     16, 1, true, true);
90         }
91     }
92     
93     /**
94      * 
95      * Returns the string form of this object
96      * 
97      * @return the string form of this object
98      */
99     public String toString() {
100         return "AudioOutput";
101     }
102 }
103
104
105
106