/*
 * Copyright (c) 2008-2018 The Aspectran Project
 *
 * 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 com.aspectran.demo.speak;

import com.aspectran.core.component.bean.ablility.DisposableBean;
import com.aspectran.core.component.bean.ablility.InitializableBean;
import com.aspectran.core.util.ToStringBuilder;
import com.aspectran.core.util.logging.Log;
import com.aspectran.core.util.logging.LogFactory;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

import java.util.Properties;
import java.util.Set;

/**
 * <p>Created: 2018. 8. 29.</p>
 */
public class TextToSpeechBean implements InitializableBean, DisposableBean {

    private static final Log log = LogFactory.getLog(TextToSpeechBean.class);

    private String voiceName;

    private Voice voice;

    public void setProperties(Properties properties) {
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            System.setProperty(key, properties.getProperty(key));
        }
    }

    public void setVoiceName(String voiceName) {
        this.voiceName = voiceName;
    }

    @Override
    public void initialize() {
        if (log.isDebugEnabled()) {
            VoiceManager voiceManager = VoiceManager.getInstance();
            Voice[] voices = voiceManager.getVoices();
            String[] arr = new String[voices.length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = voices[i].getName() + "(" + voices[i].getDomain() + " domain)";
            }
            ToStringBuilder tsb = new ToStringBuilder("All voices available");
            tsb.append("voices", arr);
            log.debug(tsb.toString());
        }

        VoiceManager voiceManager = VoiceManager.getInstance();
        if (voiceName != null) {
            voice = voiceManager.getVoice(voiceName);
        } else {
            Voice[] voices = voiceManager.getVoices();
            if (voices != null && voices.length > 0) {
                voice = voices[0];
            }
        }
        voice = voiceManager.getVoice(voiceName);
        if (voice != null) {
            voice.allocate();
        }
    }

    @Override
    public void destroy() {
        if (voice != null) {
            voice.deallocate();
        }
    }

    public void speak(String text) {
        if (voice == null) {
            throw new IllegalStateException("Cannot find a voice named " + voiceName);
        }
        voice.speak(text);
    }

    public static void listAllVoices() {
        System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
        System.out.println();
        System.out.println("All voices available:");
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice[] voices = voiceManager.getVoices();
        for (Voice voice1 : voices) {
            System.out.println("    " + voice1.getName() + " (" + voice1.getDomain() + " domain)");
        }
    }

    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");

        TextToSpeechBean textToSpeechBean = new TextToSpeechBean();
        textToSpeechBean.setProperties(props);
        textToSpeechBean.setVoiceName("kevin16");
        textToSpeechBean.initialize();
        textToSpeechBean.speak("test");
        textToSpeechBean.destroy();
    }

}

@@ -1247,24 +1247,58 @@
 an.class);%0A%0A
+    private String voicePackage;%0A%0A
     private 
@@ -1603,120 +1603,423 @@
 oice
-Name(String voiceName) %7B%0A        this.voiceName = voiceName;%0A    %7D%0A%0A    @Override%0A    public void initialize() %7B
+Package(String voicePackage) %7B%0A        this.voicePackage = voicePackage;%0A        System.setProperty(%22freetts.voices%22, voicePackage);%0A    %7D%0A%0A    public void setVoiceName(String voiceName) %7B%0A        this.voiceName = voiceName;%0A    %7D%0A%0A    @Override%0A    public void initialize() %7B%0A        if (voicePackage == null) %7B%0A            setVoicePackage(%22com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory%22);%0A        %7D%0A
 %0A   
@@ -3357,686 +3357,29 @@
 oid 
-listAllVoices() %7B%0A        System.setProperty(%22freetts.voices%22, %22com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory%22);%0A        System.out.println();%0A        System.out.println(%22All voices available:%22);%0A        VoiceManager voiceManager = VoiceManager.getInstance();%0A        Voice%5B%5D voices = voiceManager.getVoices();%0A        for (Voice voice1 : voices) %7B%0A            System.out.println(%22    %22 + voice1.getName() + %22 (%22 + voice1.getDomain() + %22 domain)%22);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Properties props = new Properties();%0A        props.setProperty(%22freetts.voices%22, %22com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory%22);%0A
+main(String%5B%5D args) %7B
 %0A   
@@ -3447,55 +3447,8 @@
 ();%0A
-        textToSpeechBean.setProperties(props);%0A
     

Update demo