Class Speech

java.lang.Object
dyntabs.ai.voice.Speech

public final class Speech extends Object
One piece of synthesized audio: the bytes plus the MIME type needed to serve them.

Why both halves travel together

A bare byte[] is not enough to answer an HTTP request: the caller must also set a Content-Type, and guessing it is how audio ends up silently unplayable in a browser. Keeping the two together means a web endpoint never has to invent the second half:


 Speech speech = EasyAI.speak().from("Order ORD-004 has been delivered.");
 return Response.ok(speech.bytes()).type(speech.mimeType()).build();
 

base64() is there for the other common destination — embedding the clip straight into a JSON reply or an HTML <audio src="data:..."> attribute.

Note on the array: bytes() hands back the internal array without copying. Audio clips are large enough that defensive copying on every call would be a real cost for a value nobody mutates in practice; treat it as read-only.

Analogy: a parcel with its shipping label already attached. The contents alone would get stuck at the depot — it is the label that tells the courier what to do with it.

See Also:
  • Constructor Details

    • Speech

      public Speech(byte[] bytes, String mimeType)
      Parameters:
      bytes - the raw audio bytes (required, non-empty)
      mimeType - the audio MIME type, e.g. "audio/mpeg"
  • Method Details

    • bytes

      public byte[] bytes()
      Returns:
      the raw audio bytes (not copied — treat as read-only).
    • mimeType

      public String mimeType()
      Returns:
      the audio MIME type, suitable for an HTTP Content-Type header.
    • base64

      public String base64()
      Returns:
      the audio as Base64, for a JSON field or a data: URI.
    • size

      public int size()
      Returns:
      the clip size in bytes.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Write the audio to a stream — e.g. a servlet's or JAX-RS StreamingOutput's.
      Parameters:
      out - the destination stream (not closed by this method)
      Throws:
      IOException - if writing fails
    • saveTo

      public void saveTo(Path path) throws IOException
      Save the audio to a file, creating or truncating it.
      Parameters:
      path - the destination file
      Throws:
      IOException - if writing fails
    • toString

      public String toString()
      Overrides:
      toString in class Object