001package info.kramann.vr;
002
003import processing.core.*;
004
005import android.speech.tts.TextToSpeech;
006import android.app.Activity;
007import java.util.Locale;
008import java.util.Set;
009import android.media.AudioManager;
010import android.content.Context;
011
012import java.util.ArrayList;
013
014/**
015*  <h4>TTS.java</h4>
016*
017*  Copyright (C) 2017 Guido Kramann<br/>
018*  kramann@fh-brandenburg.de<br/>
019*  http://www.kramann.info<br/>
020*  <br/>
021*
022*  Vereinfachte Benutzung des Sprachsynthesizers eines Smartphones.<br/>
023*  Konstruktoraufruf (Beispiel):
024*    tts = new TTS(this,this.getActivity());
025*
026*
027*/
028public class TTS implements Runnable
029{
030     PApplet pap;
031     Activity activity;
032     ArrayList<String> warteschlange = new ArrayList<String>();
033  
034     TextToSpeech tts;
035     AudioManager audio;
036     int currentVolume;
037     int maxVolume;
038
039     public void setzeSpechgeschwindigkeit(float s)
040     {
041         tts.setSpeechRate(s);    
042     }
043
044     public void setzeStimmhoehe(float h)
045     {
046         tts.setSpeechRate(h);    
047     }
048
049     public void setzeLautstaerke(float ls)
050     {
051         if(ls<0.0f)
052             ls=0.0f;
053         if(ls>1.0f)
054             ls=1.0f;
055         int LAUT  = (int) (maxVolume*ls);
056             
057         audio.setStreamVolume(AudioManager.STREAM_MUSIC, LAUT, 0);             
058     }
059
060     public void sprich(String text)
061     {
062       
063        warteschlange.add(text);
064       
065     }
066
067     public void run()
068     {
069         while(true)
070         {
071               if(tts!=null && warteschlange.size()>0 && !tts.isSpeaking() )
072               {
073                   String text = warteschlange.get(0);
074                   warteschlange.remove(0);
075                   tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
076                   //println("tts: \""+text+"\"");
077               }
078               
079               try
080               {
081                    Thread.sleep(100);
082               }
083               catch(Exception e)
084               {
085               }
086         }
087     }
088
089     public TTS(PApplet pap, Activity activity)
090     {
091         this.pap = pap;
092         this.activity = activity;
093         //NEU:  getActivity(). ab Proc. 3!
094         tts=new TextToSpeech(activity.getApplicationContext(), new TextToSpeech.OnInitListener() {
095//         tts=new TextToSpeech(pap.getApplicationContext(), new TextToSpeech.OnInitListener() {
096         //@Override
097            public void onInit(int status) {
098               if(status != TextToSpeech.ERROR) {
099                  tts.setLanguage(Locale.GERMAN);
100               }
101            }
102         });
103
104         audio = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);  //NEU:  getActivity(). ab Proc. 3!
105//         audio = (AudioManager) pap.getSystemService(Context.AUDIO_SERVICE);  //NEU:  getActivity(). ab Proc. 3!
106         currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
107         maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
108         
109         (new Thread(this)).start();
110     }
111
112     
113}