Search in sources :

Example 1 with BaseRecognizeCallback

use of com.ibm.watson.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);
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 2 with BaseRecognizeCallback

use of com.ibm.watson.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());
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) WordAlternativeResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 3 with BaseRecognizeCallback

use of com.ibm.watson.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.");
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) TargetDataLine(javax.sound.sampled.TargetDataLine) DataLine(javax.sound.sampled.DataLine) TargetDataLine(javax.sound.sampled.TargetDataLine) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)

Example 4 with BaseRecognizeCallback

use of com.ibm.watson.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 {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    SpeechToText service = new SpeechToText(authenticator);
    FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.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);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) FileInputStream(java.io.FileInputStream) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)

Example 5 with BaseRecognizeCallback

use of com.ibm.watson.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 {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    SpeechToText service = new SpeechToText(authenticator);
    // 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);
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.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.");
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) TargetDataLine(javax.sound.sampled.TargetDataLine) DataLine(javax.sound.sampled.DataLine) TargetDataLine(javax.sound.sampled.TargetDataLine) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)

Aggregations

FileInputStream (java.io.FileInputStream)9 RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)6 BaseRecognizeCallback (com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback)6 RecognizeOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)5 BaseRecognizeCallback (com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback)5 SpeechRecognitionResults (com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)5 FileNotFoundException (java.io.FileNotFoundException)5 Test (org.junit.Test)5 ExpectedException (org.junit.rules.ExpectedException)5 SpeechRecognitionResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults)4 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)3 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)3 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)3 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 AudioFormat (javax.sound.sampled.AudioFormat)2 AudioInputStream (javax.sound.sampled.AudioInputStream)2 DataLine (javax.sound.sampled.DataLine)2 TargetDataLine (javax.sound.sampled.TargetDataLine)2