1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! The CoreML implementation of zuoer's [`VadBackend`] seam, plus the
//! one-shot [`detect_speech`] entry point wired over it (design spec §2-§4).
//!
//! This is the whole of vadkit's "detector" surface: a [`CoreMlBackend`] that
//! turns 256 ms (4096-sample) chunks of audio into speech probabilities by
//! running the FluidInference unified Silero VAD graph through
//! [`crate::audio::vad::VadModel`], and a thin [`detect_speech`] that hands that backend to
//! zuoer's backend-agnostic [`zuoer::detect_speech_with`]. Every rule that
//! turns probabilities into segments — thresholding, the start/end hysteresis,
//! `min_speech`/`min_silence`, `speech_pad`, force-splitting — lives in the
//! published `zuoer` crate and stays there (spec §2-§3). vadkit authors NONE
//! of it; `tests/reexport.rs`'s `src_authors_no_detection_logic` grep gate
//! pins that.
//!
//! # Geometry and the seam contract
//!
//! [`CoreMlBackend`] declares [`frame_hop`](VadBackend::frame_hop) =
//! [`CHUNK_SAMPLES`] (4096) at 16 kHz — the model's analysis window equals its
//! hop, so the hop IS the frame size, an 8× coarser frame than the ONNX
//! backend's 512, which zuoer's geometry-parameterized detector consumes
//! unchanged (spec §3). [`push`](VadBackend::push) advances the model's
//! recurrent [`VadState`](crate::audio::vad::VadState) in place once per
//! completed frame, so successive calls form one logical stream until
//! [`reset`](VadBackend::reset); this is exactly the streaming contract
//! [`zuoer::detect_speech_with`] and [`zuoer::SpeechSegmenter`] drive.
//!
//! # Input windowing and the end-of-stream policy
//!
//! Under zuoer's push-based seam the BACKEND owns input windowing and the
//! trailing-frame policy, so [`CoreMlBackend`] buffers whatever PCM does not
//! yet complete a 4096-sample frame and carries it into the next
//! [`push`](VadBackend::push). At end of stream
//! [`finish`](VadBackend::finish) applies the Silero policy — zero-pad a
//! NON-EMPTY trailing partial frame and emit its probability; emit nothing
//! when the stream ended on an exact frame boundary. That reproduces, sample
//! for sample, what the pre-zuoer detector did on vadkit's behalf, so a clip
//! whose length is not a whole number of frames still contributes its final
//! partial frame and segments still close at the padded frame boundary.
//! `tests/vad/reexport.rs`'s
//! `coreml_backend_frames_match_a_hand_chunked_zero_padded_reference` is the
//! gate on that: it replays the pre-zuoer detector's chunking loop straight
//! through [`VadModel::predict_chunk`] and requires this backend's
//! `push` + `finish` probability stream to match it bit for bit. (The
//! end-to-end `detect_speech_on_real_audio_is_pinned` boundary pin does NOT
//! substitute for it — its deliberate ±1-frame fp16 band is exactly wide
//! enough to swallow a dropped tail.) The mock-backend `finish` scenarios in
//! the same file pin the policy hermetically.
//!
//! # Error bridging
//!
//! The seam's associated error is bridged into [`zuoer::Error`] the way its
//! trait doc prescribes for an out-of-tree backend: [`CoreMlBackend::Error`] is
//! vadkit's own [`InferError`], and `impl From<InferError> for zuoer::Error`
//! wraps it in the transparent [`zuoer::Error::Backend`] variant, whose
//! `Display`/`source` delegate to the wrapped error.
use Path;
use ;
use crate;
/// A [`zuoer::VadBackend`] over the CoreML FluidInference unified Silero VAD
/// graph: 256 ms chunks in, one speech probability per chunk out, recurrent
/// state carried across chunks by the wrapped [`VadModel`].
///
/// Construct one and hand it to [`detect_speech`] (one-shot) or drive it
/// through [`zuoer::SpeechSegmenter`] / [`zuoer::detect_speech_with`]
/// directly for streaming — both re-exported at the crate root. Because the
/// backend owns recurrent state AND the un-chunked PCM tail, a single value is
/// a single logical stream: call [`VadBackend::reset`] (or build a fresh
/// backend) to start another.
/// Bridges vadkit's [`InferError`] into [`zuoer::Error`] through the
/// transparent [`zuoer::Error::Backend`] variant — the out-of-tree backend
/// pattern zuoer's [`VadBackend::Error`] doc prescribes. This is what lets
/// [`CoreMlBackend::Error`] satisfy the trait's `Into<zuoer::Error>` bound and
/// lets a backend failure surface from [`detect_speech`] /
/// [`zuoer::detect_speech_with`] as a single `zuoer::Error`.
/// One-shot offline speech detection over the CoreML backend: the CoreML
/// counterpart to `silero::detect_speech` (which runs the bundled ONNX
/// backend). Pushes `samples` through the backend in 4096-sample (256 ms)
/// frames, flushes the zero-padded trailing partial frame, and applies zuoer's
/// segmentation rules — a pure forward to [`zuoer::detect_speech_with`],
/// authoring nothing.
///
/// `backend` is NOT reset first: pass a freshly built or
/// [`reset`](VadBackend::reset) backend to start a new stream (mirroring
/// `zuoer::detect_speech_with`'s own contract).
///
/// For streaming, drive a [`CoreMlBackend`] through the re-exported
/// [`zuoer::SpeechSegmenter`] (`push_probability` per probability
/// [`push`](VadBackend::push) emits) instead.
///
/// # Errors
/// Any frame's [`InferError`], bridged into [`zuoer::Error`] via
/// [`zuoer::Error::Backend`].