Audio

Audio values to BAML functions can be created in client libraries. This document explains how to use these functions both at compile time and runtime to handle audio data. For more details, refer to audio types.

Usage Examples

1from baml_py import Audio
2from baml_client import b
3
4async def test_audio_input():
5 # Create an Audio object from a URL
6 audio = Audio.from_url("https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg")
7 res = await b.TestAudioInput(audio=audio)
8
9 # Create an Audio object from Base64 data
10 audio_b64 = "iVB0xyz..."
11 audio = Audio.from_base64("audio/ogg", audio_b64)
12 res = await b.TestAudioInput(audio=audio)

Static Methods

fromUrl
(url: string, mediaType?: string) => Audio

Creates an Audio object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.

fromBase64
(mediaType: string, base64: string) => Audio

Creates an Audio object using Base64 encoded data along with the given MIME type.

fromFile
(file: File) => Promise<Audio>

Only available in browser environments. @boundaryml/baml/browser
Creates an Audio object from a File object. Available in browser environments only.

fromBlob
(blob: Blob, mediaType?: string) => Promise<Audio>

Only available in browser environments. @boundaryml/baml/browser
Creates an Audio object from a Blob object. Available in browser environments only.

fromUrlToBase64
(url: string) => Promise<Audio>

Only available in browser environments.
Creates an Audio object by fetching from a URL. Available in browser environments only.

Instance Methods

isUrl
() => boolean

Check if the audio is stored as a URL.

asUrl
() => string

Get the URL of the audio if it’s stored as a URL. Throws an Error if the audio is not stored as a URL.

asBase64
() => [string, string]

Get the base64 data and media type if the audio is stored as base64. Returns [base64Data, mediaType]. Throws an Error if the audio is not stored as base64.

toJSON
() => { url: string } | { base64: string; media_type: string }

Convert the audio to a JSON representation. Returns either a URL object or a base64 object with media type.