Last active
April 6, 2016 01:51
-
-
Save paritosh-gupta/7984bcfc0ec6871c3f83ee0541cd4bfd to your computer and use it in GitHub Desktop.
Sample Code for connecting to Bluemix speech-to-text from go-lang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"github.com/gorilla/websocket" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"reflect" | |
"unsafe" | |
) | |
func watsonConn(soundData SoundDataStruct) { | |
log.SetFlags(0) | |
u, err := url.Parse(GetWatsonSocketUrl()) // Get url for making request | |
if err != nil { | |
log.Fatal("could not parse watson url", err) | |
} | |
// Biolerplate for websocket connection | |
config := &tls.Config{InsecureSkipVerify: true} | |
var WatsonDialer = websocket.Dialer{ | |
Proxy: http.ProxyFromEnvironment, | |
TLSClientConfig: config, | |
} | |
c, resp, err := WatsonDialer.Dial(u.String(), nil) | |
data, readERR := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal("dial:", err) | |
} | |
if readERR != nil { | |
log.Fatal("Cant read from body", readERR) | |
} | |
// Watson connection has sucessfully been established | |
//Channel @done is used to notify for end of read | |
done := make(chan struct{}) | |
go func() { | |
defer c.Close() | |
defer close(done) | |
for { | |
mt, message, err := c.ReadMessage() // blocking call | |
if err != nil { | |
log.Println("error in read:", err) | |
break | |
} | |
if mt == websocket.TextMessage { | |
transcript, err := ParseWatsonTranscript(message) | |
if err != nil { | |
log.Printf("WatsonTranscriptParse:\n%s\n", string(message)) | |
} else if len(transcript.Results) != 0 { | |
log.Println("transcript= ", GetTranscript(transcript)) | |
} else { | |
log.Println(string(message)) | |
} | |
} | |
} | |
}() | |
// Initialize watson | |
c.WriteMessage(websocket.TextMessage, GetWatsonInit(soundData.AudioSamplerate)) | |
OuterLoop: | |
for { | |
select { | |
// recevied a text message from socket not a sound chunk | |
case message := <-soundData.Message: | |
// if close signal was sent from client application websocket | |
if message == websocket.CloseNormalClosure { | |
// Send Stop Signal on recieving websocket close | |
c.WriteMessage(websocket.TextMessage, []byte("{\"action\": \"stop\"}")) | |
log.Println("done") | |
break OuterLoop | |
} | |
// Socket read finished and exited | |
case <-done: | |
log.Println("done") | |
break OuterLoop | |
// recieved some sound chunk | |
case msg := <-soundData.Binary: | |
// Convert binary chunk to wav | |
sound := WriteWav(msg, soundData.AudioSamplerate, soundData.AudioChannelNum) | |
// Write to watson | |
c.WriteMessage(websocket.BinaryMessage, sound) | |
} | |
} | |
defer close(soundData.Binary) | |
} | |
type watsonInit struct { | |
Action string `json:"action"` | |
ContentType string `json:"content-type"` | |
InterimResults bool `json:"interim_results"` | |
Continuous bool `json:"continuous"` | |
InactivityTimeout int `json:"inactivity_timeout"` | |
Timestamps bool `json:"timestamps"` | |
ProfanityFilter bool `json:"profanity_filter"` | |
WordConfidence bool `json:"word_confidence"` | |
} | |
/** | |
* Initializes watson with open function | |
*/ | |
func GetWatsonInit(samplerate int) []byte { | |
data := watsonInit{ | |
Action: "start", | |
ContentType: "audio/wav",//;rate=" + strconv.Itoa(samplerate), | |
InterimResults: true, | |
WordConfidence: false, | |
Continuous: true, | |
InactivityTimeout: 10, | |
Timestamps: true, | |
ProfanityFilter: false, | |
} | |
temp, err := json.Marshal(data) | |
if err != nil { | |
log.Println("err in watson init json marshall") | |
} | |
log.Println("Watson Init", string(temp)) | |
return temp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment