Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Outer Ticker Loop - Dead Code #247

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/speak/stream/interactive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func (c MyCallback) Metadata(md *msginterfaces.MetadataResponse) error {
}

func (c MyCallback) Binary(byMsg []byte) error {
filePath := fmt.Sprintf("%s", AUDIO_FILE)
file, err := os.Create(filePath)
fmt.Printf("\n[Binary] Received\n")

file, err := os.OpenFile(AUDIO_FILE, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
if err != nil {
fmt.Printf("Error creating file %s: %v\n", filePath, err)
fmt.Printf("Error creating file %s: %v\n", AUDIO_FILE, err)
return err
}
defer file.Close()
Expand All @@ -45,7 +46,7 @@ func (c MyCallback) Binary(byMsg []byte) error {
return err
}

fmt.Printf("Audio data saved to %s\n", filePath)
fmt.Printf("Audio data saved to %s\n", AUDIO_FILE)
return nil
}

Expand Down
131 changes: 131 additions & 0 deletions examples/speak/stream/simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 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"
"fmt"
"os"
"strings"
"time"

msginterfaces "github.com/deepgram/deepgram-go-sdk/pkg/api/speak-stream/v1/interfaces" // Add this import
"github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
"github.com/deepgram/deepgram-go-sdk/pkg/client/speak"
)

const (
API_KEY = ""
TTS_TEXT = "Hello, this is a text to speech example using Deepgram."
AUDIO_FILE = "output.mp3"
)

// Implement your own callback
type MyCallback struct{}

func (c MyCallback) Metadata(md *msginterfaces.MetadataResponse) error {
fmt.Printf("\n[Metadata] Received\n")
fmt.Printf("Metadata.RequestID: %s\n", strings.TrimSpace(md.RequestID))
return nil
}

func (c MyCallback) Binary(byMsg []byte) error {
fmt.Printf("\n[Binary] Received\n")

file, err := os.OpenFile(AUDIO_FILE, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o666)
if err != nil {
fmt.Printf("Error creating file %s: %v\n", AUDIO_FILE, err)
return err
}
defer file.Close()

_, err = file.Write(byMsg)
if err != nil {
fmt.Printf("Error writing audio data to file: %v\n", err)
return err
}

fmt.Printf("Audio data saved to %s\n", AUDIO_FILE)
return nil
}

func (c MyCallback) Flush(fl *msginterfaces.FlushedResponse) error {
fmt.Printf("\n[Flushed] Received\n")
return nil
}

func (c MyCallback) Error(er *msginterfaces.ErrorResponse) error {
fmt.Printf("\n[Error] Received\n")
fmt.Printf("Error.Type: %s\n", er.Type)
fmt.Printf("Error.Description: %s\n\n", er.Description)
return nil
}

func (c MyCallback) Close(cr *msginterfaces.CloseResponse) error {
fmt.Printf("\n[Close] Received\n")
return nil
}

func (c MyCallback) Open(or *msginterfaces.OpenResponse) error {
fmt.Printf("\n[Open] Received\n")
return nil
}

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

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

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

// set the Client options
cOptions := &interfaces.ClientOptions{}

// create the callback
callback := MyCallback{}

// create a new stream using the NewStream function
dgClient, err := speak.NewStream(ctx, "", cOptions, ttsOptions, callback)
if err != nil {
fmt.Println("ERROR creating TTS connection:", err)
return
}

// connect the websocket to Deepgram
bConnected := dgClient.Connect()
if !bConnected {
fmt.Println("Client.Connect failed")
os.Exit(1)
}

// Send the text input
err = dgClient.WriteJSON(map[string]interface{}{
"type": "Speak",
"text": TTS_TEXT,
})
if err != nil {
fmt.Printf("Error sending text input: %v\n", err)
return
}

// Flush the text input
err = dgClient.Flush()
if err != nil {
fmt.Printf("Error sending text input: %v\n", err)
return
}

// wait for user input to exit
time.Sleep(3 * time.Second)

// close the connection
dgClient.Stop()

fmt.Printf("Program exiting...\n")
}
Loading
Loading