Skip to content

Commit f06702b

Browse files
authored
🤖 Merge PR DefinitelyTyped#72243 [Chrome] update tts namespaces by @erwanjugand
1 parent 1c06248 commit f06702b

File tree

2 files changed

+159
-114
lines changed

2 files changed

+159
-114
lines changed

types/chrome/index.d.ts

Lines changed: 106 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -12367,133 +12367,143 @@ declare namespace chrome {
1236712367
// Text to Speech
1236812368
////////////////////
1236912369
/**
12370-
* Use the `chrome.tts` API to play synthesized text-to-speech (TTS). See also the related ttsEngine API, which allows an extension to implement a speech engine.
12370+
* Use the `chrome.tts` API to play synthesized text-to-speech (TTS). See also the related {@link ttsEngine} API, which allows an extension to implement a speech engine.
1237112371
*
1237212372
* Permissions: "tts"
1237312373
*/
1237412374
export namespace tts {
12375+
/** @since Chrome 54 */
12376+
export enum EventType {
12377+
START = "start",
12378+
END = "end",
12379+
WORD = "word",
12380+
SENTENCE = "sentence",
12381+
MARKER = "marker",
12382+
INTERRUPTED = "interrupted",
12383+
CANCELLED = "cancelled",
12384+
ERROR = "error",
12385+
PAUSE = "pause",
12386+
RESUME = "resume",
12387+
}
12388+
1237512389
/** An event from the TTS engine to communicate the status of an utterance. */
1237612390
export interface TtsEvent {
12377-
/** Optional. The index of the current character in the utterance. */
12378-
charIndex?: number | undefined;
12379-
/** Optional. The error description, if the event type is 'error'. */
12380-
errorMessage?: string | undefined;
12391+
/** The index of the current character in the utterance. For word events, the event fires at the end of one word and before the beginning of the next. The `charIndex` represents a point in the text at the beginning of the next word to be spoken. */
12392+
charIndex?: number;
12393+
/** The error description, if the event type is `error`. */
12394+
errorMessage?: string;
12395+
/**
12396+
* The length of the next part of the utterance. For example, in a `word` event, this is the length of the word which will be spoken next. It will be set to -1 if not set by the speech engine.
12397+
* @since Chrome 74
12398+
*/
12399+
length?: number;
12400+
/** The type can be `start` as soon as speech has started, `word` when a word boundary is reached, `sentence` when a sentence boundary is reached, `marker` when an SSML mark element is reached, `end` when the end of the utterance is reached, `interrupted` when the utterance is stopped or interrupted before reaching the end, `cancelled` when it's removed from the queue before ever being synthesized, or `error` when any other error occurs. When pausing speech, a `pause` event is fired if a particular utterance is paused in the middle, and `resume` if an utterance resumes speech. Note that pause and resume events may not fire if speech is paused in-between utterances. */
12401+
type: `${EventType}`;
12402+
}
12403+
12404+
/**
12405+
* The speech options for the TTS engine.
12406+
* @since Chrome 77
12407+
*/
12408+
export interface TtsOptions {
12409+
/** The TTS event types that you are interested in listening to. If missing, all event types may be sent. */
12410+
desiredEventTypes?: string[];
12411+
/** If true, enqueues this utterance if TTS is already in progress. If false (the default), interrupts any current speech and flushes the speech queue before speaking this new utterance. */
12412+
enqueue?: boolean;
12413+
/** The extension ID of the speech engine to use, if known. */
12414+
extensionId?: string;
1238112415
/**
12382-
* The length of the next part of the utterance.
12383-
* For example, in a word event, this is the length of the word which will be spoken next.
12384-
* It will be set to -1 if not set by the speech engine.
12416+
* Gender of voice for synthesized speech.
12417+
* @deprecated since Chrome 77. Gender is deprecated and will be ignored.
1238512418
*/
12386-
length?: number | undefined;
12419+
gender?: `${VoiceGender}`;
12420+
/** The language to be used for synthesis, in the form _language_\-_region_. Examples: 'en', 'en-US', 'en-GB', 'zh-CN'. */
12421+
lang?: string;
12422+
/** Speaking pitch between 0 and 2 inclusive, with 0 being lowest and 2 being highest. 1.0 corresponds to a voice's default pitch. */
12423+
pitch?: number;
12424+
/** Speaking rate relative to the default rate for this voice. 1.0 is the default rate, normally around 180 to 220 words per minute. 2.0 is twice as fast, and 0.5 is half as fast. Values below 0.1 or above 10.0 are strictly disallowed, but many voices will constrain the minimum and maximum rates further—for example a particular voice may not actually speak faster than 3 times normal even if you specify a value larger than 3.0. */
12425+
rate?: number;
12426+
/** The TTS event types the voice must support. */
12427+
requiredEventTypes?: string[];
12428+
/** The name of the voice to use for synthesis. If empty, uses any available voice. */
12429+
voiceName?: string;
12430+
/** Speaking volume between 0 and 1 inclusive, with 0 being lowest and 1 being highest, with a default of 1.0. */
12431+
volume?: number;
1238712432
/**
12388-
* The type can be 'start' as soon as speech has started, 'word' when a word boundary is reached, 'sentence' when a sentence boundary is reached, 'marker' when an SSML mark element is reached, 'end' when the end of the utterance is reached, 'interrupted' when the utterance is stopped or interrupted before reaching the end, 'cancelled' when it's removed from the queue before ever being synthesized, or 'error' when any other error occurs. When pausing speech, a 'pause' event is fired if a particular utterance is paused in the middle, and 'resume' if an utterance resumes speech. Note that pause and resume events may not fire if speech is paused in-between utterances.
12389-
* One of: "start", "end", "word", "sentence", "marker", "interrupted", "cancelled", "error", "pause", or "resume"
12433+
* This function is called with events that occur in the process of speaking the utterance.
12434+
* @param event The update event from the text-to-speech engine indicating the status of this utterance.
1239012435
*/
12391-
type:
12392-
| "start"
12393-
| "end"
12394-
| "word"
12395-
| "sentence"
12396-
| "marker"
12397-
| "interrupted"
12398-
| "cancelled"
12399-
| "error"
12400-
| "pause"
12401-
| "resume";
12436+
onEvent?: (
12437+
event: TtsEvent,
12438+
) => void;
1240212439
}
1240312440

1240412441
/** A description of a voice available for speech synthesis. */
1240512442
export interface TtsVoice {
12406-
/** Optional. The language that this voice supports, in the form language-region. Examples: 'en', 'en-US', 'en-GB', 'zh-CN'. */
12407-
lang?: string | undefined;
12443+
/** All of the callback event types that this voice is capable of sending. */
12444+
eventTypes?: `${EventType}`[];
12445+
/** The ID of the extension providing this voice. */
12446+
extensionId?: string;
1240812447
/**
12409-
* Optional. This voice's gender.
12410-
* One of: "male", or "female"
12448+
* This voice's gender.
1241112449
* @deprecated since Chrome 70. Gender is deprecated and will be ignored.
1241212450
*/
12413-
gender?: string | undefined;
12414-
/** Optional. The name of the voice. */
12415-
voiceName?: string | undefined;
12416-
/** Optional. The ID of the extension providing this voice. */
12417-
extensionId?: string | undefined;
12418-
/** Optional. All of the callback event types that this voice is capable of sending. */
12419-
eventTypes?: string[] | undefined;
12420-
/**
12421-
* Optional. If true, the synthesis engine is a remote network resource. It may be higher latency and may incur bandwidth costs.
12422-
* @since Chrome 33
12423-
*/
12424-
remote?: boolean | undefined;
12451+
gender?: `${VoiceGender}`;
12452+
/** The language that this voice supports, in the form language-region. Examples: 'en', 'en-US', 'en-GB', 'zh-CN'. */
12453+
lang?: string;
12454+
/** If true, the synthesis engine is a remote network resource. It may be higher latency and may incur bandwidth costs. */
12455+
remote?: boolean;
12456+
/** The name of the voice. */
12457+
voiceName?: string;
1242512458
}
1242612459

12427-
export interface SpeakOptions {
12428-
/** Optional. Speaking volume between 0 and 1 inclusive, with 0 being lowest and 1 being highest, with a default of 1.0. */
12429-
volume?: number | undefined;
12430-
/**
12431-
* Optional.
12432-
* If true, enqueues this utterance if TTS is already in progress. If false (the default), interrupts any current speech and flushes the speech queue before speaking this new utterance.
12433-
*/
12434-
enqueue?: boolean | undefined;
12435-
/**
12436-
* Optional.
12437-
* Speaking rate relative to the default rate for this voice. 1.0 is the default rate, normally around 180 to 220 words per minute. 2.0 is twice as fast, and 0.5 is half as fast. Values below 0.1 or above 10.0 are strictly disallowed, but many voices will constrain the minimum and maximum rates further—for example a particular voice may not actually speak faster than 3 times normal even if you specify a value larger than 3.0.
12438-
*/
12439-
rate?: number | undefined;
12440-
/**
12441-
* Optional. This function is called with events that occur in the process of speaking the utterance.
12442-
* @param event The update event from the text-to-speech engine indicating the status of this utterance.
12443-
*/
12444-
onEvent?: ((event: TtsEvent) => void) | undefined;
12445-
/**
12446-
* Optional.
12447-
* Speaking pitch between 0 and 2 inclusive, with 0 being lowest and 2 being highest. 1.0 corresponds to a voice's default pitch.
12448-
*/
12449-
pitch?: number | undefined;
12450-
/** Optional. The language to be used for synthesis, in the form language-region. Examples: 'en', 'en-US', 'en-GB', 'zh-CN'. */
12451-
lang?: string | undefined;
12452-
/** Optional. The name of the voice to use for synthesis. If empty, uses any available voice. */
12453-
voiceName?: string | undefined;
12454-
/** Optional. The extension ID of the speech engine to use, if known. */
12455-
extensionId?: string | undefined;
12456-
/**
12457-
* Optional. Gender of voice for synthesized speech.
12458-
* One of: "male", or "female"
12459-
*/
12460-
gender?: string | undefined;
12461-
/** Optional. The TTS event types the voice must support. */
12462-
requiredEventTypes?: string[] | undefined;
12463-
/** Optional. The TTS event types that you are interested in listening to. If missing, all event types may be sent. */
12464-
desiredEventTypes?: string[] | undefined;
12460+
/** @deprecated since Chrome 70. Gender is deprecated and is ignored.*/
12461+
export enum VoiceGender {
12462+
FEMALE = "female",
12463+
MALE = "male",
1246512464
}
1246612465

12467-
/** Checks whether the engine is currently speaking. On Mac OS X, the result is true whenever the system speech engine is speaking, even if the speech wasn't initiated by Chrome. */
12468-
export function isSpeaking(callback?: (speaking: boolean) => void): void;
12469-
/** Stops any current speech and flushes the queue of any pending utterances. In addition, if speech was paused, it will now be un-paused for the next call to speak. */
12470-
export function stop(): void;
12471-
/** Gets an array of all available voices. */
12466+
/**
12467+
* Gets an array of all available voices.
12468+
*
12469+
* Can return its result via Promise since Chrome Chrome 101
12470+
*/
1247212471
export function getVoices(): Promise<TtsVoice[]>;
12473-
export function getVoices(callback?: (voices: TtsVoice[]) => void): void;
12472+
export function getVoices(callback: (voices: TtsVoice[]) => void): void;
12473+
1247412474
/**
12475-
* Speaks text using a text-to-speech engine.
12476-
* @param utterance The text to speak, either plain text or a complete, well-formed SSML document. Speech engines that do not support SSML will strip away the tags and speak the text. The maximum length of the text is 32,768 characters.
12477-
* @param callback Optional. Called right away, before speech finishes. Check chrome.runtime.lastError to make sure there were no errors. Use options.onEvent to get more detailed feedback.
12475+
* Checks whether the engine is currently speaking. On Mac OS X, the result is true whenever the system speech engine is speaking, even if the speech wasn't initiated by Chrome.
12476+
*
12477+
* Can return its result via Promise since Chrome Chrome 101
1247812478
*/
12479-
export function speak(utterance: string, callback?: Function): void;
12479+
export function isSpeaking(): Promise<boolean>;
12480+
export function isSpeaking(callback: (speaking: boolean) => void): void;
12481+
12482+
/** Pauses speech synthesis, potentially in the middle of an utterance. A call to resume or stop will un-pause speech. */
12483+
export function pause(): void;
12484+
12485+
/** If speech was paused, resumes speaking where it left off. */
12486+
export function resume(): void;
12487+
1248012488
/**
1248112489
* Speaks text using a text-to-speech engine.
1248212490
* @param utterance The text to speak, either plain text or a complete, well-formed SSML document. Speech engines that do not support SSML will strip away the tags and speak the text. The maximum length of the text is 32,768 characters.
1248312491
* @param options Optional. The speech options.
12484-
* @param callback Optional. Called right away, before speech finishes. Check chrome.runtime.lastError to make sure there were no errors. Use options.onEvent to get more detailed feedback.
12485-
*/
12486-
export function speak(utterance: string, options: SpeakOptions, callback?: Function): void;
12487-
/**
12488-
* Pauses speech synthesis, potentially in the middle of an utterance. A call to resume or stop will un-pause speech.
12489-
* @since Chrome 29
12492+
12493+
* Can return its result via Promise since Chrome Chrome 101
1249012494
*/
12491-
export function pause(): void;
12495+
export function speak(utterance: string, options?: TtsOptions): Promise<void>;
12496+
export function speak(utterance: string, callback: () => void): void;
12497+
export function speak(utterance: string, options: TtsOptions, callback: () => void): void;
12498+
12499+
/** Stops any current speech and flushes the queue of any pending utterances. In addition, if speech was paused, it will now be un-paused for the next call to speak. */
12500+
export function stop(): void;
12501+
1249212502
/**
12493-
* If speech was paused, resumes speaking where it left off.
12494-
* @since Chrome 29
12503+
* Called when the list of {@link TtsVoice} that would be returned by getVoices has changed.
12504+
* @since Chrome 124
1249512505
*/
12496-
export function resume(): void;
12506+
const onVoicesChanged: chrome.events.Event<() => void>;
1249712507
}
1249812508

1249912509
////////////////////

types/chrome/test/index.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,26 +1114,61 @@ function testStorage() {
11141114
chrome.storage.sync.getKeys(() => {}).then(() => {});
11151115
}
11161116

1117-
// https://developer.chrome.com/apps/tts#type-TtsVoice
1118-
async function testTtsVoice() {
1119-
chrome.tts.getVoices(voices =>
1120-
voices.forEach(voice => {
1121-
console.log(voice.voiceName);
1122-
console.log("\tlang: " + voice.lang);
1123-
console.log("\tremote: " + voice.remote);
1124-
console.log("\textensionId: " + voice.extensionId);
1125-
console.log("\teventTypes: " + voice.eventTypes);
1126-
})
1127-
);
1117+
// https://developer.chrome.com/docs/extensions/reference/api/tss
1118+
function testTts() {
1119+
chrome.tts.EventType.CANCELLED === "cancelled";
1120+
chrome.tts.EventType.END === "end";
1121+
chrome.tts.EventType.ERROR === "error";
1122+
chrome.tts.EventType.INTERRUPTED === "interrupted";
1123+
chrome.tts.EventType.MARKER === "marker";
1124+
chrome.tts.EventType.PAUSE === "pause";
1125+
chrome.tts.EventType.RESUME === "resume";
1126+
chrome.tts.EventType.SENTENCE === "sentence";
1127+
chrome.tts.EventType.START === "start";
1128+
chrome.tts.EventType.WORD === "word";
1129+
1130+
chrome.tts.VoiceGender.FEMALE === "female";
1131+
chrome.tts.VoiceGender.MALE === "male";
1132+
1133+
chrome.tts.getVoices(); // $ExpectType Promise<TtsVoice[]>
1134+
chrome.tts.getVoices(([voice]) => { // $ExpectType void
1135+
voice.eventTypes; // $ExpectType ("start" | "end" | "word" | "sentence" | "marker" | "interrupted" | "cancelled" | "error" | "pause" | "resume")[] | undefined
1136+
voice.extensionId; // $ExpectType string | undefined
1137+
voice.lang; // $ExpectType string | undefined
1138+
voice.voiceName; // $ExpectType string | undefined
1139+
voice.remote; // $ExpectType boolean | undefined
1140+
});
1141+
// @ts-expect-error
1142+
chrome.tts.getVoices(() => {}).then(() => {});
11281143

1129-
const voices = await chrome.tts.getVoices();
1130-
voices.forEach(voice => {
1131-
console.log(voice.voiceName);
1132-
console.log("\tlang: " + voice.lang);
1133-
console.log("\tremote: " + voice.remote);
1134-
console.log("\textensionId: " + voice.extensionId);
1135-
console.log("\teventTypes: " + voice.eventTypes);
1144+
chrome.tts.isSpeaking(); // $ExpectType Promise<boolean>
1145+
chrome.tts.isSpeaking((speaking) => { // $ExpectType void
1146+
speaking; // $ExpectType boolean
11361147
});
1148+
// @ts-expect-error
1149+
chrome.tts.isSpeaking(() => {}).then(() => {});
1150+
1151+
chrome.tts.pause(); // $ExpectType void
1152+
1153+
chrome.tts.resume(); // $ExpectType void
1154+
1155+
const ttsOptions: chrome.tts.TtsOptions = {
1156+
lang: "en",
1157+
};
1158+
1159+
chrome.tts.speak("Hello, World!"); // $ExpectType Promise<void>
1160+
chrome.tts.speak("Hello, World!", ttsOptions); // $ExpectType Promise<void>
1161+
chrome.tts.speak("Hello, World!", () => {}); // $ExpectType void
1162+
chrome.tts.speak("Hello, World!", ttsOptions, () => {}); // $ExpectType void
1163+
// @ts-expect-error
1164+
chrome.tts.speak("Hello, World!", () => {}).then(() => {});
1165+
1166+
chrome.tts.stop(); // $ExpectType void
1167+
1168+
chrome.tts.onVoicesChanged.addListener(() => {}); // $ExpectType void
1169+
chrome.tts.onVoicesChanged.removeListener(() => {}); // $ExpectType void
1170+
chrome.tts.onVoicesChanged.hasListener(() => {}); // $ExpectType boolean
1171+
chrome.tts.onVoicesChanged.hasListeners(); // $ExpectType boolean
11371172
}
11381173

11391174
// https://developer.chrome.com/docs/extensions/reference/api/ttsEngine

0 commit comments

Comments
 (0)