Audio function overview

Updated at:

This topic describes the usage constraints, usage patterns, audio data forms, and an example audio processing pipeline for the audio functions.

Limits

Supported only in Realtime Compute engine VVR 11.8 and later.

Usage

Description

In platform SQL jobs, the audio functions are provided by the built-in python module and require no additional dependencies. Load the python module, and then enable python and core:

LOAD MODULE python;
USE MODULES python, core;

Basic invocation

LOAD MODULE python;
USE MODULES python, core;

SELECT
 AUDIO_METADATA(audio_bytes).duration_ms AS duration_ms,
 AUDIO_STANDARDIZE(AUDIO_DECODE(audio_bytes), 16000, 1) AS waveform
FROM raw_audio;

Parameter

SQL functions accept only positional arguments in the order defined by the signature. In the signature, [ ] indicates optional parameters., AUDIO_DETECT_SPEECH has the following signature:

AUDIO_DETECT_SPEECH(
 audio_input
 [, aggressiveness
 [, frame_ms
 [, min_speech_ms
 [, merge_gap_ms
 [, max_segments]]]]]
)

Follow these rules when calling the function:

  • Omitted optional parameters use their default values.

  • To set a later optional parameter, also specify all preceding optional parameters.

  • NULL is passed as the argument value and is not equivalent to omitting the parameter. Polymorphic data parameters must use NULL can be used via CAST(NULL AS ) to specify the exact type.

  • Except for per-row audio inputs (for example, audio_input and audio_inputs), timestamps and speech_activity), all other parameters are configuration parameters and must be SQL literals; table columns and CAST.

Data types

Audio data forms

Data form

SQL type

Description

encoding

BYTES

Encoded audio bytes.

Audio URI

STRING

An audio URI accessible to the Flink file system.

Audio segment reference

ROW<uri STRING, start_time_ms BIGINT, end_time_ms BIGINT>

Points to a time segment inside encoded audio without copying the audio content.

Decoded audio waveform

AUDIO_WAVEFORM ROW

Interleaved float32 PCM samples along with sample rate, channel count, and source information.

Different functions accept different data forms. Refer to the parameter table of each function.

Audio segment reference

ROW<
 uri STRING,
 start_time_ms BIGINT,
 end_time_ms BIGINT
>

Audio segments use a half-open interval [start_time_ms, end_time_ms). The end time is exclusive. The start of the next adjacent segment equals the end of the previous one. Times must be non-negative integers; NULL as the start or end represents the beginning or end of the file, respectively.

Decoded audio waveform

ROW<
 data TENSOR('float32'),
 sample_rate INT,
 channels INT,
 frames BIGINT,
 sample_format STRING,
 layout STRING,
 duration_ms BIGINT,
 source_uri STRING,
 start_time_ms BIGINT,
 end_time_ms BIGINT
>

type

Description

data

TENSOR('float32')

(frames, channels) float32 PCM.

sample_rate

INT

Sample rate, in Hz.

channels

INT

channel.

frames

BIGINT

Number of sample frames per channel.

sample_format

STRING

The current implementation requires 'float32'.

layout

STRING

The current implementation requires 'interleaved'.

duration_ms

BIGINT

Waveform duration, in milliseconds.

source_uri

STRING

Source URI. For byte inputs or when the source cannot be preserved, this is NULL.

start_time_ms

BIGINT

Start time of the waveform in the source audio.

end_time_ms

BIGINT

End time of the waveform in the source audio, exclusive.

Valid waveform requirements sample_rate, channels and frames to be positive; the number of samples must match the shape and must not contain NaN or infinity.

Audio metadata

ROW<
 sample_rate INT,
 channels INT,
 frames BIGINT,
 duration_ms BIGINT,
 format STRING,
 codec STRING,
 bit_rate BIGINT,
 size BIGINT
>

type

Description

sample_rate

INT

Sample rate, in Hz.

channels

INT

channel.

frames

BIGINT

Number of sample frames.

duration_ms

BIGINT

Duration, in milliseconds.

format

STRING

Container format.

codec

STRING

Best-effort short codec name, for example pcm_s16le.

bit_rate

BIGINT

Bitrate. If it cannot be determined, this is NULL.

size

BIGINT

Encoded object size, in bytes.

Audio time ranges

Silence and voice detection functions return:

ROW<start_ms BIGINT, end_ms BIGINT, duration_ms BIGINT>

Timestamp splitting and speech splitting also accept ranges without duration_ms:

ROW<start_ms BIGINT, end_ms BIGINT>

Time ranges use a half-open interval [start_ms, end_ms). Time in detection and splitting parameters is usually in milliseconds; AUDIO_DURATION and AUDIO_DURATION_FILTER uses seconds. When materializing waveform segments, millisecond boundaries are converted to sample frames and rounded, so non-frame-aligned boundaries may introduce a small timing offset.

Complete pipeline example

Consider preprocessing customer-service recordings: the raw_audio table stores original recording bytes (audio_bytes BINARY, from object storage or a message queue), and downstream systems need to split them by speech intervals for ASR.

The following pipeline sequentially decodes the audio, standardizes it to 16 kHz mono, detects voice activity intervals, splits it into independent segments, and computes the duration of each segment.

LOAD MODULE python;
USE MODULES python, core;

-- Step 1: Decode raw audio bytes and standardize the audio to a 16 kHz mono waveform.
WITH decoded AS (
 SELECT
 id,
 AUDIO_STANDARDIZE(
 AUDIO_DECODE(audio_bytes),
 16000,
 1
 ) AS waveform
 FROM raw_audio
),

-- Step 2: Filter out audio that failed to decode, and detect speech intervals in each waveform.
detected AS (
 SELECT
 id,
 waveform,
 AUDIO_DETECT_SPEECH(
 waveform,
 2,
 30,
 100,
 50,
 1024
 ) AS speech_ranges
 FROM decoded
 WHERE waveform IS NOT NULL
)

-- Step 3: Split each audio into speech segments based on the detected speech intervals, and calculate the duration of each segment.
SELECT
 t.id,
 s.segment,
 AUDIO_DURATION(s.segment) AS segment_duration_seconds
FROM detected AS t
CROSS JOIN LATERAL TABLE(
 AUDIO_SPLIT_BY_SPEECH(
 t.waveform,
 t.speech_ranges,
 'audio',
 200,
 200,
 50,
 100,
 NULL,
 1024
 )
) AS s(segment);