diff --git a/examples/streaming/test/main.go b/examples/streaming/test/main.go new file mode 100644 index 00000000..cc79ff0e --- /dev/null +++ b/examples/streaming/test/main.go @@ -0,0 +1,104 @@ +// 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 + +// streaming +import ( + "bufio" + "context" + "fmt" + "os" + + microphone "github.com/deepgram/deepgram-go-sdk/pkg/audio/microphone" + interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces" + client "github.com/deepgram/deepgram-go-sdk/pkg/client/live" +) + +func main() { + // init library + microphone.Initialize() + + /* + DG Streaming API + */ + // init library + client.Init(client.InitLib{ + LogLevel: client.LogLevelDefault, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace + }) + + // Go context + ctx := context.Background() + + // set the Transcription options + tOptions := interfaces.LiveTranscriptionOptions{ + Model: "nova-2", + Language: "en-US", + Punctuate: true, + Encoding: "linear16", + Channels: 1, + SampleRate: 16000, + SmartFormat: true, + } + + // create a Deepgram client + dgClient, err := client.NewForDemo(ctx, tOptions) + if err != nil { + fmt.Println("ERROR creating LiveTranscription connection:", err) + return + } + + // connect the websocket to Deepgram + wsconn := dgClient.Connect() + if wsconn == nil { + fmt.Println("Client.Connect failed") + os.Exit(1) + } + + /* + Microphone package + */ + // mic stuf + mic, err := microphone.New(microphone.AudioConfig{ + InputChannels: 1, + SamplingRate: 16000, + }) + if err != nil { + fmt.Printf("Initialize failed. Err: %v\n", err) + os.Exit(1) + } + + // start the mic + err = mic.Start() + if err != nil { + fmt.Printf("mic.Start failed. Err: %v\n", err) + os.Exit(1) + } + + go func() { + // feed the microphone stream to the Deepgram client (this is a blocking call) + mic.Stream(dgClient) + }() + + fmt.Print("Press ENTER to exit!\n\n") + input := bufio.NewScanner(os.Stdin) + input.Scan() + + // close mic stream + err = mic.Stop() + if err != nil { + fmt.Printf("mic.Stop failed. Err: %v\n", err) + os.Exit(1) + } + + // teardown library + microphone.Teardown() + + // close DG client + dgClient.Stop() + + fmt.Printf("Program exiting...\n") + // time.Sleep(120 * time.Second) + +} diff --git a/pkg/api/analyze/v1/interfaces/types.go b/pkg/api/analyze/v1/interfaces/types.go index e871830a..b98c5324 100644 --- a/pkg/api/analyze/v1/interfaces/types.go +++ b/pkg/api/analyze/v1/interfaces/types.go @@ -32,13 +32,13 @@ type TopicsInfo struct { } type Metadata struct { - RequestID string `json:"request_id,omitempty"` - Created string `json:"created,omitempty"` - Language string `json:"language,omitempty"` - IntentsInfo IntentsInfo `json:"intents_info,omitempty"` - SentimentInfo SentimentInfo `json:"sentiment_info,omitempty"` - SummaryInfo SummaryInfo `json:"summary_info,omitempty"` - TopicsInfo TopicsInfo `json:"topics_info,omitempty"` + RequestID string `json:"request_id,omitempty"` + Created string `json:"created,omitempty"` + Language string `json:"language,omitempty"` + IntentsInfo *IntentsInfo `json:"intents_info,omitempty"` + SentimentInfo *SentimentInfo `json:"sentiment_info,omitempty"` + SummaryInfo *SummaryInfo `json:"summary_info,omitempty"` + TopicsInfo *TopicsInfo `json:"topics_info,omitempty"` } type Average struct { @@ -61,13 +61,13 @@ type Intent struct { } type Segment struct { - Text string `json:"text,omitempty"` - StartWord int `json:"start_word,omitempty"` - EndWord int `json:"end_word,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` - Topics []Topic `json:"topics,omitempty"` - Intents []Intent `json:"intents,omitempty"` + Text string `json:"text,omitempty"` + StartWord int `json:"start_word,omitempty"` + EndWord int `json:"end_word,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` + Topics *[]Topic `json:"topics,omitempty"` + Intents *[]Intent `json:"intents,omitempty"` } type Sentiments struct { @@ -84,10 +84,10 @@ type Intents struct { } type Results struct { - Sentiments Sentiments `json:"sentiments,omitempty"` - Summary Summary `json:"summary,omitempty"` - Topics Topics `json:"topics,omitempty"` - Intents Intents `json:"intents,omitempty"` + Sentiments *Sentiments `json:"sentiments,omitempty"` + Summary *Summary `json:"summary,omitempty"` + Topics *Topics `json:"topics,omitempty"` + Intents *Intents `json:"intents,omitempty"` } /***********************************/ diff --git a/pkg/api/live/v1/interfaces/types.go b/pkg/api/live/v1/interfaces/types.go index ae34aa7b..7eb33005 100644 --- a/pkg/api/live/v1/interfaces/types.go +++ b/pkg/api/live/v1/interfaces/types.go @@ -14,7 +14,7 @@ type Word struct { PunctuatedWord string `json:"punctuated_word,omitempty"` Start float64 `json:"start,omitempty"` Word string `json:"word,omitempty"` - Speaker int `json:"speaker,omitempty"` + Speaker *int `json:"speaker,omitempty"` } // Alternative is a single alternative in a transcript diff --git a/pkg/api/prerecorded/v1/interfaces/types.go b/pkg/api/prerecorded/v1/interfaces/types.go index e070410a..2a16deb2 100644 --- a/pkg/api/prerecorded/v1/interfaces/types.go +++ b/pkg/api/prerecorded/v1/interfaces/types.go @@ -46,11 +46,11 @@ type Metadata struct { Channels int `json:"channels,omitempty"` Models []string `json:"models,omitempty"` ModelInfo map[string]ModelInfo `json:"model_info,omitempty"` - Warnings []Warning `json:"warnings,omitempty"` - SummaryInfo SummaryInfo `json:"summary_info,omitempty"` - IntentsInfo IntentsInfo `json:"intents_info,omitempty"` - SentimentInfo SentimentInfo `json:"sentiment_info,omitempty"` - TopicsInfo TopicsInfo `json:"topics_info,omitempty"` + Warnings *[]Warning `json:"warnings,omitempty"` + SummaryInfo *SummaryInfo `json:"summary_info,omitempty"` + IntentsInfo *IntentsInfo `json:"intents_info,omitempty"` + SentimentInfo *SentimentInfo `json:"sentiment_info,omitempty"` + TopicsInfo *TopicsInfo `json:"topics_info,omitempty"` Extra map[string]string `json:"extra,omitempty"` } @@ -73,15 +73,15 @@ type Search struct { } type Word struct { - Word string `json:"word,omitempty"` - Start float64 `json:"start,omitempty"` - End float64 `json:"end,omitempty"` - Confidence float64 `json:"confidence,omitempty"` - Speaker int `json:"speaker,omitempty"` - SpeakerConfidence float64 `json:"speaker_confidence,omitempty"` - PunctuatedWord string `json:"punctuated_word,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` + Word string `json:"word,omitempty"` + Start float64 `json:"start,omitempty"` + End float64 `json:"end,omitempty"` + Confidence float64 `json:"confidence,omitempty"` + Speaker *int `json:"speaker,omitempty"` + SpeakerConfidence *float64 `json:"speaker_confidence,omitempty"` + PunctuatedWord string `json:"punctuated_word,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` } type Translation struct { @@ -90,13 +90,13 @@ type Translation struct { } type Alternative struct { - Transcript string `json:"transcript,omitempty"` - Confidence float64 `json:"confidence,omitempty"` - Words []Word `json:"words,omitempty"` - Paragraphs Paragraph `json:"paragraphs,omitempty"` - Entities []Entity `json:"entities,omitempty"` - Summaries []SummaryV1 `json:"summaries,omitempty"` - Translation Translation `json:"translation,omitempty"` + Transcript string `json:"transcript,omitempty"` + Confidence float64 `json:"confidence,omitempty"` + Words []Word `json:"words,omitempty"` + Paragraphs *Paragraphs `json:"paragraphs,omitempty"` + Entities *[]Entity `json:"entities,omitempty"` + Summaries *[]SummaryV1 `json:"summaries,omitempty"` + Translation *Translation `json:"translation,omitempty"` } type Paragraphs struct { @@ -109,16 +109,16 @@ type Paragraph struct { NumWords int `json:"num_words,omitempty"` Start float64 `json:"start,omitempty"` End float64 `json:"end,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` } type Sentence struct { - Text string `json:"text,omitempty"` - Start float64 `json:"start,omitempty"` - End float64 `json:"end,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` + Text string `json:"text,omitempty"` + Start float64 `json:"start,omitempty"` + End float64 `json:"end,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` } type Entity struct { @@ -130,23 +130,23 @@ type Entity struct { } type Channel struct { - Search []Search `json:"search,omitempty"` + Search *[]Search `json:"search,omitempty"` Alternatives []Alternative `json:"alternatives,omitempty"` DetectedLanguage string `json:"detected_language,omitempty"` LanguageConfidence float64 `json:"language_confidence,omitempty"` } type Utterance struct { - Start float64 `json:"start,omitempty"` - End float64 `json:"end,omitempty"` - Confidence float64 `json:"confidence,omitempty"` - Channel int `json:"channel,omitempty"` - Transcript string `json:"transcript,omitempty"` - Words []Word `json:"words,omitempty"` - Speaker int `json:"speaker,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` - ID string `json:"id,omitempty"` + Start float64 `json:"start,omitempty"` + End float64 `json:"end,omitempty"` + Confidence float64 `json:"confidence,omitempty"` + Channel int `json:"channel,omitempty"` + Transcript string `json:"transcript,omitempty"` + Words []Word `json:"words,omitempty"` + Speaker *int `json:"speaker,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` + ID string `json:"id,omitempty"` } type Intent struct { @@ -165,13 +165,13 @@ type Topic struct { } type Segment struct { - Text string `json:"text,omitempty"` - StartWord int `json:"start_word,omitempty"` - EndWord int `json:"end_word,omitempty"` - Sentiment string `json:"sentiment,omitempty"` - SentimentScore float64 `json:"sentiment_score,omitempty"` - Topics []Topic `json:"topics,omitempty"` - Intents []Intent `json:"intents,omitempty"` + Text string `json:"text,omitempty"` + StartWord int `json:"start_word,omitempty"` + EndWord int `json:"end_word,omitempty"` + Sentiment *string `json:"sentiment,omitempty"` + SentimentScore *float64 `json:"sentiment_score,omitempty"` + Topics *[]Topic `json:"topics,omitempty"` + Intents *[]Intent `json:"intents,omitempty"` } type Sentiments struct { @@ -203,17 +203,17 @@ type Summary SummaryV2 // internal reference to old name type Result struct { Channels []Channel `json:"channels,omitempty"` Utterances []Utterance `json:"utterances,omitempty"` - Summary SummaryV2 `json:"summary,omitempty"` - Sentiments Sentiments `json:"sentiments,omitempty"` - Topics Topics `json:"topics,omitempty"` - Intents Intents `json:"intents,omitempty"` + Summary *SummaryV2 `json:"summary,omitempty"` + Sentiments *Sentiments `json:"sentiments,omitempty"` + Topics *Topics `json:"topics,omitempty"` + Intents *Intents `json:"intents,omitempty"` } /***********************************/ // response/result structs /***********************************/ type PreRecordedResponse struct { - RequestID string `json:"request_id,omitempty"` // for ?callback=... - Metadata Metadata `json:"metadata,omitempty"` - Results Result `json:"results,omitempty"` + RequestID string `json:"request_id,omitempty"` // for ?callback=... + Metadata *Metadata `json:"metadata,omitempty"` + Results *Result `json:"results,omitempty"` }