Package com.tnsai.models.chat
Record Class AudioPart
java.lang.Object
java.lang.Record
com.tnsai.models.chat.AudioPart
- All Implemented Interfaces:
ContentPart
public record AudioPart(String data, String mimeType, AudioPart.AudioSource source)
extends Record
implements ContentPart
Represents an audio part of a message.
Supports Base64 data, file path, or URL.
Supported formats depend on the provider:
- OpenAI Whisper: mp3, mp4, mpeg, mpga, m4a, wav, webm
- Gemini: mp3, wav, aiff, aac, ogg, flac
Usage examples:
// From Base64 data
AudioPart audio = AudioPart.fromBase64(base64Data, "audio/mp3");
// From file
AudioPart audio = AudioPart.fromFile(new File("speech.wav"));
// From URL
AudioPart audio = AudioPart.fromUrl("https://example.com/audio.mp3");
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionAudioPart(String data, String mimeType, AudioPart.AudioSource source) Creates an instance of aAudioPartrecord class. -
Method Summary
Modifier and TypeMethodDescriptiondata()Returns the value of thedatarecord component.final booleanIndicates whether some other object is "equal to" this one.static AudioPartfromBase64(String base64Data, String mimeType) Creates an AudioPart from Base64 encoded data.static AudioPartCreates an AudioPart from a byte array.static AudioPartCreates an AudioPart from a file.static AudioPartCreates an AudioPart from a URL.byte[]getBytes()Gets the raw bytes if this is a Base64 audio part.getType()final inthashCode()Returns a hash code value for this object.booleanisBase64()Returns true if this is a Base64-based audio part.booleanisUrl()Returns true if this is a URL-based audio part.mimeType()Returns the value of themimeTyperecord component.source()Returns the value of thesourcerecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
AudioPart
Creates an instance of aAudioPartrecord class.- Parameters:
data- the value for thedatarecord componentmimeType- the value for themimeTyperecord componentsource- the value for thesourcerecord component
-
-
Method Details
-
fromBase64
Creates an AudioPart from Base64 encoded data.- Parameters:
base64Data- The Base64 encoded audio datamimeType- The MIME type (e.g., "audio/mp3", "audio/wav")- Returns:
- AudioPart with Base64 source
-
fromUrl
Creates an AudioPart from a URL.- Parameters:
url- The URL pointing to the audio file- Returns:
- AudioPart with URL source
-
fromFile
Creates an AudioPart from a file. The file is read and encoded as Base64.- Parameters:
file- The audio file- Returns:
- AudioPart with Base64 encoded file contents
- Throws:
RuntimeException- if file cannot be read
-
fromBytes
Creates an AudioPart from a byte array.- Parameters:
bytes- The audio bytesmimeType- The MIME type- Returns:
- AudioPart with Base64 encoded data
-
getType
- Specified by:
getTypein interfaceContentPart
-
isUrl
public boolean isUrl()Returns true if this is a URL-based audio part. -
isBase64
public boolean isBase64()Returns true if this is a Base64-based audio part. -
getBytes
public byte[] getBytes()Gets the raw bytes if this is a Base64 audio part.- Returns:
- Decoded bytes
- Throws:
IllegalStateException- if source is URL
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
data
Returns the value of thedatarecord component.- Returns:
- the value of the
datarecord component
-
mimeType
Returns the value of themimeTyperecord component.- Returns:
- the value of the
mimeTyperecord component
-
source
Returns the value of thesourcerecord component.- Returns:
- the value of the
sourcerecord component
-