-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon_transcribe.go
128 lines (110 loc) · 4.22 KB
/
amazon_transcribe.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package suzu
import (
"context"
"io"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/transcribestreamingservice"
)
type AmazonTranscribe struct {
LanguageCode string
MediaEncoding string
MediaSampleRateHertz int64
EnablePartialResultsStabilization bool
NumberOfChannels int64
EnableChannelIdentification bool
PartialResultsStability string
Region string
Debug bool
Config Config
}
func NewAmazonTranscribe(config Config, languageCode string, sampleRateHertz, audioChannelCount int64) *AmazonTranscribe {
return &AmazonTranscribe{
Region: config.AwsRegion,
LanguageCode: languageCode,
MediaEncoding: transcribestreamingservice.MediaEncodingOggOpus,
MediaSampleRateHertz: sampleRateHertz,
EnablePartialResultsStabilization: config.AwsEnablePartialResultsStabilization,
PartialResultsStability: config.AwsPartialResultsStability,
NumberOfChannels: audioChannelCount,
EnableChannelIdentification: config.AwsEnableChannelIdentification,
Config: config,
}
}
func NewStartStreamTranscriptionInput(at *AmazonTranscribe) transcribestreamingservice.StartStreamTranscriptionInput {
var numberOfChannels *int64
if at.EnableChannelIdentification {
numberOfChannels = aws.Int64(at.NumberOfChannels)
}
var partialResultsStability *string
if !at.EnablePartialResultsStabilization {
partialResultsStability = nil
} else {
partialResultsStability = &at.PartialResultsStability
}
return transcribestreamingservice.StartStreamTranscriptionInput{
LanguageCode: aws.String(at.LanguageCode),
MediaEncoding: aws.String(transcribestreamingservice.MediaEncodingOggOpus),
MediaSampleRateHertz: aws.Int64(at.MediaSampleRateHertz),
NumberOfChannels: numberOfChannels,
EnablePartialResultsStabilization: aws.Bool(at.EnablePartialResultsStabilization),
PartialResultsStability: partialResultsStability,
EnableChannelIdentification: aws.Bool(at.EnableChannelIdentification),
}
}
func NewAmazonTranscribeClient(config Config) *transcribestreamingservice.TranscribeStreamingService {
cfg := aws.NewConfig().WithRegion(config.AwsRegion)
if config.Debug {
cfg = cfg.WithLogLevel(aws.LogDebug)
//cfg = cfg.WithLogLevel(aws.LogDebugWithRequestErrors)
}
// TODO: 後で変更する
tr := &http.Transport{}
cfg = cfg.WithHTTPClient(&http.Client{Transport: tr})
var sess *session.Session
if config.AwsProfile != "" {
sessOpts := session.Options{
Config: *cfg,
Profile: config.AwsProfile,
SharedConfigFiles: []string{config.AwsCredentialFile},
SharedConfigState: session.SharedConfigEnable,
}
sess = session.Must(session.NewSessionWithOptions(sessOpts))
} else {
// デフォルトの HTTPClient の場合は、同時に複数接続する場合に HTTP リクエストがエラーになるため、aws.Config に独自の HTTPClient を指定する
sess = session.Must(session.NewSession(cfg))
}
return transcribestreamingservice.New(sess, cfg)
}
func (at *AmazonTranscribe) Start(ctx context.Context, r io.Reader) (*transcribestreamingservice.StartStreamTranscriptionEventStream, error) {
config := at.Config
client := NewAmazonTranscribeClient(config)
input := NewStartStreamTranscriptionInput(at)
resp, err := client.StartStreamTranscriptionWithContext(ctx, &input)
if err != nil {
if reqErr, ok := err.(awserr.RequestFailure); ok {
code := reqErr.StatusCode()
message := reqErr.Message()
var retry bool
if code == http.StatusTooManyRequests {
retry = true
}
return nil, &SuzuError{
Code: code,
Message: message,
Retry: retry,
}
}
return nil, err
}
stream := resp.GetStream()
go func() {
defer stream.Close()
if err := transcribestreamingservice.StreamAudioFromReader(ctx, stream, FrameSize, r); err != nil {
return
}
}()
return stream, nil
}