-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.go
More file actions
369 lines (355 loc) · 15 KB
/
Copy pathanalyze.go
File metadata and controls
369 lines (355 loc) · 15 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package waxflow
import (
"context"
"fmt"
"io"
"time"
"github.com/colespringer/waxflow/audio"
"github.com/colespringer/waxflow/container"
"github.com/colespringer/waxflow/dsp"
"github.com/colespringer/waxflow/dsp/loudness"
"github.com/colespringer/waxflow/dsp/mix"
"github.com/colespringer/waxflow/dsp/silence"
"github.com/colespringer/waxflow/format"
"github.com/colespringer/waxflow/waxerr"
)
// Silence detection defaults, applied to the zero fields of
// SilenceOptions. The threshold suits studio-quiet content; see
// SilenceOptions.ThresholdDB for why the right value is a property of the
// source rather than of the detector.
const (
DefaultSilenceThresholdDB = -50.0
DefaultSilenceMinDuration = 500 * time.Millisecond
)
// AnalyzeOptions configures Engine.Analyze.
type AnalyzeOptions struct {
// Channels, when non-zero, measures the loudness after mixing the
// source down to this channel count (1 or 2, matching a later
// TranscodeOptions.Channels), so a two-pass gain is computed on the
// audio the encode will meter. 0 keeps the source layout. The fold is
// the same one the encode applies (dsp/mix), but with no limiter, gain,
// or dither: a measurement observes the raw fold, so TruePeakDB stays
// honest where the encode's overshoot limiter would flatten it. This is
// the substantive difference from TranscodeOptions.Channels.
Channels int
// Progress, when non-nil, is called after each decoded chunk with the
// samples measured so far and the projected total (-1 unknown). It
// runs on the analyzing goroutine, so blocking it pauses the
// analysis; the job runner's yield-to-live-streams check rides on
// exactly that.
Progress func(done, total int64)
// Silence, when non-nil, maps the source's silent spans alongside the
// loudness measurement, from the same decode. Nil omits the map
// entirely, so an analysis that does not ask for it is unchanged.
Silence *SilenceOptions
// Tap, when non-nil, is called with each decoded chunk's planar channel
// slices at the source's own rate and layout: chans[c][i] is sample i of
// channel c, all channel slices the same length, values nominal full
// scale +-1.0. It is the seam for an analyzer WaxFlow does not own,
// riding the same decode as the meter rather than paying for a second
// one, which is what AnalyzeOptions.Silence already does for one WaxFlow
// does own.
//
// It runs on the analyzing goroutine, so blocking it pauses the
// analysis; the same contract Progress carries. An error from it fails
// the analysis, as an error from the meter does.
//
// The slices are borrowed: they alias the pooled chunk buffer and are
// valid only for the duration of the call, and the next chunk reuses
// them. A tap that keeps the samples must copy them. It must also not
// write to them, which is why it runs after the analyzers this engine
// owns rather than before: their measurements are already taken, so a
// tap that breaks the rule breaks only its own result.
Tap func(chans [][]float32) error
}
// SilenceOptions configures the silence map. Both fields are raw
// parameters rather than a closed vocabulary, which is the opposite of the
// choice gain= and dynamics= make, and deliberately: a closed vocabulary
// belongs where a value enters a cache key or a validated signal path,
// where it must mean the same thing forever. These values do neither. They
// shape a report, nothing is keyed by them, and the caller genuinely knows
// better than the daemon does.
type SilenceOptions struct {
// ThresholdDB is the silence threshold in dBFS; 0 means
// DefaultSilenceThresholdDB. It must be negative and finite, which the
// detector enforces; tighter policy clamps live at the API boundary,
// not here, exactly as they do for TranscodeOptions.GainDB.
//
// The right value is a property of the content, so there is no default
// that suits everything. See dsp/silence.New for the guidance, and
// SilenceResult.DroppedSamples for what a wrong one looks like: it does
// not fail cleanly, it reports no silence at all.
ThresholdDB float64
// MinDuration is the shortest span worth reporting; 0 means
// DefaultSilenceMinDuration. It must be positive, which the detector
// enforces.
MinDuration time.Duration
}
// resolve applies the defaults to the zero fields.
func (o SilenceOptions) resolve() (thresholdDB float64, minDur time.Duration) {
thresholdDB, minDur = o.ThresholdDB, o.MinDuration
if thresholdDB == 0 {
thresholdDB = DefaultSilenceThresholdDB
}
if minDur == 0 {
minDur = DefaultSilenceMinDuration
}
return thresholdDB, minDur
}
// SilenceSpan is one silent span of the analyzed source, in frames on its
// own timeline (ADR-0006). To is exclusive.
type SilenceSpan struct {
From int64
To int64
}
// SilenceResult is the silence map: the spans plus the parameters they were
// found with, so a caller that stores the map can tell what it means.
type SilenceResult struct {
// Version is the detector revision (ADR-0004 style). WaxFlow keys
// nothing by it, but a caller caching the map needs it to know when
// the map went stale.
Version string
// ThresholdDB and MinDuration are the resolved parameters, defaults
// applied.
ThresholdDB float64
MinDuration time.Duration
// Spans are the detected silences, in stream order.
Spans []SilenceSpan
// Dropped counts runs discarded for falling short of MinDuration.
// Read it with DroppedSamples, never alone: ordinary audio dips under
// any threshold at every zero crossing, so this is large even for a
// source with clean silences.
Dropped int
// DroppedSamples is the summed length of those runs, and it is the
// diagnostic. Against Samples it says how much of the source sat
// below the threshold without ever staying there long enough to
// report: near zero for a healthy source however large Dropped grows,
// and a sizeable share of the stream when the threshold is wrong for
// this source (see SilenceOptions.ThresholdDB).
DroppedSamples int64
// TotalSamples is the summed length of Spans, which is what a
// "time saved by trimming" figure reads.
TotalSamples int64
}
// AnalyzeResult is a full-stream loudness measurement of the decoded
// audio per ITU-R BS.1770-4 and EBU R128.
type AnalyzeResult struct {
// Format is the PCM format the measurement ran on: the source rate in
// the float domain, and the source channel layout unless
// AnalyzeOptions.Channels asked for a downmix, in which case it is the
// folded layout (the rate stays the source rate either way). When a
// downmix was asked for, every measured field below (IntegratedLUFS,
// LoudnessRange, TruePeakDB, SamplePeakDB) is on that downmix basis,
// since all come off one meter fed the folded channels: a 5.1 source
// measured at Channels 2 reports a stereo loudness, range, and true
// peak, which is what makes the two-pass gain correct.
Format audio.Format
// Samples is the number of frames measured.
Samples int64
// IntegratedLUFS is the gated integrated loudness. Silence that
// never passes the absolute gate reports math.Inf(-1).
IntegratedLUFS float64
// LoudnessRange is the EBU Tech 3342 loudness range in LU.
LoudnessRange float64
// TruePeakDB is the maximum oversampled true peak in dBTP,
// math.Inf(-1) for silence.
TruePeakDB float64
// SamplePeakDB is the maximum sample magnitude in dBFS, math.Inf(-1)
// for silence.
SamplePeakDB float64
// Silence is the silence map, non-nil exactly when AnalyzeOptions
// asked for one.
Silence *SilenceResult
}
// Analyze decodes src end to end and measures its loudness: integrated
// LUFS, loudness range, true peak, and sample peak. It powers the
// type:analyze job and the loudness:analyze two-pass transcode (the R128
// half of the loudness design: live streams stay tag-based, exact
// measurement belongs to jobs, where a second pass is affordable).
//
// AnalyzeOptions.Silence adds the silence map to the same pass. Both
// analyzers want the identical chain for the identical reason (the source's
// own rate and layout, in the float domain), so they share one decode
// rather than paying for two: the decode is the expensive half, and a
// library-wide sweep runs this over everything.
func (e *Engine) Analyze(ctx context.Context, src container.Source, hint string, opts AnalyzeOptions) (*AnalyzeResult, error) {
med, err := e.OpenStream(src, hint)
if err != nil {
return nil, err
}
defer med.Close()
return e.AnalyzeMedia(ctx, med, opts)
}
// AnalyzeMedia analyzes an already-opened Media, the same measurement as
// Analyze without the source-open step. It is the entry point for inputs
// that are not a single sniffable Source: the HLS client assembles a
// presentation from many fetched resources and exposes it as a
// format.Media, which flows through here exactly like a local file. The
// caller owns med and closes it.
func (e *Engine) AnalyzeMedia(ctx context.Context, med format.Media, opts AnalyzeOptions) (*AnalyzeResult, error) {
// A negative channel count is a malformed request, not an unsupported
// layout: reject it upfront with the same code the encode's NewChain
// gives TranscodeOptions.Channels < 0 (dsp.go), so a two-pass job that
// passes the same bad value to both passes reports it the same way.
if opts.Channels < 0 {
return nil, waxerr.New(waxerr.CodeInvalidRequest,
fmt.Sprintf("analyze: negative channel count %d", opts.Channels))
}
track := med.Info().Default()
// The chain only converts to float here (no resample, no mix): the
// meter is rate-aware, and absent a downmix it weighs the source
// channels itself, so measurement runs on the source's own timeline. An
// AnalyzeOptions.Channels downmix folds below with the same dsp/mix
// primitive mixStage uses, but deliberately outside this chain, so it
// skips the overshoot limiter the chain inserts for a downmix (dsp.go):
// that limiter holds true peak at the ceiling and acts non-linearly on
// pre-gain overshoots, which would corrupt the very loudness and
// true-peak numbers the measurement exists to report.
chain, err := dsp.NewChain(dsp.NewSource(med, track.Fmt), dsp.ChainSpec{Float: true})
if err != nil {
return nil, err
}
defer chain.Release()
f := chain.Format()
// meterFmt is the format the meter runs on: the source format, unless a
// downmix was asked for, in which case the channel count and layout
// become the fold's target. The rate stays the source rate on purpose:
// only channels are folded here (loudness is essentially
// resample-invariant and the meter is rate-aware), so a job that both
// resamples and downmixes keeps a deliberate sub-0.01 LU rate residual.
// Do not "fix" it by resampling the measurement; the multi-dB error is
// the channel count, which this handles.
meterFmt := f
var matrix *mix.Matrix
var scratch *audio.Buffer
var dstV [][]float32
if opts.Channels != 0 && opts.Channels != f.Channels {
// srcLayout mirrors the encode's mixStage fallback (dsp.go): an
// unmasked source takes its count's default layout, so the fold's
// inputs are byte-identical to the encode's. A decoded source is
// always 1..MaxChannels, all of which have a default, so in practice
// only dstLayout can come back zero.
srcLayout := f.Layout
if srcLayout == 0 {
srcLayout = audio.DefaultLayout(f.Channels)
}
dstLayout := audio.DefaultLayout(opts.Channels)
// A target count with no layout convention (above MaxChannels) has a
// zero mask; reject it before mix.For with the dsp.go phrasing. The
// srcLayout == 0 disjunct only mirrors dsp.go:292 one for one; after
// the fallback above it cannot fire for a real 1..MaxChannels source.
if srcLayout == 0 || dstLayout == 0 {
return nil, waxerr.New(waxerr.CodeUnsupportedFormat,
fmt.Sprintf("analyze: no layout convention for %d -> %d channels", f.Channels, opts.Channels))
}
// mix.For next, before any buffer: a target with a valid mask but no
// downmix (3 or 6 channels, since only mono and stereo targets exist)
// rejects here with a clean error, whereas audio.Get below panics on
// an invalid format.
matrix, err = mix.For(srcLayout, dstLayout)
if err != nil {
return nil, err
}
meterFmt.Channels = opts.Channels
meterFmt.Layout = dstLayout
scratch = audio.Get(meterFmt, audio.StandardChunk)
defer audio.Put(scratch)
dstV = make([][]float32, opts.Channels)
}
meter, err := loudness.NewMeter(meterFmt.Rate, meterFmt.Channels, meterFmt.Layout)
if err != nil {
return nil, err
}
// The silence detector and Tap keep consuming the source channels, never
// the downmix: Tap's contract is the source's own rate and layout, and
// the fold drops LFE, so an LFE-only span reads silent in a stereo fold
// yet is not silent in the source. A silence span is a source-timeline
// property, so it must be measured on the source.
var det *silence.Detector
var silThreshold float64
var silMinDur time.Duration
if opts.Silence != nil {
silThreshold, silMinDur = opts.Silence.resolve()
if det, err = silence.New(f.Rate, f.Channels, silThreshold, silMinDur); err != nil {
return nil, err
}
}
buf := audio.Get(f, audio.StandardChunk)
defer audio.Put(buf)
chans := make([][]float32, f.Channels)
var done int64
for {
if err := ctx.Err(); err != nil {
return nil, waxerr.Wrap(waxerr.CodeCanceled, "analyze canceled", err)
}
err := chain.ReadChunk(buf)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for c := range chans {
chans[c] = buf.ChanF(c)
}
if matrix != nil {
// scratch.N sizes the ChanF views below, and the meter infers
// its frame count from len(dstV[c]) (loudness.Process takes no
// explicit count). A zero-N scratch would not panic (Apply folds
// correctly into the backing array whatever N is); it would make
// the meter silently measure zero frames. Set N to this chunk's
// count so the views are the right length. buf and scratch are
// distinct pool allocations, so dst and src never alias.
scratch.N = buf.N
for c := range dstV {
dstV[c] = scratch.ChanF(c)
}
matrix.Apply(dstV, chans, buf.N)
if err := meter.Process(dstV); err != nil {
return nil, err
}
} else if err := meter.Process(chans); err != nil {
return nil, err
}
if det != nil {
if err := det.Process(chans); err != nil {
return nil, err
}
}
if opts.Tap != nil {
if err := opts.Tap(chans); err != nil {
return nil, err
}
}
done += int64(buf.N)
if opts.Progress != nil {
opts.Progress(done, track.Samples)
}
}
meter.Flush()
res := &AnalyzeResult{
Format: meterFmt,
Samples: done,
IntegratedLUFS: meter.Integrated(),
LoudnessRange: meter.Range(),
TruePeakDB: meter.TruePeak(),
SamplePeakDB: meter.SamplePeak(),
}
if det != nil {
det.Flush()
spans := make([]SilenceSpan, len(det.Spans()))
for i, s := range det.Spans() {
spans[i] = SilenceSpan{From: s.From, To: s.To}
}
res.Silence = &SilenceResult{
Version: silence.Version,
ThresholdDB: silThreshold,
MinDuration: silMinDur,
Spans: spans,
Dropped: det.Dropped(),
DroppedSamples: det.DroppedSamples(),
TotalSamples: det.TotalSamples(),
}
}
return res, nil
}