-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathscript.js
205 lines (181 loc) · 6.38 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
let apiConfig;
let lastRequestTime = 0;
function loadSpeakers() {
return $.ajax({
url: 'speakers.json',
method: 'GET',
dataType: 'json',
success: function(data) {
apiConfig = data;
updateSpeakerOptions('voice-api');
},
error: function(jqXHR, textStatus, errorThrown) {
console.error(`加载讲述者失败:${textStatus} - ${errorThrown}`);
}
});
}
function updateSpeakerOptions(apiName) {
const speakers = apiConfig[apiName].speakers;
const speakerSelect = $('#speaker');
speakerSelect.empty();
Object.entries(speakers).forEach(([key, value]) => {
speakerSelect.append(new Option(value, key));
});
}
function updateSliderLabel(sliderId, labelId) {
const slider = $(`#${sliderId}`);
const label = $(`#${labelId}`);
label.text(slider.val());
slider.on('input', function () {
label.text(this.value);
});
}
$(document).ready(function () {
loadSpeakers().then(() => {
$('[data-toggle="tooltip"]').tooltip();
$('#api').on('change', function () {
updateSpeakerOptions(this.value);
});
updateSliderLabel('rate', 'rateValue');
updateSliderLabel('pitch', 'pitchValue');
$('#text').on('input', function () {
$('#charCount').text(`字符数统计:${this.value.length}/3600`);
});
$('#text2voice-form').on('submit', function (event) {
event.preventDefault();
if (canMakeRequest()) {
generateVoice(false);
} else {
alert('请稍候再试,每5秒只能请求一次。');
}
});
$('#previewButton').on('click', function () {
if (canMakeRequest()) {
generateVoice(true);
} else {
alert('请稍候再试,每5秒只能请求一次。');
}
});
});
});
function canMakeRequest() {
const currentTime = Date.now();
if (currentTime - lastRequestTime >= 5000) {
lastRequestTime = currentTime;
return true;
}
return false;
}
function generateVoice(isPreview) {
const apiName = $('#api').val();
const apiUrl = apiConfig[apiName].url;
const speaker = $('#speaker').val();
const text = $('#text').val();
const previewText = isPreview ? text.substring(0, 20) : text;
let rate = $('#rate').val();
let pitch = $('#pitch').val();
if (apiName === 'voice-api') {
let url = `${apiUrl}?t=${encodeURIComponent(previewText)}&v=${encodeURIComponent(speaker)}`;
url += `&r=${encodeURIComponent(rate)}&p=${encodeURIComponent(pitch)}&o=audio-24khz-48kbitrate-mono-mp3`;
makeRequest(url, isPreview, text);
} else if (apiName === 'lobe-api') {
// 分别将rate和pitch的范围从-100到100转换为-1到1
const rateConverted = (rate / 100).toFixed(2);
const pitchConverted = (pitch / 100).toFixed(2);
const url = `${apiUrl}?model=${encodeURIComponent(speaker)}&input=${encodeURIComponent(previewText)}&voice=${encodeURIComponent(`rate:${rateConverted}|pitch:${pitchConverted}`)}`;
$.ajax({
url: url,
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: (blob) => {
console.log('Blob type:', blob.type); // 调试信息
handleSuccess(blob, isPreview, text);
},
error: (jqXHR, textStatus, errorThrown) => {
console.error(`请求失败:${textStatus} - ${errorThrown}`);
console.error(`响应内容:${jqXHR.responseText}`);
alert(`请求失败:${textStatus} - ${errorThrown}`);
}
});
}
}
function makeRequest(url, isPreview, text) {
$('#loading').show();
$('#result').hide();
$('#generateButton').prop('disabled', true);
$('#previewButton').prop('disabled', true);
$.ajax({
url: url,
method: 'GET',
headers: {
'x-api-key': '@ak47'
},
xhrFields: {
responseType: 'blob'
},
success: (blob) => handleSuccess(blob, isPreview, text),
error: handleError
});
}
function handleSuccess(blob, isPreview, text) {
console.log('Blob type:', blob.type); // 添加调试信息
if (blob.type !== "audio/mpeg") {
console.error('Invalid Blob type:', blob.type);
alert('请求失败:无效的音频格式');
$('#loading').hide();
$('#generateButton').prop('disabled', false);
$('#previewButton').prop('disabled', false);
return;
}
const voiceUrl = URL.createObjectURL(blob);
$('#audio').attr('src', voiceUrl);
$('#audio')[0].load();
if (!isPreview) {
$('#download').attr('href', voiceUrl);
const timestamp = new Date().toLocaleTimeString();
const shortenedText = text.length > 5 ? text.substring(0, 5) + '...' : text;
addHistoryItem(timestamp, shortenedText, voiceUrl);
}
$('#result').show();
$('#loading').hide();
$('#generateButton').prop('disabled', false);
$('#previewButton').prop('disabled', false);
}
function handleError(jqXHR, textStatus, errorThrown) {
console.error(`请求失败:${textStatus} - ${errorThrown}`);
alert(`请求失败:${textStatus} - ${errorThrown}`);
$('#loading').hide();
$('#generateButton').prop('disabled', false);
$('#previewButton').prop('disabled', false);
}
function addHistoryItem(timestamp, text, audioURL) {
const historyItems = $('#historyItems');
const historyItem = $(`
<div class="history-item">
<span>${timestamp} - ${text}</span>
<div>
<button class="btn btn-secondary" onclick="playAudio('${audioURL}')">播放</button>
<button class="btn btn-info" onclick="downloadAudio('${audioURL}')">下载</button>
</div>
</div>
`);
historyItems.append(historyItem);
}
function playAudio(audioURL) {
const audioElement = $('#audio')[0];
audioElement.src = audioURL;
audioElement.load();
audioElement.play();
}
function downloadAudio(audioURL) {
const link = document.createElement('a');
link.href = audioURL;
link.download = 'audio.mp3';
link.click();
}
function clearHistory() {
$('#historyItems').empty();
alert("历史记录已清除!");
}