BowlerKernel
TextToSpeech.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio;
2 
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8 import java.util.stream.Collectors;
9 import java.util.stream.StreamSupport;
10 
11 import javax.sound.sampled.AudioInputStream;
12 
13 import marytts.LocalMaryInterface;
14 import marytts.MaryInterface;
15 import marytts.exceptions.MaryConfigurationException;
16 import marytts.exceptions.SynthesisException;
17 import marytts.modules.synthesis.Voice;
18 import marytts.signalproc.effects.AudioEffect;
19 import marytts.signalproc.effects.AudioEffects;
20 
25 public class TextToSpeech implements ITTSEngine{
26 
27  private AudioPlayer tts;
28  private MaryInterface marytts;
29 
33  public TextToSpeech() {
34  try {
35  marytts = new LocalMaryInterface();
36 
37  } catch (MaryConfigurationException ex) {
38  Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
39  }
40  }
41 
42  //----------------------GENERAL METHODS---------------------------------------------------//
43 
59  public int speak(String text , float gainValue , boolean daemon , boolean join, ISpeakingProgress progress) {
60  // Stop the previous player
61  stopSpeaking();
62 
63 
64  try (AudioInputStream audio = marytts.generateAudio(text)) {
65 
66  // Player is a thread(threads can only run one time) so it can be
67  // used has to be initiated every time
68  tts = new AudioPlayer(text);
69  tts.setAudio(audio);
70  tts.setGain(gainValue);
71  tts.setDaemon(daemon);
72  if(progress!=null)
73  tts.setSpeakProgress(progress);
74  tts.start();
75  if (join)
76  tts.join();
77  return 0;
78  } catch (SynthesisException ex) {
79  Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error saying phrase.", ex);
80  } catch (IOException ex) {
81  Logger.getLogger(getClass().getName()).log(Level.WARNING, "IO Exception", ex);
82  } catch (InterruptedException ex) {
83  Logger.getLogger(getClass().getName()).log(Level.WARNING, "Interrupted ", ex);
84  System.out.println("Speaking clean exit");
85  tts.cancel();
86  tts.interrupt();
87  return 1;
88  }
89  return 2;
90  }
91 
95  public void stopSpeaking() {
96  // Stop the previous player
97  if (tts != null)
98  tts.cancel();
99  }
100 
101  //----------------------GETTERS---------------------------------------------------//
102 
108  public Collection<Voice> getAvailableVoices() {
109  return Voice.getAvailableVoices();
110  }
111 
115  public MaryInterface getMarytts() {
116  return marytts;
117  }
118 
124  public List<AudioEffect> getAudioEffects() {
125  return StreamSupport.stream(AudioEffects.getEffects().spliterator(), false).collect(Collectors.toList());
126  }
127 
128  //----------------------SETTERS---------------------------------------------------//
129 
135  public void setVoice(String voice) {
136  marytts.setVoice(voice);
137  }
138 
139 }
void setAudio(AudioInputStream audio)
void setSpeakProgress(ISpeakingProgress speakProgress)
int speak(String text, float gainValue, boolean daemon, boolean join, ISpeakingProgress progress)