Skip to content

Class: Audio

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / Audio

Defined in: src/core/media/Audio.ts:75

Represents an audio file that can be used in UPP messages.

Audio can be created from various sources (files, bytes, base64) and converted to different formats as needed by providers. The class provides a unified interface regardless of the underlying source type.

Note: Providers have size limits for inline audio data. Google Gemini limits inline data to 20MB per request. For larger files, consider using provider-specific file upload APIs.

// Load from file
const fileAudio = await Audio.fromPath('./recording.mp3');
// From raw bytes
const bytesAudio = Audio.fromBytes(uint8Array, 'audio/wav');
// Use in a message
const message = new UserMessage([audio.toBlock()]);

readonly data: Uint8Array

Defined in: src/core/media/Audio.ts:77

The audio data as raw bytes


readonly optional duration: number

Defined in: src/core/media/Audio.ts:81

Duration in seconds, if known


readonly mimeType: string

Defined in: src/core/media/Audio.ts:79

MIME type of the audio (e.g., ‘audio/mp3’, ‘audio/wav’)

get size(): number

Defined in: src/core/media/Audio.ts:96

Gets the size of the audio data in bytes.

number

toBase64(): string

Defined in: src/core/media/Audio.ts:105

Converts the audio to a base64-encoded string.

string

The audio data as a base64 string


toBlock(): AudioBlock

Defined in: src/core/media/Audio.ts:137

Converts this Audio to an AudioBlock for use in UPP messages.

AudioBlock

An AudioBlock that can be included in message content arrays


toBytes(): Uint8Array

Defined in: src/core/media/Audio.ts:128

Gets the audio data as raw bytes.

Uint8Array

The audio data as a Uint8Array


toDataUrl(): string

Defined in: src/core/media/Audio.ts:118

Converts the audio to a data URL suitable for embedding.

string

A data URL in the format data:{mimeType};base64,{data}


static fromBase64(base64, mimeType, duration?): Audio

Defined in: src/core/media/Audio.ts:219

Creates an Audio from a base64-encoded string.

string

The base64-encoded audio data (without data URL prefix)

string

The MIME type of the audio

number

Optional duration in seconds

Audio

An Audio containing the decoded data

const audio = Audio.fromBase64(base64String, 'audio/mp3');

static fromBlock(block): Audio

Defined in: src/core/media/Audio.ts:237

Creates an Audio from an existing AudioBlock.

Useful for converting content blocks received from providers back into Audio instances for further processing.

AudioBlock

An AudioBlock from message content

Audio

An Audio with the block’s data and metadata


static fromBytes(data, mimeType, duration?): Audio

Defined in: src/core/media/Audio.ts:202

Creates an Audio from raw byte data.

Uint8Array

The audio data as a Uint8Array

string

The MIME type of the audio

number

Optional duration in seconds

Audio

An Audio containing the byte data

const audio = Audio.fromBytes(wavData, 'audio/wav');

static fromPath(path, duration?): Promise<Audio>

Defined in: src/core/media/Audio.ts:161

Creates an Audio by reading a file from disk.

The file is read into memory as bytes. MIME type is automatically detected from the file extension.

string

Path to the audio file

number

Optional duration in seconds

Promise<Audio>

Promise resolving to an Audio with the file contents

const audio = await Audio.fromPath('./recordings/interview.mp3');