Class Speech
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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Speech
- 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
- Returns:
- the audio MIME type, suitable for an HTTP
Content-Typeheader.
-
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
Write the audio to a stream — e.g. a servlet's or JAX-RSStreamingOutput's.- Parameters:
out- the destination stream (not closed by this method)- Throws:
IOException- if writing fails
-
saveTo
Save the audio to a file, creating or truncating it.- Parameters:
path- the destination file- Throws:
IOException- if writing fails
-
toString
-