@@ -3,9 +3,11 @@ package main
33import (
44 "context"
55 "encoding/csv"
6+ "flag"
67 "fmt"
78 "os"
89 "path/filepath"
10+ "strings"
911 "sync"
1012 "time"
1113
@@ -24,6 +26,29 @@ func checkDeepgramKey() error {
2426
2527func main () {
2628
29+ var dgContainer , dgEncoding string
30+
31+ // Define the commandline flags
32+ dgModelName := flag .String ("model" , "aura-asteria-en" , "Deepgram model name. Defaults to aura-asteria-en" )
33+ dgFileFormat := flag .String ("format" , "mp3" , "File format for the generated audio files. Defaults to mp3" )
34+ outputFolder := flag .String ("output" , "audio" , "Output folder for the generated audio files." )
35+ csvLocation := flag .String ("csv" , "scripts.csv" , "Location of the CSV file containing the scripts to convert to audio." )
36+ // parse the commandline flags
37+ flag .Parse ()
38+
39+ // If the DG file format is neither wav nor mp3, raise error.
40+ if strings .ToLower (* dgFileFormat ) != "wav" && strings .ToLower (* dgFileFormat ) != "mp3" {
41+ fmt .Println ("Invalid file format. Only wav and mp3 are supported." )
42+ return
43+ }
44+
45+ if strings .ToLower (* dgFileFormat ) == "wav" {
46+ dgContainer = "wav"
47+ dgEncoding = "linear16"
48+ } else {
49+ dgContainer = ""
50+ dgEncoding = "mp3"
51+ }
2752 // First check if the Deepgram API key is set
2853 if dgErr := checkDeepgramKey (); dgErr != nil {
2954 fmt .Println (dgErr )
@@ -38,22 +63,23 @@ func main() {
3863
3964 // set the Transcription options
4065 options := & interfaces.SpeakOptions {
41- Model : "aura-asteria-en" ,
66+ Model : strings .ToLower (* dgModelName ),
67+ Container : dgContainer ,
68+ Encoding : dgEncoding ,
4269 }
4370
4471 // create a Deepgram client
4572 c := client .NewRESTWithDefaults ()
4673 dg := api .New (c )
4774
4875 // Check if the file exists
49- fileName := "scripts.csv"
50- if _ , err := os .Stat (fileName ); os .IsNotExist (err ) {
51- fmt .Print ("Enter the name of the CSV file: " )
52- fmt .Scanln (& fileName )
76+ if _ , err := os .Stat (* csvLocation ); os .IsNotExist (err ) {
77+ fmt .Print ("The file does not exist. Please enter the correct file path." )
78+ return
5379 }
5480
5581 // Open the CSV file
56- file , err := os .Open (fileName )
82+ file , err := os .Open (* csvLocation )
5783 if err != nil {
5884 fmt .Printf ("Error opening CSV file: %s\n " , err )
5985 return
@@ -71,7 +97,7 @@ func main() {
7197 }
7298
7399 // Create the "audio" directory if it doesn't exist
74- audioDir := "audio"
100+ audioDir := strings . ToLower ( * outputFolder )
75101 if _ , err := os .Stat (audioDir ); os .IsNotExist (err ) {
76102 err := os .Mkdir (audioDir , 0755 )
77103 if err != nil {
@@ -101,7 +127,7 @@ func main() {
101127 defer wg .Done ()
102128
103129 // Perform TTS and save to disk
104- audioPath := filepath .Join (audioDir , fmt .Sprintf ("%s.mp3 " , label ))
130+ audioPath := filepath .Join (audioDir , fmt .Sprintf ("%s.%s " , label , * dgFileFormat ))
105131 err := generateTTSAndSave (ctx , dg , script , options , audioPath )
106132 if err != nil {
107133 fmt .Printf ("Could not generate TTS for row %v - %v: %v\n " , i , label , err )
0 commit comments