|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package com.octo.captcha.engine.sound.utils; |
|
19 |
| |
|
20 |
| import com.octo.captcha.engine.sound.SoundCaptchaEngine; |
|
21 |
| import com.octo.captcha.engine.sound.gimpy.SimpleListSoundCaptchaEngine; |
|
22 |
| import com.octo.captcha.engine.sound.speller.SpellerSoundCaptchaEngine; |
|
23 |
| import com.octo.captcha.sound.SoundCaptcha; |
|
24 |
| |
|
25 |
| import java.io.File; |
|
26 |
| import java.io.IOException; |
|
27 |
| import java.text.DecimalFormat; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| public class SoundCaptchaToWAV { |
|
37 |
| |
|
38 |
| private static boolean SHOULD_DELETE_OLD_WAVS_FIRST = true; |
|
39 |
| |
|
40 |
0
| public static void main(String[] args) throws Exception {
|
|
41 |
0
| if (args.length < 2) {
|
|
42 |
0
| System.out.println("Usage : engineClassName outputDir iterations");
|
|
43 |
0
| System.out.println("If engineClassName is 'all', then several sound Engines are used");
|
|
44 |
0
| System.exit(1);
|
|
45 |
| } |
|
46 |
| |
|
47 |
| |
|
48 |
0
| String className = args[0];
|
|
49 |
0
| File outputDir = new File(args[1]);
|
|
50 |
0
| String iterationsString = args[2];
|
|
51 |
0
| int iterations = Integer.parseInt(iterationsString);
|
|
52 |
| |
|
53 |
0
| System.out.println("args : " +
|
|
54 |
| "sound captcha engine class='" + className + "'" + |
|
55 |
| ", output dir='" + outputDir + "'" + |
|
56 |
| ",iterations='" + iterationsString + "'"); |
|
57 |
| |
|
58 |
0
| SoundCaptchaToWAV.clearOutputDirectory(outputDir);
|
|
59 |
| |
|
60 |
0
| SoundCaptchaEngine captchaEngine = null;
|
|
61 |
0
| if (className.equals("all")) {
|
|
62 |
0
| SoundCaptchaEngine[] engines = {
|
|
63 |
| new SpellerSoundCaptchaEngine(), |
|
64 |
| new SimpleListSoundCaptchaEngine(), |
|
65 |
| |
|
66 |
| }; |
|
67 |
0
| for (int i = 0; i < engines.length; i++) {
|
|
68 |
0
| captchaEngine = engines[i];
|
|
69 |
0
| System.out.println("Beginning generation with " + captchaEngine.getClass().getName());
|
|
70 |
0
| try {
|
|
71 |
0
| SoundCaptchaToWAV.generate(iterations, captchaEngine, outputDir);
|
|
72 |
| } |
|
73 |
| catch (Exception e) { |
|
74 |
0
| System.out.println("Errors with class " + captchaEngine.getClass().getName());
|
|
75 |
| } |
|
76 |
| } |
|
77 |
| } else { |
|
78 |
| |
|
79 |
0
| try {
|
|
80 |
0
| captchaEngine = (SoundCaptchaEngine) Class.forName(className).newInstance();
|
|
81 |
| } |
|
82 |
| catch (Exception e) { |
|
83 |
0
| System.out.println("Couldn't initialize '" + className + "', trying a likely package prefix");
|
|
84 |
0
| String defaultClassPrefix = "com.octo.captcha.engine.sound.";
|
|
85 |
0
| try {
|
|
86 |
0
| captchaEngine = (SoundCaptchaEngine) Class.forName(defaultClassPrefix + className).newInstance();
|
|
87 |
| } |
|
88 |
| catch (Exception e2) { |
|
89 |
0
| System.out.println("Couldn't initialize '" + className + " -- specify a fully attributed name");
|
|
90 |
0
| System.exit(1);
|
|
91 |
| } |
|
92 |
| } |
|
93 |
| |
|
94 |
0
| SoundCaptchaToWAV.generate(iterations, captchaEngine, outputDir);
|
|
95 |
| } |
|
96 |
| |
|
97 |
0
| System.exit(0);
|
|
98 |
| } |
|
99 |
| |
|
100 |
0
| private static void clearOutputDirectory(File outputDir) {
|
|
101 |
0
| if (SoundCaptchaToWAV.SHOULD_DELETE_OLD_WAVS_FIRST) {
|
|
102 |
0
| File[] files = outputDir.listFiles();
|
|
103 |
0
| if (files == null) {
|
|
104 |
0
| return;
|
|
105 |
| } |
|
106 |
0
| if (files.length > 2) {
|
|
107 |
| |
|
108 |
0
| System.out.println("Deleting about " + (files.length - 2) + " wave files");
|
|
109 |
| } |
|
110 |
0
| for (int i = 0; i < files.length; i++) {
|
|
111 |
0
| File f = files[i];
|
|
112 |
0
| if (f.isFile() && f.getName().endsWith("wav")) {
|
|
113 |
0
| f.delete();
|
|
114 |
| } |
|
115 |
| } |
|
116 |
| } |
|
117 |
| } |
|
118 |
| |
|
119 |
0
| private static void generate(int iterations, SoundCaptchaEngine captchaEngine, File outputDir) throws IOException {
|
|
120 |
| |
|
121 |
0
| outputDir.mkdirs();
|
|
122 |
0
| String className = captchaEngine.getClass().getName().substring(captchaEngine.getClass().getPackage().getName().length() + 1);
|
|
123 |
| |
|
124 |
0
| System.out.println("Starting on " + className);
|
|
125 |
| |
|
126 |
0
| long sumSoundCreation = 0;
|
|
127 |
0
| long sumFileCreation = 0;
|
|
128 |
0
| int i = 0;
|
|
129 |
0
| try {
|
|
130 |
0
| for (i = 0; i < iterations; i++) {
|
|
131 |
0
| long t = System.currentTimeMillis();
|
|
132 |
0
| SoundCaptcha captcha = captchaEngine.getNextSoundCaptcha();
|
|
133 |
0
| sumSoundCreation += System.currentTimeMillis() - t;
|
|
134 |
0
| t = System.currentTimeMillis();
|
|
135 |
0
| File outputFile = new File(outputDir, File.separator + className + "Captcha_" + i + ".wav");
|
|
136 |
0
| SoundToFile.serialize(captcha.getSoundChallenge(), outputFile);
|
|
137 |
0
| sumFileCreation += System.currentTimeMillis() - t;
|
|
138 |
0
| System.out.print(".");
|
|
139 |
0
| if (i % 100 == 99) {
|
|
140 |
0
| System.out.println("");
|
|
141 |
| } |
|
142 |
| } |
|
143 |
| } |
|
144 |
| finally { |
|
145 |
0
| if (i < iterations) {
|
|
146 |
0
| System.out.println("exited early! i=" + i);
|
|
147 |
| } else { |
|
148 |
0
| System.out.println("done");
|
|
149 |
| } |
|
150 |
0
| DecimalFormat df = new DecimalFormat();
|
|
151 |
0
| System.out.println("Summary for " + className + ":" +
|
|
152 |
| " avg sound creation = " + df.format(sumSoundCreation / iterations) + " milliseconds/sound," + |
|
153 |
| " avg file creation = " + df.format(sumFileCreation / iterations) + " milliseconds/file"); |
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
| } |