-
Notifications
You must be signed in to change notification settings - Fork 12
/
stream.go
80 lines (65 loc) · 1.25 KB
/
stream.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/oriiolabs/revai-go"
)
func main() {
rev_token := os.Getenv("REV_TOKEN")
if rev_token == "" {
fmt.Println("Please set REV_TOKEN enviroment variable")
return
}
c := revai.NewClient(rev_token)
// select the content type for your audio
params := &revai.DialStreamParams{
ContentType: "audio/x-wav",
}
conn, err := c.Stream.Dial(context.Background(), params)
if err != nil {
panic(err)
}
go func() {
for {
msg, err := conn.Recv()
if err != nil {
if err == io.EOF {
fmt.Println("Reader got EOF")
break
} else {
panic(err)
}
}
res, err := json.Marshal(msg)
if err != nil {
panic(err)
}
fmt.Println(string(res))
}
}()
f, err := os.Open("test_data.wav")
if err != nil {
panic(err)
}
// update the match the byte rate of your audio
byte_rate := int64(64000) // bit rate / 8
buffer := make([]byte, byte_rate)
for {
if _, err := f.Read(buffer); err != nil {
if err == io.EOF {
conn.WriteDone()
time.Sleep(5 * time.Second)
break
}
} else if err := conn.Write(bytes.NewReader(buffer)); err != nil {
panic(err)
}
time.Sleep(900 * time.Millisecond)
}
conn.Close()
}