Skip to main content

Crate silence_split

Crate silence_split 

Source
Expand description

Split long PCM audio into pieces that never exceed a maximum length, cutting in silence, or at the quietest spot when there is none. No model, no FFmpeg, and every position is a sample index in the original timeline.

  1. Analyzer turns PCM of any sample rate into frame levels (Analysis). Scores from an external voice activity detector can be used instead with Analysis::from_scores.
  2. Analysis::segments finds silence and sound, and Analysis::trim the audio between the first and last sound.
  3. plan chooses cuts over the whole file under minimum, target and maximum piece lengths. adjust instead moves cut points you already have into nearby silence.
use core::time::Duration;
use silence_split::{AnalyzeConfig, Analyzer, Gap, Layout, PlanConfig, SegmentConfig, plan};

let layout = Layout { sample_rate: 16_000, channels: 1 };
let mut analyzer = Analyzer::new(layout, &AnalyzeConfig::default())?;
analyzer.push_interleaved(&pcm);
let analysis = analyzer.finish();

let config = PlanConfig::new(Duration::from_secs(2), Duration::from_secs(8), Duration::from_secs(10))?
    .gap(Gap::Drop);
let plan = plan(&analysis, &SegmentConfig::default(), &config);
for piece in &plan.pieces {
    let _samples = &pcm[piece.start as usize..piece.end as usize];
    assert!(piece.end - piece.start <= 160_000);
}

§Features

  • std (default): uses the standard library’s floating-point functions. Without it the crate is no_std with alloc and needs the libm feature.
  • libm: floating-point functions for no_std.
  • serde: Serialize and Deserialize for Analysis, Plan and the result types. Deserializing an Analysis validates it.

Structs§

AdjustConfig
Search window and cut preferences for adjust.
AdjustResult
The adjusted plan and the result for every original input point, in the caller’s order.
Analysis
Frame values on the original sample timeline, from an Analyzer or external scores.
AnalyzeConfig
Window and hop of the PCM analyzer.
Analyzer
Incremental PCM analyzer. Push decoded audio in any block size, then finish.
Cut
A boundary between two consecutive pieces.
Layout
Sample rate and channel count of the original audio.
Levels
Levels resolved against one analysis and a segmentation threshold.
Piece
A piece of the original audio, [start, end) in samples.
Plan
Pieces in order and the cuts between them: cuts[i] separates pieces[i] and pieces[i + 1].
PlanConfig
Piece length limits and cut preferences for plan.
ScoreFrames
The detector’s sample rate and hop for Analysis::from_scores.
SegmentConfig
How frame values become silence and sound spans.
Span
A half-open range [start, end) of original samples.
Weights
Cost weights of the planner.

Enums§

ConfigError
A configuration or input that cannot produce an analysis or a plan.
CutReason
What a cut falls in.
Gap
What happens to the silence at a cut.
Kind
The content of a Span.
Placement
Where to cut inside a silence that is longer than its padding.
Threshold
Where a frame turns from silence to sound.

Traits§

Sample
A PCM sample type the analyzer accepts: i16 or f32.

Functions§

adjust
Moves cut points to nearby candidates, keeping all audio and returning each point’s result.
plan
Chooses cuts over the whole audio that keep every piece within max, minimizing the total cost of all cuts and piece lengths (see Weights).
write_audacity_labels
Writes one Audacity label per piece, numbered from 1, as start\tend\tlabel lines in seconds.