2026-07-16 17:24:50 +03:00
|
|
|
// Package captions turns audio packets into presentation-neutral caption events.
|
2026-07-16 12:12:01 +03:00
|
|
|
package captions
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-16 17:24:50 +03:00
|
|
|
"strings"
|
2026-07-16 12:12:01 +03:00
|
|
|
|
|
|
|
|
"tea.chunkbyte.com/kato/captioneer/src/audio"
|
|
|
|
|
"tea.chunkbyte.com/kato/captioneer/src/config"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Processor struct {
|
2026-07-16 17:24:50 +03:00
|
|
|
settings config.Settings
|
|
|
|
|
transcriber Transcriber
|
|
|
|
|
emit func(Event) error
|
2026-07-16 12:12:01 +03:00
|
|
|
|
|
|
|
|
fixedSamples []float32
|
|
|
|
|
previewSamples []float32
|
|
|
|
|
previewActive bool
|
2026-07-16 17:24:50 +03:00
|
|
|
previewStart int
|
2026-07-16 12:12:01 +03:00
|
|
|
nextPreviewAt int
|
|
|
|
|
totalSamples int
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func New(settings config.Settings, transcriber Transcriber, emit func(Event) error) *Processor {
|
2026-07-16 12:12:01 +03:00
|
|
|
return &Processor{
|
|
|
|
|
settings: settings,
|
2026-07-16 17:24:50 +03:00
|
|
|
transcriber: transcriber,
|
|
|
|
|
emit: emit,
|
2026-07-16 12:12:01 +03:00
|
|
|
nextPreviewAt: audio.DurationSamples(settings.ChunkDuration),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) Accept(samples []float32) error {
|
2026-07-16 12:12:01 +03:00
|
|
|
if len(samples) == 0 {
|
2026-07-16 17:24:50 +03:00
|
|
|
return nil
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
if p.settings.Mode == config.ModeFixed {
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.acceptFixed(samples)
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.acceptVAD(samples)
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) Flush() error {
|
2026-07-16 12:12:01 +03:00
|
|
|
if p.settings.Mode == config.ModeFixed {
|
|
|
|
|
if len(p.fixedSamples) > 0 {
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.emitFinal(p.totalSamples, p.totalSamples+len(p.fixedSamples), p.fixedSamples)
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return nil
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
p.transcriber.FlushVAD()
|
|
|
|
|
return p.drainVAD()
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) acceptFixed(samples []float32) error {
|
2026-07-16 12:12:01 +03:00
|
|
|
p.fixedSamples = append(p.fixedSamples, samples...)
|
|
|
|
|
chunkSize := audio.DurationSamples(p.settings.ChunkDuration)
|
|
|
|
|
for len(p.fixedSamples) >= chunkSize {
|
2026-07-16 17:24:50 +03:00
|
|
|
if err := p.emitFinal(p.totalSamples, p.totalSamples+chunkSize, p.fixedSamples[:chunkSize]); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-07-16 12:12:01 +03:00
|
|
|
p.fixedSamples = p.fixedSamples[chunkSize:]
|
|
|
|
|
p.totalSamples += chunkSize
|
|
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return nil
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) acceptVAD(samples []float32) error {
|
2026-07-16 12:12:01 +03:00
|
|
|
p.totalSamples += len(samples)
|
2026-07-16 17:24:50 +03:00
|
|
|
p.transcriber.AcceptVAD(samples)
|
2026-07-16 12:12:01 +03:00
|
|
|
|
|
|
|
|
if p.previewActive || audio.RMSDBFS(samples) >= p.settings.PreviewThresholdDB {
|
2026-07-16 17:24:50 +03:00
|
|
|
if !p.previewActive {
|
|
|
|
|
p.previewActive = true
|
|
|
|
|
p.previewStart = p.totalSamples - len(samples)
|
|
|
|
|
}
|
2026-07-16 12:12:01 +03:00
|
|
|
p.previewSamples = append(p.previewSamples, samples...)
|
|
|
|
|
if len(p.previewSamples) >= p.nextPreviewAt {
|
2026-07-16 17:24:50 +03:00
|
|
|
if err := p.emitProvisional(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-07-16 12:12:01 +03:00
|
|
|
p.nextPreviewAt += audio.DurationSamples(p.settings.ChunkDuration)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.drainVAD()
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) drainVAD() error {
|
2026-07-16 12:12:01 +03:00
|
|
|
for {
|
2026-07-16 17:24:50 +03:00
|
|
|
start, samples, ok := p.transcriber.NextSpeechSegment()
|
2026-07-16 12:12:01 +03:00
|
|
|
if !ok {
|
2026-07-16 17:24:50 +03:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if err := p.emitFinal(start, start+len(samples), samples); err != nil {
|
|
|
|
|
return err
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
p.previewSamples = nil
|
|
|
|
|
p.previewActive = false
|
2026-07-16 17:24:50 +03:00
|
|
|
p.previewStart = 0
|
2026-07-16 12:12:01 +03:00
|
|
|
p.nextPreviewAt = audio.DurationSamples(p.settings.ChunkDuration)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) emitProvisional() error {
|
|
|
|
|
text := strings.TrimSpace(p.transcriber.Decode(p.previewSamples))
|
2026-07-16 12:12:01 +03:00
|
|
|
if text == "" {
|
2026-07-16 17:24:50 +03:00
|
|
|
return nil
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.emit(Event{
|
|
|
|
|
Kind: Provisional,
|
|
|
|
|
Text: text,
|
|
|
|
|
StartedAt: audio.SamplesDuration(p.previewStart),
|
|
|
|
|
EndedAt: audio.SamplesDuration(p.previewStart + len(p.previewSamples)),
|
|
|
|
|
})
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 17:24:50 +03:00
|
|
|
func (p *Processor) emitFinal(start, end int, samples []float32) error {
|
|
|
|
|
text := strings.TrimSpace(p.transcriber.Decode(samples))
|
2026-07-16 12:12:01 +03:00
|
|
|
if text == "" {
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.emit(Event{Kind: Hide})
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|
2026-07-16 17:24:50 +03:00
|
|
|
return p.emit(Event{
|
|
|
|
|
Kind: Final,
|
|
|
|
|
Text: text,
|
|
|
|
|
StartedAt: audio.SamplesDuration(start),
|
|
|
|
|
EndedAt: audio.SamplesDuration(end),
|
|
|
|
|
})
|
2026-07-16 12:12:01 +03:00
|
|
|
}
|