de.elomagic.vaadin.addon.speechsynthesis.SpeechSynthesisEvent.java Source code

Java tutorial

Introduction

Here is the source code for de.elomagic.vaadin.addon.speechsynthesis.SpeechSynthesisEvent.java

Source

/*
 * Copyright 2014 Carsten Rambow.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.elomagic.vaadin.addon.speechsynthesis;

import org.json.JSONArray;
import org.json.JSONException;

import com.vaadin.ui.Component;

/**
 *
 */
public class SpeechSynthesisEvent extends Component.Event {

    private final JSONArray jsonArray;
    private final Type type;

    public enum Type {
        Boundary, End, Error, Mark, Pause, Resume, Start
    }

    public SpeechSynthesisEvent(final Component source, final Type type, final JSONArray jsonArray) {
        super(source);
        this.type = type;
        this.jsonArray = jsonArray;
    }

    public Type getType() {
        return type;
    }

    /**
     * This attribute indicates the zero-based character index into the original utterance string that most closely approximates the current speaking position of the speech engine. No guarantee is
     * given as to where charIndex will be with respect to word boundaries (such as at the end of the previous word or the beginning of the next word), only that all text before charIndex has already
     * been spoken, and all text after charIndex has not yet been spoken. The user agent must return this value if the speech synthesis engine supports it, otherwise the user agent must return
     * undefined.
     *
     * @return
     * @throws org.json.JSONException
     */
    public int getCharIndex() throws JSONException {
        return jsonArray.getJSONObject(0).getInt("charIndex");
    }

    /**
     * This attribute indicates the time, in seconds, that this event triggered, relative to when this utterance has begun to be spoken. The user agent must return this value if the speech synthesis
     * engine supports it or the user agent can otherwise determine it, otherwise the user agent must return undefined.
     *
     * @return
     * @throws org.json.JSONException
     */
    public long getElapsedTime() throws JSONException {
        return jsonArray.getJSONObject(0).getLong("elapsedTime");
    }

    /**
     * For mark events, this attribute indicates the name of the marker, as defined in SSML as the name attribute of a mark element. [SSML] For boundary events, this attribute indicates the type of
     * boundary that caused the event: "word" or "sentence". For all other events, this value should return undefined.
     *
     * @return
     * @throws org.json.JSONException
     */
    public String getName() throws JSONException {
        return jsonArray.getJSONObject(0).getString("name");
    }

    @Override
    public String toString() {
        String s = "SpeechSynthesisEvent(";
        s += "\n\tSource=" + getSource().toString();
        s += ";\n\tType=" + getType().toString();
        s += ";\n\tEvent=" + jsonArray.toString();
        s += ";\n)";
        return s;
    }

}