Search in sources :

Example 6 with RecognizeWithWebsocketsOptions

use of com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions in project java-sdk by watson-developer-cloud.

the class SpeechToTextIT method testEndOfPhraseSilenceTimeWebSocket.

/**
 * Test end of phrase silence time web socket.
 *
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testEndOfPhraseSilenceTimeWebSocket() throws FileNotFoundException, InterruptedException {
    FileInputStream audio = new FileInputStream(SAMPLE_WAV_WITH_PAUSE);
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).endOfPhraseSilenceTime(0.2).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) {
            if (speechResults != null && speechResults.getResults() != null) {
                asyncTranscriptionResults = speechResults;
            }
        }
    });
    lock.await(1, TimeUnit.MINUTES);
    assertNotNull(asyncTranscriptionResults);
    assertTrue(asyncTranscriptionResults.getResults().size() > 1);
    // Clear for later.
    asyncTranscriptionResults = null;
}
Also used : BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 7 with RecognizeWithWebsocketsOptions

use of com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions 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);
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.Builder().audio(audio).inactivityTimeout(40).timestamps(true).maxAlternatives(2).wordAlternativesThreshold(0.5f).model(EN_BROADBAND16K).contentType(HttpMediaType.AUDIO_WAV).interimResults(true).processingMetrics(true).processingMetricsInterval(0.2f).audioMetrics(true).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) {
            if (speechResults != null) {
                if (speechResults.getResults() != null && speechResults.getResults().get(0).isXFinal()) {
                    asyncTranscriptionResults = speechResults;
                }
                if (speechResults.getAudioMetrics() != null) {
                    asyncAudioMetricsResults = speechResults;
                }
            // System.out.println(speechResults);
            }
        }
    });
    lock.await(3, TimeUnit.MINUTES);
    assertNotNull(asyncTranscriptionResults);
    assertNotNull(asyncAudioMetricsResults);
    List<WordAlternativeResults> wordAlternatives = asyncTranscriptionResults.getResults().get(asyncTranscriptionResults.getResultIndex().intValue()).getWordAlternatives();
    assertTrue(wordAlternatives != null && !wordAlternatives.isEmpty());
    assertNotNull(wordAlternatives.get(0).getAlternatives());
    assertNotNull(asyncAudioMetricsResults.getAudioMetrics());
    // Clear for later tests.
    asyncTranscriptionResults = null;
    asyncAudioMetricsResults = null;
}
Also used : BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) WordAlternativeResults(com.ibm.watson.speech_to_text.v1.model.WordAlternativeResults) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 8 with RecognizeWithWebsocketsOptions

use of com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions 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);
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.Builder().audio(audio).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);
}
Also used : BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Aggregations

RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)7 BaseRecognizeCallback (com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback)6 SpeechRecognitionResults (com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)5 FileInputStream (java.io.FileInputStream)5 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)4 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)3 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)3 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 FileNotFoundException (java.io.FileNotFoundException)3 Test (org.junit.Test)3 ExpectedException (org.junit.rules.ExpectedException)3 SpeechToText (com.ibm.watson.speech_to_text.v1.SpeechToText)1 WordAlternativeResults (com.ibm.watson.speech_to_text.v1.model.WordAlternativeResults)1 SpeechToTextWebSocketListener (com.ibm.watson.speech_to_text.v1.websocket.SpeechToTextWebSocketListener)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 AudioFormat (javax.sound.sampled.AudioFormat)1 AudioInputStream (javax.sound.sampled.AudioInputStream)1 DataLine (javax.sound.sampled.DataLine)1