use of com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback in project java-sdk by watson-developer-cloud.
the class SpeechToTextIT method testInactivityTimeoutWithWebSocket.
/**
* Test the inactivity timeout parameter for WebSockets.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testInactivityTimeoutWithWebSocket() throws FileNotFoundException, InterruptedException {
FileInputStream audio = new FileInputStream(SAMPLE_WAV_WITH_PAUSE);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).inactivityTimeout(3).timestamps(true).maxAlternatives(2).wordAlternativesThreshold(0.5f).model(EN_BROADBAND16K).contentType(HttpMediaType.AUDIO_WAV).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onDisconnected() {
lock.countDown();
}
@Override
public void onError(Exception e) {
e.printStackTrace();
lock.countDown();
}
@Override
public void onInactivityTimeout(RuntimeException runtimeException) {
inactivityTimeoutOccurred = true;
}
});
lock.await(2, TimeUnit.MINUTES);
assertTrue(inactivityTimeoutOccurred);
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback in project java-sdk by watson-developer-cloud.
the class SpeechToTextIT method testRecognizeWebSocket.
/**
* Test recognize webSocket.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testRecognizeWebSocket() throws FileNotFoundException, InterruptedException {
FileInputStream audio = new FileInputStream(SAMPLE_WAV);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).inactivityTimeout(40).timestamps(true).maxAlternatives(2).wordAlternativesThreshold(0.5f).model(EN_BROADBAND16K).contentType(HttpMediaType.AUDIO_WAV).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onConnected() {
LOG.info("onConnected()");
}
@Override
public void onDisconnected() {
LOG.info("onDisconnected()");
}
@Override
public void onTranscriptionComplete() {
LOG.info("onTranscriptionComplete()");
lock.countDown();
}
@Override
public void onError(Exception e) {
e.printStackTrace();
lock.countDown();
}
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
Long resultIndex = speechResults.getResultIndex();
if (speechResults != null && speechResults.getResults().get(resultIndex.intValue()).isFinalResults()) {
asyncResults = speechResults;
}
}
});
lock.await(2, TimeUnit.MINUTES);
assertNotNull(asyncResults);
List<WordAlternativeResults> wordAlternatives = asyncResults.getResults().get(asyncResults.getResultIndex().intValue()).getWordAlternatives();
assertTrue(wordAlternatives != null && !wordAlternatives.isEmpty());
assertNotNull(wordAlternatives.get(0).getAlternatives());
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback in project java-sdk by watson-developer-cloud.
the class MicrophoneWithWebSocketsExample method main.
/**
* The main method.
*
* @param args the arguments
* @throws Exception the exception
*/
public static void main(final String[] args) throws Exception {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
// Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
int sampleRate = 16000;
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
AudioInputStream audio = new AudioInputStream(line);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).timestamps(true).wordConfidence(true).contentType(HttpMediaType.AUDIO_RAW + ";rate=" + sampleRate).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
});
System.out.println("Listening to your voice for the next 30s...");
Thread.sleep(30 * 1000);
// closing the WebSockets underlying InputStream will close the WebSocket itself.
line.stop();
line.close();
System.out.println("Fin.");
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback in project java-sdk by watson-developer-cloud.
the class RecognizeUsingWebSocketsExample method main.
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).contentType(HttpMediaType.AUDIO_WAV).build();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
System.out.println(speechResults);
}
@Override
public void onDisconnected() {
lock.countDown();
}
});
lock.await(1, TimeUnit.MINUTES);
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback in project java-sdk by watson-developer-cloud.
the class RecognizeUsingWebSocketsWithSpeakerLabelsExample method main.
/**
* The main method.
*
* @param args the arguments
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/twospeakers.wav");
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).speakerLabels(true).model(RecognizeOptions.EN_US_NARROWBANDMODEL).contentType(HttpMediaType.AUDIO_WAV).build();
RecoTokens recoTokens = new RecoTokens();
service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {
@Override
public void onTranscription(SpeechRecognitionResults speechResults) {
recoTokens.add(speechResults);
}
@Override
public void onDisconnected() {
lock.countDown();
}
});
lock.await(1, TimeUnit.MINUTES);
}
Aggregations