websocketv1

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

This package provides the speak/streaming client implementation for the Deepgram API

This package provides the live/streaming client implementation for the Deepgram API

This package provides the speak/streaming client implementation for the Deepgram API

Index

Constants

View Source
const (
	DefaultConnectRetry int64 = 3

	ChunkSize        = 1024 * 2
	TerminationSleep = 100 * time.Millisecond
)

external constants

View Source
const (
	// MessageTypeFlush flushes the audio from the server
	MessageTypeSpeak string = "Speak"

	// MessageTypeFlush flushes the audio from the server
	MessageTypeFlush string = "Flush"

	// MessageTypeReset resets the text buffer
	MessageTypeReset string = "Reset"

	// MessageTypeClose closes the stream
	MessageTypeClose string = "Close"
)
View Source
const (
	PackageVersion string = "v1.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client = WSCallback

Client is an alias for WSCallback Deprecated: use WSCallback instead

type TextSource

type TextSource struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

external structs

type WSCallback

type WSCallback struct {
	*common.WSClient
	// contains filtered or unexported fields
}

WSCallback is a struct representing the websocket client connection using callbacks

func NewUsingCallback

New creates a new websocket connection with the specified options

Input parameters: - ctx: context.Context object - apiKey: string containing the Deepgram API key - cOptions: ClientOptions which allows overriding things like hostname, version of the API, etc. - sOptions: SpeakOptions which allows overriding things like model, etc. - callback: SpeakMessageCallback is a callback which lets you perform actions based on platform messages

Notes:

  • If apiKey is an empty string, the Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY
  • The callback handler is set to the default handler

func NewUsingCallbackForDemo

func NewUsingCallbackForDemo(ctx context.Context, options *clientinterfaces.WSSpeakOptions) (*WSCallback, error)

NewForDemo creates a new websocket connection with all default options

Notes:

  • The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY

func NewUsingCallbackWithCancel

func NewUsingCallbackWithCancel(ctx context.Context, ctxCancel context.CancelFunc, apiKey string, cOptions *clientinterfaces.ClientOptions, sOptions *clientinterfaces.WSSpeakOptions, callback msginterfaces.SpeakMessageCallback) (*WSCallback, error)

NewWithCancel creates a new websocket connection with the specified options

Input parameters: - ctx: context.Context object - ctxCancel: allow passing in own cancel - apiKey: string containing the Deepgram API key - cOptions: ClientOptions which allows overriding things like hostname, version of the API, etc. - sOptions: SpeakOptions which allows overriding things like model, etc. - callback: SpeakMessageCallback is a callback which lets you perform actions based on platform messages

Notes:

  • If apiKey is an empty string, the Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY
  • The callback handler is set to the default handler

func NewUsingCallbackWithDefaults

func NewUsingCallbackWithDefaults(ctx context.Context, options *clientinterfaces.WSSpeakOptions, callback msginterfaces.SpeakMessageCallback) (*WSCallback, error)

NewWithDefaults creates a new websocket connection with all default options

Notes:

  • The callback handler is set to the default handler

func (*WSCallback) AttemptReconnect

func (c *WSCallback) AttemptReconnect(ctx context.Context, retries int64) bool

AttemptReconnect performs a reconnect after failing retries

func (*WSCallback) AttemptReconnectWithCancel

func (c *WSCallback) AttemptReconnectWithCancel(ctx context.Context, ctxCancel context.CancelFunc, retries int64) bool

AttemptReconnect performs a reconnect after failing retries and providing a cancel function

func (*WSCallback) Connect

func (c *WSCallback) Connect() bool

Connect performs a websocket connection with "DefaultConnectRetry" number of retries.

func (*WSCallback) ConnectWithCancel

func (c *WSCallback) ConnectWithCancel(ctx context.Context, ctxCancel context.CancelFunc, retryCnt int) bool

ConnectWithCancel performs a websocket connection with specified number of retries and providing a cancel function to stop the connection

func (*WSCallback) Finish

func (c *WSCallback) Finish()

Finish the callback

func (*WSCallback) Flush

func (c *WSCallback) Flush() error

Flush will instruct the server to flush the current text buffer

func (*WSCallback) GetCloseMsg

func (c *WSCallback) GetCloseMsg() []byte

GetCloseMsg sends an application level message to Deepgram

func (*WSCallback) GetURL

func (c *WSCallback) GetURL(host string) (string, error)

GetURL returns the websocket URL

func (*WSCallback) ProcessError

func (c *WSCallback) ProcessError(err error) error

ProcessError sends an error message to the callback handler

func (*WSCallback) ProcessMessage

func (c *WSCallback) ProcessMessage(wsType int, byMsg []byte) error

ProcessMessage processes the incoming message

func (*WSCallback) Reset

func (c *WSCallback) Reset() error

Reset will instruct the server to reset the current buffer

func (*WSCallback) Speak

func (c *WSCallback) Speak(text string) error

Speak is an alias function for SpeakWithText

func (*WSCallback) SpeakWithText

func (c *WSCallback) SpeakWithText(text string) error

SpeakWithText writes text to the websocket server to obtain corresponding audio

This function will automatically wrap the text in the appropriate JSON structure and send it to the server

Args:

text: string containing the text to be spoken

Return:

error: if successful, returns nil otherwise an error object

func (*WSCallback) Start

func (c *WSCallback) Start()

Start the callback

func (*WSCallback) WriteJSON

func (c *WSCallback) WriteJSON(playload controlMessage) error

WriteJSON writes a JSON message to the websocket

type WSChannel

type WSChannel struct {
	*common.WSClient
	// contains filtered or unexported fields
}

WSChannel is a struct representing the websocket client connection using channels

func NewUsingChan

New creates a new websocket connection with the specified options

Input parameters: - ctx: context.Context object - apiKey: string containing the Deepgram API key - cOptions: ClientOptions which allows overriding things like hostname, version of the API, etc. - tOptions: LiveTranscriptionOptions which allows overriding things like language, model, etc. - chans: LiveMessageCallback which is a chans that allows you to perform actions based on the transcription

func NewUsingChanForDemo

func NewUsingChanForDemo(ctx context.Context, options *clientinterfaces.WSSpeakOptions) (*WSChannel, error)

NewForDemo creates a new websocket connection with all default options

Notes:

  • The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY

func NewUsingChanWithCancel

func NewUsingChanWithCancel(ctx context.Context, ctxCancel context.CancelFunc, apiKey string, cOptions *clientinterfaces.ClientOptions, sOptions *clientinterfaces.WSSpeakOptions, chans msginterfaces.SpeakMessageChan) (*WSChannel, error)

New creates a new websocket connection with the specified options

Input parameters: - ctx: context.Context object - ctxCancel: allow passing in own cancel - apiKey: string containing the Deepgram API key - cOptions: ClientOptions which allows overriding things like hostname, version of the API, etc. - tOptions: LiveTranscriptionOptions which allows overriding things like language, model, etc. - chans: LiveMessageCallback which is a chans that allows you to perform actions based on the transcription

func NewUsingChanWithDefaults

func NewUsingChanWithDefaults(ctx context.Context, options *clientinterfaces.WSSpeakOptions, chans msginterfaces.SpeakMessageChan) (*WSChannel, error)

NewWithDefaults creates a new websocket connection with all default options

Notes:

  • The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY
  • The chans handler is set to the default handler which just prints all messages to the console

func (*WSChannel) AttemptReconnect

func (c *WSChannel) AttemptReconnect(ctx context.Context, retries int64) bool

AttemptReconnect performs a reconnect after failing retries

func (*WSChannel) AttemptReconnectWithCancel

func (c *WSChannel) AttemptReconnectWithCancel(ctx context.Context, ctxCancel context.CancelFunc, retries int64) bool

AttemptReconnect performs a reconnect after failing retries and providing a cancel function

func (*WSChannel) Connect

func (c *WSChannel) Connect() bool

Connect performs a websocket connection with "DefaultConnectRetry" number of retries.

func (*WSChannel) ConnectWithCancel

func (c *WSChannel) ConnectWithCancel(ctx context.Context, ctxCancel context.CancelFunc, retryCnt int) bool

ConnectWithCancel performs a websocket connection with specified number of retries and providing a cancel function to stop the connection

func (*WSChannel) Finish

func (c *WSChannel) Finish()

Finish the websocket connection

func (*WSChannel) Flush

func (c *WSChannel) Flush() error

Flush will instruct the server to flush the current text buffer

func (*WSChannel) GetCloseMsg

func (c *WSChannel) GetCloseMsg() []byte

GetCloseMsg sends an application level message to Deepgram

func (*WSChannel) GetURL

func (c *WSChannel) GetURL(host string) (string, error)

GetURL returns the websocket URL

func (*WSChannel) ProcessError

func (c *WSChannel) ProcessError(err error) error

ProcessError processes the error and sends it to the callback

func (*WSChannel) ProcessMessage

func (c *WSChannel) ProcessMessage(wsType int, byMsg []byte) error

ProcessMessage processes the message and sends it to the callback

func (*WSChannel) Reset

func (c *WSChannel) Reset() error

Reset will instruct the server to reset the current buffer

func (*WSChannel) Speak

func (c *WSChannel) Speak(text string) error

Speak is an alias function for SpeakWithText

func (*WSChannel) SpeakWithText

func (c *WSChannel) SpeakWithText(text string) error

SpeakWithText writes text to the websocket server to obtain corresponding audio

This function will automatically wrap the text in the appropriate JSON structure and send it to the server

Args:

text: string containing the text to be spoken

Return:

error: if successful, returns nil otherwise an error object

func (*WSChannel) Start

func (c *WSChannel) Start()

Start the keepalive and flush threads

func (*WSChannel) WriteJSON

func (c *WSChannel) WriteJSON(playload controlMessage) error

WriteJSON writes a JSON message to the websocket

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL