Skip to content

Commit

Permalink
Merge pull request #192 from dvonthenen/implement-tts
Browse files Browse the repository at this point in the history
Implement TTS
  • Loading branch information
davidvonthenen authored Mar 11, 2024
2 parents 359cfa9 + 7f8cf80 commit 60f860a
Show file tree
Hide file tree
Showing 37 changed files with 1,148 additions and 223 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,23 @@ There are examples for **every** API call in this SDK. You can find all of these

These examples provide:

- PreRecorded Audio Transcription:
- Speech-to-Text: PreRecorded Audio:

- From an Audio File - [examples/prerecorded/file](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/prerecorded/file/main.go)
- From an URL - [examples/prerecorded/url](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/prerecorded/url/main.go)
- From an Audio Stream - [examples/prerecorded/stream](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/prerecorded/stream/main.go)

- Live Audio Transcription:
- Speech-to-Text: Live Audio:

- From a Microphone - [examples/streaming/microphone](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/streaming/microphone/main.go)
- From an HTTP Endpoint - [examples/streaming/http](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/streaming/http/main.go)

- Text-to-Speech

- Save audio to a Path - [examples/speak/save](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speak/save/main.go)
- Save audio to a user-defined Writer - [examples/speak/file](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speak/file/main.go)
- Save audio to a Buffer - [examples/speak/buffer](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/speak/buffer/main.go)

- Management API exercise the full [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations for:

- Balances - [examples/manage/balances](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/balances/main.go)
Expand Down
4 changes: 3 additions & 1 deletion examples/manage/balances/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (

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

// context
ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion examples/manage/usage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func main() {
// init library
client.Init(client.InitLib{
LogLevel: client.LogLevelStandard, // LogLevelStandard / LogLevelFull / LogLevelTrace
LogLevel: client.LogLevelDefault, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace
})

// context
Expand Down
79 changes: 79 additions & 0 deletions examples/speak/buffer/hello-world/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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"
"fmt"
"os"

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

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

const (
textToSpeech string = "Hello, World!"
filePath string = "./test.mp3"
)

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

// Go context
ctx := context.Background()

// set the Transcription options
options := interfaces.SpeakOptions{
Model: "aura-asteria-en",
}

// create a Deepgram client
c := client.NewWithDefaults()
dg := speak.New(c)

// create file
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("os.Create failed. Err: %v\n", err)
os.Exit(1)
}
defer file.Close()

// buffer
var buffer interfaces.RawResponse

// send/process file to Deepgram
res, err := dg.ToStream(ctx, textToSpeech, options, &buffer)
if err != nil {
fmt.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

// save the file
_, err = file.Write(buffer.Bytes())
if err != nil {
fmt.Printf("file.Write failed. Err: %v\n", err)
os.Exit(1)
}

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

// make the JSON pretty
prettyJson, err := prettyjson.Format(data)
if err != nil {
fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
80 changes: 80 additions & 0 deletions examples/speak/buffer/woodchuck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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"
"fmt"
"os"

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

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

const (
textToSpeech string = "How much wood could a woodchuck chuck? If a woodchuck could chuck wood? As much wood as a woodchuck could chuck, if a woodchuck could chuck wood."
filePath string = "./test.mp3"
)

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

// Go context
ctx := context.Background()

// set the Transcription options
options := interfaces.SpeakOptions{
Model: "aura-asteria-en",
}

// create a Deepgram client
c := client.NewWithDefaults()
dg := speak.New(c)

// create file
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("os.Create failed. Err: %v\n", err)
os.Exit(1)
}
defer file.Close()

// buffer
var buffer interfaces.RawResponse

// send/process file to Deepgram
res, err := dg.ToStream(ctx, textToSpeech, options, &buffer)
if err != nil {
fmt.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

// save the file
_, err = buffer.WriteTo(file)
// _, err = file.Write(buffer.Bytes())
if err != nil {
fmt.Printf("file.Write failed. Err: %v\n", err)
os.Exit(1)
}

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

// make the JSON pretty
prettyJson, err := prettyjson.Format(data)
if err != nil {
fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
61 changes: 61 additions & 0 deletions examples/speak/file/hello-world/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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"
"fmt"
"os"

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

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

const (
textToSpeech string = "Hello, World!"
filePath string = "./test.mp3"
)

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

// Go context
ctx := context.Background()

// set the Transcription options
options := interfaces.SpeakOptions{
Model: "aura-asteria-en",
}

// create a Deepgram client
c := client.NewWithDefaults()
dg := speak.New(c)

// send/process file to Deepgram
res, err := dg.ToSave(ctx, filePath, textToSpeech, options)
if err != nil {
fmt.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

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

// make the JSON pretty
prettyJson, err := prettyjson.Format(data)
if err != nil {
fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
63 changes: 63 additions & 0 deletions examples/speak/file/woodchuck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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"
"fmt"
"os"

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

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

const (
textToSpeech string = "How much wood could a woodchuck chuck? If a woodchuck could chuck wood? As much wood as a woodchuck could chuck, if a woodchuck could chuck wood."
filePath string = "./test.mp3"
)

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

// Go context
ctx := context.Background()

// set the Transcription options
options := interfaces.SpeakOptions{
Model: "aura-asteria-en",
}

// create a Deepgram client
c := client.NewWithDefaults()
dg := speak.New(c)

// send/process file to Deepgram
res, err := dg.ToSave(ctx, filePath, textToSpeech, options)
if err != nil {
fmt.Printf("FromStream failed. Err: %v\n", err)
os.Exit(1)
}

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

// make the JSON pretty
prettyJson, err := prettyjson.Format(data)
if err != nil {
fmt.Printf("prettyjson.Marshal failed. Err: %v\n", err)
os.Exit(1)
}
fmt.Printf("\n\nResult:\n%s\n\n", prettyJson)
}
Loading

0 comments on commit 60f860a

Please sign in to comment.