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");
 
  • Constructor Details

    • AudioPart

      public AudioPart(String data, String mimeType, AudioPart.AudioSource source)
      Creates an instance of a AudioPart record class.
      Parameters:
      data - the value for the data record component
      mimeType - the value for the mimeType record component
      source - the value for the source record component
  • Method Details

    • fromBase64

      public static AudioPart fromBase64(String base64Data, String mimeType)
      Creates an AudioPart from Base64 encoded data.
      Parameters:
      base64Data - The Base64 encoded audio data
      mimeType - The MIME type (e.g., "audio/mp3", "audio/wav")
      Returns:
      AudioPart with Base64 source
    • fromUrl

      public static AudioPart fromUrl(String url)
      Creates an AudioPart from a URL.
      Parameters:
      url - The URL pointing to the audio file
      Returns:
      AudioPart with URL source
    • fromFile

      public static AudioPart fromFile(File file)
      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

      public static AudioPart fromBytes(byte[] bytes, String mimeType)
      Creates an AudioPart from a byte array.
      Parameters:
      bytes - The audio bytes
      mimeType - The MIME type
      Returns:
      AudioPart with Base64 encoded data
    • getType

      public String getType()
      Specified by:
      getType in interface ContentPart
    • 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

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • data

      public String data()
      Returns the value of the data record component.
      Returns:
      the value of the data record component
    • mimeType

      public String mimeType()
      Returns the value of the mimeType record component.
      Returns:
      the value of the mimeType record component
    • source

      public AudioPart.AudioSource source()
      Returns the value of the source record component.
      Returns:
      the value of the source record component