Skip to content

Commit

Permalink
重试功能,更新版本
Browse files Browse the repository at this point in the history
  • Loading branch information
liugaowei committed Aug 5, 2023
1 parent 9b97253 commit f15203d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tts-vue",
"version": "1.9.12",
"version": "1.9.15",
"main": "dist/electron/main/index.js",
"description": "🎤 微软语音合成工具,使用 Electron + Vue + ElementPlus + Vite 构建。",
"author": "沫離Loker <[email protected]>",
Expand Down
45 changes: 41 additions & 4 deletions src/components/configpage/ConfigPage.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="config-page">
<div class="config-side">
<el-form :model="config" label-position="top">
<div class="config-side" label-position="right">
<el-form :model="config" >
<el-form-item label="下载路径">
<el-input
v-model="config.savePath"
Expand All @@ -14,6 +14,24 @@
</template>
</el-input>
</el-form-item>
<el-form-item label="重试次数">
<el-input
type="number"
v-model="config.retryCount"
size="small"
class="input-path"
@change="setRetryCount"
/>
</el-form-item>
<el-form-item label="重试间隔(s)">
<el-input
type="number"
v-model="config.retryInterval"
size="small"
class="input-path"
@change="setRetryInterval"
/>
</el-form-item>
<el-form-item label="SpeechKey">
<el-input
v-model="config.speechKey"
Expand All @@ -31,7 +49,7 @@
@change="setServiceRegion"
/>
</el-form-item>
<el-form-item label="自动播放(仅单文本模式)">
<el-form-item label="自动播放">
<el-switch
v-model="config.autoplay"
active-text=""
Expand All @@ -40,7 +58,7 @@
@change="switchChange"
/>
</el-form-item>
<el-form-item label="版本更新弹窗提醒">
<el-form-item label="新版本提醒">
<el-switch
v-model="config.updateNotification"
active-text=""
Expand Down Expand Up @@ -188,6 +206,7 @@ const savePathConfig = () => {
duration: 2000,
});
};
const auditionConfig = () => {
ttsStore.setAuditionConfig();
ElMessage({
Expand Down Expand Up @@ -241,6 +260,24 @@ const setServiceRegion = () => {
duration: 2000,
});
};
const setRetryCount = () => {
ttsStore.setRetryCount();
ElMessage({
message: "保存成功,请点击“刷新配置”立即应用。。",
type: "success",
duration: 2000,
});
};
const setRetryInterval = () => {
ttsStore.setRetryInterval();
ElMessage({
message: "保存成功,请点击“刷新配置”立即应用。。",
type: "success",
duration: 2000,
});
};
</script>

<style scoped>
Expand Down
6 changes: 6 additions & 0 deletions src/global/initLocalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ export default async function initStore() {
if (!store.has("disclaimers")) {
store.set("disclaimers", false);
}
if (!store.has("retryCount")) {
store.set("retryCount", 10);
}
if (!store.has("retryInterval")) {
store.set("retryInterval", 3);
}
}
25 changes: 23 additions & 2 deletions src/store/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async function getTTSData(
pitch = 0,
api: number,
key: string,
region: string
region: string,
retryCount: number,
retryInterval = 1,
) {
let SSML = "";
if (inps.activeIndex == "1" && (api == 1 || api == 3)) {
Expand Down Expand Up @@ -46,7 +48,7 @@ async function getTTSData(
ipcRenderer.send("log.info", SSML);
console.log(SSML);
if (api == 1) {
const result = await ipcRenderer.invoke("speech", SSML);
const result = await retrySpeechInvocation(SSML, retryCount, retryInterval * 1000);
return result;
} else if (api == 2) {
const result = await ipcRenderer.invoke("edgeApi", SSML);
Expand All @@ -56,4 +58,23 @@ async function getTTSData(
return result;
}
}
async function retrySpeechInvocation(SSML: string, retryCount: number, delay: number) {
let retry = 0;
while (retry < retryCount) {
try {
console.log("Speech invocation attempt", retry + 1);
const result = await ipcRenderer.invoke("speech", SSML);
return result; // 执行成功,返回结果
} catch (error) {
console.error("Speech invocation failed:", error);
await sleep(delay); // 暂停一段时间后再重试
}
retry++;
}
throw new Error(`Speech invocation failed after ${retryCount} retries`); // 重试次数用尽,抛出异常
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

export default getTTSData;
49 changes: 33 additions & 16 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const useTtsStore = defineStore("ttsStore", {
speechKey: store.get("speechKey"),
serviceRegion: store.get("serviceRegion"),
disclaimers: store.get("disclaimers"),
retryCount: store.get("retryCount"),
retryInterval: store.get("retryInterval"),
},
isLoading: false,
currMp3Buffer: Buffer.alloc(0),
Expand Down Expand Up @@ -95,10 +97,16 @@ export const useTtsStore = defineStore("ttsStore", {
store.set("autoplay", this.config.autoplay);
},
setSpeechKey() {
store.set("speechKey", this.config.speechKey);
store.set("speechKey", this.config.speechKey);
},
setServiceRegion() {
store.set("serviceRegion", this.config.serviceRegion);
store.set("serviceRegion", this.config.serviceRegion);
},
setRetryCount() {
store.set("retryCount", parseInt(this.config.retryCount));
},
setRetryInterval() {
store.set("retryInterval", parseInt(this.config.retryInterval));
},
addFormConfig() {
this.config.formConfigJson[this.currConfigName] = this.formConfig;
Expand Down Expand Up @@ -134,8 +142,12 @@ export const useTtsStore = defineStore("ttsStore", {
? this.inputs.inputValue
: this.inputs.ssmlValue,
};
if (this.page.tabIndex == "1" && this.formConfig.api == 1 && this.inputs.inputValue.length > 400) {
const delimiters = ",。?,.?".split("");
if (
this.page.tabIndex == "1" &&
this.formConfig.api == 1 &&
this.inputs.inputValue.length > 400
) {
const delimiters = [",", "。", "?", ",", ".", "?", "\n"];
const maxSize = 300;
ipcRenderer.send("log.info", "字数过多,正在对文本切片。。。");

Expand Down Expand Up @@ -177,7 +189,8 @@ export const useTtsStore = defineStore("ttsStore", {
(this.formConfig.pitch - 1) * 50,
this.formConfig.api,
this.config.speechKey,
this.config.serviceRegion
this.config.serviceRegion,
this.config.retryCount,
);
this.currMp3Buffer = Buffer.concat([this.currMp3Buffer, buffers]);
ipcRenderer.send(
Expand Down Expand Up @@ -220,7 +233,8 @@ export const useTtsStore = defineStore("ttsStore", {
(this.formConfig.pitch - 1) * 50,
this.formConfig.api,
this.config.speechKey,
this.config.serviceRegion
this.config.serviceRegion,
this.config.retryCount,
)
.then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
Expand Down Expand Up @@ -318,7 +332,8 @@ export const useTtsStore = defineStore("ttsStore", {
(this.formConfig.pitch - 1) * 50,
this.formConfig.api,
this.config.speechKey,
this.config.serviceRegion
this.config.serviceRegion,
this.config.retryCount,
);
buffer = Buffer.concat([buffer, buffers]);
ipcRenderer.send(
Expand Down Expand Up @@ -365,7 +380,8 @@ export const useTtsStore = defineStore("ttsStore", {
(this.formConfig.pitch - 1) * 50,
this.formConfig.api,
this.config.speechKey,
this.config.serviceRegion
this.config.serviceRegion,
this.config.retryCount,
)
.then((mp3buffer: any) => {
fs.writeFileSync(filePath, mp3buffer);
Expand Down Expand Up @@ -430,7 +446,8 @@ export const useTtsStore = defineStore("ttsStore", {
(this.formConfig.pitch - 1) * 50,
this.formConfig.api,
this.config.speechKey,
this.config.serviceRegion
this.config.serviceRegion,
this.config.retryCount,
)
.then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
Expand All @@ -448,16 +465,16 @@ export const useTtsStore = defineStore("ttsStore", {
showDisclaimers() {
if (!this.config.disclaimers) {
ElMessageBox.confirm(
'该软件以及代码仅为个人学习测试使用,请在下载后24小时内删除,不得用于商业用途,否则后果自负。任何违规使用造成的法律后果与本人无关。该软件也永远不会收费,如果您使用该软件前支付了额外费用,或付费获得源码以及成品软件,那么你一定被骗了!',
'注意!',
"该软件以及代码仅为个人学习测试使用,请在下载后24小时内删除,不得用于商业用途,否则后果自负。任何违规使用造成的法律后果与本人无关。该软件也永远不会收费,如果您使用该软件前支付了额外费用,或付费获得源码以及成品软件,那么你一定被骗了!",
"注意!",
{
confirmButtonText: '我已确认,不再弹出',
cancelButtonText: '取消',
type: 'warning',
confirmButtonText: "我已确认,不再弹出",
cancelButtonText: "取消",
type: "warning"
}
).then(() => {
store.set('disclaimers', true)
})
store.set("disclaimers", true);
});
}
}
},
Expand Down

0 comments on commit f15203d

Please sign in to comment.