|
// Copyright 2016 Google Inc. All rights reserved. |
|
// Use of this source code is governed by the Apache 2.0 |
|
// license that can be found in the LICENSE file. |
|
|
|
// Command caption reads an audio file and outputs the transcript for it. |
|
package main |
|
|
|
import ( |
|
"fmt" |
|
"io" |
|
"log" |
|
"os" |
|
|
|
"golang.org/x/net/context" |
|
"google.golang.org/api/option" |
|
"google.golang.org/api/transport" |
|
speech "google.golang.org/genproto/googleapis/cloud/speech/v1beta1" |
|
) |
|
|
|
func main() { |
|
ctx := context.Background() |
|
conn, err := transport.DialGRPC(ctx, |
|
option.WithEndpoint("speech.googleapis.com:443"), |
|
option.WithScopes("https://www.googleapis.com/auth/cloud-platform"), |
|
) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer conn.Close() |
|
|
|
stream, err := speech.NewSpeechClient(conn).StreamingRecognize(ctx) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
// send the initial configuration message. |
|
if err := stream.Send(&speech.StreamingRecognizeRequest{ |
|
StreamingRequest: &speech.StreamingRecognizeRequest_StreamingConfig{ |
|
StreamingConfig: &speech.StreamingRecognitionConfig{ |
|
Config: &speech.RecognitionConfig{ |
|
Encoding: speech.RecognitionConfig_LINEAR16, |
|
SampleRate: 16000, |
|
}, |
|
}, |
|
}, |
|
}); err != nil { |
|
log.Fatal(err) |
|
} |
|
|
|
go func() { |
|
// pipe stdin to the API |
|
buf := make([]byte, 1024) |
|
for { |
|
n, err := os.Stdin.Read(buf) |
|
if err == io.EOF { |
|
return // nothing else to pipe, kill this goroutine |
|
} |
|
if err != nil { |
|
log.Printf("reading stdin error: %v", err) |
|
continue |
|
} |
|
if err = stream.Send(&speech.StreamingRecognizeRequest{ |
|
StreamingRequest: &speech.StreamingRecognizeRequest_AudioContent{ |
|
AudioContent: buf[:n], |
|
}, |
|
}); err != nil { |
|
log.Printf("sending audio error: %v", err) |
|
} |
|
} |
|
}() |
|
|
|
for { |
|
resp, err := stream.Recv() |
|
if err == io.EOF { |
|
break |
|
} |
|
if err != nil { |
|
// TODO: handle error |
|
continue |
|
} |
|
if resp.Error != nil { |
|
// TODO: handle error |
|
continue |
|
} |
|
for _, result := range resp.Results { |
|
fmt.Printf("result: %+v\n", result) |
|
} |
|
} |
|
} |
How I can test this in mac environment?
I tried in mac and got this responce only
bash-3.2$ rec -c 1 -r 8000 -t wav - | go run mylivecaption.go
rec WARN formats: can't set sample rate 8000; using 44100
rec WARN formats: can't set 1 channels; using 2
rec WARN wav: Length in output .wav header will be wrong since can't seek to fix it
Input File : 'default' (coreaudio)
Channels : 2
Sample Rate : 44100
Precision : 32-bit
Sample Encoding: 32-bit Signed Integer PCM
In:0.00% 00:01:02.00 [00:00:00.00] Out:495k [ | ] Clip:0 2016/12/23 13:11:53 sending audio error: EOF
2016/12/23 13:11:53 sending audio error: EOF
2016/12/23 13:11:53 sending audio error: EOF
pls let me know how to fix it