Audio splitting

Updated at:

Audio splitting functions split the input audio into segments by fixed duration, specified timestamps, or voice activity ranges. They are suitable for long-audio segmentation and pre-splitting before speech recognition.

Description

The functions in this topic are provided by the Python multimodal function library (VVR 11.8+). Load the module at the beginning of the SQL job before use:

LOAD MODULE python;
USE MODULES python, core;
  • Audio splitting functions return a table with a single segment column, one segment per row. Audio segments use a half-open interval [start_ms, end_ms).

  • The segment_type Must be a constant 'audio' or 'ref', which also determines the input requirement and the static type of the returned column:

    • 'audio' requires a decoded audio waveform and returns a materialized waveform.

    • 'ref' requires a URI or audio segment reference and returns a reference containing only the URI and time bounds.

  • Audio splitting functions are table functions (TableFunction) and must be invoked via LATERAL TABLE.

Function summary

Function

Whether ref mode opens the source file

Description

AUDIO_SPLIT_BY_DURATION

(probes duration)

Splits audio into segments of the specified fixed duration in order, based on segment_type output, either an actual audio waveform or a segment reference.Suitable for handling audio longer than the downstream single-call limit., for example splitting a one-hour recording into 30-second segments and sending them to ASR in parallel. The last segment may be shorter.

AUDIO_SPLIT_BY_TIMESTAMP

(fully lazy)

Splits audio using caller-provided start and end time ranges and outputs audio waveforms or segment references in the original order.Suitable for scenarios where subtitles, logs, meeting minutes, or manually annotated timelines are already available., for example extracting audio by subtitle timing or locating spoken segments from meeting minutes.

AUDIO_SPLIT_BY_SPEECH

(reads metadata)

Based on existing voice activity ranges, this function pads, merges, filters short segments, and splits long ones, then outputs audio waveforms or segment references.Suitable for handling calls, interviews, or podcasts with lots of silence., for example combined with AUDIO_DETECT_SPEECH Skips gaps and produces only speech segments suitable for ASR.

AUDIO_SPLIT_BY_DURATION

Split audio into segments of a fixed duration.

AUDIO_SPLIT_BY_DURATION(
 audio_input,
 segment_duration_ms
 [, segment_type
 [, max_segments]]
)

Parameter

Type

Default

Description

audio_input

audio waveform ROW; or 'ref' In STRING, audio segment ROW

Required

Audio to split.input NULL, no rows are emitted.

segment_duration_ms

INT NOT NULL

Required

Target duration per segment, in milliseconds. Must be greater than 0.

segment_type

STRING NOT NULL

'audio'

outputmode: 'audio' or 'ref'.

max_segments

INT NOT NULL

1024

Maximum number of segments per input. Must be greater than 0.

Returns: segment. segment_type 'audio', the static type is audio waveform ROW; 'ref', the static type is audio segment reference ROW.

Description:

  • 'audio' mode requires the input to be a decoded audio waveform and materializes the samples of each segment.

  • 'ref' mode requires a URI or audio segment reference. The function probes the duration and clips against the actual duration and existing reference bounds, generating references only without copying audio content.

  • The last segment may be shorter than segment_duration_ms.

  • When the required segment count exceeds max_segments, an exception is raised rather than silently truncating.

  • segment_type, defaults to 'audio'. In that case, the input is treated as a waveform. If audio_input is a URI or an audio segment reference, you must explicitly pass 'ref'; otherwise a type-mismatch error occurs at runtime.

SELECT t.id, s.segment
FROM audio_waveforms AS t
CROSS JOIN LATERAL TABLE(
 AUDIO_SPLIT_BY_DURATION(t.waveform, 30000, 'audio', 1024)
) AS s(segment);

SELECT t.id, s.segment
FROM audio_files AS t
CROSS JOIN LATERAL TABLE(
 AUDIO_SPLIT_BY_DURATION(t.uri, 30000, 'ref', 1024)
) AS s(segment);

AUDIO_SPLIT_BY_TIMESTAMP

Split audio using caller-provided time ranges.

AUDIO_SPLIT_BY_TIMESTAMP(
 audio_input,
 timestamps
 [, segment_type]
)

Parameter

Type

Default

Description

audio_input

audio waveform ROW; or 'ref' In STRING, audio segment ROW

Required

Audio to split.

timestamps

range ROW array

Required

ARRAY<ROW<start_ms BIGINT, end_ms BIGINT>> or duration_ms field with the three-field form. This parameter can come from a table column.

segment_type

STRING NOT NULL

'audio'

outputmode: 'audio' or 'ref'.

Returns: segment. Return type segment_type.

range, end_ms must be greater than or equal to start_ms. In the three-field form, duration_ms is not used in computation; only start_ms and end_ms are used. If the input or timestamps is NULL, no rows are emitted.

  • 'audio' mode clips ranges against the actual timeline of the waveform. Ranges that are entirely out of range or empty after clipping are dropped.

  • 'ref' mode is fully lazy for plain URIs: it does not open the file, does not verify that the URI exists, and does not clip against the actual audio duration.

  • 'ref' mode only clips existing audio segment references against their declared bounds, without validating whether the bounds exceed the actual file duration.

  • Output order follows timestamps input,.

SELECT t.id, s.segment
FROM audio_files AS t
CROSS JOIN LATERAL TABLE(
 AUDIO_SPLIT_BY_TIMESTAMP(
 t.uri,
 ARRAY[
 ROW(CAST(1000 AS BIGINT), CAST(3500 AS BIGINT)),
 ROW(CAST(5000 AS BIGINT), CAST(7000 AS BIGINT))
 ],
 'ref'
 )
) AS s(segment);

AUDIO_SPLIT_BY_SPEECH

Splits audio using upstream-generated voice activity ranges. This function does not perform speech detection itself; use AUDIO_DETECT_SPEECH output as speech_activity input.

SQL supports only the 2-, 3-, and 9-argument forms:

AUDIO_SPLIT_BY_SPEECH(audio_input, speech_activity)

AUDIO_SPLIT_BY_SPEECH(
 audio_input,
 speech_activity,
 segment_type
)

AUDIO_SPLIT_BY_SPEECH(
 audio_input,
 speech_activity,
 segment_type,
 pre_padding_ms,
 post_padding_ms,
 merge_gap_ms,
 min_segment_ms,
 max_segment_ms,
 max_segments
)

Parameter

Type

Default

Description

audio_input

audio waveform ROW; or 'ref' In STRING, audio segment ROW

Required

Audio to split.

speech_activity

range ROW array

Required

Array of two-field or three-field time ranges. In the three-field form, duration_ms is not used in computation. This parameter can come from a table column.

segment_type

STRING NOT NULL

'audio'

outputmode: 'audio' or 'ref'.

pre_padding_ms

INT NOT NULL

0

Padding to add before each segment. Must be greater than or equal to 0.

post_padding_ms

INT NOT NULL

0

Padding to add after each segment. Must be greater than or equal to 0.

merge_gap_ms

INT NOT NULL

0

Merges ranges whose gap is at most this value. Must be greater than or equal to 0.

min_segment_ms

INT NOT NULL

0

Discards ranges shorter than this value. Must be greater than or equal to 0.

max_segment_ms

INT

NULL

Ranges longer than this value are split into consecutive non-overlapping chunks of at most max_segment_ms. The last chunk of the same range may be shorter. Chunking is based on the padded and merged ranges, so pre_padding_ms and post_padding_ms is honored. When non-NULL, must be greater than 0.

max_segments

INT NOT NULL

1024

Maximum number of resulting segments. Must be greater than 0.

Returns: segment. Return type segment_type.

To set any tuning parameter, you must specify all nine positional arguments at once; the four- to eight-argument forms cannot be used. When max_segment_ms is not enabled, use NULL directly.

The function processes speech ranges in the following order:

  1. Adds padding before and after each range; the start is clamped to 0.

  2. by start time.

  3. Merges overlapping ranges as well as those whose gap is at most merge_gap_ms interval.

  4. Drops ranges with non-positive duration and those shorter than min_segment_ms interval.

  5. max_segment_ms. Long ranges are further split into consecutive chunks, each no longer than this value; the last chunk may be shorter.

  6. result max_segments, an exception is raised.

  7. Clips against the waveform or audio object bounds and drops ranges that are entirely out of range.

and AUDIO_SPLIT_BY_TIMESTAMP(..., 'ref') differs: speech splitting 'ref' mode reads metadata to determine and clip against the actual audio duration. If the input or speech_activity NULL, no rows are emitted.

WITH detected AS (
 SELECT
 id,
 waveform,
 AUDIO_DETECT_SPEECH(waveform, 2, 30, 100, 50, 1024) AS speech_ranges
 FROM audio_waveforms
)
SELECT t.id, s.segment
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);