@@ -44,67 +44,110 @@ def cli(ctx, args):
44
44
# If no arguments or a subcommand is provided, invoke the group command
45
45
cli_group .main (args = args )
46
46
else :
47
- # Otherwise, treat it as a direct text-to-speech conversion
48
- output = None
49
- default_model , default_voice = get_config ()
50
- model = default_model
51
- voice = default_voice
52
- text = []
53
-
54
- i = 0
55
- while i < len (args ):
56
- if args [i ] in ["-o" , "--output" ] and i + 1 < len (args ):
57
- output = args [i + 1 ]
58
- i += 2
59
- elif args [i ] in ["-m" , "--model" ] and i + 1 < len (args ):
60
- try :
61
- model = validate_model (None , None , args [i + 1 ])
62
- except click .BadParameter as e :
63
- click .echo (f"Error: { str (e )} " , err = True )
64
- ctx .exit (1 )
65
- i += 2
66
- elif args [i ] in ["-v" , "--voice" ] and i + 1 < len (args ):
67
- try :
68
- voice = validate_voice (None , None , args [i + 1 ])
69
- except click .BadParameter as e :
70
- click .echo (f"Error: { str (e )} " , err = True )
47
+ # Check if the first argument is a file
48
+ if Path (args [0 ]).is_file ():
49
+ # Treat it as a file conversion
50
+ input_file = args [0 ]
51
+ output = None
52
+ default_model , default_voice = get_config ()
53
+ model = default_model
54
+ voice = default_voice
55
+
56
+ i = 1
57
+ while i < len (args ):
58
+ if args [i ] in ["-o" , "--output" ] and i + 1 < len (args ):
59
+ output = args [i + 1 ]
60
+ i += 2
61
+ elif args [i ] in ["-m" , "--model" ] and i + 1 < len (args ):
62
+ try :
63
+ model = validate_model (None , None , args [i + 1 ])
64
+ except click .BadParameter as e :
65
+ click .echo (f"Error: { str (e )} " , err = True )
66
+ ctx .exit (1 )
67
+ i += 2
68
+ elif args [i ] in ["-v" , "--voice" ] and i + 1 < len (args ):
69
+ try :
70
+ voice = validate_voice (None , None , args [i + 1 ])
71
+ except click .BadParameter as e :
72
+ click .echo (f"Error: { str (e )} " , err = True )
73
+ ctx .exit (1 )
74
+ i += 2
75
+ else :
76
+ click .echo (f"Unexpected argument: { args [i ]} " , err = True )
71
77
ctx .exit (1 )
72
- i += 2
73
- else :
74
- text .append (args [i ])
75
- i += 1
76
-
77
- text = " " .join (text )
78
78
79
- try :
80
- if output :
81
- output = validate_output_file (output )
82
- convert_text_to_speech_direct (text , Path (output ), model , voice )
83
- click .echo (f"Successfully converted text to { output } " )
84
- else :
85
- click .echo ("Converting text to speech..." )
86
- audio_data = convert_text_to_speech_stream (text , model , voice )
87
- with tempfile .NamedTemporaryFile (
88
- suffix = ".mp3" , delete = False
89
- ) as temp_file :
90
- temp_file .write (audio_data .getvalue ())
91
- temp_file_path = temp_file .name
92
-
93
- play_audio (Path (temp_file_path ))
94
- Path (temp_file_path ).unlink () # Delete the temporary file after playing
95
- except PlaybackError as e :
96
- logging .error (f"Playback error: { str (e )} " )
97
- click .echo (f"Error during audio playback: { str (e )} " , err = True )
98
- click .echo (
99
- click .style (
100
- "No audio output device was found. Please check your sound settings." ,
101
- fg = "red" ,
102
- bold = True ,
79
+ try :
80
+ input_file = validate_input_file (input_file )
81
+ output = validate_output_file (output or "speech.mp3" )
82
+ convert_text_to_speech (input_file , output , model , voice )
83
+ click .echo (f"Successfully converted { input_file } to { output } " )
84
+ except Exception as e :
85
+ logging .error (f"Error during conversion: { str (e )} " )
86
+ click .echo (f"Error: { str (e )} " , err = True )
87
+ else :
88
+ # Direct text-to-speech conversion
89
+ output = None
90
+ default_model , default_voice = get_config ()
91
+ model = default_model
92
+ voice = default_voice
93
+ text = []
94
+
95
+ i = 0
96
+ while i < len (args ):
97
+ if args [i ] in ["-o" , "--output" ] and i + 1 < len (args ):
98
+ output = args [i + 1 ]
99
+ i += 2
100
+ elif args [i ] in ["-m" , "--model" ] and i + 1 < len (args ):
101
+ try :
102
+ model = validate_model (None , None , args [i + 1 ])
103
+ except click .BadParameter as e :
104
+ click .echo (f"Error: { str (e )} " , err = True )
105
+ ctx .exit (1 )
106
+ i += 2
107
+ elif args [i ] in ["-v" , "--voice" ] and i + 1 < len (args ):
108
+ try :
109
+ voice = validate_voice (None , None , args [i + 1 ])
110
+ except click .BadParameter as e :
111
+ click .echo (f"Error: { str (e )} " , err = True )
112
+ ctx .exit (1 )
113
+ i += 2
114
+ else :
115
+ text .append (args [i ])
116
+ i += 1
117
+
118
+ text = " " .join (text )
119
+
120
+ try :
121
+ if output :
122
+ output = validate_output_file (output )
123
+ convert_text_to_speech_direct (text , Path (output ), model , voice )
124
+ click .echo (f"Successfully converted text to { output } " )
125
+ else :
126
+ click .echo ("Converting text to speech..." )
127
+ audio_data = convert_text_to_speech_stream (text , model , voice )
128
+ with tempfile .NamedTemporaryFile (
129
+ suffix = ".mp3" , delete = False
130
+ ) as temp_file :
131
+ temp_file .write (audio_data .getvalue ())
132
+ temp_file_path = temp_file .name
133
+
134
+ play_audio (Path (temp_file_path ))
135
+ Path (
136
+ temp_file_path
137
+ ).unlink () # Delete the temporary file after playing
138
+ except PlaybackError as e :
139
+ logging .error (f"Playback error: { str (e )} " )
140
+ click .echo (f"Error during audio playback: { str (e )} " , err = True )
141
+ click .echo (
142
+ click .style (
143
+ "No audio output device was found. Please check your sound settings." ,
144
+ fg = "red" ,
145
+ bold = True ,
146
+ )
103
147
)
104
- )
105
- except Exception as e :
106
- logging .error (f"Error during conversion or playback: { str (e )} " )
107
- click .echo (f"Error: { str (e )} " , err = True )
148
+ except Exception as e :
149
+ logging .error (f"Error during conversion or playback: { str (e )} " )
150
+ click .echo (f"Error: { str (e )} " , err = True )
108
151
109
152
110
153
@click .group ()
0 commit comments