Skip to content

Commit

Permalink
Merge pull request #123 from dvonthenen/example-live-with-microphone
Browse files Browse the repository at this point in the history
Implement Microphone Streaming Example and Clean Up Other Examples
  • Loading branch information
dvonthenen authored Nov 4, 2023
2 parents 3ec778a + a5e2041 commit 43a790f
Show file tree
Hide file tree
Showing 23 changed files with 555 additions and 106 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Binaries for programs and plugins
.DS_Store
*.exe
*.exe~
*.dll
*.o
*.a
*.so
*.dylib

# Folders
_obj
_test
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# cgo stuff
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
.idea/
*.iml
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
65 changes: 65 additions & 0 deletions examples/prerecorded/file/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"encoding/json"
"log"
"os"

prettyjson "github.com/hokaccha/go-prettyjson"

prerecorded "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/prerecorded/v1"
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

const (
filePath string = "./Bueller-Life-moves-pretty-fast.mp3"
)

func main() {
// init library
client.Init(client.InitLib{
LogLevel: client.LogLevelFull,
})

// context
ctx := context.Background()

//client
c := client.NewWithDefaults()
dg := prerecorded.New(c)

// send file stream
res, err := dg.FromFile(
ctx,
filePath,
interfaces.PreRecordedTranscriptionOptions{
Punctuate: true,
Diarize: true,
Language: "en-US",
Utterances: true,
},
)
if err != nil {
log.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

data, err := json.Marshal(res)
if err != nil {
log.Printf("json.Marshal failed. Err: %v\n", err)
os.Exit(1)
}

prettyJson, err := prettyjson.Format(data)
if err != nil {
log.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
96 changes: 0 additions & 96 deletions examples/prerecorded/main.go

This file was deleted.

Binary file not shown.
72 changes: 72 additions & 0 deletions examples/prerecorded/stream/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"encoding/json"
"log"
"os"

prettyjson "github.com/hokaccha/go-prettyjson"

prerecorded "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/prerecorded/v1"
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

const (
filePath string = "./Bueller-Life-moves-pretty-fast.mp3"
)

func main() {
// init library
client.Init(client.InitLib{
LogLevel: client.LogLevelFull,
})

// context
ctx := context.Background()

//client
c := client.NewWithDefaults()
dg := prerecorded.New(c)

// send file stream
file, err := os.Open(filePath)
if err != nil {
log.Printf("os.Open(%s) failed. Err: %v\n", filePath, err)
os.Exit(1)
}
defer file.Close()

res, err := dg.FromStream(
ctx,
file,
interfaces.PreRecordedTranscriptionOptions{
Punctuate: true,
Diarize: true,
Language: "en-US",
Utterances: true,
},
)
if err != nil {
log.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

data, err := json.Marshal(res)
if err != nil {
log.Printf("json.Marshal failed. Err: %v\n", err)
os.Exit(1)
}

prettyJson, err := prettyjson.Format(data)
if err != nil {
log.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
64 changes: 64 additions & 0 deletions examples/prerecorded/url/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"encoding/json"
"log"
"os"

prettyjson "github.com/hokaccha/go-prettyjson"

prerecorded "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/prerecorded/v1"
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

const (
url string = "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"
)

func main() {
// init library
client.InitWithDefault()

// context
ctx := context.Background()

//client
c := client.NewWithDefaults()
dg := prerecorded.New(c)

// send stream
res, err := dg.FromURL(
ctx,
url,
interfaces.PreRecordedTranscriptionOptions{
Punctuate: true,
Diarize: true,
Language: "en-US",
Utterances: true,
Redact: []string{"pci", "ssn"},
},
)
if err != nil {
log.Printf("FromURL failed. Err: %v\n", err)
os.Exit(1)
}

data, err := json.Marshal(res)
if err != nil {
log.Printf("json.Marshal failed. Err: %v\n", err)
os.Exit(1)
}

prettyJson, err := prettyjson.Format(data)
if err != nil {
log.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ import (
"net/http"
"os"
"reflect"
"time"

interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/live"
)

const (
STREAM_URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
CHUNK_SIZE = 1024 * 2
TEN_MILLISECONDS_SLEEP = 10 * time.Millisecond
STREAM_URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
)

func main() {
Expand Down Expand Up @@ -75,6 +72,4 @@ func main() {

// close client
dgClient.Stop()

log.Printf("Succeeded!\n\n")
}
20 changes: 20 additions & 0 deletions examples/streaming/microphone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Live API (Real-Time) Example

This example uses the Microphone as input in order to detect conversation insights in what is being said. This example required additional components (for the microphone) to be installed in order for this example to function correctly.

## Configuration

The SDK (and this example) needs to be initialized with your account's credentials `DEEPGRAM_API_KEY`, which are available in your [Deepgram Console][dg-console]. If you don't have a Deepgram account, you can [sign up here][dg-signup] for free.

You must add your `DEEPGRAM_API_KEY` to your list of environment variables. We use environment variables because they are easy to configure, support PaaS-style deployments, and work well in containerized environments like Docker and Kubernetes.

```sh
export DEEPGRAM_API_KEY=YOUR-APP-KEY-HERE
```

## Installation

The Live API (Real-Time) example makes use of a [microphone package](https://github.com/deepgram-devs/deepgram-go-sdk/tree/main/pkg/audio/microphone) contained within the repository. That package makes use of the [PortAudio library](http://www.portaudio.com/) which is a cross-platform open source audio library. If you are on Linux, you can install this library using whatever package manager is available (yum, apt, etc.) on your operating system. If you are on macOS, you can install this library using [brew](https://brew.sh/).

[dg-console]: https://console.deepgram.com/
[dg-signup]: https://console.deepgram.com/signup
Loading

0 comments on commit 43a790f

Please sign in to comment.