/
stt.go
44 lines (37 loc) · 1.09 KB
/
stt.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
package api
import (
"bytes"
"encoding/json"
"io"
"github.com/auroraapi/aurora-go/api/backend"
"github.com/auroraapi/aurora-go/audio"
"github.com/auroraapi/aurora-go/config"
)
// STTResponse is the response returned by the API if the speech was
// successfully able to be transcribed.
type STTResponse struct {
Transcript string `json:"transcript"`
}
// GetSTT queries the API with the provided audio file and returns
// a transcript of the speech.
func GetSTT(c *config.Config, audio *audio.File) (*STTResponse, error) {
return GetSTTFromStream(c, bytes.NewReader(audio.WAVData()))
}
// GetSTTFromStream queries the API with the provided raw WAV audio stream
// and returns a transcript of the speech.
func GetSTTFromStream(c *config.Config, audio io.Reader) (*STTResponse, error) {
params := &backend.CallParams{
Credentials: c.GetCredentials(),
Method: "POST",
Path: sttEndpoint,
Body: audio,
}
res, err := c.Backend.Call(params)
if err != nil {
return nil, err
}
defer res.Body.Close()
var stt STTResponse
json.NewDecoder(res.Body).Decode(&stt)
return &stt, nil
}