Liaisons Java

libgspeech dispose de liaisons JNI et d'une API Java basée sur les liaisons JNI. Exemples d'applications utilisant les liaisons Java

Liaisons Java

Les liaisons Java pour libgspeech sont à un stade de développement précoce et sont susceptibles d'être modifiées.

package com.google.libgspeech;

public class GoogleSpeech {
  public enum PollingType {
    NON_BLOCKING,
    BLOCKING_TIMEOUT,
    BLOCKING,
  }

  static {
    System.loadLibrary("gspeech_jni");
  }

  /** Create Speech object with API key `key` */
  public GoogleSpeech(String key) { ... }

  /**
   * Initialize Speech with the provided initialization configuration.
   * This methods will load models into memory.
  */
  public boolean init(SpeechInitConfig initConfig) throws GoogleSpeechException { ... }

  /** Check if Speech isPrepared, initialized but not shutdown. */
  public boolean isPrepared() throws GoogleSpeechException { ... }

  /**
   * Start Speech, starts the pipeline and starts reading from the
   * internal audio buffer.
  */
  public boolean start(SpeechStartConfig startConfig) throws GoogleSpeechException { ... }

  /** Check if Speech is running by not stopped. */
  public boolean isRunning() throws GoogleSpeechException { ... }

  /** Stop Speech, undo the start. */
  public boolean stop() throws GoogleSpeechException { ... }

  /** Shutdown Speech, undo the initialization. */
  public boolean shutdown() throws GoogleSpeechException { ... }

  /** Poll Speech for events. */
  public SpeechEvents pollEvents(GoogleSpeech.PollingType pollingType, int timeoutMs) throws GoogleSpeechException { ... }

  /** Tell G_Speech that the current audio source is done. */
  public void markEndOfAudio();

  /** Push audio into Speech. */
  public boolean pushAudio(byte[] audio) throws GoogleSpeechException { ... }

  /** Clean-up. */
  protected void finalize() throws GoogleSpeechException { ... }
}

Liaisons JNI

Liaisons JNI pour libgspeech. Veuillez consulter les liaisons C pour obtenir une documentation plus détaillée. Ces méthodes sont des wrappers fins pour les méthodes C.

/**
 * Native bindings to the libgspeech JNI interface. The methods in this class maps directly onto the
 * C API. Please see the C-API documentation for more detailed documentation on the methods and
 * parameters.
 */
public class GoogleSpeechNative {
  /** Types of polling libgspeech can do. */
  public static final class PollingType {
    public static final int NON_BLOCKING = 1;
    public static final int BLOCKING_TIMEOUT = 2;
    public static final int BLOCKING = 3;
    private PollingType() {}
  }
  public static native void printVersion();
  /**
   * Allocate a `G_SpeechCallContext`. The return value is the address to the context. One use-case
   * is to receive more detailed error messages on failures.
   */
  public static native long createCallContext();
  /** Get the error message in a `G_SpeechCallContext`. Will be 'OK' if no error is set. */
  public static native String getErrorMessage(long context);
  /** Free the allocated `G_SpeechCallContext`. */
  public static native boolean freeCallContext(long context);
  /**
   * Allocate a `G_SpeechEventsOwner`. The return value is the address of the events_owner. The
   * events_owner holds the results from `pollEvents` which can be accessed by `getEvents`.
   */
  public static native long createEventsOwner(long context);
  /**
   * Check if a `G_SpeechEventsOwner` has any available events. NOTE: this is not the same as
   * `G_Speech` having events. This will return true if after polling `G_SpeechEventsOwner` received
   * more than 0 events.
   */
  public static native boolean hasEvents(long eventsOwner, long context);
  /** Retrieve events from a `G_SpeechEventsOwner`. */
  public static native byte[] getEvents(long eventsOwner, long context);
  /** Free `G_SpeechEventsOwner`. */
  public static native boolean freeEventsOwner(long eventsOwner, long context);
  /**
   * Allocate a `G_Speech` object. The return value is the address of the object. Accepts an API key
   * and a `G_SpeechCallContext`.
   */
  public static native long create(String key, long context);
  /**
   * Initialize the `G_Speech` object with a serialized initialization configuration. This allocates
   * most of the resources.
   */
  public static native boolean init(long googleSpeech, byte[] initConfig, long context);
  /** Check if the `G_Speech` object is prepared. Initialized and not shutdown. */
  public static native boolean isPrepared(long googleSpeech, long context);
  /** Start the `G_Speech` object with a serialized start configuration. */
  public static native boolean start(long googleSpeech, byte[] startConfig, long context);
  /** Check if the `G_Speech` object is running. Started and not stopped. */
  public static native boolean isRunning(long googleSpeech, long context);
  /** Tell G_Speech that the current audio source is done. */
  public static native boolean markEndOfAudio(long googleSpeech, long context);
  /** Push audio into the `G_Speech` object. */
  public static native boolean pushAudio(long googleSpeech, byte[] audio, long context);
  /**
   * Poll the `G_Speech` object for events. events, possibly empty, will be populated in the
   * eventsOwner.
   */
  public static native boolean pollEvents(
      long googleSpeech, long eventsOwner, int pollingType, int timeoutMs, long context);
  /** Stop the `G_Speech` object, undoing start. */
  public static native boolean stop(long googleSpeech, long context);
  /** Shutdown the `G_Speech` object, undoing initialize, this deallocates most resources. */
  public static native boolean shutdown(long googleSpeech, long context);
  /** Free the `G_Speech` object, deallocating the remaining resources. */
  public static native boolean free(long googleSpeech, long context); }