⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animator.java

📁 使用Exlipse编写的一个语音程序
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Common
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Berthold Daum
 ******************************************************************************/

package com.sun.speech.freetts.relp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.swing.Timer;

import com.sun.speech.freetts.*;

/**
 * @author Berthold Daum
 *
 * Creation date: 01.10.2003
 * 
 */
public class Animator 
	implements UtteranceProcessor, ActionListener, LineListener {

		// List of AnimationListener instances
		List listeners = new ArrayList(3);
		// Swing Timer object
		Timer timer;
		// Current segment in the segment list
		Item segment;
		// Start time of current segment
		int currentTime = 0;
		/**
		 * Method addAnimationListener.
		 * @param l AnimationListener object
		 */
		public void addAnimationListener(AnimationListener l) {
			listeners.add(l);
		}
		/**
		 * Method removeAnimationListener.
		 * @param l AnimationListener object
		 */
		public void removeAnimationListener(AnimationListener l) {
			listeners.remove(l);
		}
		/**
		 * @see com.sun.speech.freetts.UtteranceProcessor#
		 * processUtterance(Utterance)
		 */
		public void processUtterance(Utterance utterance) 
			throws ProcessException {
				// Reset current time
				currentTime = 0;
				// Stop time if it is still running (previous utterance)
				if (timer != null && timer.isRunning())
					timer.stop();
				// Fetch first segment of utterance
				segment = utterance.getRelation(Relation.SEGMENT).getHead();
			}
			/**
			 * @see
			 * java.awt.event.ActionListener#actionPerformed(ActionEvent)
			 */
			// Is executed when the timer expires

			public void actionPerformed(ActionEvent e) {
				// Fire event
				fireAnimationEvent();
			}

			/**
			 * Method fireAnimationEvent.
			 */
			private void fireAnimationEvent() {
				// If segment == null we have reached the end of the list
				if (segment != null) {
					// Fetch end time from segment and convert to msec
					int end = 
						(int) (1000 * segment.getFeatures().getFloat("end"));
					// Get phoneme from segment
					String phone = segment.getFeatures().getString("name");
					// Advance in segment list
					segment = segment.getNext();
					// Create new AnimationEvent object
					AnimationEvent e = new AnimationEvent(end, phone);
					// Send it to all AnimationListener objects
					Iterator iter = listeners.iterator();
					while (iter.hasNext()) {
						AnimationListener listener = 
							(AnimationListener) iter.next();
						listener.processAnimationEvent(e);
					}
					// Create new timer that expires at the end time
					// of the current phoneme.
					timer = new Timer(end - currentTime, this);
					timer.setRepeats(false);
					timer.setCoalesce(false);
					timer.start();
					// Update current time
					currentTime = end;
				} 
			}
			/**
			 * @see javax.sound.sampled.LineListener#update(LineEvent)
			 */
			public void update(LineEvent event) {
				if (event.getType().equals(LineEvent.Type.START)) {
					// Audio output has started 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -