Audio decoding, encoding, and transformation
This topic describes how to use the decoding, encoding, and transformation functions among audio functions, including AUDIO_DECODE, AUDIO_ENCODE, AUDIO_CONVERT_FORMAT, AUDIO_STANDARDIZE, AUDIO_RESAMPLE, and AUDIO_CONCAT.
The functions in this topic are provided by the Python multimodal function library (VVR 11.8 or later). Before you use them, add the following statements at the beginning of the SQL deployment:
LOAD MODULE python;
USE MODULES python, core;
Audio decoding and encoding functions convert between encoded bytes and float32 PCM waveforms. Transformation functions operate directly on waveforms to change the sample rate or the number of channels, or to concatenate multiple audio segments. Most downstream audio processing functions, such as segmentation and speech recognition, require a decoded waveform as input.
Function list
|
Function |
Category |
Description |
|
Decoding |
Decodes |
|
|
Encoding |
Encodes a float32 PCM audio waveform into WAV, FLAC, AIFF, MP3, or OGG bytes.Suitable for persisting or transferring a waveform after transformation, concatenation, or segmentation, or for passing it to a system that accepts only encoded files. For example, save a standardized 16 kHz mono waveform as WAV. |
|
|
Format conversion |
Decodes the input and then re-encodes it into new bytes in the target format.Suitable when downstream systems require a specific container or encoding format. For example, normalize audio from different sources into formats such as WAV and FLAC. |
|
|
Transformation |
Converts channels and then resamples to standardize an audio waveform to the specified sample rate and number of channels.Suitable for normalizing input from different sources before ASR, VAD, or model inference. |
|
|
Transformation |
Changes the sample rate of an audio waveform by using soxr HQ while retaining the original number of channels.Suitable when downstream systems require a specific sample rate but the original channel layout must be preserved. For example, convert a 48 kHz stereo recording to 16 kHz stereo. |
|
|
Transformation |
Concatenates multiple audio waveforms that have the same sample rate, number of channels, and sample format, in array order.Suitable for reassembling segmented clips or merging the results of segmented processing. For example, restore consecutive clips that were denoised in parallel into a single audio stream. |
Audio decoding, encoding, and transformation
AUDIO_DECODE
Decodes encoded audio or an audio segment reference into a float32 PCM waveform.
AUDIO_DECODE(
audio_input
[, on_error
[, max_decoded_bytes]]
)
|
Parameter |
Type |
Default value |
Description |
|
|
|
Required |
The encoded audio bytes, URI, or audio segment reference. |
|
|
|
|
The error handling policy. |
|
|
|
|
The maximum number of bytes of the decoded PCM tensor for a single row. The value must be greater than 0 and cannot exceed |
Return type: audio waveform ROW.
-
The default
on_errorin SQL is'null'. This differs from the default value of the Python DataFrame interface. -
The decoded size is calculated as
frames × channels × 4. The default upper limit is 1 GiB. -
'null'handles media data errors such as corrupted audio, truncated audio, empty audio, and a decoded size that exceeds the limit. URI access errors, invalid segment references, missing dependencies, parameter errors, and other internal errors are still thrown. -
For a segment reference, the output start and end times are converted based on the actual sample frame boundaries. For a bytes input, the source URI and the start and end times are
NULL.
SELECT AUDIO_DECODE(audio_bytes) AS waveform
FROM raw_audio;
AUDIO_ENCODE
Encodes an audio waveform into bytes in the specified format.
AUDIO_ENCODE(audio_input [, format])
|
Parameter |
Type |
Default value |
Description |
|
|
Audio waveform |
Required |
The audio waveform to encode. |
|
|
|
|
The target format. The value is case-insensitive. |
Return type: BYTES. If the input is NULL, the function returns NULL.
The supported canonical formats and aliases are as follows:
|
Canonical format |
Alias |
Default encoding subtype |
|
|
|
PCM 16 bit |
|
|
None |
PCM 16 bit |
|
|
|
MPEG Layer III |
|
|
|
Vorbis |
|
|
|
PCM 16 bit |
The formats actually available also depend on soundfile/libsndfile in the runtime environment. The function throws an exception if the format or encoding subtype is not supported by the runtime, the waveform structure is invalid, or the samples contain NaN or infinite values.
SELECT AUDIO_ENCODE(waveform, 'flac') AS flac_bytes
FROM audio_waveforms;
AUDIO_CONVERT_FORMAT
Converts encoded audio into another encoding format. The function decodes the audio first and then re-encodes it in the target format.
AUDIO_CONVERT_FORMAT(
audio_input
[, format
[, on_error
[, max_decoded_bytes]]]
)
|
Parameter |
Type |
Default value |
Description |
|
|
|
Required |
The encoded audio bytes or URI. Audio segment references and audio waveforms are not supported. |
|
|
|
|
The target format. The valid values are the same as those of |
|
|
|
|
The decoding error handling policy. |
|
|
|
|
The maximum number of bytes of the intermediate PCM tensor. The constraints are the same as those of |
Return type: BYTES.
The default on_error in SQL is 'null'.'null' handles only recognizable media data errors in the decoding stage. An invalid target format, a failure in the encoding stage, URI errors, dependency errors, and parameter errors are still thrown. If the input is NULL, the function returns NULL.
SELECT AUDIO_CONVERT_FORMAT(audio_bytes, 'flac') AS flac_bytes
FROM raw_audio;
AUDIO_STANDARDIZE
Standardizes an audio waveform to the specified sample rate and number of channels.
AUDIO_STANDARDIZE(audio_input [, sample_rate [, channels]])
|
Parameter |
Type |
Default value |
Description |
|
|
Audio waveform |
Required |
The audio waveform to standardize. |
|
|
|
|
The target sample rate. The value must be greater than 0. |
|
|
|
|
The target number of channels. The value must be greater than 0. |
Return type: audio waveform ROW. If the input is NULL, the function returns NULL.
The function converts channels first and then resamples by using soxr HQ. When converting multi-channel to mono, the average of the channels is taken. When converting mono to multi-channel, the mono channel is duplicated. When converting multi-channel to a different number of channels, the audio is first mixed down to mono and then duplicated. The output retains the source_uri, start_time_ms and end_time_ms.
SELECT AUDIO_STANDARDIZE(AUDIO_DECODE(audio_bytes), 16000, 1)
FROM raw_audio;
AUDIO_RESAMPLE
Changes only the sample rate of an audio waveform and retains the number of channels.
AUDIO_RESAMPLE(audio_input, sample_rate)
|
Parameter |
Type |
Default value |
Description |
|
|
Audio waveform |
Required |
The audio waveform to resample. |
|
|
|
Required |
The target sample rate. The value must be greater than 0. |
Return type: audio waveform ROW. If the input is NULL, the function returns NULL.
The function resamples by using soxr HQ and adjusts the number of output frames to ceil(input frames × target sample rate / input sample rate). Truncation or zero padding is applied if necessary. If the target sample rate equals the input sample rate, the function validates the waveform and returns it unchanged. The output retains the number of channels and the source fields.
SELECT AUDIO_RESAMPLE(waveform, 8000)
FROM audio_waveforms;
AUDIO_CONCAT
Concatenates multiple audio waveforms in array order.
AUDIO_CONCAT(audio_inputs)
|
Parameter |
Type |
Default value |
Description |
|
|
|
Required |
A non-empty array of audio waveforms. |
Return type: audio waveform ROW.
-
If the input array is
NULL, the function returnsNULL. An empty array throws an exception. -
All waveforms must have the same sample rate, number of channels, and sample format, and must meet the float32 interleaved waveform constraints.
-
When there is only one input, its source URI and start and end times are retained as is, including cases where only some source fields are set.
-
For multiple inputs, the merged source and start and end times are retained only when the
source_urivalues are all identical, every segment has complete start and end times, and the start of each subsequent segment equals the end of the previous one. Otherwise, all three source fields are set toNULL.
SELECT AUDIO_CONCAT(ARRAY[segment_1, segment_2]) AS combined
FROM audio_segments;