Repository navigation
Expand file tree
/
Copy pathwhispercpp.go
More file actions
287 lines (263 loc) · 10.1 KB
/
Copy pathwhispercpp.go
File metadata and controls
287 lines (263 loc) · 10.1 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
package transcript
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
const whisperDiagnosticLimit = 2048
var whisperPathPattern = regexp.MustCompile(`(?i)([a-z]:\\|/)[^\s"']+`)
// WhisperCPPTranscriber transcribes audio with the whisper.cpp command-line
// program. It stages caller-owned audio in isolated temporary storage and uses
// FFmpeg to create 16 kHz mono PCM WAV input unless Audio.Format is "wav".
// The adapter is safe for concurrent use after construction.
type WhisperCPPTranscriber struct {
executable string
model string
ffmpeg string
runner whisperCommandRunner
}
// NewWhisperCPPTranscriber constructs an opt-in local whisper.cpp adapter.
// executable names whisper-cli (or a compatible whisper.cpp executable),
// model is the required model file, and ffmpeg names the converter used for
// non-WAV input. Empty executable and ffmpeg values select "whisper-cli" and
// "ffmpeg", respectively. Model must not be empty.
func NewWhisperCPPTranscriber(executable, model, ffmpeg string) (*WhisperCPPTranscriber, error) {
if strings.TrimSpace(model) == "" {
return nil, fmt.Errorf("%w: whisper.cpp model path must not be empty", ErrInvalidInput)
}
if strings.TrimSpace(executable) == "" {
executable = "whisper-cli"
}
if strings.TrimSpace(ffmpeg) == "" {
ffmpeg = "ffmpeg"
}
return &WhisperCPPTranscriber{executable: executable, model: model, ffmpeg: ffmpeg, runner: whisperExecRunner{}}, nil
}
// Transcribe stages audio, conditionally normalizes it with FFmpeg, invokes
// whisper.cpp for JSON segment output, and removes every staged artifact before
// returning. The first non-empty language hint is passed to whisper.cpp;
// otherwise language auto-detection is requested. Transcribe does not close
// audio.Data because ownership remains with the caller.
func (w *WhisperCPPTranscriber) Transcribe(ctx context.Context, audio Audio, languageHints []string) (Transcription, error) {
if ctx == nil {
return Transcription{}, fmt.Errorf("%w: context must not be nil", ErrInvalidInput)
}
if w == nil || w.runner == nil || strings.TrimSpace(w.executable) == "" || strings.TrimSpace(w.model) == "" {
return Transcription{}, fmt.Errorf("%w: whisper.cpp transcriber is not configured", ErrInvalidInput)
}
if audio.Data == nil {
return Transcription{}, fmt.Errorf("%w: audio data must not be nil", ErrInvalidInput)
}
directory, err := os.MkdirTemp("", "voxscripta-whisper-")
if err != nil {
return Transcription{}, fmt.Errorf("%w: create whisper.cpp workspace: %v", ErrProviderFailure, err)
}
defer os.RemoveAll(directory)
format := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(audio.Format), "."))
if format == "" {
format = "audio"
}
inputPath := filepath.Join(directory, "input."+format)
input, err := os.OpenFile(inputPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
if err != nil {
return Transcription{}, fmt.Errorf("%w: stage audio: %v", ErrProviderFailure, err)
}
_, copyErr := io.Copy(input, audio.Data)
closeErr := input.Close()
if copyErr != nil || closeErr != nil {
return Transcription{}, fmt.Errorf("%w: stage audio: %v", ErrProviderFailure, errors.Join(copyErr, closeErr))
}
whisperInput := inputPath
compatibleWAV, err := isWhisperCompatibleWAV(inputPath)
if err != nil {
return Transcription{}, fmt.Errorf("%w: inspect staged audio: %v", ErrProviderFailure, err)
}
if !compatibleWAV {
whisperInput = filepath.Join(directory, "normalized.wav")
if err := w.run(ctx, w.ffmpeg, "-nostdin", "-hide_banner", "-loglevel", "error", "-y", "-i", inputPath, "-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le", whisperInput); err != nil {
return Transcription{}, classifyWhisperError("normalize audio with ffmpeg", err)
}
}
outputPrefix := filepath.Join(directory, "transcript")
language := firstLanguageHint(languageHints)
args := []string{"--model", w.model, "--output-json", "--output-file", outputPrefix, "--language", language, "--file", whisperInput}
if err := w.run(ctx, w.executable, args...); err != nil {
return Transcription{}, classifyWhisperError("transcribe audio", err)
}
contents, err := os.ReadFile(outputPrefix + ".json")
if err != nil {
return Transcription{}, fmt.Errorf("%w: read whisper.cpp JSON output: %v", ErrProviderFailure, err)
}
return parseWhisperCPPJSON(contents, language)
}
// isWhisperCompatibleWAV reports whether path contains RIFF/WAVE audio with a
// PCM, mono, 16 kHz, 16-bit fmt chunk matching whisper.cpp's portable input
// contract. Unknown chunks are skipped without loading the complete file.
func isWhisperCompatibleWAV(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
}
defer file.Close()
header := make([]byte, 12)
if _, err := io.ReadFull(file, header); err != nil {
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
return false, nil
}
return false, err
}
if string(header[:4]) != "RIFF" || string(header[8:]) != "WAVE" {
return false, nil
}
chunkHeader := make([]byte, 8)
for {
if _, err := io.ReadFull(file, chunkHeader); err != nil {
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
return false, nil
}
return false, err
}
size := int64(binary.LittleEndian.Uint32(chunkHeader[4:]))
if string(chunkHeader[:4]) == "fmt " {
if size < 16 {
return false, nil
}
format := make([]byte, 16)
if _, err := io.ReadFull(file, format); err != nil {
return false, nil
}
return binary.LittleEndian.Uint16(format[0:]) == 1 &&
binary.LittleEndian.Uint16(format[2:]) == 1 &&
binary.LittleEndian.Uint32(format[4:]) == 16000 &&
binary.LittleEndian.Uint16(format[14:]) == 16, nil
}
if size%2 != 0 {
size++
}
if _, err := file.Seek(size, io.SeekCurrent); err != nil {
return false, err
}
}
}
func (w *WhisperCPPTranscriber) run(ctx context.Context, executable string, args ...string) error {
return w.runner.Run(ctx, executable, args...)
}
// firstLanguageHint returns the first non-blank ordered language preference or
// "auto" when the caller supplied no usable preference.
func firstLanguageHint(hints []string) string {
for _, hint := range hints {
if hint = strings.TrimSpace(hint); hint != "" {
return hint
}
}
return "auto"
}
type whisperJSON struct {
Result struct {
Language string `json:"language"`
} `json:"result"`
Transcription []struct {
Offsets struct {
From int64 `json:"from"`
To int64 `json:"to"`
} `json:"offsets"`
Text string `json:"text"`
} `json:"transcription"`
}
// parseWhisperCPPJSON converts whisper.cpp's millisecond offset units into
// normalized transcript segments and rejects output without usable speech.
func parseWhisperCPPJSON(contents []byte, requestedLanguage string) (Transcription, error) {
var output whisperJSON
if err := json.Unmarshal(contents, &output); err != nil {
return Transcription{}, fmt.Errorf("%w: decode whisper.cpp JSON output: %v", ErrProviderFailure, err)
}
segments := make([]Segment, 0, len(output.Transcription))
for _, item := range output.Transcription {
text := strings.TrimSpace(item.Text)
if text == "" {
continue
}
segments = append(segments, Segment{Start: time.Duration(item.Offsets.From) * time.Millisecond, End: time.Duration(item.Offsets.To) * time.Millisecond, Text: text})
}
language := strings.TrimSpace(output.Result.Language)
if language == "" && requestedLanguage != "auto" {
language = requestedLanguage
}
if language == "" || language == "auto" || len(segments) == 0 {
return Transcription{}, fmt.Errorf("%w: whisper.cpp returned no usable language or segments", ErrTranscriptUnavailable)
}
return Transcription{Language: Language{Code: language}, Provider: ProviderMetadata{Name: "whisper.cpp"}, Segments: segments}, nil
}
type whisperCommandRunner interface {
Run(ctx context.Context, executable string, args ...string) error
}
type whisperExecRunner struct{}
// Run directly invokes one speech-processing command with bounded output and
// context-controlled cancellation.
func (whisperExecRunner) Run(ctx context.Context, executable string, args ...string) error {
command := exec.CommandContext(ctx, executable, args...)
stderr := &limitedWriter{limit: 64 << 10}
command.Stdout = &limitedWriter{limit: 64 << 10}
command.Stderr = stderr
if err := command.Run(); err != nil {
return &whisperCommandError{cause: err, diagnostic: safeWhisperDiagnostic(string(stderr.data))}
}
return nil
}
type limitedWriter struct {
data []byte
limit int
}
// Write retains a bounded prefix while reporting the complete write consumed.
func (w *limitedWriter) Write(value []byte) (int, error) {
length := len(value)
remaining := w.limit - len(w.data)
if remaining > len(value) {
remaining = len(value)
}
if remaining > 0 {
w.data = append(w.data, value[:remaining]...)
}
return length, nil
}
type whisperCommandError struct {
cause error
diagnostic string
}
// Error renders a process failure with its optional sanitized diagnostic.
func (e *whisperCommandError) Error() string {
if e.diagnostic == "" {
return fmt.Sprintf("speech command failed: %v", e.cause)
}
return fmt.Sprintf("speech command failed: %v: %s", e.cause, e.diagnostic)
}
// Unwrap exposes the process cause for errors.Is and errors.As classification.
func (e *whisperCommandError) Unwrap() error { return e.cause }
// safeWhisperDiagnostic normalizes, path-redacts, and bounds process stderr.
func safeWhisperDiagnostic(value string) string {
value = strings.Join(strings.Fields(value), " ")
value = whisperPathPattern.ReplaceAllString(value, "[redacted-path]")
if len(value) > whisperDiagnosticLimit {
value = value[:whisperDiagnosticLimit] + "..."
}
return value
}
// classifyWhisperError maps speech-process failures to stable public errors.
func classifyWhisperError(operation string, err error) error {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, exec.ErrDot) {
return fmt.Errorf("%w: %s: %v", ErrMissingDependency, operation, err)
}
return fmt.Errorf("%w: %s: %v", ErrProviderFailure, operation, err)
}