-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestenv.go
More file actions
98 lines (87 loc) · 2.01 KB
/
testenv.go
File metadata and controls
98 lines (87 loc) · 2.01 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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
"zee/audio"
"zee/beep"
"zee/clipboard"
"zee/encoder"
"zee/hotkey"
"zee/log"
)
func runTestMode(wavPath string) {
beep.Disable()
if err := log.Init(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: could not init logging: %v\n", err)
}
defer log.Close()
log.SessionStart(activeTranscriber.Name(), activeFormat, activeFormat)
if autoPaste {
if err := clipboard.Init(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: paste init failed: %v\n", err)
}
}
fakeCtx, err := audio.NewFakeContext(wavPath, streamEnabled)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading WAV: %v\n", err)
os.Exit(1)
}
capture, err := fakeCtx.NewCapture(nil, audio.CaptureConfig{
SampleRate: encoder.SampleRate, Channels: encoder.Channels,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating capture: %v\n", err)
os.Exit(1)
}
defer capture.Close()
fakeCapture := capture.(*audio.FakeCapture)
hk := hotkey.NewFake()
recordingDone := make(chan struct{}, 1)
// Stdin driver in background -- sends hotkey events, handles WAIT/SLEEP/QUIT
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
cmd := strings.TrimSpace(scanner.Text())
switch cmd {
case "KEYDOWN":
hk.SimKeydown()
case "KEYUP":
hk.SimKeyup()
case "WAIT":
<-recordingDone
case "WAIT_AUDIO_DONE":
<-fakeCapture.AudioDone()
case "QUIT":
log.SessionEnd(transcriptionCount)
os.Exit(0)
default:
if strings.HasPrefix(cmd, "SLEEP ") {
if ms, err := strconv.Atoi(cmd[6:]); err == nil {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
}
}
os.Exit(0)
}()
// Event loop -- same pattern as run()
for {
<-hk.Keydown()
done, err := handleRecording(capture, recSession{Stop: hk.Keyup(), SilenceClose: &atomic.Bool{}})
if err != nil {
log.Errorf("recording error: %v", err)
}
if done != nil {
<-done
}
select {
case recordingDone <- struct{}{}:
default:
}
}
}