-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (134 loc) · 3.3 KB
/
main.go
File metadata and controls
157 lines (134 loc) · 3.3 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
package main
import (
"context"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/ceoifung/go-dashscope/dashscope"
"github.com/ceoifung/go-dashscope/examples/audio"
"github.com/eiannone/keyboard"
)
type MyRecognitionCallback struct{}
func (c *MyRecognitionCallback) OnOpen() {
fmt.Println("Recognition Open")
}
func (c *MyRecognitionCallback) OnComplete() {
fmt.Println("Recognition Complete")
}
func (c *MyRecognitionCallback) OnError(err error) {
fmt.Printf("Recognition Error: %v\n", err)
}
func (c *MyRecognitionCallback) OnClose() {
fmt.Println("Recognition Closed")
}
func (c *MyRecognitionCallback) OnEvent(result *dashscope.RecognitionResult) {
if len(result.Sentences) > 0 {
for _, s := range result.Sentences {
if sentMap, ok := s.(map[string]interface{}); ok {
fmt.Printf("Text: %s (End: %v)\n", sentMap["text"], sentMap["end_time"])
}
}
}
}
func main() {
apiKey := os.Getenv("DASHSCOPE_API_KEY")
if apiKey == "" {
fmt.Println("Please set DASHSCOPE_API_KEY environment variable")
return
}
if err := keyboard.Open(); err != nil {
fmt.Println("Keyboard Error:", err)
return
}
defer keyboard.Close()
fmt.Println("\n--- Interactive Real-time Recognition ---")
fmt.Println("Press SPACE to start/stop recording. Press ESC to quit.")
var rec *dashscope.Recognition
var recording bool
var mu sync.Mutex
var recorder *audio.Recorder
var lastPressTime time.Time
for {
_, key, err := keyboard.GetKey()
if err != nil {
break
}
if key == keyboard.KeyEsc {
break
}
if key == keyboard.KeySpace {
if time.Since(lastPressTime) < 500*time.Millisecond {
continue
}
lastPressTime = time.Now()
mu.Lock()
if !recording {
fmt.Println("Starting recording...")
// Start Recognition
rec = dashscope.NewRecognition("paraformer-realtime-v1", apiKey, "pcm", 16000)
cb := &MyRecognitionCallback{}
err := rec.Start(context.Background(), cb, nil)
if err != nil {
fmt.Println("Start Recognition Error:", err)
mu.Unlock()
continue
}
// Start Recorder
recorder, err = audio.NewRecorder()
if err != nil {
fmt.Println("Start Recorder Error:", err)
rec.Stop()
mu.Unlock()
continue
}
if err := recorder.Start(); err != nil {
fmt.Println("Recorder Start Error:", err)
recorder.Close()
rec.Stop()
mu.Unlock()
continue
}
recording = true
fmt.Println("Recording started. Speak now...")
// Start reading audio and sending
go func() {
buf := make([]byte, 3200) // 100ms
totalBytes := 0
for {
n, err := recorder.Read(buf)
if n > 0 {
// fmt.Printf(".") // Visual feedback
if sendErr := rec.SendAudioFrame(buf[:n]); sendErr != nil {
fmt.Printf("\nSend Error: %v\n", sendErr)
break
}
totalBytes += n
}
if err != nil {
if err != io.EOF {
fmt.Printf("\nRecorder Read Error: %v\n", err)
}
break
}
}
fmt.Printf("\nTotal audio sent: %d bytes\n", totalBytes)
}()
} else {
fmt.Println("Stopping recording...")
recording = false
// Stop Recorder
if recorder != nil {
recorder.Close()
}
// Stop Recognition
if rec != nil {
rec.Stop()
}
fmt.Println("Stopped. Waiting for final result...")
}
mu.Unlock()
}
}
}