Search in sources :

Example 1 with SpeechToTextWebSocketListener

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

the class SpeechToText method recognizeUsingWebSocket.

/**
 * Sends audio and returns transcription results for recognition requests over a WebSocket connection. Requests and
 * responses are enabled over a single TCP connection that abstracts much of the complexity of the request to offer
 * efficient implementation, low latency, high throughput, and an asynchronous response. By default, only final
 * results are returned for any request; to enable interim results, set the interimResults parameter to true.
 *
 * The service imposes a data size limit of 100 MB per utterance (per recognition request). You can send multiple
 * utterances over a single WebSocket connection. The service automatically detects the endianness of the incoming
 * audio and, for audio that includes multiple channels, downmixes the audio to one-channel mono during transcoding.
 * (For the audio/l16 format, you can specify the endianness.)
 *
 * @param recognizeOptions the recognize options
 * @param callback the {@link RecognizeCallback} instance where results will be sent
 * @return the {@link WebSocket}
 */
public WebSocket recognizeUsingWebSocket(RecognizeOptions recognizeOptions, RecognizeCallback callback) {
    Validator.notNull(recognizeOptions, "recognizeOptions cannot be null");
    Validator.notNull(recognizeOptions.audio(), "audio cannot be null");
    Validator.notNull(callback, "callback cannot be null");
    HttpUrl.Builder urlBuilder = HttpUrl.parse(getEndPoint() + "/v1/recognize").newBuilder();
    if (recognizeOptions.model() != null) {
        urlBuilder.addQueryParameter("model", recognizeOptions.model());
    }
    if (recognizeOptions.customizationId() != null) {
        urlBuilder.addQueryParameter("customization_id", recognizeOptions.customizationId());
    }
    if (recognizeOptions.acousticCustomizationId() != null) {
        urlBuilder.addQueryParameter("acoustic_customization_id", recognizeOptions.acousticCustomizationId());
    }
    if (recognizeOptions.version() != null) {
        urlBuilder.addQueryParameter("version", recognizeOptions.version());
    }
    if (recognizeOptions.customizationWeight() != null) {
        urlBuilder.addQueryParameter("customization_weight", String.valueOf(recognizeOptions.customizationWeight()));
    }
    String url = urlBuilder.toString().replace("https://", "wss://");
    Request.Builder builder = new Request.Builder().url(url);
    setAuthentication(builder);
    setDefaultHeaders(builder);
    OkHttpClient client = configureHttpClient();
    return client.newWebSocket(builder.build(), new SpeechToTextWebSocketListener(recognizeOptions, callback));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SpeechToTextWebSocketListener(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.SpeechToTextWebSocketListener) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 2 with SpeechToTextWebSocketListener

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

the class SpeechToText method recognizeUsingWebSocket.

/**
 * Sends audio and returns transcription results for recognition requests over a WebSocket
 * connection. Requests and responses are enabled over a single TCP connection that abstracts much
 * of the complexity of the request to offer efficient implementation, low latency, high
 * throughput, and an asynchronous response. By default, only final results are returned for any
 * request; to enable interim results, set the interimResults parameter to true.
 *
 * <p>The service imposes a data size limit of 100 MB per utterance (per recognition request). You
 * can send multiple utterances over a single WebSocket connection. The service automatically
 * detects the endianness of the incoming audio and, for audio that includes multiple channels,
 * downmixes the audio to one-channel mono during transcoding. (For the audio/l16 format, you can
 * specify the endianness.)
 *
 * @param recognizeOptions the recognize options
 * @param callback the {@link RecognizeCallback} instance where results will be sent
 * @return the {@link WebSocket}
 */
public WebSocket recognizeUsingWebSocket(RecognizeWithWebsocketsOptions recognizeOptions, RecognizeCallback callback) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(recognizeOptions, "recognizeOptions cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.notNull(recognizeOptions.audio(), "audio cannot be null");
    com.ibm.cloud.sdk.core.util.Validator.notNull(callback, "callback cannot be null");
    HttpUrl.Builder urlBuilder = HttpUrl.parse(getServiceUrl() + "/v1/recognize").newBuilder();
    if (recognizeOptions.model() != null) {
        urlBuilder.addQueryParameter("model", recognizeOptions.model());
    }
    if (recognizeOptions.customizationId() != null) {
        urlBuilder.addQueryParameter("customization_id", recognizeOptions.customizationId());
    }
    if (recognizeOptions.languageCustomizationId() != null) {
        urlBuilder.addQueryParameter("language_customization_id", recognizeOptions.languageCustomizationId());
    }
    if (recognizeOptions.acousticCustomizationId() != null) {
        urlBuilder.addQueryParameter("acoustic_customization_id", recognizeOptions.acousticCustomizationId());
    }
    if (recognizeOptions.baseModelVersion() != null) {
        urlBuilder.addQueryParameter("base_model_version", recognizeOptions.baseModelVersion());
    }
    String url = urlBuilder.toString().replace("https://", "wss://");
    Request.Builder builder = new Request.Builder().url(url);
    setAuthentication(builder);
    setDefaultHeaders(builder);
    OkHttpClient client = configureHttpClient();
    return client.newWebSocket(builder.build(), new SpeechToTextWebSocketListener(recognizeOptions, callback));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SpeechToTextWebSocketListener(com.ibm.watson.speech_to_text.v1.websocket.SpeechToTextWebSocketListener) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Aggregations

HttpUrl (okhttp3.HttpUrl)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 SpeechToTextWebSocketListener (com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.SpeechToTextWebSocketListener)1 SpeechToTextWebSocketListener (com.ibm.watson.speech_to_text.v1.websocket.SpeechToTextWebSocketListener)1