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.
Analyzerturns PCM of any sample rate into frame levels (Analysis). Scores from an external voice activity detector can be used instead withAnalysis::from_scores.Analysis::segmentsfinds silence and sound, andAnalysis::trimthe audio between the first and last sound.planchooses cuts over the whole file under minimum, target and maximum piece lengths.adjustinstead 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
Structs§
- Analysis
- Frame values on the original sample timeline, from an
Analyzeror external scores. - Analyze
Config - 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.
- Piece
- A piece of the original audio,
[start, end)in samples. - Plan
- Pieces in order and the cuts between them:
cuts[i]separatespieces[i]andpieces[i + 1]. - Plan
Config - Piece length limits and cut preferences for
plan. - Segment
Config - 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§
- Config
Error - 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:
i16orf32.
Functions§
- adjust
- Moves each requested cut point to the best candidate within
windowof it, keeping the order of the points and all audio (Gap::Keep). - plan
- Chooses cuts over the whole audio that keep every piece within
max, minimizing the total cost of all cuts and piece lengths (seeWeights). - write_
audacity_ labels - Writes one Audacity label per piece, numbered from 1, as
start\tend\tlabellines in seconds.