Skip to content

Commit

Permalink
Restores the VTT/SRT, Fixes Comments, and etc
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Nov 29, 2023
1 parent ff71f5b commit b4eddfe
Show file tree
Hide file tree
Showing 32 changed files with 149 additions and 144 deletions.
14 changes: 14 additions & 0 deletions examples/prerecorded/file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ func main() {
os.Exit(1)
}
log.Printf("\n\nResult:\n%s\n\n", prettyJson)

vtt, err := res.ToWebVTT()
if err != nil {
log.Printf("ToWebVTT failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("\n\n\nVTT:\n%s\n\n\n", vtt)

srt, err := res.ToSRT()
if err != nil {
log.Printf("ToSRT failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("\n\n\nSRT:\n%s\n\n\n", srt)
}
12 changes: 6 additions & 6 deletions pkg/api/live/v1/interfaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

package interfaces

/*
Shared defintions for the Deepgram API
*/
/***********************************/
// shared/common structs
/***********************************/
// Word is a single word in a transcript
type Word struct {
Confidence float64 `json:"confidence,omitempty"`
Expand Down Expand Up @@ -42,9 +42,9 @@ type Metadata struct {
RequestID string `json:"request_id,omitempty"`
}

/*
Results from Live Transcription
*/
/***********************************/
// Results from Live Transcription
/***********************************/
// MessageResponse is the response from a live transcription
type MessageResponse struct {
Channel Channel `json:"channel,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/manage/v1/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

/*
This package contains the code for the Keys APIs in the Deepgram Manage API
*/
package manage

import (
Expand Down
64 changes: 0 additions & 64 deletions pkg/api/prerecorded/v1/interfaces/interfaces.go

This file was deleted.

66 changes: 66 additions & 0 deletions pkg/api/prerecorded/v1/interfaces/prerecorded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// // 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

/*
This package provides the types for the Deepgram PreRecorded API.
*/
package interfaces

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)

// ToWebVTT implements output for VTT
func (resp *PreRecordedResponse) ToWebVTT() (string, error) {
if resp.Results.Utterances == nil {
return "", errors.New("this function requires a transcript that was generated with the utterances feature")
}

vtt := "WEBVTT\n\n"
vtt += "NOTE\nTranscription provided by Deepgram\nRequest ID: " + resp.Metadata.RequestID + "\nCreated: " + resp.Metadata.Created + "\n\n"

for i, utterance := range resp.Results.Utterances {
utterance := utterance
start := SecondsToTimestamp(utterance.Start)
end := SecondsToTimestamp(utterance.End)
vtt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)
}
return vtt, nil
}

// ToSRT implements output for SRT
func (resp *PreRecordedResponse) ToSRT() (string, error) {
if resp.Results.Utterances == nil {
return "", errors.New("this function requires a transcript that was generated with the utterances feature")
}

srt := ""

for i, utterance := range resp.Results.Utterances {
utterance := utterance
start := SecondsToTimestamp(utterance.Start)
end := SecondsToTimestamp(utterance.End)
end = strings.ReplaceAll(end, ".", ",")
srt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)

}
return srt, nil
}

func SecondsToTimestamp(seconds float64) string {
hours := int(seconds / 3600)
minutes := int((seconds - float64(hours*3600)) / 60)
seconds = seconds - float64(hours*3600) - float64(minutes*60)
return fmt.Sprintf("%02d:%02d:%02.3f", hours, minutes, seconds)
}

func GetJson(resp *http.Response, target interface{}) error {
defer resp.Body.Close()

return json.NewDecoder(resp.Body).Decode(target)
}
9 changes: 8 additions & 1 deletion pkg/api/prerecorded/v1/interfaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

/*
This package provides the types for the Deepgram PreRecorded API.
*/
package interfaces

/***********************************/
// share/common structs
/***********************************/
type Metadata struct {
TransactionKey string `json:"transaction_key,omitempty"`
RequestID string `json:"request_id,omitempty"`
Expand Down Expand Up @@ -134,7 +139,9 @@ type SummaryV2 struct {
}
type Summary SummaryV2 // internal reference to old name

// Response
/***********************************/
// response/result structs
/***********************************/
type PreRecordedResponse struct {
RequestID string `json:"request_id,omitempty"` // for ?callback=...
Metadata Metadata `json:"metadata,omitempty"`
Expand Down
49 changes: 0 additions & 49 deletions pkg/api/prerecorded/v1/prerecorded.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,52 +121,3 @@ func (c *PrerecordedClient) FromURL(ctx context.Context, url string, options int

return &resp, nil
}

// func (resp *PreRecordedResponse) ToWebVTT() (string, error) {
// if resp.Results.Utterances == nil {
// return "", errors.New("this function requires a transcript that was generated with the utterances feature")
// }

// vtt := "WEBVTT\n\n"

// vtt += "NOTE\nTranscription provided by Deepgram\nRequest ID: " + resp.Metadata.RequestId + "\nCreated: " + resp.Metadata.Created + "\n\n"

// for i, utterance := range resp.Results.Utterances {
// utterance := utterance
// start := SecondsToTimestamp(utterance.Start)
// end := SecondsToTimestamp(utterance.End)
// vtt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)
// }
// return vtt, nil
// }

// func (resp *PreRecordedResponse) ToSRT() (string, error) {
// if resp.Results.Utterances == nil {
// return "", errors.New("this function requires a transcript that was generated with the utterances feature")
// }

// srt := ""

// for i, utterance := range resp.Results.Utterances {
// utterance := utterance
// start := SecondsToTimestamp(utterance.Start)
// end := SecondsToTimestamp(utterance.End)
// end = strings.ReplaceAll(end, ".", ",")
// srt += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", i+1, start, end, utterance.Transcript)

// }
// return srt, nil
// }

// func SecondsToTimestamp(seconds float64) string {
// hours := int(seconds / 3600)
// minutes := int((seconds - float64(hours*3600)) / 60)
// seconds = seconds - float64(hours*3600) - float64(minutes*60)
// return fmt.Sprintf("%02d:%02d:%02.3f", hours, minutes, seconds)
// }

// func GetJson(resp *http.Response, target interface{}) error {
// defer resp.Body.Close()

// return json.NewDecoder(resp.Body).Decode(target)
// }
9 changes: 0 additions & 9 deletions pkg/api/prerecorded/v1/types.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/api/version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

// This package handles the versioning in the API for live/streaming transcription
package version

import "errors"
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/version/manage-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

/*
This package handles the versioning in the API both async and streaming
*/
// This package handles the versioning in the API for live/streaming transcription
package version

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/audio/microphone/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

// Implementation microphones using portaudio
// Implementation microphone using portaudio
package microphone

const (
Expand Down
7 changes: 3 additions & 4 deletions pkg/audio/microphone/microphone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

// Implementation microphones using portaudio
// Implementation microphone using portaudio
package microphone

import (
Expand All @@ -19,15 +19,15 @@ import (
func Initialize() {
err := portaudio.Initialize()
if err != nil {
klog.V(1).Printf("portaudio.Initialize failed. Err: %v\n", err)
klog.V(1).Infof("portaudio.Initialize failed. Err: %v\n", err)
}
}

// Teardown cleans up the library
func Teardown() {
err := portaudio.Terminate()
if err != nil {
klog.V(1).Printf("portaudio.Terminate failed. Err: %v\n", err)
klog.V(1).Infof("portaudio.Terminate failed. Err: %v\n", err)
}
}

Expand Down Expand Up @@ -97,7 +97,6 @@ func (m *Microphone) Stream(w io.Writer) error {
}

byteCount, err := w.Write(m.int16ToLittleEndianByte(m.intBuf))
// byteCount, err := w.Write(m.int16ToLittleEndianByte(m.intBuf))
if err != nil {
klog.V(1).Infof("w.Write failed. Err: %v\n", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/audio/microphone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

// Implementation microphones using portaudio
// Implementation microphone using portaudio
package microphone

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/interfaces/types-prerecorded.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

/*
This package contains the interface to manage the prerecorded interfaces for the Deepgram API
This package contains the interface to manage the prerecorded and live/stream interfaces for the Deepgram API
*/
package interfaces

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/interfaces/types-stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

/*
This package contains the interface to manage the live/streaming interfaces for the Deepgram API
This package contains the interface to manage the prerecorded and live/stream interfaces for the Deepgram API
*/
package interfaces

Expand Down
3 changes: 3 additions & 0 deletions pkg/client/interfaces/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

/*
This package contains the interface to manage the prerecorded and live/stream interfaces for the Deepgram API
*/
package interfaces

import (
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/live/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

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

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/live/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

/*
This package contains the initialization code for the Deepgram Go SDK
This package provides the live/streaming client implementation for the Deepgram API
*/
package live

Expand Down
Loading

0 comments on commit b4eddfe

Please sign in to comment.