Audio decoding, encoding, and transformation
This topic describes the audio decoding, encoding, format conversion, standardization, resampling, and waveform concatenation operators in the Python DataFrame API.
Limits
-
Available only in Realtime Compute Engine VVR 11.8 and later.
-
This topic uses audio segment reference (AUDIO_CLIP_REF_TYPE) and decoded audio waveform (AUDIO_WAVEFORM_TYPE), which are DataFrame API custom types defined in
pyflink.multimodal.types. For details, see Audio operator overview.
Operator list
|
Category |
Operator |
Description |
|
Decoding |
Decodes |
|
|
Encodes a float32 PCM |
||
|
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, you can normalize audio from different sources into formats such as WAV and FLAC. |
|
|
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. |
||
|
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, you can convert a 48 kHz stereo recording to 16 kHz stereo. |
||
|
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, you can restore consecutive clips that were denoised in parallel into a single audio stream. |
Common runtime parameters
Operator signatures retain the runtime parameters actually supported by each operator. To avoid repetition, operator parameter tables describe only business parameters. Runtime parameters are described below.
|
Parameter |
Type |
Default |
Applies to |
Description |
|
|
|
|
All operators on this page |
The UDF concurrency. |
Audio decoding and encoding
audio_decode
Decodes encoded audio or an audio segment reference into a float32 PCM waveform.
Input type: DataType.binary() (encoded audio bytes column), DataType.string() (audio URI column), or AUDIO_CLIP_REF_TYPE (audio segment reference column).
Signature:
audio_decode(
*columns,
on_error="raise",
max_decoded_bytes=1024 * 1024 * 1024,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
|
|
|
|
|
The maximum number of decoded bytes for a single row. The value must be greater than 0 and cannot exceed 2147483647. |
A null input returns None. Inaccessible URIs, unsupported input types, and invalid segment references still throw exceptions.
Return type: AUDIO_WAVEFORM_TYPE.
from pyflink.dataframe import col
from pyflink.multimodal.operators import audio_decode
result = df.with_column(
"waveform",
audio_decode(
col("audio_bytes"),
on_error="null",
max_decoded_bytes=512 * 1024 * 1024
)
)
audio_encode
Encodes an audio waveform into bytes in the specified format.
Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.
Signature:
audio_encode(
*columns,
format="wav",
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The target audio format. |
The supported formats, aliases, and default encoding subtypes are as follows:
|
Format |
Available names |
Default encoding subtype |
|
WAV |
|
|
|
FLAC |
|
|
|
OGG |
|
|
|
AIFF |
|
|
|
MP3 |
|
|
If the current libsndfile does not support the target format or default encoding subtype, the operator throws an exception.
A null input returns None. An exception is thrown if the input is not valid audio, or if the waveform contains NaN or Inf values.
Return type: DataType.binary().
from pyflink.multimodal.operators import audio_encode
result = df.with_column(
"wav_bytes",
audio_encode(
col("waveform"),
format="wav"
)
)
audio_convert_format
Converts encoded audio into another encoding format. The operator decodes the audio first and then re-encodes it in the target format.
Input type: DataType.binary() or DataType.string(), an encoded audio bytes column or an audio URI column.
Signature:
audio_convert_format(
*columns,
format="wav",
on_error="raise",
max_decoded_bytes=1024 * 1024 * 1024,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The target audio format. |
|
|
|
|
|
|
|
|
|
The maximum number of intermediate PCM bytes allowed during the conversion. |
URI access errors, encoding errors, unsupported formats, and unavailable target encoding subtypes still fail the job. A null input returns None.
Return type: DataType.binary().
from pyflink.multimodal.operators import audio_convert_format
result = df.with_column(
"wav_bytes",
audio_convert_format(
col("audio_uri"),
format="wav",
on_error="null"
)
)
Audio waveform transformation
audio_standardize
Standardizes an audio waveform to the specified sample rate and number of channels.
Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.
Signature:
audio_standardize(
*columns,
sample_rate=16000,
channels=1,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The target sample rate, in Hz. The value must be greater than 0. |
|
|
|
|
The target number of channels. The value must be greater than 0. |
The operator converts channels first and then resamples by using soxr HQ. When converting multi-channel audio to mono, the channels are averaged. When converting mono to multi-channel, the mono channel is duplicated. When converting between different multi-channel counts, the audio is first mixed down to mono and then duplicated to the target number of channels. The output retains the source URI and clip time fields.
Return type: AUDIO_WAVEFORM_TYPE.
from pyflink.multimodal.operators import audio_standardize
result = df.with_column(
"speech_waveform",
audio_standardize(
col("waveform"),
sample_rate=16000,
channels=1
)
)
audio_resample
Changes only the sample rate of an audio waveform and retains the number of channels.
Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.
Signature:
audio_resample(
*columns,
sample_rate,
concurrency=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
Required |
The target sample rate, in Hz. The value must be greater than 0. |
The operator resamples by using soxr HQ and retains the number of channels, the source URI, and the clip time fields. The number of output frames is:
ceil(input_frames * target_sample_rate / input_sample_rate)
The operator truncates or pads the result if necessary to match this frame count. If the target sample rate equals the input sample rate, the operator returns the original waveform after input validation.
Return type: AUDIO_WAVEFORM_TYPE.
from pyflink.multimodal.operators import audio_resample
result = df.with_column(
"resampled",
audio_resample(
col("waveform"),
sample_rate=8000,
),
)
audio_concat
Concatenates multiple audio waveforms in array order.
Input type: DataType.list(AUDIO_WAVEFORM_TYPE), a non-empty array column of audio waveforms.
Signature:
audio_concat(
*columns,
concurrency=None,
)
-
If the input array is
None, the operator returnsNone. 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 toNone.
Return type: AUDIO_WAVEFORM_TYPE.
from pyflink.multimodal.operators import audio_concat
result = df.with_column(
"joined",
audio_concat(col("clips")),
)