Audio function overview
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.
-
NULLis passed as the argument value and is not equivalent to omitting the parameter. Polymorphic data parameters must useNULLcan be used viaCAST(NULL ASto specify the exact type.) -
Except for per-row audio inputs (for example,
audio_inputandaudio_inputs),timestampsandspeech_activity), all other parameters are configuration parameters and must be SQL literals; table columns andCAST.
Data types
Audio data forms
|
Data form |
SQL type |
Description |
|
encoding |
|
Encoded audio bytes. |
|
Audio URI |
|
An audio URI accessible to the Flink file system. |
|
|
Points to a time segment inside encoded audio without copying the audio content. |
|
|
|
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 |
|
|
|
|
|
|
|
Sample rate, in Hz. |
|
|
|
channel. |
|
|
|
Number of sample frames per channel. |
|
|
|
The current implementation requires |
|
|
|
The current implementation requires |
|
|
|
Waveform duration, in milliseconds. |
|
|
|
Source URI. For byte inputs or when the source cannot be preserved, this is |
|
|
|
Start time of the waveform in the source audio. |
|
|
|
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, in Hz. |
|
|
|
channel. |
|
|
|
Number of sample frames. |
|
|
|
Duration, in milliseconds. |
|
|
|
Container format. |
|
|
|
Best-effort short codec name, for example |
|
|
|
Bitrate. If it cannot be determined, this is |
|
|
|
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);