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

Java tutorial

Introduction

Here is the source code for de.elomagic.vaadin.addon.speechsynthesis.SpeechSynthesis.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 java.util.ArrayList;
import java.util.List;

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

import com.google.gson.Gson;
import com.vaadin.annotations.JavaScript;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.JavaScriptFunction;

import de.elomagic.vaadin.addon.speechsynthesis.SpeechSynthesisEvent.Type;

/**
 * SpeechSynthesis is a non visualization class for controlling a text-to-speech output.
 * <p/>
 * This class is experimental and runs only under latest version of Google Chrome browsers.
 *
 * @author Carsten Rambow
 */
@JavaScript({ "js/speechsynthesis-connector.js" })
public class SpeechSynthesis extends AbstractJavaScriptComponent {
    private final List<SpeechSynthesisListener> eventListener = new ArrayList<>();
    private final List<SpeechSynthesisVoice> voiceList = new ArrayList<>();

    public SpeechSynthesis() {
        super();

        addFunction("onVoiceList", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arg) throws JSONException {
                JSONArray a = arg.getJSONArray(0);
                voiceList.clear();
                Gson gson = new Gson();
                for (int i = 0; i < a.length(); i++) {
                    SpeechSynthesisVoice voice = gson.fromJson(a.getJSONObject(i).toString(),
                            SpeechSynthesisVoice.class);
                    voiceList.add(voice);
                }
            }
        });

        addFunction("onStart", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Start));
            }
        });
        addFunction("onEnd", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.End));
            }
        });
        addFunction("onPause", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Pause));
            }
        });
        addFunction("onMark", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Mark));
            }
        });
        addFunction("onBoundary", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Boundary));
            }
        });
        addFunction("onError", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Error));
            }
        });
        addFunction("onResume", new JavaScriptFunction() {

            @Override
            public void call(final JSONArray arguments) throws JSONException {
                fireEvent(mapToEvent(arguments, Type.Resume));
            }
        });

    }

    /**
     * Returns a list of available voices.
     * <p>
     * It is user agent dependent which voices are available.
     *
     * @return
     */
    public List<SpeechSynthesisVoice> getVoices() {
        return new ArrayList(voiceList);
    }

    public SpeechSynthesisUtterance getUtterance() {
        return getState().data;
    }

    /**
     * This method appends the SpeechSynthesisUtterance object to the end of the queue for the global SpeechSynthesis instance. It does not change the paused state of the SpeechSynthesis instance. If
     * the SpeechSynthesis instance is paused, it remains paused. If it is not paused and no other utterances are in the queue, then this utterance is spoken immediately, else this utterance is queued
     * to begin speaking after the other utterances in the queue have been spoken. If changes are made to the SpeechSynthesisUtterance object after calling this method and prior to the corresponding
     * end or error event, it is not defined whether those changes will affect what is spoken, and those changes may cause an error to be returned.
     *
     * @param text
     */
    public void speak(final String text) {
        getState().data.text = text;
    }

    /**
     * This method removes all utterances from the queue. If an utterance is being spoken, speaking ceases immediately. This method does not change the paused state of the global SpeechSynthesis
     * instance.
     */
    public void cancel() {
        callFunction("cancel");
    }

    /**
     * This method puts the global SpeechSynthesis instance into the paused state. If an utterance was being spoken, it pauses mid-utterance. (If called when the SpeechSynthesis instance was already
     * in the paused state, it does nothing.)
     */
    public void pause() {
        callFunction("pause");
    }

    /**
     * This method puts the global SpeechSynthesis instance into the non-paused state. If an utterance was speaking, it continues speaking the utterance at the point at which it was paused, else it
     * begins speaking the next utterance in the queue (if any). (If called when the SpeechSynthesis instance was already in the non-paused state, it does nothing.)
     */
    public void resume() {
        callFunction("resume");
    }

    @Override
    protected SpeechSynthesisState getState() {
        return (SpeechSynthesisState) super.getState();
    }

    private void fireEvent(final SpeechSynthesisEvent event) {
        System.out.println("Fire event error");
        for (SpeechSynthesisListener listener : eventListener) {
            listener.onSpeechSynthesisEvent(event);
        }
    }

    public void addSpeechSynthesisListener(final SpeechSynthesisListener listener) {
        if (eventListener.contains(listener)) {
            return;
        }

        eventListener.add(listener);
    }

    public void removeSpeechSynthesisListener(final SpeechSynthesisListener listener) {
        eventListener.remove(listener);
    }

    public SpeechSynthesisEvent mapToEvent(final JSONArray array, final Type type) {
        return new SpeechSynthesisEvent(this, type, array);
    }

}