-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
288 lines (239 loc) · 10.5 KB
/
script.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// 默认配置(加密存储)
const defaultConfig = {
key: btoa('276c99e4645a4616829848e9dc75d05a'),
region: btoa('eastasia')
};
// 创建星号字符串
function createAsterisks(length) {
return '*'.repeat(length);
}
// 音频历史记录
let audioHistory = [];
// 创建历史记录项
function createHistoryItem(text, audioData, timestamp, index) {
const container = document.createElement('div');
container.className = 'history-item';
const header = document.createElement('div');
header.className = 'history-header';
// 创建左侧信息容器
const infoContainer = document.createElement('div');
infoContainer.className = 'history-info';
// 标题和时间放在左侧
const title = document.createElement('div');
title.className = 'history-title';
// 限制标题长度为8个字符
const shortText = text.slice(0, 8) + (text.length > 8 ? '...' : '');
title.innerHTML = `<span class="history-index">${index}. </span>${shortText}`;
title.title = text; // 添加完整文本作为提示
const time = document.createElement('div');
time.className = 'history-time';
// 只显示时间(时:分)
const timeStr = new Date(timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
time.textContent = timeStr;
infoContainer.appendChild(title);
infoContainer.appendChild(time);
// 创建右侧按钮容器
const buttonsContainer = document.createElement('div');
buttonsContainer.className = 'history-buttons';
const deleteBtn = document.createElement('button');
deleteBtn.className = 'history-delete';
deleteBtn.innerHTML = '<svg viewBox="0 0 24 24" width="14" height="14"><path fill="currentColor" d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>';
deleteBtn.title = '删除';
deleteBtn.onclick = () => {
const index = audioHistory.findIndex(item => item.timestamp === timestamp);
if (index !== -1) {
audioHistory.splice(index, 1);
container.remove();
URL.revokeObjectURL(audio.src);
updateHistoryDisplay();
}
};
buttonsContainer.appendChild(deleteBtn);
header.appendChild(infoContainer);
header.appendChild(buttonsContainer);
// 音频播放器
const audio = document.createElement('audio');
audio.controls = true;
audio.preload = 'metadata';
const blob = new Blob([audioData], { type: 'audio/wav' });
audio.src = URL.createObjectURL(blob);
container.appendChild(header);
container.appendChild(audio);
return container;
}
// 更新历史记录显示
function updateHistoryDisplay() {
const historyContainer = document.getElementById('audioHistory');
if (!historyContainer) return;
historyContainer.innerHTML = '';
if (audioHistory.length === 0) {
const emptyMsg = document.createElement('div');
emptyMsg.className = 'history-empty';
emptyMsg.textContent = '暂无历史记录';
historyContainer.appendChild(emptyMsg);
return;
}
// 按时间倒序排序并添加序号
audioHistory.sort((a, b) => b.timestamp - a.timestamp)
.forEach((item, index) => {
const historyItem = createHistoryItem(item.text, item.audioData, item.timestamp, audioHistory.length - index);
historyContainer.appendChild(historyItem);
});
}
document.addEventListener('DOMContentLoaded', () => {
// 历史记录面板折叠功能
const historyPanel = document.getElementById('historyPanel');
const historyToggle = document.querySelector('.history-toggle');
// 根据屏幕宽度设置初始状态
const setInitialState = () => {
if (window.innerWidth <= 1200) {
historyPanel.classList.add('collapsed');
} else {
historyPanel.classList.remove('collapsed');
}
};
setInitialState();
window.addEventListener('resize', setInitialState);
historyToggle.addEventListener('click', () => {
historyPanel.classList.toggle('collapsed');
});
// 设置默认值(密钥显示为星号)
const apiKeyInput = document.getElementById('apiKey');
const regionInput = document.getElementById('region');
// 将密钥显示为星号,地区正常显示
apiKeyInput.value = createAsterisks(32);
regionInput.value = atob(defaultConfig.region);
// 存储实际的API密钥
apiKeyInput.dataset.apiKey = defaultConfig.key;
// 获取其他 DOM 元素
const synthesizeButton = document.getElementById('synthesizeButton');
const downloadButton = document.getElementById('downloadButton');
const audioPlayer = document.getElementById('audioPlayer');
const progressIndicator = document.getElementById('progressIndicator');
const textInput = document.getElementById('text');
const voiceSelect = document.getElementById('voiceName');
let audioData = null;
// 切换密码可见性
const togglePassword = document.querySelector('.toggle-password');
const eyeIcon = document.querySelector('.eye-icon');
const eyeOffIcon = document.querySelector('.eye-off-icon');
togglePassword.addEventListener('click', function() {
const type = apiKeyInput.type === 'password' ? 'text' : 'password';
apiKeyInput.type = type;
eyeIcon.style.display = type === 'password' ? 'block' : 'none';
eyeOffIcon.style.display = type === 'password' ? 'none' : 'block';
});
// 处理API密钥输入
apiKeyInput.addEventListener('input', function(e) {
// 直接更新API密钥
if (this.value.length > 0) {
this.dataset.apiKey = btoa(this.value);
} else {
this.dataset.apiKey = '';
}
});
synthesizeButton.addEventListener('click', async () => {
try {
const apiKey = atob(apiKeyInput.dataset.apiKey);
const region = regionInput.value.trim() || atob(defaultConfig.region);
const text = textInput.value.trim();
const voice = voiceSelect.value;
if (!text) {
alert('请输入要转换的文本内容');
return;
}
synthesizeButton.disabled = true;
downloadButton.style.display = 'none';
audioPlayer.style.display = 'none';
progressIndicator.style.display = 'block';
const speechConfig = SpeechSDK.SpeechConfig.fromSubscription(apiKey, region);
speechConfig.speechSynthesisVoiceName = voice;
// 创建一个自定义的音频输出流
const audioConfig = SpeechSDK.AudioConfig.fromDefaultSpeakerOutput();
const synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
// 禁用默认的音频输出
if (window.audioContext) {
window.audioContext.suspend();
}
window.audioContext = new (window.AudioContext || window.webkitAudioContext)();
window.audioContext.suspend();
const result = await new Promise((resolve, reject) => {
synthesizer.speakTextAsync(
text,
result => {
synthesizer.close();
resolve(result);
},
error => {
synthesizer.close();
reject(error);
}
);
});
// 处理音频数据
audioData = result.audioData;
// 添加到历史记录
audioHistory.push({
text,
audioData: new Uint8Array(audioData),
timestamp: Date.now(),
voice
});
updateHistoryDisplay();
// 更新当前音频播放器
const blob = new Blob([audioData], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
// 设置音频播放器
audioPlayer.src = url;
audioPlayer.style.display = 'block';
downloadButton.style.display = 'block';
// 确保不自动播放
audioPlayer.autoplay = false;
audioPlayer.load(); // 强制重新加载
// 加载完成后的处理
audioPlayer.addEventListener('loadedmetadata', () => {
audioPlayer.pause();
audioPlayer.currentTime = 0;
}, { once: true });
// 播放时的处理
audioPlayer.addEventListener('play', () => {
const updateProgress = () => {
if (!audioPlayer.paused) {
const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100;
const progressBar = document.querySelector('.progress-bar-value');
if (progressBar) {
progressBar.style.transform = `translateX(${progress - 100}%)`;
}
requestAnimationFrame(updateProgress);
}
};
updateProgress();
});
// 清理
audioPlayer.onended = () => {
URL.revokeObjectURL(url);
};
} catch (error) {
console.error('语音合成失败:', error);
alert('语音合成失败: ' + error.message);
} finally {
synthesizeButton.disabled = false;
progressIndicator.style.display = 'none';
}
});
downloadButton.addEventListener('click', () => {
if (!audioData) return;
const blob = new Blob([audioData], { type: 'audio/wav' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = (textInput.value.slice(0, 10) || '语音合成') + '.wav';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// 初始化历史记录显示
updateHistoryDisplay();
});