Skip to content

Commit

Permalink
Updated chatgpt.speak(), added synonyms
Browse files Browse the repository at this point in the history
+ Added instructional comments
+ Added arg validation
± Changed default arg values
- Renamed `voiceIndex` to `voice`
- Removed out-of-bounds check on `voiceIndex` (since all number values work)
- Removed extraneous space in `console.error` ↞ [auto-sync from `kudoai/chatgpt.js`]
  • Loading branch information
adamlui authored and kudo-sync-bot committed Aug 12, 2023
1 parent 7a2e5b1 commit f3f33b9
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions extension/lib/chatgpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,23 +1025,30 @@ const chatgpt = {
}}},

speak: function(msg, options = {}) {
const { voiceIndex = 0, pitch = 1, speed = 1 } = options;
try {
const voices = speechSynthesis.getVoices();
if (voiceIndex >= 0 && voiceIndex < voices.length) {
const utterance = new SpeechSynthesisUtterance();
utterance.text = msg;
utterance.voice = voices[voiceIndex];
utterance.pitch = pitch;
utterance.rate = speed;
speechSynthesis.speak(utterance);
}
else {
console.error('🤖 chatgpt.js >> Invalid voice index');
}
} catch (error) {
console.error('🤖 chatgpt.js >> ', error);
// Usage example: chatgpt.speak(await chatgpt.getLastResponse(), { voice: 1, pitch: 2, speed: 3 })
// options.voice = index of voices available on user device
// options.pitch = float for pitch of speech from 0 to 2
// options.speed = float for rate of speech from 0.1 to 10

const { voice = 2, pitch = 2, speed = 1.1 } = options;

// Validate args
for (let key in options) {
const value = options[key];
if (typeof value !== 'number' && !/^\d+$/.test(value))
return console.error(
`🤖 chatgpt.js >> Invalid ${ key } index '${ value }'. Must be a number`);
}

try { // to speak msg using {options}
const voices = speechSynthesis.getVoices(),
utterance = new SpeechSynthesisUtterance();
utterance.text = msg;
utterance.voice = voices[voice];
utterance.pitch = pitch;
utterance.rate = speed;
speechSynthesis.speak(utterance);
} catch (error) { console.error('🤖 chatgpt.js >>', error); }
},

toggleScheme: function() {
Expand Down Expand Up @@ -1103,7 +1110,8 @@ const functionAliases = [ // whole function names to cross-alias
];
const synonyms = [ // constituent synonyms within function names
['activate', 'turnOn'], ['account', 'acct'], ['ask', 'send', 'submit'], ['chat', 'conversation', 'convo'], ['data', 'details'],
['deactivate', 'deActivate', 'turnOff'], ['generating', 'generation'], ['render', 'parse'], ['reply', 'response']
['deactivate', 'deActivate', 'turnOff'], ['generating', 'generation'], ['render', 'parse'], ['reply', 'response'],
['speak', 'say', 'speech', 'talk', 'tts']
];
for (const prop in chatgpt) {

Expand Down

0 comments on commit f3f33b9

Please sign in to comment.