View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  package com.octo.captcha.engine.sound;
13  
14  import com.octo.captcha.component.sound.soundconfigurator.FreeTTSSoundConfigurator;
15  import com.octo.captcha.component.sound.soundconfigurator.SoundConfigurator;
16  import com.octo.captcha.component.sound.wordtosound.FreeTTSWordToSound;
17  import com.octo.captcha.component.sound.wordtosound.WordToSound;
18  import com.octo.captcha.component.word.DefaultSizeSortedWordList;
19  import com.octo.captcha.component.word.DictionaryReader;
20  import com.octo.captcha.component.word.SizeSortedWordList;
21  import com.octo.captcha.component.word.worddecorator.SpellerWordDecorator;
22  import com.octo.captcha.component.word.wordgenerator.DictionaryWordGenerator;
23  import com.octo.captcha.component.word.wordgenerator.WordGenerator;
24  import com.octo.captcha.engine.sound.utils.SoundToFile;
25  import com.octo.captcha.sound.SoundCaptcha;
26  import com.octo.captcha.sound.SoundCaptchaFactory;
27  import com.octo.captcha.sound.speller.SpellerSoundFactory;
28  
29  import javax.sound.sampled.AudioInputStream;
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.InputStreamReader;
34  import java.util.Locale;
35  
36  /***
37   * Test sample for a sound captcha
38   *
39   * @author Benoit Doumas
40   */
41  public class SoundEngineSample {
42      private static String voiceName = "kevin16";
43  
44      private static String voicePackage = "com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory,com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory";
45  
46      static String[] wordlist;
47  
48      static WordGenerator words;
49  
50      static SoundCaptchaFactory factory;
51  
52      static WordToSound wordToSound;
53  
54      public static void main(String[] args) {
55          SoundEngineSample.wordlist = new String[]{"and", "oh", "test", "test", "hello", "lame",
56                  "eating", "snake", "roots", "yeah", "azerty"};
57  
58          SoundEngineSample.words = new DictionaryWordGenerator(
59                  (new SoundEngineSample()).new ArrayDictionary(wordlist));
60  
61          SoundConfigurator configurator = new FreeTTSSoundConfigurator("kevin16",
62                  "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory", 1.0f, 100, 70);
63          SoundEngineSample.wordToSound = new FreeTTSWordToSound(new FreeTTSSoundConfigurator(
64                  voiceName, voicePackage, 1.0f, 100, 100), 3, 6);
65          SpellerWordDecorator decorator = new SpellerWordDecorator(", ");
66          SoundEngineSample.factory = new SpellerSoundFactory(words, wordToSound, decorator);
67          //SoundEngineSample.factory = new GimpySoundFactory(words, wordToSound);
68          for (int i = 1; i <= 10; i++)
69              test();
70  
71      }
72  
73      public static void test() {
74          SoundCaptcha tCaptcha = factory.getSoundCaptcha(Locale.US);
75  
76          System.out.println(tCaptcha.getQuestion());
77          AudioInputStream tInputStream = tCaptcha.getSoundChallenge();
78          try {
79              SoundToFile.serialize(tInputStream, new File("c://test.wav"));
80          }
81          catch (IOException e1) {
82              e1.printStackTrace();
83          }
84          String text = null;
85          BufferedReader reader;
86          reader = new BufferedReader(new InputStreamReader(System.in));
87          System.out.print("Enter text: ");
88          System.out.flush();
89  
90          try {
91              text = reader.readLine();
92          }
93          catch (IOException e) {
94              e.printStackTrace();
95          }
96  
97          if (tCaptcha.validateResponse(text).booleanValue()) {
98              System.out.print("Passed!!!");
99          } else {
100             System.out.print("Failed!!!");
101         }
102         tCaptcha.disposeChallenge();
103     }
104 
105     private class ArrayDictionary implements DictionaryReader {
106         private String[] list;
107 
108         private DefaultSizeSortedWordList wordList;
109 
110         public ArrayDictionary(String[] list) {
111             this.list = list;
112             wordList = new DefaultSizeSortedWordList(Locale.getDefault());
113             for (int i = 0; i < list.length; i++) {
114                 wordList.addWord(list[i]);
115             }
116         }
117 
118         /*
119          * (non-Javadoc)
120          * 
121          * @see com.octo.captcha.component.word.DictionaryReader#getWordList()
122          */
123         public SizeSortedWordList getWordList() {
124 
125             return wordList;
126         }
127 
128         /*
129          * (non-Javadoc)
130          * 
131          * @see com.octo.captcha.component.word.DictionaryReader#getWordList(java.util.Locale)
132          */
133         public SizeSortedWordList getWordList(Locale arg0) {
134 
135             return wordList;
136         }
137 
138     }
139 }