Class VoiceEngine

java.lang.Object
dyntabs.ai.voice.VoiceEngine

public final class VoiceEngine extends Object
Bridges EasyAI to LangChain4J's audio models — the voice counterpart of RagEngine and MilvusEngine.

What this class actually does

It is the single place that knows about AudioTranscriptionModel and TextToSpeechModel. Everything above it (EasyAI.listen(), EasyAI.speak()) deals only in byte[], String and Speech — the shapes a Jakarta EE web tier already has in hand. Four small jobs live here:

  1. Build the model from a VoiceConfig (provider, key, base URL, model name).
  2. Wrap the bytes as a LangChain4J Audio — including the detail that an upload arrives as a byte[] while a model may want Base64.
  3. Unwrap the answer — a transcript as String, synthesized audio as Speech with its MIME type intact.
  4. Fail with a usable message when a key is missing, instead of a provider-level NPE.

Providers

Only "openai" is built in, which covers any OpenAI-compatible endpoint via VoiceConfig.baseUrl() — Groq's Whisper being the obvious example, and the reason voice typically needs no new provider and no new key. Any other provider id fails fast with a message naming what is supported, rather than silently doing something else. New providers plug in here and nowhere else.

Analogy: the adapter plug for a foreign appliance. The appliance (your web endpoint) keeps its own plain plug; the adapter knows the local socket's shape. Change country and you change the adapter, not the appliance.

See Also:
  • Method Details

    • transcribe

      public static String transcribe(byte[] audio, String mimeType, VoiceConfig config)
      Transcribe one spoken utterance.
      Parameters:
      audio - the recorded bytes (e.g. a WebM/Opus blob from a browser's MediaRecorder)
      mimeType - the audio MIME type, e.g. "audio/webm"; null is allowed but providers generally transcribe more reliably when told
      config - which model to call and how to reach it
      Returns:
      the recognised text (possibly empty if nothing intelligible was heard)
      Throws:
      IllegalArgumentException - if audio is null or empty
      IllegalStateException - if no API key is configured
    • synthesize

      public static Speech synthesize(String text, VoiceConfig config)
      Synthesize speech for one piece of text.
      Parameters:
      text - the words to speak (required, non-blank)
      config - which model and voice to use, and how to reach it
      Returns:
      the audio plus its MIME type
      Throws:
      IllegalArgumentException - if text is null or blank
      IllegalStateException - if no API key is configured, or the provider returned no audio
    • transcriptionModel

      public static dev.langchain4j.model.audio.AudioTranscriptionModel transcriptionModel(VoiceConfig config)
      Build the LangChain4J transcription model described by config.

      Exposed for callers who want the raw LangChain4J model — the escape hatch, so choosing EasyAI never means losing access to the layer underneath.

      Parameters:
      config - the voice settings
      Returns:
      a ready AudioTranscriptionModel
    • speechModel

      public static dev.langchain4j.model.audio.TextToSpeechModel speechModel(VoiceConfig config)
      Build the LangChain4J speech-synthesis model described by config.

      Exposed as the same escape hatch as transcriptionModel(VoiceConfig).

      Parameters:
      config - the voice settings
      Returns:
      a ready TextToSpeechModel