12 Replies Latest reply: Oct 6, 2006 1:38 AM by 843802 RSS

    I need a Text to Speech conversion program

    843802
      Hi everyone,

      I need a program for conversion of text to speech(TTS) in java. If you have this program or website address. please send me as much fast as possible
        • 1. Re: I need a Text to Speech conversion program
          843802
          I dont know one, but have you tried googling for it in the mean time?
          • 2. Re: I need a Text to Speech conversion program
            843802
            I searched in google, i got a java program for TTS with DLL file connection. Actually i need a pure java program for Text to Speech (TTS) conversion. Please give any more information about TTS program.

            Message was edited by:
            manikandan.s
            • 3. Re: I need a Text to Speech conversion program
              843802
              http://freetts.sourceforge.net/

              regards
              • 4. Re: I need a Text to Speech conversion program
                843802
                try this coding after applying the the classpath's to jsapi.jar, freetts.jar


                import javax.speech.*;
                import javax.speech.synthesis.*;
                import javax.speech.recognition.*;


                import java.util.*;

                public class Speech
                {
                     Speech(String v)
                     {     
                     try{
                     
                          SynthesizerModeDesc desc=new SynthesizerModeDesc(null,"general",Locale.US,null,null);
                          Synthesizer synth = Central.createSynthesizer(desc);
                          synth.allocate();
                          //synth.resume();
                          synth.speakPlainText(v,null);
                          synth.waitEngineState(Synthesizer.QUEUE_EMPTY);

                          //synth.deallocate();
                System.exit(0);

                     }catch(Exception ex){     
                          System.out.println(ex);
                          }
                     }
                public static void main(String[] ar)
                {     
                     int le=ar.length;
                     String str="";
                     for(int i=0;i<le;i++){
                     str=str+ar[i]+" ";
                     }
                     new Speech(str);
                }
                }

                c:\> javac Speech.java
                c:\> java Speech Hello Manikandan
                Regards
                Sulfikkar
                • 5. Re: I need a Text to Speech conversion program
                  843802
                  Okay, I wrote this a looong time ago with intentions to only use as a joke (sorry if it is not well structured)...

                  I turn the speakers up very loud in my cubicle and make my computer call out to other people in the office... "Help Help, someone help me... " or "Joe... why do you abuse me so?" you get the idea... hehe.. anyway, two Classes: Gui and SpeechModule. Enjoy, I have. : )

                  GUI
                  import javax.swing.JFrame;
                  import javax.swing.JButton;
                  import java.awt.BorderLayout;
                  import oracle.jdeveloper.layout.XYLayout;
                  import oracle.jdeveloper.layout.XYConstraints;
                  import java.awt.Insets;
                  import javax.swing.JTextField;
                  import javax.swing.JTextArea;
                  import javax.swing.JScrollPane;
                  import java.awt.event.ActionListener;
                  import java.awt.event.ActionEvent;
                  import java.awt.event.WindowListener;
                  import java.awt.event.WindowEvent;
                  
                  public class Gui extends JFrame  {
                  
                       protected SpeechModule speechModule = new SpeechModule();
                  
                  
                    JButton cmdSpeak = new JButton();
                    XYLayout xYLayout1 = new XYLayout();
                    JScrollPane jScrollPane1 = new JScrollPane();
                    JTextArea txtWords = new JTextArea();
                    JTextField txtPitch = new JTextField();
                    JTextField txtShift = new JTextField();
                  
                    public Gui() {
                      super("Java Speech");  
                      try {
                        jbInit();
                      } catch(Exception e) {
                        e.printStackTrace();
                      }
                    }
                    
                    public static void main(String[] args) {
                      Gui gui = new Gui();
                      gui.show();
                    }  
                    
                    private void jbInit() throws Exception {
                      this.getContentPane().setLayout(xYLayout1);
                      this.addWindowListener(new java.awt.event.WindowAdapter()
                        {
                          public void windowClosing(WindowEvent e)
                          {
                            this_windowClosing(e);
                          }
                        });
                      cmdSpeak.setText("Speak");
                      cmdSpeak.setActionCommand("cmdSpeak");
                      cmdSpeak.setMargin(new Insets(0, 0, 0, 0));
                      cmdSpeak.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) {
                            cmdSpeak_actionPerformed(e);
                          }
                        });
                      xYLayout1.setWidth(454);
                      xYLayout1.setHeight(327);
                      txtWords.setText("Hello. Hello.  Testing  one  two  three.");
                      txtWords.setSelectionEnd(1);
                      txtPitch.setText("10");
                      txtShift.setText("100");
                      jScrollPane1.getViewport().add(txtWords, null);
                      this.getContentPane().add(txtShift, new XYConstraints(40, 260, 55, 25));
                      this.getContentPane().add(txtPitch, new XYConstraints(40, 220, 55, 25));
                      this.getContentPane().add(jScrollPane1, new XYConstraints(50, 40, 360, 150));
                      this.getContentPane().add(cmdSpeak, new XYConstraints(160, 230, 75, 25));
                      this.setSize(600, 400);
                    }
                  
                    void cmdSpeak_actionPerformed(ActionEvent e) {
                  
                      System.out.println("pitch" + Float.valueOf(txtPitch.getText()).floatValue());
                      System.out.println("pitchShift" + Float.valueOf(txtShift.getText()).floatValue());
                      speechModule.setPitch( Float.valueOf(txtPitch.getText()).floatValue() );
                      speechModule.setPitchShift( Float.valueOf(txtShift.getText()).floatValue() );
                      speechModule.speak(txtWords.getText());
                    }
                  
                    private void this_windowClosing(WindowEvent e)  {
                      System.exit(0);
                    }
                  }
                  SpeechModule

                  import com.sun.speech.freetts.Voice;
                  import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
                  import com.sun.speech.freetts.en.us.CMULexicon;
                  
                  
                  public class SpeechModule {
                       
                       public static Class voiceClass = null;
                       
                       static {
                            try {
                                 //voiceClass = Class.forName("CMUDiphoneVoice");
                                 voiceClass = Class.forName("com.sun.speech.freetts.en.us.CMUDiphoneVoice");
                            } catch (ClassNotFoundException cnfe) {
                                 cnfe.printStackTrace();
                            }
                       }
                       
                       private Voice voice = null;
                       
                       public SpeechModule() {
                            try {
                                 this.voice = (Voice) voiceClass.newInstance();
                        voice.setPitch(100);
                        voice.setPitchShift(10000);
                            } catch (InstantiationException ie) {
                                 ie.printStackTrace();
                            } catch (IllegalAccessException iae) {
                                 iae.printStackTrace();
                            }
                            
                            voice.setLexicon(new CMULexicon());
                            voice.setAudioPlayer(new JavaClipAudioPlayer());
                            voice.load();
                       }
                  
                  
                    public void setPitch(float n){
                        voice.setPitch(n);
                    }
                  
                    public void setPitchShift(float n){
                        voice.setPitchShift(n);
                    }
                  
                  
                       public void speak(String value) {
                            voice.speak(value);
                       }
                  }
                  • 6. Re: I need a Text to Speech conversion program
                    843802
                    I got a null pointer exception ( in synth.allocate() method) in that program.... What shall i do?.. Give me some advice.
                    • 7. Re: I need a Text to Speech conversion program
                      843802
                      I don't know where is that class "oracle.jdeveloper.layout.XYLayout"... I need that jar file.. If you have that link then plz send me...
                      by
                      manikandan.s
                      • 8. Re: I need a Text to Speech conversion program
                        843802
                        Yeah, just have to remove the XYLayout stuff. That is a custom class that Oracle has built in to it's Java Dev Environment. Here is the class without the XY Layout...


                        import java.awt.Dimension;
                        
                        import java.awt.GridLayout;
                        import java.awt.event.WindowAdapter;
                        
                        import javax.swing.JFrame;
                        import javax.swing.JButton;
                        import java.awt.BorderLayout;
                        import java.awt.FlowLayout;
                        
                        import javax.swing.JLabel;
                        
                        import oracle.jdeveloper.layout.XYLayout;
                        import oracle.jdeveloper.layout.XYConstraints;
                        import java.awt.Insets;
                        import javax.swing.JTextField;
                        import javax.swing.JTextArea;
                        import javax.swing.JScrollPane;
                        import java.awt.event.ActionListener;
                        import java.awt.event.ActionEvent;
                        import java.awt.event.WindowListener;
                        import java.awt.event.WindowEvent;
                        
                        public class Gui extends JFrame  {
                        
                             protected SpeechModule speechModule = new SpeechModule();
                        
                        
                          JButton cmdSpeak = new JButton();
                            //XYLayout xYLayout1 = new XYLayout();
                            JScrollPane jScrollPane1 = new JScrollPane();
                          JTextArea txtWords = new JTextArea();
                          JTextField txtPitch = new JTextField();
                          JTextField txtShift = new JTextField();
                            private GridLayout gridLayout1 = new GridLayout();
                            private JLabel jLabel1 = new JLabel();
                            private JLabel jLabel2 = new JLabel();
                        
                            public Gui() {
                            super("Java Speech");  
                            try {
                              jbInit();
                            } catch(Exception e) {
                              e.printStackTrace();
                            }
                          }
                          
                          
                          private void jbInit() throws Exception {
                            this.getContentPane().setLayout(gridLayout1);
                                this.setSize(new Dimension(472, 289));
                                this.addWindowListener(new WindowAdapter()
                              {
                                public void windowClosing(WindowEvent e)
                                {
                                  this_windowClosing(e);
                                }
                              });
                            cmdSpeak.setText("Speak");
                            cmdSpeak.setActionCommand("cmdSpeak");
                            cmdSpeak.setMargin(new Insets(0, 0, 0, 0));
                            cmdSpeak.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                  cmdSpeak_actionPerformed(e);
                                }
                              });
                            txtWords.setText("Would you like to play a game?");
                            txtWords.setSelectionEnd(1);
                            txtPitch.setText("10");
                            txtShift.setText("20");
                                gridLayout1.setColumns(2);
                                gridLayout1.setRows(3);
                                jLabel1.setText("PITCH");
                                jLabel1.setAlignmentX((float) 2.0);
                                jLabel2.setText("SHIFT");
                                jLabel2.setAlignmentX((float) 2.0);
                                this.getContentPane().add(jLabel2, null);
                                this.getContentPane().add(txtShift, new XYConstraints(40, 260, 55, 25));
                                this.getContentPane().add(jLabel1, null);
                                this.getContentPane().add(txtPitch, 
                                                          new XYConstraints(40, 220, 55, 25));
                                jScrollPane1.getViewport().add(txtWords, null);
                                this.getContentPane().add(jScrollPane1, 
                                                          new XYConstraints(50, 40, 360, 150));
                                this.getContentPane().add(cmdSpeak, 
                                                          new XYConstraints(160, 230, 75, 25));
                                this.setSize(600, 400);
                          }
                        
                            public static void main(String[] args) {
                              Gui gui = new Gui();
                              gui.show();
                            }  
                        
                        
                          void cmdSpeak_actionPerformed(ActionEvent e) {
                        
                            System.out.println("pitch" + Float.valueOf(txtPitch.getText()).floatValue());
                            System.out.println("pitchShift" + Float.valueOf(txtShift.getText()).floatValue());
                            speechModule.setPitch( Float.valueOf(txtPitch.getText()).floatValue() );
                            speechModule.setPitchShift( Float.valueOf(txtShift.getText()).floatValue() );
                            speechModule.speak(txtWords.getText());
                          }
                        
                          private void this_windowClosing(WindowEvent e)  {
                            System.exit(0);
                          }
                        }
                        • 9. Re: I need a Text to Speech conversion program
                          843802
                          heh... sorry, just noticed that the IDE must have stuck the XYLayout/Constraint stuff back in there when I compiled. Just remove the XY stuff and use that GridLayout. It is just the layout, nothing that hiders the functionality.
                          • 10. Re: I need a Text to Speech conversion program
                            843802
                            i am also trying but i cant get it
                            • 11. Re: I need a Text to Speech conversion program
                              843802
                              import javax.speech.*;
                              import javax.speech.recognition.*;
                              import java.io.FileReader;
                              import java.util.Locale;

                              public class HelloWorld extends ResultAdapter {
                                   static Recognizer rec;

                                   // Receives RESULT_ACCEPTED event: print it, clean up, exit
                                   public void resultAccepted(ResultEvent e) {
                                        Result r = (Result)(e.getSource());
                                        ResultToken tokens[] = r.getBestTokens();

                                        for (int i = 0; i < tokens.length; i++)
                                             System.out.print(tokens.getSpokenText() + " ");
                                        System.out.println();

                                        // Deallocate the recognizer and exit
                                        try
                                        {
                                        rec.deallocate();
                                        }
                                        catch(Exception e1)
                                        {
                                        }
                                        System.exit(0);
                                   }

                                   public static void main(String args[]) {
                                        try {
                                             // Create a recognizer that supports English.
                                             rec = Central.createRecognizer(
                                                                 new EngineModeDesc(Locale.ENGLISH));
                                             
                                             // Start up the recognizer
                                             rec.allocate();
                                   
                                             // Load the grammar from a file, and enable it
                                             FileReader reader = new FileReader(args[0]);
                                             RuleGrammar gram = rec.loadJSGF(reader);
                                             gram.setEnabled(true);
                                   
                                             // Add the listener to get results
                                             rec.addResultListener(new HelloWorld());
                                   
                                             // Commit the grammar
                                             rec.commitChanges();
                                   
                                             // Request focus and start listening
                                             rec.requestFocus();
                                             rec.resume();
                                        } catch (Exception e) {
                                             e.printStackTrace();
                                        }
                                   }
                              }

                              i am getting "java.lang.NullPointerException" can any one help me what is the problem with this program.
                              • 12. Re: I need a Text to Speech conversion program
                                PhHein
                                Welcome to the forum. Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

                                I'm locking this thread now.