/
process.go
153 lines (127 loc) · 3.33 KB
/
process.go
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
package main
import (
"context"
"fmt"
"sync"
"time"
whisper "github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
multierror "github.com/hashicorp/go-multierror"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type Process struct {
wg sync.WaitGroup
cancel context.CancelFunc
ch chan []float32
t time.Duration
model whisper.Model
buf []float32
n int
}
///////////////////////////////////////////////////////////////////////////////
// CONSTANTS
const (
defaultWindow = 2 * time.Second
)
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
func NewProcess(model string, window time.Duration) (*Process, error) {
process := new(Process)
process.ch = make(chan []float32)
// Set default window
if window == 0 {
window = defaultWindow
}
// Create a buffer for the samples. The idea is to make a circular buffer eventually
process.n = int(whisper.SampleRate * defaultWindow / time.Second)
process.buf = make([]float32, 0, process.n)
// Load the model
if model, err := whisper.New(model); err != nil {
return nil, err
} else {
process.model = model
}
// Create background goroutine
ctx, cancel := context.WithCancel(context.Background())
process.wg.Add(1)
process.cancel = cancel
go func() {
defer process.wg.Done()
process.process(ctx)
}()
// Return success
return process, nil
}
func (process *Process) Close() error {
var result error
// Cancel context and wait for end of context
process.cancel()
process.wg.Wait()
// Close channel, release buffer
close(process.ch)
process.ch = nil
process.buf = nil
// Close model
if err := process.model.Close(); err != nil {
result = multierror.Append(result, err)
}
// Return success or errors
return result
}
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Return channel to send samples to
func (process *Process) C() chan<- []float32 {
return process.ch
}
// Return the timestamp of the queued samples
func (process *Process) T() time.Duration {
return process.t
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// process incoming samples
func (process *Process) process(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case samples := <-process.ch:
if len(samples) > 0 {
process.buf = append(process.buf, samples...)
if len(process.buf) >= process.n {
samples := make([]float32, len(process.buf))
copy(samples, process.buf)
go func() {
if err := process.do(ctx, process.t, samples); err != nil {
fmt.Println(err)
}
}()
process.n = len(process.buf)
process.buf = process.buf[:0]
}
process.t += time.Second * time.Duration(len(samples)) / whisper.SampleRate
}
}
}
}
func (process *Process) do(ctx context.Context, offset time.Duration, samples []float32) error {
context, err := process.model.NewContext()
if err != nil {
return err
}
// Process samples
if err := context.Process(samples, nil); err != nil {
return err
}
// Print out the results
for {
segment, err := context.NextSegment()
if err != nil {
break
}
fmt.Printf("[%6s->%6s] %s\n", segment.Start+offset, segment.End+offset, segment.Text)
}
// Return success
return nil
}