办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { mlog } from "./mjapi";
 
export interface recType {
    timeOut: number;
    asrLanguage?: string;
    listener?: (result: string) => void;
    onEnd?: () => void;
    onStart?: () => void;
}
export class Recognition {
    private recognition: any;
    private listener?: (result: string) => void;
    private isStop = false;
    private recOpt: recType = { timeOut: 2000 };
    private handleTime: any;
    private hTime: Date | undefined;
    private asrLanguage = "cmn-Hans-CN";
    private onEnd?: () => void;
    private onStart?: () => void;
 
    public setListener(fn: (result: string) => void) {
        this.listener = fn;
        return this;
    }
 
    public setOnEnd(fn: () => void) {
        this.onEnd = fn;
        return this;
    }
 
    public setOpt(opt: recType) {
        this.recOpt = opt;
        if (opt.listener) this.setListener(opt.listener);
        if (opt.onEnd) this.setOnEnd(opt.onEnd);
        if (opt.asrLanguage) this.setLang(opt.asrLanguage);
        if (opt.onStart) this.onStart = opt.onStart;
        return this;
    }
 
    public setLang(lang: string) {
        this.asrLanguage = lang;
        return this;
    }
 
    public start() {
        this.isStop = false;
        if (
            typeof window === "undefined" ||
            (!window.SpeechRecognition && !window.webkitSpeechRecognition)
        ) {
            console.warn("当前浏览器不支持 SpeechRecognition,请使用 Chrome 或 Edge");
            return;
        }
 
        if (!this.recognition) {
            const recognition = new (window.SpeechRecognition ||
                window.webkitSpeechRecognition)();
            this.recognition = recognition;
        }
        const recognition = this.recognition;
 
        recognition.interimResults = true;
        recognition.lang = this.asrLanguage;
        recognition.continuous = true;
 
        this.hTime = new Date();
        this.handleTime = setInterval(() => this.check(this), this.recOpt.timeOut);
 
        recognition.addEventListener("result", (event: any) => {
            let transcript = "";
            for (let index = 0; index < event.results.length; index++) {
                const item = event.results[index];
                if (transcript && this.asrLanguage.includes("Han")) transcript += ",";
                transcript += (item as unknown as SpeechRecognitionAlternative[])[0]
                    ?.transcript;
            }
            if (!transcript) return;
            this.hTime = new Date();
            this.listener?.(transcript);
        });
 
        recognition.addEventListener("end", () => {
            if (this.isStop) {
                clearInterval(this.handleTime);
                return;
            }
            setTimeout(() => recognition.start(), 1000);
        });
 
        recognition.start();
        this.onStart?.();
        return this;
    }
 
    private check(that: Recognition) {
        if (!that.hTime) return;
        const dt = new Date().getTime() - that.hTime.getTime();
        if (dt > that.recOpt.timeOut) {
            that.stop(); // 只在 stop 中触发 onEnd
        }
    }
 
    public stop() {
        if (this.isStop) return this; // 如果已经停止,直接返回
        this.isStop = true;
        this.recognition?.stop();
        clearInterval(this.handleTime);
        this.onEnd?.(); // 只在这里触发一次 onEnd
        return this;
    }
 
    // public stop() {
    //   this.isStop = true;
    //   this.recognition?.stop();
    //   return this;
    // }
 
    // private check(that: Recognition) {
    //   if (!that.hTime) return;
    //   const dt = new Date().getTime() - that.hTime.getTime();
    //   if (dt > that.recOpt.timeOut) that.stop();
    // }
}
 
export const supportLanguages: Record<string, string> = {
    "cmn-Hans-CN": "普通话 (中国大陆)",
    "cmn-Hans-HK": "普通话 (香港)",
    "yue-Hant-HK": "粵語 (香港)",
    "en-US": "English(United States)",
    "en-GB": "English(United Kingdom)",
    "en-IN": "English(India)",
    "es-ES": "Español",
    "fr-FR": "Français",
    "de-DE": "Deutsch",
    "it-IT": "Italiano",
    "ja-JP": "日本語",
    "ko-KR": "한국어",
    "ar-SA": "العربية",
    "pt-BR": "Português",
    "ru-RU": "Русский",
    "nl-NL": "Nederlands",
    "tr-TR": "Türkçe",
    "sv-SE": "Svenska",
    "hi-IN": "हिन्दी",
    "el-GR": "Ελληνικά",
    "he-IL": "עברית",
    "id-ID": "Bahasa Indonesia",
    "pl-PL": "Polski",
    "th-TH": "ไทย",
    "cs-CZ": "Čeština",
    "hu-HU": "Magyar",
    "da-DK": "Dansk",
    "fi-FI": "Suomi",
    "no-NO": "Norsk",
    "sk-SK": "Slovenčina",
    "uk-UA": "Українська",
    "vi-VN": "Tiếng Việt",
};
 
function sleep(time: number) {
    return new Promise((resolve) => setTimeout(resolve, time));
}
 
//浏览器文字播放
export async function speakText(
    content: string,
    callback: (playing: boolean) => void
) {
    if (!window.speechSynthesis) return;
    if (speechSynthesis.speaking) {
        speechSynthesis.cancel();
        callback(false);
    }
 
    await sleep(300);
 
    const msg = new SpeechSynthesisUtterance(content);
    msg.lang = "zh";
    msg.rate = 1;
    msg.addEventListener("end", () => {
        callback(false);
    });
    msg.addEventListener("error", () => {
        callback(false);
    });
    callback(true);
    speechSynthesis.speak(msg);
}