Skip to content

Instantly share code, notes, and snippets.

@abnercosta
Forked from rakyll/livecaption.go
Created April 2, 2018 01:48
Show Gist options
  • Select an option

  • Save abnercosta/06ccc56e26f836be0e36f7b6400e1d17 to your computer and use it in GitHub Desktop.

Select an option

Save abnercosta/06ccc56e26f836be0e36f7b6400e1d17 to your computer and use it in GitHub Desktop.

Revisions

  1. Jaana Burcu Dogan revised this gist Aug 30, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions livecaption.go
    Original file line number Diff line number Diff line change
    @@ -74,11 +74,11 @@ func main() {
    break
    }
    if err != nil {
    log.Printf("streaming response error: %v", err)
    // TODO: handle error
    continue
    }
    if resp.Error != nil {
    log.Printf("grpc error: %v", resp.Error)
    // TODO: handle error
    continue
    }
    for _, result := range resp.Results {
  2. Jaana Burcu Dogan revised this gist Aug 30, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion livecaption.go
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,7 @@ func main() {
    continue
    }
    for _, result := range resp.Results {
    fmt.Printf("result: %+v", result)
    fmt.Printf("result: %+v\n", result)
    }
    }
    }
  3. Jaana Burcu Dogan revised this gist Aug 30, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions livecaption.go
    Original file line number Diff line number Diff line change
    @@ -70,6 +70,9 @@ func main() {

    for {
    resp, err := stream.Recv()
    if err == io.EOF {
    break
    }
    if err != nil {
    log.Printf("streaming response error: %v", err)
    continue
  4. Jaana Burcu Dogan created this gist Aug 30, 2016.
    85 changes: 85 additions & 0 deletions livecaption.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    // 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 != nil {
    log.Printf("streaming response error: %v", err)
    continue
    }
    if resp.Error != nil {
    log.Printf("grpc error: %v", resp.Error)
    continue
    }
    for _, result := range resp.Results {
    fmt.Printf("result: %+v", result)
    }
    }
    }