2 Replies Latest reply: May 23, 2008 3:54 AM by 843802 RSS

    simple text to speech problem

    843802
      Hi All

      I'm trying to create a text to speech using java but i cant use the free TTS so i created the following program, it works and speech does output but no in the order i write it. The speech is outputted by the order i created the program. The program below is a sample and i was wondering could the speech be outputted in the order i write it?

      any 1 got a clue
      help will be appreciated
      import java.awt.event.*;
      import java.io.*;
      import javax.swing.*;
      import java.awt.*;
      import javax.media.*;
      import java.applet.*;
      import sun.audio.AudioPlayer;
      import sun.audio.AudioStream;
      import java.text.MessageFormat;
      import java.util.*;
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.util.Scanner;
       
       
       
      public class MainCl extends JFrame  implements ActionListener {
        JTextField jtf = new JTextField(15);
       // jtf.setText(".");
        public MainCl() {
      JPanel jp = new JPanel();
      jp.add(jtf, BorderLayout.NORTH);
      getContentPane().add(jp);
            //jp.add(jtf);
            jtf.addActionListener(this);
       
      }
       
      public void actionPerformed(ActionEvent ae) {
       
      String str = jtf.getText();
                if (str.indexOf("co") > -1)
       
                {
                     {
                       try
                                       {
                 AudioStream wav = new AudioStream(new FileInputStream(new File("co.wav")));
                
                 AudioPlayer.player.start(wav);
       
                                  }
                                           catch(Exception ex)
                                          {
                                           ex.printStackTrace();
                                          }
       
                     }
       
      }
      try {
                        long numMillisecondsToSleep = 1000; // 5 seconds
                        Thread.sleep(numMillisecondsToSleep);
                    } catch (InterruptedException e) {
          }
           
       
       
      if (str.indexOf("no") > -1)
      {
           {
             try
                             {
       AudioStream wav = new AudioStream(new FileInputStream(new File("no.wav")));
       
       AudioPlayer.player.start(wav);
       
                        }
                                 catch(Exception ex)
                                {
                                 ex.printStackTrace();
                                }
       
           }
       
       
       
       
      }
       
           
           try{
      Scanner scanner = new Scanner (new File(jtf.getText()));
       
       
       
      } catch (IOException e) {}
       
      }
       
        public static void main(String[] args) {
         MainCl frame = new MainCl();
       
       
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 200);
          frame.setVisible(true);
       
       
        }
       
      }
      e.g. if i was to type no co in my texfield, the audio output will still be co no,.
      I don't even know if its possible to make a text to speech with my program so any guidance will be good.

      Thanks in advance
        • 1. Re: simple text to speech problem
          843802
          ?
          • 2. Re: simple text to speech problem
            843802
            hi

            I think I figured out the reason why your program ALWAYS says "co no".

            Look at your IF structures, you always use indexOf.
            IndexOf will search the whole input string for the part you give, and if it finds it, it will return a value.

            In your case, if you enter "co no" or "no co", it will always say that it finds "co".

            In order to make your program function correct, you should break up the inputString into multiple strings and then check those individual strings.
            Check code below
            // EXAMPLE CODE
            String inputString = jtf.getText();
            String[] words = inputString.split(' ');
            
            for(String word : words){
                 if(word.equals("co"){
                       // make sound of co
                 }
                 if(word.equals("no"){
                       // make sound of no
                 }
            }
            
            // other code