# scorpiox-audio-silence-splitter

Pure C99 energy-based silence / chapter splitter for assistant Coder.

**No ffmpeg, ONNX, RNNoise, or FFT libraries.** Vendored dependency: **yyjson only**.

## Build

```bash
make shared   # libscorpiox-audio-silence-splitter.so
make test     # unit + fixture tests
make cli      # bin/silsplit
make all      # shared + cli
```

Flags: `-std=c99 -Wall -Wextra -Werror` (yyjson without `-Werror`).

## API

```c
typedef int (*silsplit_progress_cb)(int percent, const char *stage, void *user_data);
const char *silsplit_analyze(const char *wav_path, const char *options_json);
const char *silsplit_analyze_progress(const char *wav_path, const char *options_json,
                                      silsplit_progress_cb cb, void *user_data);
const char *silsplit_analyze_pcm(const float *pcm, int n_samples, const char *options_json);
int         silsplit_write_chapters(const char *wav_path, const char *options_json, const char *out_dir);
void        silsplit_free_string(char *ptr);
const char *silsplit_last_error(void);
const char *silsplit_version(void);
```

Analyze returns a **malloc'd** JSON string; free with `silsplit_free_string`.

### Progress callback (v1.1.0)

`silsplit_analyze_progress` is `silsplit_analyze` plus a progress callback; `silsplit_analyze` is
exactly `silsplit_analyze_progress(path, opts, NULL, NULL)`.

`cb(percent, stage, user_data)` is called with a **monotonic** `percent` in `0..100` and a `stage`:

| Stage | Percent band |
|-------|--------------|
| `read` | 0 … 20 (WAV decode → mono) |
| `resample` | 20 … 40 (`sil_resample_to_16k`) |
| `detect` | 40 … 100 (frame RMS + silence scan) |
| `done` | 100 (analysis complete) |

Events are throttled to at most one per 1 % / stage change. Return **non-zero** from `cb` to
cancel: the call frees everything and returns `NULL` with `silsplit_last_error() == "cancelled"`.

### Options JSON (defaults)

| Key | Default | Meaning |
|-----|---------|---------|
| `threshold_db` | -40 | Silence energy threshold (dBFS) |
| `min_silence_s` | 0.8 | Min silence to end a chapter |
| `min_chapter_s` | 5.0 | Drop shorter speech regions |
| `padding_s` | 0.25 | Pad chapter edges |
| `max_chapter_s` | 0 | Hard-split long chapters (0=off) |
| `sample_rate` | 16000 | Input rate for `analyze_pcm` |

### Analyze JSON

`sample_rate`, `duration_s`, `chapters[{i,t0,t1,dur,peak_db}]`, `silences_removed_s`.

## CLI

```bash
./bin/silsplit analyze file.wav [--opts '{...}']
./bin/silsplit split   file.wav out_dir [--opts '{...}']
```

`split` writes `ch_001.wav…` and `chapters.json` into `out_dir`.

## Algorithm

1. Read WAV (PCM 8/16/24/32 + float), any rate → mono float → resample to 16 kHz (linear).
2. Frame 20 ms, hop 10 ms → RMS → dBFS.
3. Hysteresis: enter silence when below threshold for `min_silence_s`; resume speech when above threshold+6 dB.
4. Keep speech ≥ `min_chapter_s`, pad ±`padding_s`, merge if gap < `min_silence_s`, optional hard split by `max_chapter_s`.

Limits: WAV ≤ 2 GB, chapters ≤ 10 000.

## License

Project code: for SCORPIOX / Coder use. yyjson: MIT (ibireme).
