-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcriber.go
45 lines (42 loc) · 907 Bytes
/
transcriber.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
package transcriber
import (
"encoding/binary"
"errors"
"github.com/xlab/pocketsphinx-go/sphinx"
"io"
)
func Transcribe(dec *sphinx.Decoder, c <-chan []int16) (string, float64, error) {
if !dec.StartStream() {
return "", 0, errors.New("sphinx failed to start stream")
}
if !dec.StartUtt() {
return "", 0, errors.New("sphinx failed to start utterance")
}
for data := range c {
dec.ProcessRaw(data, false, false)
}
if !dec.EndUtt() {
return "", 0, errors.New("sphinx failed to stop utterance")
}
hyp, score := dec.Hypothesis()
return hyp, dec.LogMath().Exp(score), nil
}
func Read(r io.Reader) <-chan []int16 {
c := make(chan []int16)
go func() {
data := make([]int16, 512)
for {
err := binary.Read(r, binary.LittleEndian, data)
if err == io.EOF {
break
}
if err == io.ErrUnexpectedEOF {
c <- data
break
}
c <- data
}
close(c)
}()
return c
}