Skip to content

Commit 37d09a6

Browse files
committed
Only support language and voice (ordered) in puter.js txt2speech
1 parent aa3032d commit 37d09a6

File tree

1 file changed

+17
-17
lines changed
  • src/puter-js/src/modules

1 file changed

+17
-17
lines changed

src/puter-js/src/modules/AI.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,30 +137,30 @@ class AI{
137137
}
138138

139139
// * ai.txt2speech('Hello, world!', 'en-US')
140-
// * ai.txt2speech('Hello, world!', 'Brian')
141140
// * ai.txt2speech('Hello, world!', 'en-US', 'Brian')
142-
// * ai.txt2speech('Hello, world!', 'Brian', 'en-US')
143141
if (args[1] && typeof args[1] === 'string') {
144-
// Check if it's a language code (ISO 639-1 or with region)
145-
// Pattern matches: en, es, fr, de, en-US, es-ES, fr-FR, etc.
146-
const languageCodePattern = /^[a-z]{2}(-[A-Z]{2})?$/;
147142

148-
// * ai.txt2speech('Hello, world!', 'en-US')
149-
if (languageCodePattern.test(args[1])) {
143+
// Determine language
144+
if (args[1] && typeof args[1] === 'string') {
145+
// Check if it's a language code (ISO 639-1 or with region)
146+
// Pattern matches: en, es, fr, de, en-US, es-ES, fr-FR, etc.
147+
const languageCodePattern = /^[a-z]{2}(-[A-Z]{2})?$/;
148+
149+
// if language code is invalid, throw an error
150+
if(!languageCodePattern.test(args[1])){
151+
throw { message: 'Invalid language code', code: 'invalid_language_code' };
152+
}
153+
154+
// set language
150155
options.language = args[1];
151156
}
152-
// * ai.txt2speech('Hello, world!', 'Brian')
153-
else {
154-
options.voice = args[1];
155-
}
156-
// * ai.txt2speech('Hello, world!', 'en-US', 'Brian')
157-
if (args[2] && typeof args[2] === 'string' && options.language) {
157+
// Determine voice
158+
// Note that voice is optional, and if not provided, the default voice for the language will be used
159+
// Also, it is important that a language is set before a voice is set since voices are language-specific
160+
if (options.language && args[2] && typeof args[2] === 'string') {
161+
// set voice
158162
options.voice = args[2];
159163
}
160-
// * ai.txt2speech('Hello, world!', 'Brian', 'en-US')
161-
else if (args[2] && typeof args[2] === 'string' && options.voice) {
162-
options.language = args[2];
163-
}
164164
}
165165

166166
// check input size

0 commit comments

Comments
 (0)