?? animator.java
字號(hào):
/*******************************************************************************
* 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 ?start animation, too.
// Fire first event
fireAnimationEvent();
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
return "Animator";
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -