Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nuance.http_2_0.asr;
- import com.nuance.samples.util.Chunk;
- import com.nuance.samples.util.FileLoader;
- import com.nuance.samples.util.LatencyMonitor;
- import com.nuance.samples.util.LogData;
- import com.nuance.samples.util.ResponseParser;
- import com.nuance.samples.util.SocketFactory;
- import com.nuance.samples.util.UserIDManager;
- import com.nuance.samples.util.audio.AudioChopper;
- import com.nuance.samples.util.audio.AudioChopperFactory;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.Socket;
- import java.net.SocketTimeoutException;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import java.security.CodeSource;
- import java.security.ProtectionDomain;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.UUID;
- import java.util.concurrent.CountDownLatch;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioFormat.Encoding;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.DataLine.Info;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.TargetDataLine;
- import org.apache.commons.collections4.queue.CircularFifoQueue;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class HttpAsrClient
- implements IHttpAsrClient
- {
- static final float MAX_8_BITS_SIGNED = 127.0F;
- static final float MAX_8_BITS_UNSIGNED = 255.0F;
- static final float MAX_16_BITS_SIGNED = 32767.0F;
- static final float MAX_16_BITS_UNSIGNED = 65535.0F;
- private String _host;
- private int _port;
- private String _appId;
- private String _appKey;
- private boolean _useTLS;
- class AudioPacket
- {
- byte[] audio;
- int size = 0;
- public AudioPacket(byte[] buf, int len)
- {
- this.audio = buf;
- this.size = len;
- }
- }
- private boolean _requireTrustedRootCert = true;
- private String _userID;
- private boolean _streamingResults = true;
- protected boolean _profanityFilteringEnabled = false;
- private TargetDataLine line = null;
- private boolean stopRequested = false;
- protected boolean batchMode = false;
- protected boolean startOfSpeechDetectionEnabled = true;
- protected boolean verbose = false;
- protected boolean listening = false;
- protected String audioPath = null;
- private Socket s = null;
- protected OutputStream out = null;
- protected String boundary = null;
- protected boolean queryFailed = false;
- protected boolean queryCancelled = false;
- protected LatencyMonitor transactionLatency = new LatencyMonitor();
- protected LogData _logData = new LogData();
- private static Object waitLock = new Object();
- protected int txnTimeout = 5000;
- private CountDownLatch latch;
- protected RequestData _requestData;
- protected class RequestData
- {
- static final String APP_NAME = "Nuance Java Sample HTTP App";
- static final String APP_VERSION = "1.0";
- static final String ORGANIZATION_ID = "NUANCE";
- public String IN_CODEC = "PCM_16_16K";
- public String OUT_CODEC = "PCM_16_16K";
- static final String AUDIO_SOURCE = "SpeakerAndMicrophone";
- static final String COMMAND_NAME = "NMDP_ASR_CMD";
- public String LANGUAGE = "eng-USA";
- public String DICTATION_TYPE = "Dictation";
- static final String UI_LANGUAGE = "en";
- static final String CARRIER = "unknown";
- static final String PHONE_NETWORK = "wifi";
- final String PHONE_OS = System.getProperty("os.name");
- final String DEVICE_MODEL = System.getProperty("os.arch");
- final String PHONE_SUBMODEL = System.getProperty("os.version");
- static final String LOCALE = "USA";
- static final String LOCATION = "off";
- static final String APPLICATION_STATE_ID = "0";
- private String _applicationSessionID = null;
- private int _utteranceNumber = 1;
- protected RequestData() {}
- public void clearApplicationSessionID()
- {
- this._applicationSessionID = null;
- }
- public String initApplicationSessionID()
- {
- this._applicationSessionID = UUID.randomUUID().toString();
- return this._applicationSessionID;
- }
- public String getApplicationSessionID()
- {
- return this._applicationSessionID;
- }
- public void resetUtteranceNumber()
- {
- this._utteranceNumber = 1;
- }
- public int incrementUtteranceNumber()
- {
- return this._utteranceNumber++;
- }
- public int getUtteranceNumber()
- {
- return this._utteranceNumber;
- }
- }
- public HttpAsrClient(String host, int port, String appId, String appKey, boolean useTLS)
- {
- this(host, port, useTLS, appId, appKey, "Dictation", "eng-USA", null);
- }
- public HttpAsrClient(String host, int port, boolean useTLS, String appId, String appKey, String topic, String langCode, String userID)
- {
- write("Host: " + host + ":" + port);
- this._host = host;
- this._port = port;
- this._appId = appId;
- this._appKey = appKey;
- this._useTLS = useTLS;
- this._userID = userID;
- this._requestData = new RequestData();
- this._requestData.LANGUAGE = langCode;
- this._requestData.DICTATION_TYPE = topic;
- }
- public void enableBatchMode()
- {
- this.batchMode = true;
- }
- public void disableBatchMode()
- {
- this.batchMode = false;
- }
- public boolean batchModeEnabled()
- {
- return this.batchMode;
- }
- public void setSavedAudioPath(String val)
- {
- this.audioPath = val;
- }
- public String getSavedAudioPath()
- {
- return this.audioPath;
- }
- public void enableStartOfSpeechDetection()
- {
- this.startOfSpeechDetectionEnabled = true;
- }
- public void disableStartOfSpeechDetection()
- {
- this.startOfSpeechDetectionEnabled = false;
- }
- public boolean isStartOfSpeechDetectionEnabled()
- {
- return this.startOfSpeechDetectionEnabled;
- }
- public void enableProfanityFiltering()
- {
- this._profanityFilteringEnabled = true;
- }
- public void disableProfanityFiltering()
- {
- this._profanityFilteringEnabled = false;
- }
- public boolean isProfanityFilteringEnabled()
- {
- return this._profanityFilteringEnabled;
- }
- public void setTxnTimeout(int val)
- {
- this.txnTimeout = val;
- }
- public int getTxnTimeout()
- {
- return this.txnTimeout;
- }
- public void enableTrustedRootCert()
- {
- this._requireTrustedRootCert = true;
- }
- public void disableTrustedRootCert()
- {
- this._requireTrustedRootCert = false;
- }
- public void enableVerbose()
- {
- this.verbose = true;
- }
- public void disableVerbose()
- {
- this.verbose = false;
- }
- public boolean isVerbose()
- {
- return this.verbose;
- }
- protected void shutdown()
- {
- this.listening = false;
- if (this.verbose) {
- write("shutting down...");
- }
- System.exit(0);
- }
- protected void toggleListening()
- {
- if (this.listening) {
- stopListening();
- } else {
- startListening();
- }
- }
- protected void startListening()
- {
- this.transactionLatency.reset();
- this.transactionLatency.setMarker("start");
- connectToServer();
- captureAudio();
- }
- protected void stopListening()
- {
- this.listening = false;
- if (!this.stopRequested) {
- closeInputLine();
- }
- }
- private void closeInputLine()
- {
- this.stopRequested = true;
- if ((this.line.isActive()) || (this.line.isRunning())) {
- this.line.stop();
- }
- if (this.line.isOpen()) {
- this.line.close();
- }
- }
- public void monitorConsoleEvents()
- {
- write("Press enter to start capturing audio. (q <enter> to quit)");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- for (;;)
- {
- String s = null;
- try
- {
- s = br.readLine();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- if ((s != null) && (s.equalsIgnoreCase("q"))) {
- shutdown();
- }
- if ((s != null) && (s.length() == 0)) {
- toggleListening();
- }
- }
- }
- protected void captureAudio()
- {
- try
- {
- final AudioFormat format = getFormat();
- DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
- if (this.verbose) {
- write("Audio Data line Info: " + info);
- }
- this.line = ((TargetDataLine)AudioSystem.getLine(info));
- this.line.open(format);
- this.line.start();
- final int bufferSize = (int)(format.getSampleRate() * format.getFrameSize() / 50.0F);
- write("bufferSize: " + bufferSize);
- Runnable runner = new Runnable()
- {
- public void run()
- {
- HttpAsrClient.this.listening = true;
- try
- {
- File dir = HttpAsrClient.this.audioPath == null ? new File(".") : new File(HttpAsrClient.this.audioPath);
- long unixTime = System.currentTimeMillis() / 1000L;
- File fout = new File(dir.getCanonicalPath() + File.separator + "audio-" + unixTime + ".pcm");
- dir = new File(fout.getParent());
- if (HttpAsrClient.this.verbose) {
- HttpAsrClient.this.write("Writing to " + fout.getCanonicalPath());
- }
- if (!fout.exists())
- {
- dir.mkdirs();
- fout.createNewFile();
- }
- String boundary = null;
- OutputStream FileOutput = new FileOutputStream(fout);
- int numPacketsToBufferStartOfSpeech = 7;
- int numPacketsToBufferLeadingSilence = 12;
- float audioEnergyThreshold = HttpAsrClient.this.isStartOfSpeechDetectionEnabled() ? 0.03F : 0.0F;
- CircularFifoQueue<HttpAsrClient.AudioPacket> startOfSpeechQueue = new CircularFifoQueue(numPacketsToBufferStartOfSpeech);
- CircularFifoQueue<HttpAsrClient.AudioPacket> leadingSilenceQueue = new CircularFifoQueue(numPacketsToBufferLeadingSilence);
- int startOfSpeechThresholdCounter = 0;
- boolean startOfSpeechDetected = false;
- boolean queryCommandsSent = false;
- HttpAsrClient.this.write("Listening...");
- while ((HttpAsrClient.this.listening) && (!HttpAsrClient.this.queryFailed))
- {
- byte[] buffer = new byte[bufferSize];
- int count = HttpAsrClient.this.line.read(buffer, 0, buffer.length);
- float audioLevel = HttpAsrClient.this.calculateLevel(format, buffer, 0, bufferSize - count);
- if (HttpAsrClient.this.verbose) {
- HttpAsrClient.this.write(" [audio level: " + audioLevel + "] ");
- }
- if (count > 0) {
- if (!startOfSpeechDetected)
- {
- if (audioLevel < audioEnergyThreshold)
- {
- leadingSilenceQueue.add(new HttpAsrClient.AudioPacket(HttpAsrClient.this, buffer, count));
- startOfSpeechQueue.clear();
- startOfSpeechThresholdCounter = 0;
- }
- else
- {
- startOfSpeechThresholdCounter++;
- if (startOfSpeechThresholdCounter == numPacketsToBufferStartOfSpeech)
- {
- HttpAsrClient.this.write("Start of speech detected!");
- startOfSpeechDetected = true;
- HttpAsrClient.this.write("Sending NCS Query commands...");
- boundary = HttpAsrClient.this.sendAsrQueryCommands();
- queryCommandsSent = true;
- HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_begin");
- Iterator<HttpAsrClient.AudioPacket> itr1 = leadingSilenceQueue.iterator();
- while (itr1.hasNext())
- {
- HttpAsrClient.AudioPacket packet = (HttpAsrClient.AudioPacket)itr1.next();
- FileOutput.write(packet.audio, 0, packet.size);
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(packet.audio, 0, packet.size).array(), boundary);
- }
- Iterator<HttpAsrClient.AudioPacket> itr2 = startOfSpeechQueue.iterator();
- while (itr2.hasNext())
- {
- HttpAsrClient.AudioPacket packet = (HttpAsrClient.AudioPacket)itr2.next();
- FileOutput.write(packet.audio, 0, packet.size);
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(packet.audio, 0, packet.size).array(), boundary);
- }
- FileOutput.write(buffer, 0, count);
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, ByteBuffer.wrap(buffer, 0, count).array(), boundary);
- }
- else
- {
- startOfSpeechQueue.add(new HttpAsrClient.AudioPacket(HttpAsrClient.this, buffer, count));
- }
- }
- }
- else
- {
- ByteBuffer bbuf = ByteBuffer.wrap(buffer, 0, count);
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, bbuf.array(), boundary);
- FileOutput.write(buffer, 0, count);
- }
- }
- }
- HttpAsrClient.this.write();
- if (!HttpAsrClient.this.stopRequested) {
- HttpAsrClient.this.closeInputLine();
- }
- HttpAsrClient.this.stopRequested = false;
- HttpAsrClient.this.write("Done listening");
- if (HttpAsrClient.this.verbose) {
- HttpAsrClient.this.write("Done saving audio to file");
- }
- FileOutput.close();
- if (!queryCommandsSent)
- {
- fout.delete();
- HttpAsrClient.this.write("No speech detected - request not sent.");
- HttpAsrClient.this.out.close();
- HttpAsrClient.this.s.close();
- }
- else
- {
- HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_end");
- HttpAsrClient.this._logData.audioDuration = ((float)(fout.length() / bufferSize) / 50.0F);
- HttpAsrClient.this.sendTerminatingChunk(HttpAsrClient.this.out, boundary);
- HttpAsrClient.this.write("Processing ...");
- HttpAsrClient.wait4TerminateSignal(HttpAsrClient.this.getTxnTimeout());
- }
- }
- catch (IOException e)
- {
- HttpAsrClient.this.write("I/O problems: " + e);
- System.exit(-1);
- }
- }
- };
- Thread captureThread = new Thread(runner);
- captureThread.start();
- }
- catch (LineUnavailableException e)
- {
- write("Line unavailable: " + e);
- System.exit(-2);
- }
- }
- protected void streamAudioFile(final File audioFile)
- {
- AudioFormat format = getFormat();
- final int bufferSize = (int)(format.getSampleRate() * format.getFrameSize() / 50.0F);
- Runnable runner = new Runnable()
- {
- public void run()
- {
- String boundary = HttpAsrClient.this.sendAsrQueryCommands();
- try
- {
- HttpAsrClient.this.write("Streaming audio...");
- HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_begin");
- int audioDuration = 0;
- byte[] audio = FileLoader.load(audioFile);
- AudioChopper chopper = AudioChopperFactory.getAudioChopper(HttpAsrClient.this._requestData.IN_CODEC, audio);
- if (chopper != null)
- {
- HttpAsrClient.this.write("Sending");
- while ((chopper.hasMoreFrame()) && (!HttpAsrClient.this.queryFailed) && (!HttpAsrClient.this.queryCancelled))
- {
- byte[] buffer = chopper.getNextFrame();
- int wait = chopper.getFrameLengthInMs();
- synchronized (this)
- {
- wait(wait);
- }
- audioDuration += wait;
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, buffer, boundary);
- }
- }
- HttpAsrClient.this.write("WARNING - Real-time Audio Streaming Emulation Not Available for Audio File Type: " + HttpAsrClient.this._requestData.IN_CODEC);
- FileInputStream inputStream = new FileInputStream(audioFile);
- int nRead = 0;
- while ((nRead != -1) && (!HttpAsrClient.this.queryFailed) && (!HttpAsrClient.this.queryCancelled))
- {
- byte[] buffer = new byte[bufferSize];
- nRead = inputStream.read(buffer, 0, bufferSize);
- if (nRead > 0)
- {
- ByteBuffer bbuf = ByteBuffer.wrap(buffer, 0, nRead);
- HttpAsrClient.this.sendAudioChunk(HttpAsrClient.this.out, bbuf.array(), boundary);
- synchronized (this)
- {
- wait(20L);
- }
- }
- }
- inputStream.close();
- HttpAsrClient.this.write();
- HttpAsrClient.this.transactionLatency.setMarker("audio_streaming_end");
- if (audioDuration == 0) {
- HttpAsrClient.this._logData.audioDuration = ((float)(audioFile.length() / bufferSize) / 50.0F);
- } else {
- HttpAsrClient.this._logData.audioDuration = audioDuration;
- }
- HttpAsrClient.this.sendTerminatingChunk(HttpAsrClient.this.out, boundary);
- HttpAsrClient.this.write("\nProcessing ...");
- }
- catch (IOException|InterruptedException e)
- {
- HttpAsrClient.this.write("I/O problems: " + e);
- System.exit(-1);
- }
- catch (Exception e)
- {
- HttpAsrClient.this.write("Exception: " + e);
- System.exit(-1);
- }
- finally
- {
- if (HttpAsrClient.this.latch != null) {
- HttpAsrClient.this.latch.countDown();
- }
- }
- }
- };
- Thread fileStreamThread = new Thread(runner);
- fileStreamThread.start();
- }
- protected float calculateLevel(AudioFormat format, byte[] buffer, int readPoint, int leftOver)
- {
- int max = 0;
- float energyLevel = 0.0F;
- boolean use16Bit = format.getSampleSizeInBits() == 16;
- boolean signed = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED;
- boolean bigEndian = format.isBigEndian();
- if (use16Bit) {
- for (int i = readPoint; i < buffer.length - leftOver; i += 2)
- {
- int value = 0;
- int hiByte = bigEndian ? buffer[i] : buffer[(i + 1)];
- int loByte = bigEndian ? buffer[(i + 1)] : buffer[i];
- if (signed)
- {
- short shortVal = (short)hiByte;
- shortVal = (short)(shortVal << 8 | (byte)loByte);
- value = shortVal;
- }
- else
- {
- value = hiByte << 8 | loByte;
- }
- max = Math.max(max, value);
- }
- } else {
- for (int i = readPoint; i < buffer.length - leftOver; i++)
- {
- int value = 0;
- if (signed)
- {
- value = buffer[i];
- }
- else
- {
- short shortVal = 0;
- shortVal = (short)(shortVal | buffer[i]);
- value = shortVal;
- }
- max = Math.max(max, value);
- }
- }
- if (signed)
- {
- if (use16Bit) {
- energyLevel = max / 32767.0F;
- } else {
- energyLevel = max / 127.0F;
- }
- }
- else if (use16Bit) {
- energyLevel = max / 65535.0F;
- } else {
- energyLevel = max / 255.0F;
- }
- return energyLevel;
- }
- protected AudioFormat getFormat()
- {
- float sampleRate = 16000.0F;
- int sampleSizeInBits = 16;
- int channels = 1;
- boolean signed = true;
- boolean bigEndian = false;
- return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
- }
- protected Socket connectToServer()
- {
- try
- {
- if ((this.s == null) || (this.s.isClosed()))
- {
- write("Creating socket connection");
- this.s = SocketFactory.createSocket(this._host, this._port, this._useTLS, this._requireTrustedRootCert);
- this.transactionLatency.setMarker("connected");
- this._logData.timeToConnect = this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected");
- write("Time to establish socket connection: " + this._logData.timeToConnect);
- this.boundary = UUID.randomUUID().toString().replaceAll("-", "");
- this.out = this.s.getOutputStream();
- sendHeaders(this.out, this._host, this.boundary);
- }
- else
- {
- write("Socket connection already created");
- }
- return this.s;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- System.exit(-2);
- }
- return null;
- }
- protected String sendAsrQueryCommands()
- {
- Socket s = connectToServer();
- Thread t = new Thread(new Runnable()
- {
- Socket _s;
- public void run()
- {
- try
- {
- HttpAsrClient.this.processResponse(this._s);
- }
- catch (UnknownHostException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (JSONException e1)
- {
- e1.printStackTrace();
- }
- }
- });
- t.start();
- this.transactionLatency.setMarker("query_begin");
- sendRequestData(this.out, this._appId, this._appKey, this._userID, this._requestData.getApplicationSessionID(), this._requestData.getUtteranceNumber(), this.boundary);
- sendDictParameter(this.out, this.boundary);
- this.transactionLatency.setMarker("query_complete");
- return this.boundary;
- }
- protected void sendHeaders(OutputStream out, String host, String boundary)
- {
- try
- {
- StringBuilder sb = new StringBuilder();
- sb.append("POST /NmspServlet/ HTTP/1.1\r\n");
- sb.append("Host: " + host + "\r\n");
- sb.append("Connection: Keep-Alive\r\n");
- sb.append("Keep-Alive: timeout=100\r\n");
- sb.append("User-Agent: Nuance Java Sample HTTP App 1.0\r\n");
- sb.append("Transfer-Encoding: chunked\r\n");
- sb.append("Content-Type: multipart/form-data; boundary=" + boundary + "\r\n");
- sb.append("\r\n");
- out.write(sb.toString().getBytes());
- out.flush();
- Chunk chunk = new Chunk();
- chunk.append(sb.toString());
- printDataSent(chunk);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- protected void sendRequestData(OutputStream out, String appId, String appKey, String uId, String appSessionId, int uttNumber, String boundary)
- {
- try
- {
- JSONObject json = new JSONObject();
- JSONObject cmdDict = new JSONObject();
- json.put("appId", appId);
- json.put("appKey", appKey);
- json.put("uId", uId);
- json.put("inCodec", this._requestData.IN_CODEC);
- json.put("outCodec", this._requestData.OUT_CODEC);
- json.put("cmdName", "NMDP_ASR_CMD");
- json.put("appName", "Nuance Java Sample HTTP App");
- json.put("appVersion", "1.0");
- json.put("language", this._requestData.LANGUAGE);
- json.put("carrier", "unknown");
- json.put("deviceModel", this._requestData.DEVICE_MODEL);
- cmdDict.put("dictation_type", this._requestData.DICTATION_TYPE);
- cmdDict.put("dictation_language", this._requestData.LANGUAGE);
- cmdDict.put("locale", "USA");
- cmdDict.put("application_name", "Nuance Java Sample HTTP App");
- cmdDict.put("organization_id", "NUANCE");
- cmdDict.put("phone_OS", this._requestData.PHONE_OS);
- cmdDict.put("phone_network", "wifi");
- cmdDict.put("audio_source", "SpeakerAndMicrophone");
- cmdDict.put("location", "off");
- cmdDict.put("application_session_id", appSessionId);
- cmdDict.put("utterance_number", uttNumber);
- cmdDict.put("ui_language", "en");
- cmdDict.put("phone_submodel", this._requestData.PHONE_SUBMODEL);
- cmdDict.put("application_state_id", "0");
- json.put("cmdDict", cmdDict);
- Chunk chunk = new Chunk();
- chunk.append("--" + boundary + "\r\n");
- chunk.append("Content-Disposition: form-data; name=\"RequestData\"\r\n");
- chunk.append("Content-Type: application/json; charset=utf-8\r\n");
- chunk.append("Content-Transfer-Encoding: 8bit\r\n");
- chunk.append("\r\n");
- chunk.append(json.toString(2));
- chunk.append("\r\n");
- chunk.writeTo(out);
- printDataSent(chunk);
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- protected void sendDictParameter(OutputStream out, String boundary)
- {
- try
- {
- JSONObject json = new JSONObject();
- json.put("start", 0);
- json.put("end", 0);
- json.put("text", "");
- json.put("nbest_text_results", 1);
- if (this._streamingResults) {
- json.put("intermediate_response_mode", "NoUtteranceDetectionWithPartialRecognition");
- }
- if (this._profanityFilteringEnabled) {
- json.put("enable_profanity_filtering", 1);
- }
- Chunk chunk = new Chunk();
- chunk.append("--" + boundary + "\r\n");
- chunk.append("Content-Disposition: form-data; name=\"DictParameter\";paramName=\"REQUEST_INFO\"\r\n");
- chunk.append("Content-Type: application/json; charset=utf-8\r\n");
- chunk.append("Content-Transfer-Encoding: 8bit\r\n");
- chunk.append("\r\n");
- chunk.append(json.toString(2));
- chunk.append("\r\n");
- chunk.writeTo(out);
- printDataSent(chunk);
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- protected void sendAudioChunk(OutputStream out, byte[] audioData, String boundary)
- {
- try
- {
- Chunk chunk = new Chunk();
- chunk.append("--" + boundary + "\r\n");
- chunk.append("Content-Disposition: form-data; name=\"ConcludingAudioParameter\";paramName=\"AUDIO_INFO\"\r\n");
- chunk.append("Content-Type: audio/x-wav;codec=pcm;bit=16;rate=16000\r\n");
- chunk.append("Content-Transfer-Encoding: binary\r\n");
- chunk.append("\r\n");
- chunk.append(audioData);
- chunk.append("\r\n");
- chunk.writeTo(out);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- this.queryFailed = true;
- }
- }
- protected void sendUserProfileResetRequestData(OutputStream out, String boundary)
- {
- try
- {
- JSONObject json = new JSONObject();
- JSONObject cmdDict = new JSONObject();
- json.put("appId", this._appId);
- json.put("appKey", this._appKey);
- json.put("uId", this._userID);
- json.put("inCodec", this._requestData.IN_CODEC);
- json.put("outCodec", this._requestData.OUT_CODEC);
- json.put("cmdName", "NVC_RESET_USER_PROFILE_CMD");
- json.put("appName", "Nuance Java Sample HTTP App");
- json.put("appVersion", "1.0");
- json.put("language", this._requestData.LANGUAGE);
- json.put("carrier", "unknown");
- json.put("deviceModel", this._requestData.DEVICE_MODEL);
- cmdDict.put("dictation_language", this._requestData.LANGUAGE);
- cmdDict.put("locale", "USA");
- cmdDict.put("application_name", "Nuance Java Sample HTTP App");
- cmdDict.put("organization_id", "NUANCE");
- cmdDict.put("phone_OS", this._requestData.PHONE_OS);
- cmdDict.put("phone_network", "wifi");
- cmdDict.put("audio_source", "SpeakerAndMicrophone");
- cmdDict.put("location", "off");
- cmdDict.put("ui_language", "en");
- cmdDict.put("phone_submodel", this._requestData.PHONE_SUBMODEL);
- cmdDict.put("application_state_id", "0");
- json.put("cmdDict", cmdDict);
- Chunk chunk = new Chunk();
- chunk.append("--" + boundary + "\r\n");
- chunk.append("Content-Disposition: form-data; name=\"RequestData\"\r\n");
- chunk.append("Content-Type: application/json; charset=utf-8\r\n");
- chunk.append("Content-Transfer-Encoding: 8bit\r\n");
- chunk.append("\r\n");
- chunk.append(json.toString(2));
- chunk.append("\r\n");
- chunk.writeTo(out);
- printDataSent(chunk);
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- protected void sendTerminatingChunk(OutputStream out, String boundary)
- {
- try
- {
- Chunk chunk = new Chunk();
- chunk.append("--" + boundary + "--\r\n");
- chunk.writeTo(out);
- Chunk terminatingChunk = new Chunk();
- terminatingChunk.writeTo(out);
- printDataSent(chunk);
- write("Sent terminating chunk.");
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- protected void processResponse(Socket s)
- throws IOException, JSONException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- ResponseParser rp = new ResponseParser();
- try
- {
- boolean firstResponse = true;
- boolean headersParsed = false;
- boolean successfulStatusCode = true;
- Map<String, String> headers = new HashMap();
- String boundary = null;
- String t;
- while ((t = br.readLine()) != null) {
- if (firstResponse)
- {
- this.transactionLatency.setMarker("initial_response");
- write();
- if (this._streamingResults)
- {
- write("Time from first audio packet to first reponse: " + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "initial_response") + " seconds");
- this._logData.timeToFirstResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_begin", "initial_response");
- }
- else
- {
- write("Time from last audio packet to first reponse: " + this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "initial_response") + " seconds");
- this._logData.timeToFirstResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "initial_response");
- }
- firstResponse = false;
- if (!t.contains("200"))
- {
- successfulStatusCode = false;
- this.queryFailed = true;
- }
- write("Status Line: " + t);
- }
- else if ((!firstResponse) && (!headersParsed))
- {
- headers = rp.parseHeaders(br);
- headersParsed = true;
- write("\r\nHeaders: ");
- for (Map.Entry<String, String> entry : headers.entrySet()) {
- write("\t" + (String)entry.getKey() + ": " + (String)entry.getValue());
- }
- write();
- if (headers.containsKey("nuance-sessionid")) {
- this._logData.sessionId = ((String)headers.get("nuance-sessionid"));
- }
- if (headers.containsKey("content-type"))
- {
- String contentType = (String)headers.get("content-type");
- if (contentType.contains("boundary="))
- {
- String[] arr = contentType.split("=", 2);
- if ((arr != null) && (arr.length == 2)) {
- boundary = arr[1];
- }
- }
- }
- }
- else if ((headersParsed) && (!successfulStatusCode))
- {
- write(t);
- if (t.equals("0")) {
- if (this.transactionLatency.getMarker("final_response") == -1L)
- {
- this.transactionLatency.setMarker("final_response");
- this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
- }
- }
- }
- else if ((headersParsed) && (successfulStatusCode))
- {
- Map<String, String> bodyParts;
- if ((boundary == null) && (headers.containsKey("content-disposition")))
- {
- Map<String, String> bodyParts = new HashMap();
- bodyParts.putAll(headers);
- bodyParts.put("json_1", t);
- }
- else
- {
- bodyParts = rp.parseBodyPart(br, boundary);
- }
- if (t.length() > 2)
- {
- String[] arr = t.split(":", 2);
- if (((arr != null ? 1 : 0) & (arr.length == 2 ? 1 : 0)) != 0) {
- bodyParts.put(arr[0].trim().toLowerCase(), arr[1].trim().toLowerCase());
- }
- }
- if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryerror")))
- {
- this.queryFailed = true;
- write("Server Response: ");
- write((String)bodyParts.get("json_1"));
- }
- else if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryretry")))
- {
- this.queryFailed = true;
- write("Server Response: ");
- write((String)bodyParts.get("json_1"));
- }
- else if ((bodyParts.containsKey("content-disposition")) && (((String)bodyParts.get("content-disposition")).contains("queryresult")))
- {
- String json_1 = bodyParts.containsKey("json_1") ? (String)bodyParts.get("json_1") : null;
- if ((json_1 == null) || (!isJSON(json_1))) {
- continue;
- }
- JSONObject json = new JSONObject(json_1);
- if ((json.has("final_response")) && (json.getInt("final_response") == 0))
- {
- if (json.has("transcriptions")) {
- write("Streaming Response: " + json.getJSONArray("transcriptions").getString(0));
- } else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").getJSONObject(0).has("text"))) {
- write("Streaming Response [text]: " + json.getJSONObject("appserver_results")
- .getJSONObject("payload")
- .getJSONArray("actions")
- .getJSONObject(0)
- .getString("text"));
- } else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").getJSONObject(0).has("nbest_text"))) {
- write("Streaming Response [nbest text]: " + json.getJSONObject("appserver_results")
- .getJSONObject("payload")
- .getJSONArray("actions")
- .getJSONObject(0)
- .getJSONObject("nbest_text")
- .getJSONArray("transcriptions")
- .getString(0));
- } else {
- write(json.toString(4));
- }
- }
- else if ((json.has("final_response")) && (json.getInt("final_response") == 1))
- {
- if (json.has("transcriptions"))
- {
- write("Final Response: " + json.getJSONArray("transcriptions").getString(0));
- write("Final JSON Response: " + json.toString(4));
- }
- else if ((json.has("appserver_results")) && (json.getJSONObject("appserver_results").has("payload")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").has("actions")) &&
- (json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions").length() > 0))
- {
- JSONArray actions = json.getJSONObject("appserver_results").getJSONObject("payload").getJSONArray("actions");
- for (int i = 0; i < actions.length(); i++)
- {
- JSONObject action = actions.getJSONObject(i);
- if ((action.has("type")) && (action.getString("type").equalsIgnoreCase("nlu_results"))) {
- write("Final Response: " + action.getJSONObject("Input")
- .getJSONArray("Interpretations")
- .getString(0));
- }
- }
- write("Final JSON Response: " + json.toString(4));
- }
- else
- {
- write("Unsupported Response Format: " + json.toString(4));
- }
- }
- else {
- write("Unknown Response: " + json.toString(4));
- }
- }
- if ((bodyParts.containsKey("size")) && (Integer.valueOf((String)bodyParts.get("size")).intValue() == 0)) {
- if (this.transactionLatency.getMarker("final_response") == -1L)
- {
- this.transactionLatency.setMarker("final_response");
- this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
- }
- }
- }
- }
- }
- catch (SocketTimeoutException e)
- {
- write(e.getMessage());
- }
- finally
- {
- br.close();
- if (this.transactionLatency.getMarker("final_response") == -1L)
- {
- this.transactionLatency.setMarker("final_response");
- this._logData.timeToFinalResponse = this.transactionLatency.calculateDistanceBetweenMarkers("audio_streaming_end", "final_response");
- }
- printLineSeparator();
- write("Done reading response...");
- this.transactionLatency.setMarker("stop");
- this._logData.totalTrxnDuration = this.transactionLatency.calculateDistanceBetweenMarkers("start", "stop");
- printLineSeparator();
- showLatencyMarkers();
- this._logData.writeToFile();
- synchronized (waitLock)
- {
- try
- {
- waitLock.notifyAll();
- }
- catch (IllegalMonitorStateException e)
- {
- write(e.getMessage());
- }
- }
- }
- }
- protected boolean isJSON(String str)
- {
- try
- {
- JSONObject localJSONObject = new JSONObject(str);
- }
- catch (JSONException e)
- {
- return false;
- }
- return true;
- }
- protected boolean isNumeric(String str)
- {
- try
- {
- int i = Integer.parseInt(str, 16);
- }
- catch (NumberFormatException nfe)
- {
- return false;
- }
- return true;
- }
- protected void initialize()
- {
- this._userID = ((this._userID != null) && (!this._userID.isEmpty()) ? this._userID : UserIDManager.createUserIDManager().initUserID());
- this._requestData.initApplicationSessionID();
- this._requestData.resetUtteranceNumber();
- }
- private void write()
- {
- System.out.println();
- }
- private void write(String msg)
- {
- System.out.println(msg);
- }
- protected void showLatencyMarkers()
- {
- write("marker\t\t\t\t\tDuration Since T0\tDuration Since Last Marker");
- write("[t0] start\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "start") + "sec\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "start") + "sec");
- write("[t1] connected\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected") + "sec\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "connected") + "sec");
- write("[t2] query_begin\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "query_begin") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("connected", "query_begin") + "sec");
- write("[t3] query_complete\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "query_complete") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("query_begin", "query_complete") + "sec");
- 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");
- 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");
- if (this._streamingResults) {
- 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)");
- } else {
- 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)");
- }
- if (this._streamingResults) {
- 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");
- } else {
- write("[t7] final_response\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "final_response") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("initial_response", "final_response") + "sec");
- }
- write("[t8] stop\t\t\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("start", "stop") + "sec\t\t" + this.transactionLatency.calculateDistanceBetweenMarkers("final_response", "stop") + "sec");
- }
- protected void printLineSeparator()
- {
- write();
- write("---------------------------------");
- write();
- }
- protected void printDataSent(Chunk chunk)
- {
- if (isVerbose())
- {
- write();
- write("<<<<<< Sending >>>>>>");
- try
- {
- chunk.writeTo(System.out);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- write();
- }
- }
- private static void wait4TerminateSignal(int timeout)
- {
- synchronized (waitLock)
- {
- try
- {
- waitLock.wait(timeout);
- }
- catch (InterruptedException e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
- public void start(String audioFilename)
- {
- start(audioFilename, "PCM_16_16K", true);
- }
- public void start(String audioPath, String codec, boolean streamingResults)
- {
- this._requestData.IN_CODEC = codec;
- this._requestData.OUT_CODEC = codec;
- this._streamingResults = streamingResults;
- initialize();
- printLineSeparator();
- if (batchModeEnabled()) {
- batchModeAsr(audioPath);
- } else if (audioPath != null) {
- batchModeAsr(audioPath);
- } else {
- monitorConsoleEvents();
- }
- }
- protected void batchModeAsr(String path)
- {
- File dir = new File(path);
- File[] directoryListing = dir.listFiles();
- if (directoryListing != null)
- {
- for (File file : directoryListing)
- {
- this.transactionLatency.reset();
- this.transactionLatency.setMarker("start");
- write("Processing audio file: " + file.getAbsolutePath());
- this.latch = new CountDownLatch(1);
- streamAudioFile(file);
- try
- {
- this.latch.await();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- wait4TerminateSignal(getTxnTimeout());
- }
- }
- else
- {
- File file = new File(path);
- this.transactionLatency.reset();
- this.transactionLatency.setMarker("start");
- write("Processing audio file: " + file.getAbsolutePath());
- this.latch = new CountDownLatch(1);
- streamAudioFile(file);
- try
- {
- this.latch.await();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- wait4TerminateSignal(getTxnTimeout());
- }
- }
- public void resetUserProfile()
- {
- initialize();
- this.transactionLatency.setMarker("start");
- Socket s = connectToServer();
- this.transactionLatency.setMarker("query_begin");
- sendUserProfileResetRequestData(this.out, this.boundary);
- sendTerminatingChunk(this.out, this.boundary);
- this.transactionLatency.setMarker("query_complete");
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String t;
- while ((t = br.readLine()) != null)
- {
- write(t);
- if (t.equals("0")) {
- return;
- }
- }
- }
- catch (Exception e)
- {
- write(e.getMessage());
- }
- }
- public static void printUsage()
- {
- String path = HttpAsrClient.class.getProtectionDomain().getCodeSource().getLocation().getFile();
- File f = new File(path);
- String jar = f.getName();
- 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");
- }
- public static void main(String[] args)
- throws Exception
- {
- String host = "{Provide your hostname}";
- String nmaid = "{Provide your NMAID}";
- String appKey = "{Provide your 128-byte String App Key}";
- String userID = null;
- int port = 443;
- boolean useTLS = true;
- boolean requireTrustedRootCert = true;
- String topic = "Dictation";
- String langCode = "eng-USA";
- String codec = "PCM_16_16K";
- String audioPath = null;
- boolean streamingResults = true;
- boolean enableProfanityFiltering = false;
- String savedAudioPath = null;
- boolean batchMode = false;
- boolean enableVAD = true;
- boolean resetUserProfile = false;
- boolean verbose = false;
- for (int i = 0; i < args.length; i += 2)
- {
- if (args[i].equals("-help"))
- {
- printUsage();
- return;
- }
- if (args[i].equals("-uid"))
- {
- userID = args[(i + 1)];
- }
- else if (args[i].equals("-h"))
- {
- host = args[(i + 1)];
- }
- else if (args[i].equals("-p"))
- {
- port = Integer.parseInt(args[(i + 1)]);
- }
- else if (args[i].equals("-s"))
- {
- useTLS = Boolean.parseBoolean(args[(i + 1)]);
- }
- else if (args[i].equals("-tr"))
- {
- requireTrustedRootCert = Boolean.parseBoolean(args[(i + 1)]);
- }
- else if (args[i].equals("-n"))
- {
- nmaid = args[(i + 1)];
- }
- else if (args[i].equals("-a"))
- {
- appKey = args[(i + 1)];
- }
- else if (args[i].equals("-t"))
- {
- topic = args[(i + 1)];
- }
- else if (args[i].equals("-l"))
- {
- langCode = args[(i + 1)];
- }
- else if (args[i].equals("-c"))
- {
- codec = args[(i + 1)];
- }
- else if (args[i].equals("-f"))
- {
- audioPath = args[(i + 1)];
- }
- else if (args[i].equals("-streaming"))
- {
- streamingResults = Boolean.parseBoolean(args[(i + 1)]);
- }
- else if (args[i].equals("-pf"))
- {
- enableProfanityFiltering = Boolean.parseBoolean(args[(i + 1)]);
- }
- else if (args[i].equalsIgnoreCase("-sap"))
- {
- savedAudioPath = args[(i + 1)];
- }
- else if (args[i].equalsIgnoreCase("-bm"))
- {
- batchMode = true;
- audioPath = args[(i + 1)];
- }
- else if (args[i].equalsIgnoreCase("-vad"))
- {
- enableVAD = Boolean.parseBoolean(args[(i + 1)]);
- }
- else if (args[i].equalsIgnoreCase("-rup"))
- {
- resetUserProfile = true;
- }
- else if (args[i].equalsIgnoreCase("-v"))
- {
- verbose = Boolean.parseBoolean(args[(i + 1)]);
- }
- }
- if ((host.equalsIgnoreCase("{Provide your hostname}")) ||
- (nmaid.equalsIgnoreCase("{Provide your NMAID}")) ||
- (appKey.equalsIgnoreCase("{Provide your 128-byte String App Key}")) ||
- (host.length() == 0) ||
- (nmaid.length() == 0) ||
- (appKey.length() != 128))
- {
- printUsage();
- System.exit(0);
- }
- IHttpAsrClient asrClient = new HttpAsrClient(host, port, useTLS, nmaid, appKey, topic, langCode, userID);
- if (verbose) {
- asrClient.enableVerbose();
- }
- if (!requireTrustedRootCert) {
- asrClient.disableTrustedRootCert();
- }
- if (resetUserProfile)
- {
- asrClient.resetUserProfile();
- System.exit(0);
- }
- if (batchMode) {
- asrClient.enableBatchMode();
- }
- if (savedAudioPath != null) {
- asrClient.setSavedAudioPath(savedAudioPath);
- }
- if (!enableVAD) {
- asrClient.disableStartOfSpeechDetection();
- }
- if (enableProfanityFiltering) {
- asrClient.enableProfanityFiltering();
- }
- asrClient.start(audioPath, codec, streamingResults);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement