forked from deepgram/deepgram-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveTranscription_example.go
65 lines (52 loc) · 1.32 KB
/
liveTranscription_example.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
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"reflect"
"time"
"github.com/Jeffail/gabs/v2"
"github.com/deepgram-devs/deepgram-go-sdk/deepgram"
"github.com/gorilla/websocket"
)
func main() {
client := new(http.Client)
// IMPORTANT: Make sure you add your own API key here
dg := *deepgram.NewClient("YOUR_API_KEY")
resp, err := client.Get("http://stream.live.vc.bbcmedia.co.uk/bbc_radio_fourlw_online_nonuk")
if err != nil {
log.Println("ERRROR getting stream", err)
}
fmt.Println("Stream is up and running ", reflect.TypeOf(resp))
reader := bufio.NewReader(resp.Body)
options := deepgram.LiveTranscriptionOptions{
Language: "en-US",
Punctuate: true,
}
dgConn, _, err := dg.LiveTranscription(options)
chunk := make([]byte, 1024*2)
go func() {
for {
_, message, err := dgConn.ReadMessage()
if err != nil {
fmt.Println("ERROR reading message")
log.Fatal(err)
}
jsonParsed, jsonErr := gabs.ParseJSON(message)
if jsonErr != nil {
log.Fatal(err)
}
log.Printf("recv: %s", jsonParsed.Path("channel.alternatives.0.transcript").String())
}
}()
for {
bytesRead, err := reader.Read(chunk)
if err != nil {
fmt.Println("ERROR reading chunk")
log.Fatal(err)
}
dgConn.WriteMessage(websocket.BinaryMessage, chunk[:bytesRead])
time.Sleep(10 * time.Millisecond)
}
}