freettssynthesizer.java
来自「FreeTTS is a speech synthesis system wri」· Java 代码 · 共 727 行 · 第 1/2 页
JAVA
727 行
if (voices[i].match(voice)) { try { if (setCurrentVoice((FreeTTSVoice) voices[i])) { try { super.setVoice(voice); break; } catch (PropertyVetoException pve) { continue; } } } catch (EngineException ee) { System.err.println("Engine Exception: " + ee.getMessage()); } } } } } /** * Set the baseline pitch for the current synthesis voice. * * @param hertz sets the current pitch * * @throws PropertyVetoException if the synthesizer rejects or * limits the new value */ public void setPitch(float hertz) throws PropertyVetoException { if (hertz != getPitch()) { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); voice.setPitch(hertz); super.setPitch(hertz); } } /** * Get the pitch range for synthesis. * * @return the current range of pitch in hertz */ public float getPitchRange() { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); return voice.getPitchRange(); } /** * Set the pitch range for the current synthesis voice. * * @throws PropertyVetoException if the synthesizer rejects or * limits the new value */ public void setPitchRange(float hertz) throws PropertyVetoException { if (hertz != getPitchRange()) { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); voice.setPitchRange(hertz); super.setPitchRange(hertz); } } /** * Gets the current target speaking rate. * * @return the current speaking rate in words per minute */ public float getSpeakingRate() { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); return voice.getRate(); } /** * Set the target speaking rate. * * @param wpm sets the target speaking rate in * words per minute * * @throws PropertyVetoException if the synthesizer rejects or * limits the new value */ public void setSpeakingRate(float wpm) throws PropertyVetoException { if (wpm != getSpeakingRate()) { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); voice.setRate(wpm); super.setSpeakingRate(wpm); } } /** * Gets the current volume. * * @return the current volume setting (between 0 and 1.0) */ public float getVolume() { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); return voice.getVolume(); } /** * Sets the volume * * @param volume the new volume setting (between 0 and 1) * * @throws PropertyVetoException if the synthesizer rejects or * limits the new value */ public void setVolume(float volume) throws PropertyVetoException { if (volume > 1.0f) volume = 1.0f; else if (volume < 0.0f) volume = 0.0f; if (volume != getVolume()) { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); voice.setVolume(volume); super.setVolume(volume); } } } /** * The OutputHandler is responsible for taking items off of the * input queue and sending them to the current voice. */ class OutputHandler extends Thread { protected boolean done = false; /** * Internal speech output queue that will contain a set of * FreeTTSSynthesizerQueueItems. * * @see BaseSynthesizerQueueItem */ protected Vector queue; /** * Create a new OutputHandler for the given Synthesizer. */ public OutputHandler() { queue = new Vector(); } /** * shuts down this output handler */ public synchronized void terminate() { synchronized (queue) { done = true; queue.notify(); } } /** * Returns an enumeration of the queue * * @return the enumeration queue */ public Enumeration enumerateQueue() { synchronized(queue) { return queue.elements(); } } /** * Determines if the input queue is empty * * @return true if the queue is empty; otherwise false */ public boolean isQueueEmpty() { synchronized(queue) { return queue.size() == 0; } } /** * Add an item to be spoken to the output queue. Fires the * appropriate queue events * * @param item the item to add to the queue */ public void appendQueue(FreeTTSSynthesizerQueueItem item) { boolean topOfQueueChanged; synchronized(queue) { topOfQueueChanged = (queue.size() == 0); queue.addElement(item); queue.notifyAll(); } if (topOfQueueChanged) { long[] states = setEngineState(QUEUE_EMPTY, QUEUE_NOT_EMPTY); postQueueUpdated(topOfQueueChanged, states[0], states[1]); } } /** * Cancel the current item */ protected void cancelItem() { FreeTTSSynthesizerQueueItem item = null; synchronized(queue) { audio.cancel(); if (queue.size() != 0) { item = (FreeTTSSynthesizerQueueItem) queue.remove(0); if (item != null) { // item.postSpeakableCancelled(); item.cancelled(); queueDrained(); } } } } /** * Cancel all items in the queue */ protected void cancelAllItems() { FreeTTSSynthesizerQueueItem item = null; Vector copy; synchronized(queue) { audio.cancel(); copy = (Vector) queue.clone(); queue.clear(); queueDrained(); } for (Iterator i = copy.iterator(); i.hasNext(); ) { item = (FreeTTSSynthesizerQueueItem) i.next(); // item.postSpeakableCancelled(); item.cancelled(); } } /** * Cancel the given item. * * @param source the item to cancel. */ protected void cancelItem(Object source) { FreeTTSSynthesizerQueueItem item = null; synchronized(queue) { int index = queue.indexOf(source); if (index == 0) { cancelItem(); } else { item = (FreeTTSSynthesizerQueueItem) queue.remove(index); if (item != null) { // item.postSpeakableCancelled(); item.cancelled(); queueDrained(); } } } } /** * Gets the next item from the queue and outputs it */ public void run() { FreeTTSSynthesizerQueueItem item; int currentCommand; boolean queueEmptied; while (!done) { item = getQueueItem(); if (item != null) { outputItem(item); removeQueueItem(item); } } } /** * Return, but do not remove, the first item on the queue. * * @return a queue item */ protected FreeTTSSynthesizerQueueItem getQueueItem() { FreeTTSSynthesizerQueueItem item = null; synchronized(queue) { while (queue.size() == 0 && !done) { try { queue.wait(); } catch (InterruptedException e) { error("Unexpected interrupt"); // Ignore interrupts and we'll loop around } } if (done) { return null; } item = (FreeTTSSynthesizerQueueItem) queue.elementAt(0); } item.postTopOfQueue(); return item; } /** * removes the given item, posting the appropriate * events. The item may have already been removed (due to a * cancel). * * @param item the item to remove */ protected void removeQueueItem(FreeTTSSynthesizerQueueItem item) { boolean queueEmptied = false; synchronized(queue) { boolean found = queue.remove(item); if (found) { queueDrained(); } } } /** * Should be called iff one or more items have been removed * from the queue. Generates the appropriate state changes and * events. */ private void queueDrained() { if (queue.size() == 0) { long[] states = setEngineState(QUEUE_NOT_EMPTY, QUEUE_EMPTY); postQueueEmptied(states[0], states[1]); } else { long[] states = setEngineState(QUEUE_NOT_EMPTY, QUEUE_NOT_EMPTY); postQueueUpdated(true, states[0], states[1]); } } /** * Outputs the given queue item to the current voice * * @param item the item to output */ protected void outputItem(FreeTTSSynthesizerQueueItem item) { com.sun.speech.freetts.Voice voice = curVoice.getVoice(); voice.speak(item); } /** * Output an error message * * @param s the message to output */ private void error(String s) { System.out.println("Error: " + s); } /** * Output a trace message * * @param s the message to output */ private void trace(String s) { System.out.println("Trace: " + s); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?