📄 basesynthesizerqueueitem.java
字号:
/** * Copyright 1998-2001 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */package com.sun.speech.engine.synthesis;import java.util.Enumeration;import java.util.Vector;import java.net.URL;import java.io.IOException;import javax.speech.SpeechEvent;import javax.speech.synthesis.Speakable;import javax.speech.synthesis.SpeakableListener;import javax.speech.synthesis.SpeakableEvent;import javax.speech.synthesis.SynthesizerQueueItem;import javax.speech.synthesis.JSMLException;import org.w3c.dom.Document;import com.sun.speech.engine.SpeechEventUtilities;import com.sun.speech.engine.SpeechEventDispatcher;/** * Extends the JSAPI 1.0 <code>SynthesizerQueueItem</code> with handling * for JSML, generation of engine-specific text, and other features. */public class BaseSynthesizerQueueItem extends SynthesizerQueueItem implements SpeechEventDispatcher { private volatile boolean done = false; private volatile boolean cancelled = false; /** * The object containing the DOM of the parsed JSML. */ private Document document = null; /** * Global count of queue items used for debug. */ protected static int itemNumber = 0; /** * Count for this item used for debug. */ protected int thisItemNumber = 0; /** * <code>Synthesizer</code> that has queued this item. */ protected BaseSynthesizer synth; /** * Class constructor. */ public BaseSynthesizerQueueItem() { super(null, null, false, null); thisItemNumber = itemNumber++; } /** * Sets queue item data with a <code>Speakable</code> source. * * @param synth the synthesizer * @param source the <code>Speakable</code> * @param listener the <code>SpeakableListener</code> to be * notified as this object is processed * * @throws JSMLException if the <code>source</code> contains JSML errors */ protected void setData(BaseSynthesizer synth, Speakable source, SpeakableListener listener) throws JSMLException { this.synth = synth; this.source = source; this.text = source.getJSMLText(); this.plainText = false; this.listener = listener; document = new JSMLParser(this.text, false).getDocument(); } /** * Sets queue item data with a <code>String</code> source that is * either plain text or JSML. * * @param synth the synthesizer * @param source the text * @param plainText <code>true</code> only if the * <code>source</code> is plain text * @param listener the <code>SpeakableListener</code> to be * notified as this object is processed * * @throws JSMLException if the <code>source</code> contains JSML errors */ protected void setData(BaseSynthesizer synth, String source, boolean plainText, SpeakableListener listener) throws JSMLException { this.synth = synth; this.source = source; this.text = source; this.plainText = plainText; this.listener = listener; if (!plainText) { document = new JSMLParser(this.text, false).getDocument(); } } /** * Sets queue item data with a <code>URL</code> source. * * @param synth the synthesizer * @param source the <code>URL</code> containing JSML text * @param listener the <code>SpeakableListener</code> to be * notified as this object is processed * * @throws JSMLException if the <code>source</code> contains JSML errors * @throws IOException if there are problems working with the URL. */ protected void setData(BaseSynthesizer synth, URL source, SpeakableListener listener) throws JSMLException, IOException { this.synth = synth; this.source = source; this.text = null; this.plainText = false; this.listener = listener; document = new JSMLParser(source, false).getDocument(); } /** * Gets the DOM document for this object. * * @return the DOM document for this object. */ protected Document getDocument() { return document; } /** * determines if this queue item has been cancelled * * @return <code> true </code> if this item has been cancelled; * otherwise <code> false </code> */ protected synchronized boolean isCancelled() { return cancelled; } /** * returns true if this queue item has been * processed. * @return true if it has been processed */ public synchronized boolean isCompleted() { return done; } /** * wait for this queue item to be completed * * @return true if the item was completed successfully, false if * the item was cancelled or an error occurred. */ public synchronized boolean waitCompleted() { while (!isCompleted()) { try { wait(); } catch (InterruptedException ie) { System.err.println( "FreeTTSSynthesizerQueueItem.Wait interrupted"); return false; } } return !isCancelled(); } /** * indicate that this item has been cancelled */ public synchronized void cancelled() { postSpeakableCancelled(); notifyAll(); } /** * indicate that this item has been completed */ public synchronized void completed() { postSpeakableEnded(); notifyAll(); } /** * indicate that this item has been started */ public void started() { postSpeakableStarted(); } /** * Gets the item number for debug purposes only. Each queue item * is given a unique ID. * * @return the unique ID for this queue item */ public int getItemNumber() { return thisItemNumber; } /** * Utility function that generates a * <code>MARKER_REACHED</code> event and posts it * to the event queue. Eventually * <code>fireMarkerReached</code> will be called * by <code>dispatchSpeechEvent</code> as a result * of this action. * * @param text the text of the marker * @param markerType the type of marker * * @see SpeakableEvent#getMarkerType * @see #fireMarkerReached * @see #dispatchSpeechEvent */ public void postMarkerReached(String text, int markerType) { SpeechEventUtilities.postSpeechEvent( this, new SpeakableEvent(source, SpeakableEvent.MARKER_REACHED, text, markerType)); } /** * Utility function that sends a <code>MARKER_REACHED</code> event * to all speakable listeners. * * @param event the <code>MARKER_REACHED</code> event * * @see #postMarkerReached */ public void fireMarkerReached(SpeakableEvent event) { if (listener != null) { listener.markerReached(event); } Enumeration E; if (synth.speakableListeners != null) { E = synth.speakableListeners.elements(); while (E.hasMoreElements()) { SpeakableListener sl = (SpeakableListener) E.nextElement(); sl.markerReached(event); } } } /** * Utility function that generates a * <code>SPEAKABLE_CANCELLED</code> event and posts it * to the event queue. Eventually * <code>fireSpeakableCancelled</code> will be called * by <code>dispatchSpeechEvent</code> as a result * of this action. * * @see #fireSpeakableCancelled * @see #dispatchSpeechEvent */ public void postSpeakableCancelled() { boolean shouldPost; // The JSAPI docs say that once a cancelled event is sent, no // others will be. This makes sure that a cancelled will never be // sent twice. This deals with the race that can occur when an // item that is playing is cancelled. synchronized(this) { shouldPost = !done; done = true; cancelled = true; } if (shouldPost) { SpeechEventUtilities.postSpeechEvent( this, new SpeakableEvent(source, SpeakableEvent.SPEAKABLE_CANCELLED)); } } /** * Utility function that sends a <code>SPEAKABLE_CANCELLED</code> event * to all speakable listeners. * * @param event the <code>SPEAKABLE_CANCELLED</code> event * * @see #postSpeakableCancelled */ public void fireSpeakableCancelled(SpeakableEvent event) { if (listener != null) { listener.speakableCancelled(event); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -