Advertisement
Guest User

Recon.java http://darylrobotproject.wordpress.com/

a guest
Sep 21st, 2012
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import javax.speech.*;
  2. import t2s.son.LecteurTexte;
  3. import javax.speech.recognition.*;
  4. import javax.speech.synthesis.Synthesizer;
  5. import javax.speech.synthesis.Voice;
  6. import java.io.FileReader;
  7. import java.util.Locale;
  8.  
  9. public class Recon extends ResultAdapter {
  10.  
  11.     static Recognizer recognizer = null;
  12.     static Voice voice = null;
  13.     static Synthesizer synth = null;
  14.  
  15.     // Permet de jouer une string en synthèse vocale
  16.     public static void lectureVocale(String reponse) {
  17.         LecteurTexte lt = new LecteurTexte(reponse);
  18.         lt.playAll();
  19.     }
  20.  
  21.  
  22.     // Call à la détection d'un nouvel ordre
  23.     public void resultCreated(ResultEvent e) {
  24.         System.out.println("Result Created...");
  25.     }
  26.  
  27.     // Call à la detection d'un nouveau mot ou syllabe?
  28.     public void resultUpdated(ResultEvent e) {
  29.         Result r = (Result)(e.getSource());
  30.         System.out.println("Result Updated... "+r);
  31.     }
  32.  
  33.     // Call si rien n'est trouvé (bruit ambiant...)
  34.     @Override
  35.     public void resultRejected(ResultEvent e) {
  36.         System.out.println("Result Rejected... ");
  37.     }
  38.  
  39.     // Call si match avec un élément de la grammaire
  40.     @Override
  41.     public void resultAccepted(ResultEvent re) {
  42.         try {
  43.             Result res = (Result) (re.getSource());
  44.             // On récupère le meilleur token
  45.             ResultToken tokens[] = res.getBestTokens();
  46.  
  47.             String phrase = "";
  48.             String gst = "";
  49.  
  50.             for (int i = 0; i < tokens.length; i++) {
  51.                 gst = tokens[i].getSpokenText();
  52.                 phrase += gst + " ";
  53.                 System.out.print(gst + " ");
  54.             }
  55.  
  56.             System.out.println();
  57.             phrase = phrase.trim();
  58.  
  59.             // Si égale à STOP on stop la recon
  60.             if (gst.equals("STOP")) {
  61.  
  62.                 recognizer.deallocate();
  63.                 System.out.println(Constante.RECON_STOP);
  64.                 lectureVocale(Constante.RECON_STOP);
  65.                 System.exit(0);
  66.  
  67.             } else {
  68.  
  69.                 // On met la recon en pause pour que la réponse ne face pas d'interférence
  70.                 recognizer.pause();
  71.                
  72.                 // On cherche une réponse
  73.                 String reponse = Constante.retournerReponse(phrase);
  74.  
  75.                 // Si réponse alors on joue le son OK et la réponse avec la synthèse vocale
  76.                 if (!reponse.equals("")) {
  77.  
  78.                     Sound.jouerSon(Constante.PATH_SON_OK);
  79.                     lectureVocale(reponse);
  80.  
  81.                 } else {
  82.  
  83.                     // Sinon on joue le son KO
  84.                     Sound.jouerSon(Constante.PATH_SON_KO);
  85.                 }
  86.                 recognizer.resume();
  87.             }
  88.         } catch (Exception ex) {
  89.             System.out.println("Erreur: " + ex);
  90.         }
  91.     }
  92.  
  93.     public static void main(String args[]) {
  94.         try {
  95.  
  96.             // On initialise le moteur de reco
  97.             recognizer = Central.createRecognizer(new EngineModeDesc(Locale.ROOT));
  98.             recognizer.allocate();
  99.  
  100.             // On lui indique le fichier de grammaire
  101.             FileReader grammar = new FileReader(Constante.PATH_GRAMM);
  102.             RuleGrammar rg = recognizer.loadJSGF(grammar);
  103.             rg.setEnabled(true);
  104.  
  105.             // On lui indique que le mode dictation est activé en plus de la grammaire
  106.             DictationGrammar dictation;
  107.             dictation = recognizer.getDictationGrammar("dictation");
  108.             dictation.setEnabled(true);
  109.  
  110.             SpeakerManager speakerManager = recognizer.getSpeakerManager();
  111.  
  112.             // On applique le profil de reco Windows
  113.             SpeakerProfile profile = new SpeakerProfile();
  114.             SpeakerProfile[] profs = speakerManager.listKnownSpeakers();
  115.             for(int i=0;i<profs.length; i++) {
  116.                 System.out.println("Found Profile "+i+" = "+profs[i].getName());
  117.                 profile = profs[i];
  118.             }
  119.  
  120.             speakerManager.setCurrentSpeaker(profile);
  121.             System.out.println("Current Profile set to "+speakerManager.getCurrentSpeaker().getName());
  122.  
  123.             // On ajoute un listener
  124.             recognizer.addResultListener(new Recon());
  125.  
  126.             lectureVocale(Constante.RECON_ACTIVE);
  127.             System.out.println(Constante.RECON_ACTIVE);
  128.  
  129.             recognizer.commitChanges();
  130.             recognizer.requestFocus();
  131.             recognizer.resume();
  132.  
  133.         } catch (Exception e) {
  134.  
  135.             System.out.println("Exception: " + e.toString());
  136.             e.printStackTrace();
  137.             System.exit(0);
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement