Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.73 KB | None | 0 0
  1. package com.nuance.http_2_0.asr;
  2.  
  3. import com.nuance.samples.util.Chunk;
  4. import com.nuance.samples.util.FileLoader;
  5. import com.nuance.samples.util.LatencyMonitor;
  6. import com.nuance.samples.util.LogData;
  7. import com.nuance.samples.util.ResponseParser;
  8. import com.nuance.samples.util.SocketFactory;
  9. import com.nuance.samples.util.UserIDManager;
  10. import com.nuance.samples.util.audio.AudioChopper;
  11. import com.nuance.samples.util.audio.AudioChopperFactory;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStreamReader;
  18. import java.io.OutputStream;
  19. import java.io.PrintStream;
  20. import java.net.Socket;
  21. import java.net.SocketTimeoutException;
  22. import java.net.URL;
  23. import java.net.UnknownHostException;
  24. import java.nio.ByteBuffer;
  25. import java.security.CodeSource;
  26. import java.security.ProtectionDomain;
  27. import java.util.HashMap;
  28. import java.util.Iterator;
  29. import java.util.Map;
  30. import java.util.Map.Entry;
  31. import java.util.UUID;
  32. import java.util.concurrent.CountDownLatch;
  33. import javax.sound.sampled.AudioFormat;
  34. import javax.sound.sampled.AudioFormat.Encoding;
  35. import javax.sound.sampled.AudioSystem;
  36. import javax.sound.sampled.DataLine.Info;
  37. import javax.sound.sampled.LineUnavailableException;
  38. import javax.sound.sampled.TargetDataLine;
  39. import org.apache.commons.collections4.queue.CircularFifoQueue;
  40. import org.json.JSONArray;
  41. import org.json.JSONException;
  42. import org.json.JSONObject;
  43.  
  44. public class HttpAsrClient
  45. implements IHttpAsrClient
  46. {
  47. static final float MAX_8_BITS_SIGNED = 127.0F;
  48. static final float MAX_8_BITS_UNSIGNED = 255.0F;
  49. static final float MAX_16_BITS_SIGNED = 32767.0F;
  50. static final float MAX_16_BITS_UNSIGNED = 65535.0F;
  51. private String _host;
  52. private int _port;
  53. private String _appId;
  54. private String _appKey;
  55. private boolean _useTLS;
  56.  
  57. class AudioPacket
  58. {
  59. byte[] audio;
  60. int size = 0;
  61.  
  62. public AudioPacket(byte[] buf, int len)
  63. {
  64. this.audio = buf;
  65. this.size = len;
  66. }
  67. }
  68.  
  69. private boolean _requireTrustedRootCert = true;
  70. private String _userID;
  71. private boolean _streamingResults = true;
  72. protected boolean _profanityFilteringEnabled = false;
  73. private TargetDataLine line = null;
  74. private boolean stopRequested = false;
  75. protected boolean batchMode = false;
  76. protected boolean startOfSpeechDetectionEnabled = true;
  77. protected boolean verbose = false;
  78. protected boolean listening = false;
  79. protected String audioPath = null;
  80. private Socket s = null;
  81. protected OutputStream out = null;
  82. protected String boundary = null;
  83. protected boolean queryFailed = false;
  84. protected boolean queryCancelled = false;
  85. protected LatencyMonitor transactionLatency = new LatencyMonitor();
  86. protected LogData _logData = new LogData();
  87. private static Object waitLock = new Object();
  88. protected int txnTimeout = 5000;
  89. private CountDownLatch latch;
  90. protected RequestData _requestData;
  91.  
  92. protected class RequestData
  93. {
  94. static final String APP_NAME = "Nuance Java Sample HTTP App";
  95. static final String APP_VERSION = "1.0";
  96. static final String ORGANIZATION_ID = "NUANCE";
  97. public String IN_CODEC = "PCM_16_16K";
  98. public String OUT_CODEC = "PCM_16_16K";
  99. static final String AUDIO_SOURCE = "SpeakerAndMicrophone";
  100. static final String COMMAND_NAME = "NMDP_ASR_CMD";
  101. public String LANGUAGE = "eng-USA";
  102. public String DICTATION_TYPE = "Dictation";
  103. static final String UI_LANGUAGE = "en";
  104. static final String CARRIER = "unknown";
  105. static final String PHONE_NETWORK = "wifi";
  106. final String PHONE_OS = System.getProperty("os.name");
  107. final String DEVICE_MODEL = System.getProperty("os.arch");
  108. final String PHONE_SUBMODEL = System.getProperty("os.version");
  109. static final String LOCALE = "USA";
  110. static final String LOCATION = "off";
  111. static final String APPLICATION_STATE_ID = "0";
  112. private String _applicationSessionID = null;
  113. private int _utteranceNumber = 1;
  114.  
  115. protected RequestData() {}
  116.  
  117. public void clearApplicationSessionID()
  118. {
  119. this._applicationSessionID = null;
  120. }
  121.  
  122. public String initApplicationSessionID()
  123. {
  124. this._applicationSessionID = UUID.randomUUID().toString();
  125.  
  126. return this._applicationSessionID;
  127. }
  128.  
  129. public String getApplicationSessionID()
  130. {
  131. return this._applicationSessionID;
  132. }
  133.  
  134. public void resetUtteranceNumber()
  135. {
  136. this._utteranceNumber = 1;
  137. }
  138.  
  139. public int incrementUtteranceNumber()
  140. {
  141. return this._utteranceNumber++;
  142. }
  143.  
  144. public int getUtteranceNumber()
  145. {
  146. return this._utteranceNumber;
  147. }
  148. }
  149.  
  150. public HttpAsrClient(String host, int port, String appId, String appKey, boolean useTLS)
  151. {
  152. this(host, port, useTLS, appId, appKey, "Dictation", "eng-USA", null);
  153. }
  154.  
  155. public HttpAsrClient(String host, int port, boolean useTLS, String appId, String appKey, String topic, String langCode, String userID)
  156. {
  157. write("Host: " + host + ":" + port);
  158. this._host = host;
  159. this._port = port;
  160. this._appId = appId;
  161. this._appKey = appKey;
  162. this._useTLS = useTLS;
  163. this._userID = userID;
  164.  
  165. this._requestData = new RequestData();
  166. this._requestData.LANGUAGE = langCode;
  167. this._requestData.DICTATION_TYPE = topic;
  168. }
  169.  
  170. public void enableBatchMode()
  171. {
  172. this.batchMode = true;
  173. }
  174.  
  175. public void disableBatchMode()
  176. {
  177. this.batchMode = false;
  178. }
  179.  
  180. public boolean batchModeEnabled()
  181. {
  182. return this.batchMode;
  183. }
  184.  
  185. public void setSavedAudioPath(String val)
  186. {
  187. this.audioPath = val;
  188. }
  189.  
  190. public String getSavedAudioPath()
  191. {
  192. return this.audioPath;
  193. }
  194.  
  195. public void enableStartOfSpeechDetection()
  196. {
  197. this.startOfSpeechDetectionEnabled = true;
  198. }
  199.  
  200. public void disableStartOfSpeechDetection()
  201. {
  202. this.startOfSpeechDetectionEnabled = false;
  203. }
  204.  
  205. public boolean isStartOfSpeechDetectionEnabled()
  206. {
  207. return this.startOfSpeechDetectionEnabled;
  208. }
  209.  
  210. public void enableProfanityFiltering()
  211. {
  212. this._profanityFilteringEnabled = true;
  213. }
  214.  
  215. public void disableProfanityFiltering()
  216. {
  217. this._profanityFilteringEnabled = false;
  218. }
  219.  
  220. public boolean isProfanityFilteringEnabled()
  221. {
  222. return this._profanityFilteringEnabled;
  223. }
  224.  
  225. public void setTxnTimeout(int val)
  226. {
  227. this.txnTimeout = val;
  228. }
  229.  
  230. public int getTxnTimeout()
  231. {
  232. return this.txnTimeout;
  233. }
  234.  
  235. public void enableTrustedRootCert()
  236. {
  237. this._requireTrustedRootCert = true;
  238. }
  239.  
  240. public void disableTrustedRootCert()
  241. {
  242. this._requireTrustedRootCert = false;
  243. }
  244.  
  245. public void enableVerbose()
  246. {
  247. this.verbose = true;
  248. }
  249.  
  250. public void disableVerbose()
  251. {
  252. this.verbose = false;
  253. }
  254.  
  255. public boolean isVerbose()
  256. {
  257. return this.verbose;
  258. }
  259.  
  260. protected void shutdown()
  261. {
  262. this.listening = false;
  263. if (this.verbose) {
  264. write("shutting down...");
  265. }
  266. System.exit(0);
  267. }
  268.  
  269. protected void toggleListening()
  270. {
  271. if (this.listening) {
  272. stopListening();
  273. } else {
  274. startListening();
  275. }
  276. }
  277.  
  278. protected void startListening()
  279. {
  280. this.transactionLatency.reset();
  281. this.transactionLatency.setMarker("start");
  282. connectToServer();
  283. captureAudio();
  284. }
  285.  
  286. protected void stopListening()
  287. {
  288. this.listening = false;
  289. if (!this.stopRequested) {
  290. closeInputLine();
  291. }
  292. }
  293.  
  294. private void closeInputLine()
  295. {
  296. this.stopRequested = true;
  297. if ((this.line.isActive()) || (this.line.isRunning())) {
  298. this.line.stop();
  299. }
  300. if (this.line.isOpen()) {
  301. this.line.close();
  302. }
  303. }
  304.  
  305. public void monitorConsoleEvents()
  306. {
  307. write("Press enter to start capturing audio. (q <enter> to quit)");
  308.  
  309. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  310. for (;;)
  311. {
  312. String s = null;
  313. try
  314. {
  315. s = br.readLine();
  316. }
  317. catch (IOException e)
  318. {
  319. e.printStackTrace();
  320. }
  321. if ((s != null) && (s.equalsIgnoreCase("q"))) {
  322. shutdown();
  323. }
  324. if ((s != null) && (s.length() == 0)) {
  325. toggleListening();
  326. }
  327. }
  328. }
  329.  
  330. protected void captureAudio()
  331. {
  332. try
  333. {
  334. final AudioFormat format = getFormat();
  335. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  336. if (this.verbose) {
  337. write("Audio Data line Info: " + info);
  338. }
  339. this.line = ((TargetDataLine)AudioSystem.getLine(info));
  340. this.line.open(format);
  341. this.line.start();
  342.  
  343. final int bufferSize = (int)(format.getSampleRate() * format.getFrameSize() / 50.0F);
  344. write("bufferSize: " + bufferSize);
  345.  
  346. Runnable runner = new Runnable()
  347. {
  348. public void run()
  349. {
  350. HttpAsrClient.this.listening = true;
  351. try
  352. {
  353. File dir = HttpAsrClient.this.audioPath == null ? new File(".") : new File(HttpAsrClient.this.audioPath);
  354.  
  355. long unixTime = System.currentTimeMillis() / 1000L;
  356. File fout = new File(dir.getCanonicalPath() + File.separator + "audio-" + unixTime + ".pcm");
  357. dir = new File(fout.getParent());
  358. if (HttpAsrClient.this.verbose) {
  359. HttpAsrClient.this.write("Writing to " + fout.getCanonicalPath());
  360. }
  361. if (!fout.exists())
  362. {
  363. dir.mkdirs();
  364. fout.createNewFile();
  365. }
  366. String boundary = null;
  367.  
  368. OutputStream FileOutput = new FileOutputStream(fout);
  369.  
  370. int numPacketsToBufferStartOfSpeech = 7;
  371. int numPacketsToBufferLeadingSilence = 12;
  372. float audioEnergyThreshold = HttpAsrClient.this.isStartOfSpeechDetectionEnabled() ? 0.03F : 0.0F;
  373.  
  374. CircularFifoQueue<HttpAsrClient.AudioPacket> startOfSpeechQueue = new CircularFifoQueue(numPacketsToBufferStartOfSpeech);
  375. CircularFifoQueue<HttpAsrClient.AudioPacket> leadingSilenceQueue = new CircularFifoQueue(numPacketsToBufferLeadingSilence);
  376.  
  377. int startOfSpeechThresholdCounter = 0;
  378.  
  379. boolean startOfSpeechDetected = false;
  380. boolean queryCommandsSent = false;
  381.  
  382. HttpAsrClient.this.write("Listening...");
  383. while ((HttpAsrClient.this.listening) && (!HttpAsrClient.this.queryFailed))
  384. {
  385. byte[] buffer = new byte[bufferSize];
  386. int count = HttpAsrClient.this.line.read(buffer, 0, buffer.length);
  387.  
  388. float audioLevel = HttpAsrClient.this.calculateLevel(format, buffer, 0, bufferSize - count);
  389. if (HttpAsrClient.this.verbose) {
  390. HttpAsrClient.this.write(" [audio level: " + audioLevel + "] ");
  391. }
  392. if (count > 0) {
  393. if (!startOfSpeechDetected)
  394. {
  395. if (audioLevel < audioEnergyThreshold)
  396. {
  397. leadingSilenceQueue.add(new HttpAsrClient.AudioPacket(HttpAsrClient.this, buffer, count));
  398.  
  399. startOfSpeechQueue.clear();
  400. startOfSpeechThresholdCounter = 0;
  401. }
  402. else
  403. {
  404. startOfSpeechThresholdCounter++;
  405. if (startOfSpeechThresholdCounter == numPacketsToBufferStartOfSpeech)
  406. {
  407. HttpAsrClient.this.write("Start of speech detected!");
  408. startOfSpeechDetected = true;
  409.  
  410. HttpAsrClient.this.write("Sending NCS Query commands...");
  411. boundary = HttpAsrClient.this.sendAsrQueryCommands();
  412. queryCommandsSent = true;
  413.  
  414. HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_begin");
  415.  
  416. Iterator<HttpAsrClient.AudioPacket> itr1 = leadingSilenceQueue.iterator();
  417. while (itr1.hasNext())
  418. {
  419. HttpAsrClient.AudioPacket packet = (HttpAsrClient.AudioPacket)itr1.next();
  420. FileOutput.write(packet.audio, 0, packet.size);
  421. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(packet.audio, 0, packet.size).array(), boundary);
  422. }
  423. Iterator<HttpAsrClient.AudioPacket> itr2 = startOfSpeechQueue.iterator();
  424. while (itr2.hasNext())
  425. {
  426. HttpAsrClient.AudioPacket packet = (HttpAsrClient.AudioPacket)itr2.next();
  427. FileOutput.write(packet.audio, 0, packet.size);
  428. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(packet.audio, 0, packet.size).array(), boundary);
  429. }
  430. FileOutput.write(buffer, 0, count);
  431. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(buffer, 0, count).array(), boundary);
  432. }
  433. else
  434. {
  435. startOfSpeechQueue.add(new HttpAsrClient.AudioPacket(HttpAsrClient.this, buffer, count));
  436. }
  437. }
  438. }
  439. else
  440. {
  441. ByteBuffer bbuf = ByteBuffer.wrap(buffer, 0, count);
  442. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, bbuf.array(), boundary);
  443. FileOutput.write(buffer, 0, count);
  444. }
  445. }
  446. }
  447. HttpAsrClient.this.write();
  448. if (!HttpAsrClient.this.stopRequested) {
  449. HttpAsrClient.this.closeInputLine();
  450. }
  451. HttpAsrClient.this.stopRequested = false;
  452.  
  453. HttpAsrClient.this.write("Done listening");
  454. if (HttpAsrClient.this.verbose) {
  455. HttpAsrClient.this.write("Done saving audio to file");
  456. }
  457. FileOutput.close();
  458. if (!queryCommandsSent)
  459. {
  460. fout.delete();
  461. HttpAsrClient.this.write("No speech detected - request not sent.");
  462. HttpAsrClient.this.out.close();
  463. HttpAsrClient.this.s.close();
  464. }
  465. else
  466. {
  467. HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_end");
  468. HttpAsrClient.this._logData.audioDuration = ((float)(fout.length() / bufferSize) / 50.0F);
  469. HttpAsrClient.this.sendTerminatingChunk(HttpAsrClient.this.out, boundary);
  470. HttpAsrClient.this.write("Processing ...");
  471. HttpAsrClient.wait4TerminateSignal(HttpAsrClient.this.getTxnTimeout());
  472. }
  473. }
  474. catch (IOException e)
  475. {
  476. HttpAsrClient.this.write("I/O problems: " + e);
  477. System.exit(-1);
  478. }
  479. }
  480. };
  481. Thread captureThread = new Thread(runner);
  482. captureThread.start();
  483. }
  484. catch (LineUnavailableException e)
  485. {
  486. write("Line unavailable: " + e);
  487. System.exit(-2);
  488. }
  489. }
  490.  
  491. protected void streamAudioFile(final File audioFile)
  492. {
  493. AudioFormat format = getFormat();
  494. final int bufferSize = (int)(format.getSampleRate() * format.getFrameSize() / 50.0F);
  495.  
  496. Runnable runner = new Runnable()
  497. {
  498. public void run()
  499. {
  500. String boundary = HttpAsrClient.this.sendAsrQueryCommands();
  501. try
  502. {
  503. HttpAsrClient.this.write("Streaming audio...");
  504. HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_begin");
  505.  
  506. int audioDuration = 0;
  507. byte[] audio = FileLoader.load(audioFile);
  508. AudioChopper chopper = AudioChopperFactory.getAudioChopper(HttpAsrClient.this._requestData.IN_CODEC, audio);
  509. if (chopper != null)
  510. {
  511. HttpAsrClient.this.write("Sending");
  512. while ((chopper.hasMoreFrame()) && (!HttpAsrClient.this.queryFailed) && (!HttpAsrClient.this.queryCancelled))
  513. {
  514. byte[] buffer = chopper.getNextFrame();
  515. int wait = chopper.getFrameLengthInMs();
  516. synchronized (this)
  517. {
  518. wait(wait);
  519. }
  520. audioDuration += wait;
  521. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, buffer, boundary);
  522. }
  523. }
  524. HttpAsrClient.this.write("WARNING - Real-time Audio Streaming Emulation Not Available for Audio File Type: " + HttpAsrClient.this._requestData.IN_CODEC);
  525. FileInputStream inputStream = new FileInputStream(audioFile);
  526. int nRead = 0;
  527. while ((nRead != -1) && (!HttpAsrClient.this.queryFailed) && (!HttpAsrClient.this.queryCancelled))
  528. {
  529. byte[] buffer = new byte[bufferSize];
  530. nRead = inputStream.read(buffer, 0, bufferSize);
  531. if (nRead > 0)
  532. {
  533. ByteBuffer bbuf = ByteBuffer.wrap(buffer, 0, nRead);
  534. HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, bbuf.array(), boundary);
  535. synchronized (this)
  536. {
  537. wait(20L);
  538. }
  539. }
  540. }
  541. inputStream.close();
  542.  
  543. HttpAsrClient.this.write();
  544.  
  545. HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_end");
  546. if (audioDuration == 0) {
  547. HttpAsrClient.this._logData.audioDuration = ((float)(audioFile.length() / bufferSize) / 50.0F);
  548. } else {
  549. HttpAsrClient.this._logData.audioDuration = audioDuration;
  550. }
  551. HttpAsrClient.this.sendTerminatingChunk(HttpAsrClient.this.out, boundary);
  552.  
  553. HttpAsrClient.this.write("\nProcessing ...");
  554. }
  555. catch (IOException|InterruptedException e)
  556. {
  557. HttpAsrClient.this.write("I/O problems: " + e);
  558. System.exit(-1);
  559. }
  560. catch (Exception e)
  561. {
  562. HttpAsrClient.this.write("Exception: " + e);
  563. System.exit(-1);
  564. }
  565. finally
  566. {
  567. if (HttpAsrClient.this.latch != null) {
  568. HttpAsrClient.this.latch.countDown();
  569. }
  570. }
  571. }
  572. };
  573. Thread fileStreamThread = new Thread(runner);
  574. fileStreamThread.start();
  575. }
  576.  
  577. protected float calculateLevel(AudioFormat format, byte[] buffer, int readPoint, int leftOver)
  578. {
  579. int max = 0;
  580. float energyLevel = 0.0F;
  581.  
  582. boolean use16Bit = format.getSampleSizeInBits() == 16;
  583. boolean signed = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED;
  584. boolean bigEndian = format.isBigEndian();
  585. if (use16Bit) {
  586. for (int i = readPoint; i < buffer.length - leftOver; i += 2)
  587. {
  588. int value = 0;
  589.  
  590. int hiByte = bigEndian ? buffer[i] : buffer[(i + 1)];
  591. int loByte = bigEndian ? buffer[(i + 1)] : buffer[i];
  592. if (signed)
  593. {
  594. short shortVal = (short)hiByte;
  595. shortVal = (short)(shortVal << 8 | (byte)loByte);
  596. value = shortVal;
  597. }
  598. else
  599. {
  600. value = hiByte << 8 | loByte;
  601. }
  602. max = Math.max(max, value);
  603. }
  604. } else {
  605. for (int i = readPoint; i < buffer.length - leftOver; i++)
  606. {
  607. int value = 0;
  608. if (signed)
  609. {
  610. value = buffer[i];
  611. }
  612. else
  613. {
  614. short shortVal = 0;
  615. shortVal = (short)(shortVal | buffer[i]);
  616. value = shortVal;
  617. }
  618. max = Math.max(max, value);
  619. }
  620. }
  621. if (signed)
  622. {
  623. if (use16Bit) {
  624. energyLevel = max / 32767.0F;
  625. } else {
  626. energyLevel = max / 127.0F;
  627. }
  628. }
  629. else if (use16Bit) {
  630. energyLevel = max / 65535.0F;
  631. } else {
  632. energyLevel = max / 255.0F;
  633. }
  634. return energyLevel;
  635. }
  636.  
  637. protected AudioFormat getFormat()
  638. {
  639. float sampleRate = 16000.0F;
  640. int sampleSizeInBits = 16;
  641. int channels = 1;
  642. boolean signed = true;
  643. boolean bigEndian = false;
  644. return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
  645. }
  646.  
  647. protected Socket connectToServer()
  648. {
  649. try
  650. {
  651. if ((this.s == null) || (this.s.isClosed()))
  652. {
  653. write("Creating socket connection");
  654. this.s = SocketFactory.createSocket(this._host, this._port, this._useTLS, this._requireTrustedRootCert);
  655. this.transactionLatency.setMarker("connected");
  656. this._logData.timeToConnect = this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected");
  657. write("Time to establish socket connection: " + this._logData.timeToConnect);
  658.  
  659. this.boundary = UUID.randomUUID().toString().replaceAll("-", "");
  660. this.out = this.s.getOutputStream();
  661.  
  662. sendHeaders(this.out, this._host, this.boundary);
  663. }
  664. else
  665. {
  666. write("Socket connection already created");
  667. }
  668. return this.s;
  669. }
  670. catch (Exception e)
  671. {
  672. e.printStackTrace();
  673. System.exit(-2);
  674. }
  675. return null;
  676. }
  677.  
  678. protected String sendAsrQueryCommands()
  679. {
  680. Socket s = connectToServer();
  681.  
  682. Thread t = new Thread(new Runnable()
  683. {
  684. Socket _s;
  685.  
  686. public void run()
  687. {
  688. try
  689. {
  690. HttpAsrClient.this.processResponse(this._s);
  691. }
  692. catch (UnknownHostException e)
  693. {
  694. e.printStackTrace();
  695. }
  696. catch (IOException e)
  697. {
  698. e.printStackTrace();
  699. }
  700. catch (JSONException e1)
  701. {
  702. e1.printStackTrace();
  703. }
  704. }
  705. });
  706. t.start();
  707.  
  708. this.transactionLatency.setMarker("query_begin");
  709.  
  710. sendRequestData(this.out, this._appId, this._appKey, this._userID, this._requestData.getApplicationSessionID(), this._requestData.getUtteranceNumber(), this.boundary);
  711.  
  712. sendDictParameter(this.out, this.boundary);
  713.  
  714. this.transactionLatency.setMarker("query_complete");
  715.  
  716. return this.boundary;
  717. }
  718.  
  719. protected void sendHeaders(OutputStream out, String host, String boundary)
  720. {
  721. try
  722. {
  723. StringBuilder sb = new StringBuilder();
  724. sb.append("POST /NmspServlet/ HTTP/1.1\r\n");
  725. sb.append("Host: " + host + "\r\n");
  726. sb.append("Connection: Keep-Alive\r\n");
  727. sb.append("Keep-Alive: timeout=100\r\n");
  728. sb.append("User-Agent: Nuance Java Sample HTTP App 1.0\r\n");
  729. sb.append("Transfer-Encoding: chunked\r\n");
  730. sb.append("Content-Type: multipart/form-data; boundary=" + boundary + "\r\n");
  731. sb.append("\r\n");
  732.  
  733. out.write(sb.toString().getBytes());
  734. out.flush();
  735.  
  736. Chunk chunk = new Chunk();
  737. chunk.append(sb.toString());
  738. printDataSent(chunk);
  739. }
  740. catch (IOException e)
  741. {
  742. e.printStackTrace();
  743. }
  744. }
  745.  
  746. protected void sendRequestData(OutputStream out, String appId, String appKey, String uId, String appSessionId, int uttNumber, String boundary)
  747. {
  748. try
  749. {
  750. JSONObject json = new JSONObject();
  751. JSONObject cmdDict = new JSONObject();
  752.  
  753. json.put("appId", appId);
  754. json.put("appKey", appKey);
  755. json.put("uId", uId);
  756. json.put("inCodec", this._requestData.IN_CODEC);
  757. json.put("outCodec", this._requestData.OUT_CODEC);
  758. json.put("cmdName", "NMDP_ASR_CMD");
  759. json.put("appName", "Nuance Java Sample HTTP App");
  760. json.put("appVersion", "1.0");
  761. json.put("language", this._requestData.LANGUAGE);
  762.  
  763. json.put("carrier", "unknown");
  764. json.put("deviceModel", this._requestData.DEVICE_MODEL);
  765.  
  766. cmdDict.put("dictation_type", this._requestData.DICTATION_TYPE);
  767. cmdDict.put("dictation_language", this._requestData.LANGUAGE);
  768. cmdDict.put("locale", "USA");
  769. cmdDict.put("application_name", "Nuance Java Sample HTTP App");
  770. cmdDict.put("organization_id", "NUANCE");
  771. cmdDict.put("phone_OS", this._requestData.PHONE_OS);
  772. cmdDict.put("phone_network", "wifi");
  773. cmdDict.put("audio_source", "SpeakerAndMicrophone");
  774. cmdDict.put("location", "off");
  775. cmdDict.put("application_session_id", appSessionId);
  776. cmdDict.put("utterance_number", uttNumber);
  777. cmdDict.put("ui_language", "en");
  778. cmdDict.put("phone_submodel", this._requestData.PHONE_SUBMODEL);
  779. cmdDict.put("application_state_id", "0");
  780.  
  781. json.put("cmdDict", cmdDict);
  782.  
  783. Chunk chunk = new Chunk();
  784. chunk.append("--" + boundary + "\r\n");
  785. chunk.append("Content-Disposition: form-data; name=\"RequestData\"\r\n");
  786. chunk.append("Content-Type: application/json; charset=utf-8\r\n");
  787. chunk.append("Content-Transfer-Encoding: 8bit\r\n");
  788. chunk.append("\r\n");
  789. chunk.append(json.toString(2));
  790. chunk.append("\r\n");
  791. chunk.writeTo(out);
  792.  
  793. printDataSent(chunk);
  794. }
  795. catch (JSONException e)
  796. {
  797. e.printStackTrace();
  798. }
  799. catch (IOException e)
  800. {
  801. e.printStackTrace();
  802. }
  803. }
  804.  
  805. protected void sendDictParameter(OutputStream out, String boundary)
  806. {
  807. try
  808. {
  809. JSONObject json = new JSONObject();
  810. json.put("start", 0);
  811. json.put("end", 0);
  812. json.put("text", "");
  813. json.put("nbest_text_results", 1);
  814. if (this._streamingResults) {
  815. json.put("intermediate_response_mode", "NoUtteranceDetectionWithPartialRecognition");
  816. }
  817. if (this._profanityFilteringEnabled) {
  818. json.put("enable_profanity_filtering", 1);
  819. }
  820. Chunk chunk = new Chunk();
  821. chunk.append("--" + boundary + "\r\n");
  822. chunk.append("Content-Disposition: form-data; name=\"DictParameter\";paramName=\"REQUEST_INFO\"\r\n");
  823. chunk.append("Content-Type: application/json; charset=utf-8\r\n");
  824. chunk.append("Content-Transfer-Encoding: 8bit\r\n");
  825. chunk.append("\r\n");
  826. chunk.append(json.toString(2));
  827. chunk.append("\r\n");
  828. chunk.writeTo(out);
  829.  
  830. printDataSent(chunk);
  831. }
  832. catch (JSONException e)
  833. {
  834. e.printStackTrace();
  835. }
  836. catch (IOException e)
  837. {
  838. e.printStackTrace();
  839. }
  840. }
  841.  
  842. protected void sendAudioChunk(OutputStream out, byte[] audioData, String boundary)
  843. {
  844. try
  845. {
  846. Chunk chunk = new Chunk();
  847. chunk.append("--" + boundary + "\r\n");
  848. chunk.append("Content-Disposition: form-data; name=\"ConcludingAudioParameter\";paramName=\"AUDIO_INFO\"\r\n");
  849. chunk.append("Content-Type: audio/x-wav;codec=pcm;bit=16;rate=16000\r\n");
  850. chunk.append("Content-Transfer-Encoding: binary\r\n");
  851. chunk.append("\r\n");
  852. chunk.append(audioData);
  853. chunk.append("\r\n");
  854. chunk.writeTo(out);
  855. }
  856. catch (IOException e)
  857. {
  858. e.printStackTrace();
  859. this.queryFailed = true;
  860. }
  861. }
  862.  
  863. protected void sendUserProfileResetRequestData(OutputStream out, String boundary)
  864. {
  865. try
  866. {
  867. JSONObject json = new JSONObject();
  868. JSONObject cmdDict = new JSONObject();
  869.  
  870. json.put("appId", this._appId);
  871. json.put("appKey", this._appKey);
  872. json.put("uId", this._userID);
  873. json.put("inCodec", this._requestData.IN_CODEC);
  874. json.put("outCodec", this._requestData.OUT_CODEC);
  875. json.put("cmdName", "NVC_RESET_USER_PROFILE_CMD");
  876. json.put("appName", "Nuance Java Sample HTTP App");
  877. json.put("appVersion", "1.0");
  878. json.put("language", this._requestData.LANGUAGE);
  879.  
  880. json.put("carrier", "unknown");
  881. json.put("deviceModel", this._requestData.DEVICE_MODEL);
  882.  
  883. cmdDict.put("dictation_language", this._requestData.LANGUAGE);
  884. cmdDict.put("locale", "USA");
  885. cmdDict.put("application_name", "Nuance Java Sample HTTP App");
  886. cmdDict.put("organization_id", "NUANCE");
  887. cmdDict.put("phone_OS", this._requestData.PHONE_OS);
  888. cmdDict.put("phone_network", "wifi");
  889. cmdDict.put("audio_source", "SpeakerAndMicrophone");
  890. cmdDict.put("location", "off");
  891. cmdDict.put("ui_language", "en");
  892. cmdDict.put("phone_submodel", this._requestData.PHONE_SUBMODEL);
  893. cmdDict.put("application_state_id", "0");
  894.  
  895. json.put("cmdDict", cmdDict);
  896.  
  897. Chunk chunk = new Chunk();
  898. chunk.append("--" + boundary + "\r\n");
  899. chunk.append("Content-Disposition: form-data; name=\"RequestData\"\r\n");
  900. chunk.append("Content-Type: application/json; charset=utf-8\r\n");
  901. chunk.append("Content-Transfer-Encoding: 8bit\r\n");
  902. chunk.append("\r\n");
  903. chunk.append(json.toString(2));
  904. chunk.append("\r\n");
  905. chunk.writeTo(out);
  906.  
  907. printDataSent(chunk);
  908. }
  909. catch (JSONException e)
  910. {
  911. e.printStackTrace();
  912. }
  913. catch (IOException e)
  914. {
  915. e.printStackTrace();
  916. }
  917. }
  918.  
  919. protected void sendTerminatingChunk(OutputStream out, String boundary)
  920. {
  921. try
  922. {
  923. Chunk chunk = new Chunk();
  924. chunk.append("--" + boundary + "--\r\n");
  925. chunk.writeTo(out);
  926.  
  927. Chunk terminatingChunk = new Chunk();
  928. terminatingChunk.writeTo(out);
  929.  
  930. printDataSent(chunk);
  931. write("Sent terminating chunk.");
  932. }
  933. catch (IOException e)
  934. {
  935. e.printStackTrace();
  936. }
  937. }
  938.  
  939. protected void processResponse(Socket s)
  940. throws IOException, JSONException
  941. {
  942. BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  943. ResponseParser rp = new ResponseParser();
  944. try
  945. {
  946. boolean firstResponse = true;
  947. boolean headersParsed = false;
  948. boolean successfulStatusCode = true;
  949. Map<String, String> headers = new HashMap();
  950.  
  951. String boundary = null;
  952. String t;
  953. while ((t = br.readLine()) != null) {
  954. if (firstResponse)
  955. {
  956. this.transactionLatency.setMarker("initial_response");
  957. write();
  958. if (this._streamingResults)
  959. {
  960. write("Time from first audio packet to first reponse: " + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "initial_response") + " seconds");
  961. this._logData.timeToFirstResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "initial_response");
  962. }
  963. else
  964. {
  965. write("Time from last audio packet to first reponse: " + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "initial_response") + " seconds");
  966. this._logData.timeToFirstResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "initial_response");
  967. }
  968. firstResponse = false;
  969. if (!t.contains("200"))
  970. {
  971. successfulStatusCode = false;
  972. this.queryFailed = true;
  973. }
  974. write("Status Line: " + t);
  975. }
  976. else if ((!firstResponse) && (!headersParsed))
  977. {
  978. headers = rp.parseHeaders(br);
  979. headersParsed = true;
  980.  
  981. write("\r\nHeaders: ");
  982. for (Map.Entry<String, String> entry : headers.entrySet()) {
  983. write("\t" + (String)entry.getKey() + ": " + (String)entry.getValue());
  984. }
  985. write();
  986. if (headers.containsKey("nuance-sessionid")) {
  987. this._logData.sessionId = ((String)headers.get("nuance-sessionid"));
  988. }
  989. if (headers.containsKey("content-type"))
  990. {
  991. String contentType = (String)headers.get("content-type");
  992. if (contentType.contains("boundary="))
  993. {
  994. String[] arr = contentType.split("=", 2);
  995. if ((arr != null) && (arr.length == 2)) {
  996. boundary = arr[1];
  997. }
  998. }
  999. }
  1000. }
  1001. else if ((headersParsed) && (!successfulStatusCode))
  1002. {
  1003. write(t);
  1004. if (t.equals("0")) {
  1005. if (this.transactionLatency.getMarker("final_response") == -1L)
  1006. {
  1007. this.transactionLatency.setMarker("final_response");
  1008. this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
  1009. }
  1010. }
  1011. }
  1012. else if ((headersParsed) && (successfulStatusCode))
  1013. {
  1014. Map<String, String> bodyParts;
  1015. if ((boundary == null) && (headers.containsKey("content-disposition")))
  1016. {
  1017. Map<String, String> bodyParts = new HashMap();
  1018. bodyParts.putAll(headers);
  1019. bodyParts.put("json_1", t);
  1020. }
  1021. else
  1022. {
  1023. bodyParts = rp.parseBodyPart(br, boundary);
  1024. }
  1025. if (t.length() > 2)
  1026. {
  1027. String[] arr = t.split(":", 2);
  1028. if (((arr != null ? 1 : 0) & (arr.length == 2 ? 1 : 0)) != 0) {
  1029. bodyParts.put(arr[0].trim().toLowerCase(), arr[1].trim().toLowerCase());
  1030. }
  1031. }
  1032. if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryerror")))
  1033. {
  1034. this.queryFailed = true;
  1035. write("Server Response: ");
  1036. write((String)bodyParts.get("json_1"));
  1037. }
  1038. else if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryretry")))
  1039. {
  1040. this.queryFailed = true;
  1041. write("Server Response: ");
  1042. write((String)bodyParts.get("json_1"));
  1043. }
  1044. else if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryresult")))
  1045. {
  1046. String json_1 = bodyParts.containsKey("json_1") ? (String)bodyParts.get("json_1") : null;
  1047. if ((json_1 == null) || (!isJSON(json_1))) {
  1048. continue;
  1049. }
  1050. JSONObject json = new JSONObject(json_1);
  1051. if ((json.has("final_response")) && (json.getInt("final_response") == 0))
  1052. {
  1053. if (json.has("transcriptions")) {
  1054. write("Streaming Response: " + json.getJSONArray("transcriptions").getString(0));
  1055. } else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
  1056. (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
  1057. (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0) &&
  1058. (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").getJSONObject(0).has("text"))) {
  1059. write("Streaming Response [text]: " + json.getJSONObject("appserver_results")
  1060. .getJSONObject("payload")
  1061. .getJSONArray("actions")
  1062. .getJSONObject(0)
  1063. .getString("text"));
  1064. } else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
  1065. (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
  1066. (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0) &&
  1067. (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").getJSONObject(0).has("nbest_text"))) {
  1068. write("Streaming Response [nbest text]: " + json.getJSONObject("appserver_results")
  1069. .getJSONObject("payload")
  1070. .getJSONArray("actions")
  1071. .getJSONObject(0)
  1072. .getJSONObject("nbest_text")
  1073. .getJSONArray("transcriptions")
  1074. .getString(0));
  1075. } else {
  1076. write(json.toString(4));
  1077. }
  1078. }
  1079. else if ((json.has("final_response")) && (json.getInt("final_response") == 1))
  1080. {
  1081. if (json.has("transcriptions"))
  1082. {
  1083. write("Final Response: " + json.getJSONArray("transcriptions").getString(0));
  1084. write("Final JSON Response: " + json.toString(4));
  1085. }
  1086. else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
  1087. (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
  1088. (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0))
  1089. {
  1090. JSONArray actions = json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions");
  1091. for (int i = 0; i < actions.length(); i++)
  1092. {
  1093. JSONObject action = actions.getJSONObject(i);
  1094. if ((action.has("type")) && (action.getString("type").equalsIgnoreCase("nlu_results"))) {
  1095. write("Final Response: " + action.getJSONObject("Input")
  1096. .getJSONArray("Interpretations")
  1097. .getString(0));
  1098. }
  1099. }
  1100. write("Final JSON Response: " + json.toString(4));
  1101. }
  1102. else
  1103. {
  1104. write("Unsupported Response Format: " + json.toString(4));
  1105. }
  1106. }
  1107. else {
  1108. write("Unknown Response: " + json.toString(4));
  1109. }
  1110. }
  1111. if ((bodyParts.containsKey("size")) && (Integer.valueOf((String)bodyParts.get("size")).intValue() == 0)) {
  1112. if (this.transactionLatency.getMarker("final_response") == -1L)
  1113. {
  1114. this.transactionLatency.setMarker("final_response");
  1115. this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. catch (SocketTimeoutException e)
  1122. {
  1123. write(e.getMessage());
  1124. }
  1125. finally
  1126. {
  1127. br.close();
  1128. if (this.transactionLatency.getMarker("final_response") == -1L)
  1129. {
  1130. this.transactionLatency.setMarker("final_response");
  1131. this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
  1132. }
  1133. printLineSeparator();
  1134. write("Done reading response...");
  1135.  
  1136. this.transactionLatency.setMarker("stop");
  1137. this._logData.totalTrxnDuration = this.transactionLatency.calculateDistanceBetweenMarkers("start", "stop");
  1138.  
  1139. printLineSeparator();
  1140. showLatencyMarkers();
  1141.  
  1142. this._logData.writeToFile();
  1143. synchronized (waitLock)
  1144. {
  1145. try
  1146. {
  1147. waitLock.notifyAll();
  1148. }
  1149. catch (IllegalMonitorStateException e)
  1150. {
  1151. write(e.getMessage());
  1152. }
  1153. }
  1154. }
  1155. }
  1156.  
  1157. protected boolean isJSON(String str)
  1158. {
  1159. try
  1160. {
  1161. JSONObject localJSONObject = new JSONObject(str);
  1162. }
  1163. catch (JSONException e)
  1164. {
  1165. return false;
  1166. }
  1167. return true;
  1168. }
  1169.  
  1170. protected boolean isNumeric(String str)
  1171. {
  1172. try
  1173. {
  1174. int i = Integer.parseInt(str, 16);
  1175. }
  1176. catch (NumberFormatException nfe)
  1177. {
  1178. return false;
  1179. }
  1180. return true;
  1181. }
  1182.  
  1183. protected void initialize()
  1184. {
  1185. this._userID = ((this._userID != null) && (!this._userID.isEmpty()) ? this._userID : UserIDManager.createUserIDManager().initUserID());
  1186. this._requestData.initApplicationSessionID();
  1187. this._requestData.resetUtteranceNumber();
  1188. }
  1189.  
  1190. private void write()
  1191. {
  1192. System.out.println();
  1193. }
  1194.  
  1195. private void write(String msg)
  1196. {
  1197. System.out.println(msg);
  1198. }
  1199.  
  1200. protected void showLatencyMarkers()
  1201. {
  1202. write("marker\t\t\t\t\tDuration Since T0\tDuration Since Last Marker");
  1203. write("[t0] start\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "start") + "sec\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "start") + "sec");
  1204. write("[t1] connected\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected") + "sec\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected") + "sec");
  1205. write("[t2] query_begin\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "query_begin") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("connected", "query_begin") + "sec");
  1206. write("[t3] query_complete\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "query_complete") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("query_begin", "query_complete") + "sec");
  1207.  
  1208. write("[t4] audio_streaming_begin\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "audio_streaming_begin") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("query_begin", "audio_streaming_begin") + "sec");
  1209. write("[t5] audio_streaming_end\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "audio_streaming_end") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "audio_streaming_end") + "sec");
  1210. if (this._streamingResults) {
  1211. write("[t6] initial_response\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "initial_response") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "initial_response") + "sec (from t4)");
  1212. } else {
  1213. write("[t6] initial_response\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "initial_response") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "initial_response") + "sec (from t5)");
  1214. }
  1215. if (this._streamingResults) {
  1216. write("[t7] final_response\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "final_response") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response") + "sec");
  1217. } else {
  1218. write("[t7] final_response\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "final_response") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("initial_response", "final_response") + "sec");
  1219. }
  1220. write("[t8] stop\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "stop") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("final_response", "stop") + "sec");
  1221. }
  1222.  
  1223. protected void printLineSeparator()
  1224. {
  1225. write();
  1226. write("---------------------------------");
  1227. write();
  1228. }
  1229.  
  1230. protected void printDataSent(Chunk chunk)
  1231. {
  1232. if (isVerbose())
  1233. {
  1234. write();
  1235. write("<<<<<< Sending >>>>>>");
  1236. try
  1237. {
  1238. chunk.writeTo(System.out);
  1239. }
  1240. catch (IOException e)
  1241. {
  1242. e.printStackTrace();
  1243. }
  1244. write();
  1245. }
  1246. }
  1247.  
  1248. private static void wait4TerminateSignal(int timeout)
  1249. {
  1250. synchronized (waitLock)
  1251. {
  1252. try
  1253. {
  1254. waitLock.wait(timeout);
  1255. }
  1256. catch (InterruptedException e)
  1257. {
  1258. System.out.println(e.getMessage());
  1259. }
  1260. }
  1261. }
  1262.  
  1263. public void start(String audioFilename)
  1264. {
  1265. start(audioFilename, "PCM_16_16K", true);
  1266. }
  1267.  
  1268. public void start(String audioPath, String codec, boolean streamingResults)
  1269. {
  1270. this._requestData.IN_CODEC = codec;
  1271. this._requestData.OUT_CODEC = codec;
  1272. this._streamingResults = streamingResults;
  1273.  
  1274. initialize();
  1275. printLineSeparator();
  1276. if (batchModeEnabled()) {
  1277. batchModeAsr(audioPath);
  1278. } else if (audioPath != null) {
  1279. batchModeAsr(audioPath);
  1280. } else {
  1281. monitorConsoleEvents();
  1282. }
  1283. }
  1284.  
  1285. protected void batchModeAsr(String path)
  1286. {
  1287. File dir = new File(path);
  1288. File[] directoryListing = dir.listFiles();
  1289. if (directoryListing != null)
  1290. {
  1291. for (File file : directoryListing)
  1292. {
  1293. this.transactionLatency.reset();
  1294. this.transactionLatency.setMarker("start");
  1295. write("Processing audio file: " + file.getAbsolutePath());
  1296. this.latch = new CountDownLatch(1);
  1297. streamAudioFile(file);
  1298. try
  1299. {
  1300. this.latch.await();
  1301. }
  1302. catch (InterruptedException e)
  1303. {
  1304. e.printStackTrace();
  1305. }
  1306. wait4TerminateSignal(getTxnTimeout());
  1307. }
  1308. }
  1309. else
  1310. {
  1311. File file = new File(path);
  1312. this.transactionLatency.reset();
  1313. this.transactionLatency.setMarker("start");
  1314. write("Processing audio file: " + file.getAbsolutePath());
  1315. this.latch = new CountDownLatch(1);
  1316. streamAudioFile(file);
  1317. try
  1318. {
  1319. this.latch.await();
  1320. }
  1321. catch (InterruptedException e)
  1322. {
  1323. e.printStackTrace();
  1324. }
  1325. wait4TerminateSignal(getTxnTimeout());
  1326. }
  1327. }
  1328.  
  1329. public void resetUserProfile()
  1330. {
  1331. initialize();
  1332.  
  1333. this.transactionLatency.setMarker("start");
  1334.  
  1335. Socket s = connectToServer();
  1336.  
  1337. this.transactionLatency.setMarker("query_begin");
  1338.  
  1339. sendUserProfileResetRequestData(this.out, this.boundary);
  1340.  
  1341. sendTerminatingChunk(this.out, this.boundary);
  1342. this.transactionLatency.setMarker("query_complete");
  1343. try
  1344. {
  1345. BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  1346. String t;
  1347. while ((t = br.readLine()) != null)
  1348. {
  1349. write(t);
  1350. if (t.equals("0")) {
  1351. return;
  1352. }
  1353. }
  1354. }
  1355. catch (Exception e)
  1356. {
  1357. write(e.getMessage());
  1358. }
  1359. }
  1360.  
  1361. public static void printUsage()
  1362. {
  1363. String path = HttpAsrClient.class.getProtectionDomain().getCodeSource().getLocation().getFile();
  1364. File f = new File(path);
  1365. String jar = f.getName();
  1366.  
  1367. System.out.println("\nUsage: java -jar " + jar + " -h host -n your_maid -a your_128_byte_string_app_key [OPTIONS]\n" + "\n" + "Required options:\n" + "\t-h host \n" + "\t-n nmaid \n" + "\t-a appkey \n" + "\n" + "Optional inputs:\n" + "\t-help (display this usage information)\n" + "\t-p port (default is 443)\n" + "\t-s use tls (default is true. specify false to disable tls)\n" + "\t-tr require trusted root certificate chain (default is true. specify false to not require a trusted root certificate chain.)\n" + "\t-t topic (default is nma_dm_main)\n" + "\t-l language code (default is eng-USA)\n" + "\t-c codec (default is PCM_16_16K)\n" + "\t-f audio file (only required if not using batch mode or console input)\n" + "\t-streaming enable/disable word-by-word streaming (default is true (enabled). specify false to disable. NOTE: requires special server-side configuration)\n" + "\t-pf enable/disable profanity filtering (default is false (disabled). specify true to enable. NOTE: requires special server-side configuration)\n" + "\t-sap saved audio path (default is present working directory. Specify a path to save captured audio to)\n" + "\t-bm batch mode (default is false. specify path to audio files to enable batch mode. NOTE: batch mode takes precedence over the -f option)\n" + "\t-vad voice activity detection (default is true. specify false to disable speech detection)\n" + "\t-rup reset user profile (specify this flag to reset the user's acoustic and language model profiles on the server)\n" + "\t-v verbose (default is false. specify true to increase verbosity level)\n");
  1368. }
  1369.  
  1370. public static void main(String[] args)
  1371. throws Exception
  1372. {
  1373. String host = "{Provide your hostname}";
  1374. String nmaid = "{Provide your NMAID}";
  1375. String appKey = "{Provide your 128-byte String App Key}";
  1376.  
  1377. String userID = null;
  1378. int port = 443;
  1379. boolean useTLS = true;
  1380. boolean requireTrustedRootCert = true;
  1381. String topic = "Dictation";
  1382. String langCode = "eng-USA";
  1383. String codec = "PCM_16_16K";
  1384. String audioPath = null;
  1385. boolean streamingResults = true;
  1386. boolean enableProfanityFiltering = false;
  1387. String savedAudioPath = null;
  1388. boolean batchMode = false;
  1389. boolean enableVAD = true;
  1390. boolean resetUserProfile = false;
  1391. boolean verbose = false;
  1392. for (int i = 0; i < args.length; i += 2)
  1393. {
  1394. if (args[i].equals("-help"))
  1395. {
  1396. printUsage();
  1397. return;
  1398. }
  1399. if (args[i].equals("-uid"))
  1400. {
  1401. userID = args[(i + 1)];
  1402. }
  1403. else if (args[i].equals("-h"))
  1404. {
  1405. host = args[(i + 1)];
  1406. }
  1407. else if (args[i].equals("-p"))
  1408. {
  1409. port = Integer.parseInt(args[(i + 1)]);
  1410. }
  1411. else if (args[i].equals("-s"))
  1412. {
  1413. useTLS = Boolean.parseBoolean(args[(i + 1)]);
  1414. }
  1415. else if (args[i].equals("-tr"))
  1416. {
  1417. requireTrustedRootCert = Boolean.parseBoolean(args[(i + 1)]);
  1418. }
  1419. else if (args[i].equals("-n"))
  1420. {
  1421. nmaid = args[(i + 1)];
  1422. }
  1423. else if (args[i].equals("-a"))
  1424. {
  1425. appKey = args[(i + 1)];
  1426. }
  1427. else if (args[i].equals("-t"))
  1428. {
  1429. topic = args[(i + 1)];
  1430. }
  1431. else if (args[i].equals("-l"))
  1432. {
  1433. langCode = args[(i + 1)];
  1434. }
  1435. else if (args[i].equals("-c"))
  1436. {
  1437. codec = args[(i + 1)];
  1438. }
  1439. else if (args[i].equals("-f"))
  1440. {
  1441. audioPath = args[(i + 1)];
  1442. }
  1443. else if (args[i].equals("-streaming"))
  1444. {
  1445. streamingResults = Boolean.parseBoolean(args[(i + 1)]);
  1446. }
  1447. else if (args[i].equals("-pf"))
  1448. {
  1449. enableProfanityFiltering = Boolean.parseBoolean(args[(i + 1)]);
  1450. }
  1451. else if (args[i].equalsIgnoreCase("-sap"))
  1452. {
  1453. savedAudioPath = args[(i + 1)];
  1454. }
  1455. else if (args[i].equalsIgnoreCase("-bm"))
  1456. {
  1457. batchMode = true;
  1458. audioPath = args[(i + 1)];
  1459. }
  1460. else if (args[i].equalsIgnoreCase("-vad"))
  1461. {
  1462. enableVAD = Boolean.parseBoolean(args[(i + 1)]);
  1463. }
  1464. else if (args[i].equalsIgnoreCase("-rup"))
  1465. {
  1466. resetUserProfile = true;
  1467. }
  1468. else if (args[i].equalsIgnoreCase("-v"))
  1469. {
  1470. verbose = Boolean.parseBoolean(args[(i + 1)]);
  1471. }
  1472. }
  1473. if ((host.equalsIgnoreCase("{Provide your hostname}")) ||
  1474. (nmaid.equalsIgnoreCase("{Provide your NMAID}")) ||
  1475. (appKey.equalsIgnoreCase("{Provide your 128-byte String App Key}")) ||
  1476. (host.length() == 0) ||
  1477. (nmaid.length() == 0) ||
  1478. (appKey.length() != 128))
  1479. {
  1480. printUsage();
  1481. System.exit(0);
  1482. }
  1483. IHttpAsrClient asrClient = new HttpAsrClient(host, port, useTLS, nmaid, appKey, topic, langCode, userID);
  1484. if (verbose) {
  1485. asrClient.enableVerbose();
  1486. }
  1487. if (!requireTrustedRootCert) {
  1488. asrClient.disableTrustedRootCert();
  1489. }
  1490. if (resetUserProfile)
  1491. {
  1492. asrClient.resetUserProfile();
  1493. System.exit(0);
  1494. }
  1495. if (batchMode) {
  1496. asrClient.enableBatchMode();
  1497. }
  1498. if (savedAudioPath != null) {
  1499. asrClient.setSavedAudioPath(savedAudioPath);
  1500. }
  1501. if (!enableVAD) {
  1502. asrClient.disableStartOfSpeechDetection();
  1503. }
  1504. if (enableProfanityFiltering) {
  1505. asrClient.enableProfanityFiltering();
  1506. }
  1507. asrClient.start(audioPath, codec, streamingResults);
  1508. }
  1509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement