chore: add Apache License 2.0 and ignore build directory
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package captions
|
||||
|
||||
import "time"
|
||||
|
||||
type EventKind uint8
|
||||
|
||||
const (
|
||||
Provisional EventKind = iota + 1
|
||||
Final
|
||||
Hide
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Kind EventKind
|
||||
Text string
|
||||
StartedAt time.Duration
|
||||
EndedAt time.Duration
|
||||
}
|
||||
|
||||
type Transcriber interface {
|
||||
Decode(samples []float32) string
|
||||
AcceptVAD(samples []float32)
|
||||
FlushVAD()
|
||||
NextSpeechSegment() (start int, samples []float32, ok bool)
|
||||
}
|
||||
+58
-51
@@ -1,120 +1,127 @@
|
||||
// Package captions turns audio packets into fixed or speech-aware captions.
|
||||
// Package captions turns audio packets into presentation-neutral caption events.
|
||||
package captions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"tea.chunkbyte.com/kato/captioneer/src/audio"
|
||||
"tea.chunkbyte.com/kato/captioneer/src/config"
|
||||
"tea.chunkbyte.com/kato/captioneer/src/models"
|
||||
)
|
||||
|
||||
type Processor struct {
|
||||
settings config.Settings
|
||||
resources *models.Resources
|
||||
output io.Writer
|
||||
settings config.Settings
|
||||
transcriber Transcriber
|
||||
emit func(Event) error
|
||||
|
||||
fixedSamples []float32
|
||||
previewSamples []float32
|
||||
previewActive bool
|
||||
previewVisible bool
|
||||
previewStart int
|
||||
nextPreviewAt int
|
||||
totalSamples int
|
||||
}
|
||||
|
||||
func New(settings config.Settings, resources *models.Resources, output io.Writer) *Processor {
|
||||
func New(settings config.Settings, transcriber Transcriber, emit func(Event) error) *Processor {
|
||||
return &Processor{
|
||||
settings: settings,
|
||||
resources: resources,
|
||||
output: output,
|
||||
transcriber: transcriber,
|
||||
emit: emit,
|
||||
nextPreviewAt: audio.DurationSamples(settings.ChunkDuration),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Processor) Accept(samples []float32) {
|
||||
func (p *Processor) Accept(samples []float32) error {
|
||||
if len(samples) == 0 {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if p.settings.Mode == config.ModeFixed {
|
||||
p.acceptFixed(samples)
|
||||
return
|
||||
return p.acceptFixed(samples)
|
||||
}
|
||||
p.acceptVAD(samples)
|
||||
return p.acceptVAD(samples)
|
||||
}
|
||||
|
||||
func (p *Processor) Flush() {
|
||||
func (p *Processor) Flush() error {
|
||||
if p.settings.Mode == config.ModeFixed {
|
||||
if len(p.fixedSamples) > 0 {
|
||||
p.emitCommitted(p.totalSamples, p.totalSamples+len(p.fixedSamples), p.fixedSamples)
|
||||
return p.emitFinal(p.totalSamples, p.totalSamples+len(p.fixedSamples), p.fixedSamples)
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
p.resources.FlushVAD()
|
||||
p.drainVAD()
|
||||
p.clearPreview()
|
||||
p.transcriber.FlushVAD()
|
||||
return p.drainVAD()
|
||||
}
|
||||
|
||||
func (p *Processor) acceptFixed(samples []float32) {
|
||||
func (p *Processor) acceptFixed(samples []float32) error {
|
||||
p.fixedSamples = append(p.fixedSamples, samples...)
|
||||
chunkSize := audio.DurationSamples(p.settings.ChunkDuration)
|
||||
for len(p.fixedSamples) >= chunkSize {
|
||||
p.emitCommitted(p.totalSamples, p.totalSamples+chunkSize, p.fixedSamples[:chunkSize])
|
||||
if err := p.emitFinal(p.totalSamples, p.totalSamples+chunkSize, p.fixedSamples[:chunkSize]); err != nil {
|
||||
return err
|
||||
}
|
||||
p.fixedSamples = p.fixedSamples[chunkSize:]
|
||||
p.totalSamples += chunkSize
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Processor) acceptVAD(samples []float32) {
|
||||
func (p *Processor) acceptVAD(samples []float32) error {
|
||||
p.totalSamples += len(samples)
|
||||
p.resources.AcceptVAD(samples)
|
||||
p.transcriber.AcceptVAD(samples)
|
||||
|
||||
if p.previewActive || audio.RMSDBFS(samples) >= p.settings.PreviewThresholdDB {
|
||||
p.previewActive = true
|
||||
if !p.previewActive {
|
||||
p.previewActive = true
|
||||
p.previewStart = p.totalSamples - len(samples)
|
||||
}
|
||||
p.previewSamples = append(p.previewSamples, samples...)
|
||||
if len(p.previewSamples) >= p.nextPreviewAt {
|
||||
p.emitPreview(p.previewSamples)
|
||||
if err := p.emitProvisional(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.nextPreviewAt += audio.DurationSamples(p.settings.ChunkDuration)
|
||||
}
|
||||
}
|
||||
p.drainVAD()
|
||||
return p.drainVAD()
|
||||
}
|
||||
|
||||
func (p *Processor) drainVAD() {
|
||||
func (p *Processor) drainVAD() error {
|
||||
for {
|
||||
segment, ok := p.resources.NextSpeechSegment()
|
||||
start, samples, ok := p.transcriber.NextSpeechSegment()
|
||||
if !ok {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if err := p.emitFinal(start, start+len(samples), samples); err != nil {
|
||||
return err
|
||||
}
|
||||
p.clearPreview()
|
||||
p.emitCommitted(segment.Start, segment.Start+len(segment.Samples), segment.Samples)
|
||||
p.previewSamples = nil
|
||||
p.previewActive = false
|
||||
p.previewStart = 0
|
||||
p.nextPreviewAt = audio.DurationSamples(p.settings.ChunkDuration)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Processor) emitPreview(samples []float32) {
|
||||
text := p.resources.Decode(samples)
|
||||
func (p *Processor) emitProvisional() error {
|
||||
text := strings.TrimSpace(p.transcriber.Decode(p.previewSamples))
|
||||
if text == "" {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(p.output, "\r\033[2K… %s", text)
|
||||
p.previewVisible = true
|
||||
return p.emit(Event{
|
||||
Kind: Provisional,
|
||||
Text: text,
|
||||
StartedAt: audio.SamplesDuration(p.previewStart),
|
||||
EndedAt: audio.SamplesDuration(p.previewStart + len(p.previewSamples)),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *Processor) emitCommitted(start, end int, samples []float32) {
|
||||
text := p.resources.Decode(samples)
|
||||
func (p *Processor) emitFinal(start, end int, samples []float32) error {
|
||||
text := strings.TrimSpace(p.transcriber.Decode(samples))
|
||||
if text == "" {
|
||||
return
|
||||
}
|
||||
p.clearPreview()
|
||||
fmt.Fprintf(p.output, "[%s-%s] %s\n", audio.FormatTime(start), audio.FormatTime(end), text)
|
||||
}
|
||||
|
||||
func (p *Processor) clearPreview() {
|
||||
if p.previewVisible {
|
||||
fmt.Fprint(p.output, "\r\033[2K")
|
||||
p.previewVisible = false
|
||||
return p.emit(Event{Kind: Hide})
|
||||
}
|
||||
return p.emit(Event{
|
||||
Kind: Final,
|
||||
Text: text,
|
||||
StartedAt: audio.SamplesDuration(start),
|
||||
EndedAt: audio.SamplesDuration(end),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package captions
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"tea.chunkbyte.com/kato/captioneer/src/audio"
|
||||
"tea.chunkbyte.com/kato/captioneer/src/config"
|
||||
)
|
||||
|
||||
type fakeSegment struct {
|
||||
start int
|
||||
samples []float32
|
||||
}
|
||||
|
||||
type fakeTranscriber struct {
|
||||
text string
|
||||
decodeTexts []string
|
||||
segments []fakeSegment
|
||||
flushed bool
|
||||
}
|
||||
|
||||
func (f *fakeTranscriber) Decode([]float32) string {
|
||||
if len(f.decodeTexts) == 0 {
|
||||
return f.text
|
||||
}
|
||||
text := f.decodeTexts[0]
|
||||
f.decodeTexts = f.decodeTexts[1:]
|
||||
return text
|
||||
}
|
||||
func (f *fakeTranscriber) AcceptVAD([]float32) {}
|
||||
func (f *fakeTranscriber) FlushVAD() { f.flushed = true }
|
||||
func (f *fakeTranscriber) NextSpeechSegment() (int, []float32, bool) {
|
||||
if len(f.segments) == 0 {
|
||||
return 0, nil, false
|
||||
}
|
||||
segment := f.segments[0]
|
||||
f.segments = f.segments[1:]
|
||||
return segment.start, segment.samples, true
|
||||
}
|
||||
|
||||
func TestFixedModeEmitsFinalCaption(t *testing.T) {
|
||||
transcriber := &fakeTranscriber{text: "hello"}
|
||||
var events []Event
|
||||
processor := New(config.Settings{Mode: config.ModeFixed, ChunkDuration: time.Second}, transcriber, func(event Event) error {
|
||||
events = append(events, event)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := processor.Accept(make([]float32, audio.SampleRate)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) != 1 || events[0].Kind != Final || events[0].Text != "hello" {
|
||||
t.Fatalf("unexpected events: %#v", events)
|
||||
}
|
||||
if events[0].StartedAt != 0 || events[0].EndedAt != time.Second {
|
||||
t.Fatalf("unexpected timestamps: %#v", events[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixedModeFlushesFinalPartialChunkWithExactTimestamps(t *testing.T) {
|
||||
transcriber := &fakeTranscriber{decodeTexts: []string{"first", "partial"}}
|
||||
var events []Event
|
||||
processor := New(config.Settings{Mode: config.ModeFixed, ChunkDuration: time.Second}, transcriber, func(event Event) error {
|
||||
events = append(events, event)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := processor.Accept(make([]float32, audio.SampleRate+audio.SampleRate/2)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := processor.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(events) != 2 {
|
||||
t.Fatalf("event count = %d, want 2", len(events))
|
||||
}
|
||||
if events[1].Text != "partial" || events[1].StartedAt != time.Second || events[1].EndedAt != 1500*time.Millisecond {
|
||||
t.Fatalf("unexpected partial event: %#v", events[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestVADModeEmitsProvisionalThenFinal(t *testing.T) {
|
||||
transcriber := &fakeTranscriber{text: "speech"}
|
||||
var events []Event
|
||||
processor := New(config.Settings{
|
||||
Mode: config.ModeVAD,
|
||||
ChunkDuration: time.Second,
|
||||
PreviewThresholdDB: -45,
|
||||
}, transcriber, func(event Event) error {
|
||||
events = append(events, event)
|
||||
return nil
|
||||
})
|
||||
|
||||
loud := make([]float32, audio.SampleRate)
|
||||
for i := range loud {
|
||||
loud[i] = 0.5
|
||||
}
|
||||
if err := processor.Accept(loud); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
transcriber.segments = []fakeSegment{{start: 0, samples: loud}}
|
||||
if err := processor.Accept(make([]float32, audio.SamplesPerPacket())); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(events) != 2 || events[0].Kind != Provisional || events[1].Kind != Final {
|
||||
t.Fatalf("unexpected events: %#v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyFinalEmitsHide(t *testing.T) {
|
||||
transcriber := &fakeTranscriber{text: " \n\t "}
|
||||
var event Event
|
||||
processor := New(config.Settings{Mode: config.ModeFixed, ChunkDuration: time.Second}, transcriber, func(got Event) error {
|
||||
event = got
|
||||
return nil
|
||||
})
|
||||
if err := processor.Accept(make([]float32, audio.SampleRate)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if event.Kind != Hide {
|
||||
t.Fatalf("event kind = %d, want Hide", event.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlushFlushesVAD(t *testing.T) {
|
||||
transcriber := &fakeTranscriber{}
|
||||
processor := New(config.Settings{Mode: config.ModeVAD, ChunkDuration: time.Second}, transcriber, func(Event) error { return nil })
|
||||
if err := processor.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !transcriber.flushed {
|
||||
t.Fatal("VAD was not flushed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user