|
1 | | -import * as vscode from "vscode" |
| 1 | +interface Say { |
| 2 | + speak: (text: string, voice?: string, speed?: number, callback?: (err?: string) => void) => void |
| 3 | + stop: () => void |
| 4 | +} |
| 5 | + |
| 6 | +type PlayTtsOptions = { |
| 7 | + onStart?: () => void |
| 8 | + onStop?: () => void |
| 9 | +} |
| 10 | + |
| 11 | +type QueueItem = { |
| 12 | + message: string |
| 13 | + options: PlayTtsOptions |
| 14 | +} |
2 | 15 |
|
3 | 16 | let isTtsEnabled = false |
| 17 | + |
| 18 | +export const setTtsEnabled = (enabled: boolean) => (isTtsEnabled = enabled) |
| 19 | + |
4 | 20 | let speed = 1.0 |
5 | | -let isSpeaking = false |
6 | | -const utteranceQueue: string[] = [] |
7 | | - |
8 | | -/** |
9 | | - * Set tts configuration |
10 | | - * @param enabled boolean |
11 | | - */ |
12 | | -export const setTtsEnabled = (enabled: boolean): void => { |
13 | | - isTtsEnabled = enabled |
| 21 | + |
| 22 | +export const setTtsSpeed = (newSpeed: number) => (speed = newSpeed) |
| 23 | + |
| 24 | +let sayInstance: Say | undefined = undefined |
| 25 | +let queue: QueueItem[] = [] |
| 26 | + |
| 27 | +export const playTts = async (message: string, options: PlayTtsOptions = {}) => { |
| 28 | + if (!isTtsEnabled) { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + queue.push({ message, options }) |
| 34 | + await processQueue() |
| 35 | + } catch (error) {} |
14 | 36 | } |
15 | 37 |
|
16 | | -/** |
17 | | - * Set tts speed |
18 | | - * @param speed number |
19 | | - */ |
20 | | -export const setTtsSpeed = (newSpeed: number): void => { |
21 | | - speed = newSpeed |
| 38 | +export const stopTts = () => { |
| 39 | + sayInstance?.stop() |
| 40 | + sayInstance = undefined |
| 41 | + queue = [] |
22 | 42 | } |
23 | 43 |
|
24 | | -/** |
25 | | - * Process the next item in the utterance queue |
26 | | - */ |
27 | 44 | const processQueue = async (): Promise<void> => { |
28 | | - if (!isTtsEnabled || isSpeaking || utteranceQueue.length === 0) { |
| 45 | + if (!isTtsEnabled || sayInstance) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + const item = queue.shift() |
| 50 | + |
| 51 | + if (!item) { |
29 | 52 | return |
30 | 53 | } |
31 | 54 |
|
32 | 55 | try { |
33 | | - isSpeaking = true |
34 | | - const nextUtterance = utteranceQueue.shift()! |
35 | | - const say = require("say") |
| 56 | + const { message: nextUtterance, options } = item |
36 | 57 |
|
37 | | - // Wrap say.speak in a promise to handle completion |
38 | 58 | await new Promise<void>((resolve, reject) => { |
39 | | - say.speak(nextUtterance, null, speed, (err: Error) => { |
| 59 | + const say: Say = require("say") |
| 60 | + sayInstance = say |
| 61 | + options.onStart?.() |
| 62 | + |
| 63 | + say.speak(nextUtterance, undefined, speed, (err) => { |
| 64 | + options.onStop?.() |
| 65 | + |
40 | 66 | if (err) { |
41 | | - reject(err) |
| 67 | + reject(new Error(err)) |
42 | 68 | } else { |
43 | 69 | resolve() |
44 | 70 | } |
| 71 | + |
| 72 | + sayInstance = undefined |
45 | 73 | }) |
46 | 74 | }) |
47 | 75 |
|
48 | | - isSpeaking = false |
49 | | - // Process next item in queue if any |
50 | 76 | await processQueue() |
51 | 77 | } catch (error: any) { |
52 | | - isSpeaking = false |
53 | | - //vscode.window.showErrorMessage(error.message) |
54 | | - // Try to continue with next item despite error |
55 | | - await processQueue() |
56 | | - } |
57 | | -} |
58 | | - |
59 | | -/** |
60 | | - * Queue a tts message to be spoken |
61 | | - * @param message string |
62 | | - * @return void |
63 | | - */ |
64 | | -export const playTts = async (message: string): Promise<void> => { |
65 | | - if (!isTtsEnabled) { |
66 | | - return |
67 | | - } |
68 | | - |
69 | | - try { |
70 | | - utteranceQueue.push(message) |
| 78 | + sayInstance = undefined |
71 | 79 | await processQueue() |
72 | | - } catch (error: any) { |
73 | | - //vscode.window.showErrorMessage(error.message) |
74 | 80 | } |
75 | 81 | } |
0 commit comments