增加tts初步支持,大文件存储改用lfs
This commit is contained in:
1 parent
512651be68
commit
7df49b37f3
13 files changed
+159
-277
No files matched your search
+2
-1
@@ -1,2 +1,3 @@
|
||||
./outputs/
|
||||
|
||||
outputs/*
|
||||
private_key.asc
|
||||
@@ -5,6 +5,7 @@
|
||||
"imports": {
|
||||
"@std/assert": "jsr:@std/assert@1",
|
||||
"@std/toml": "jsr:@std/toml@^1.0.11",
|
||||
"fs": "npm:fs@^0.0.1-security",
|
||||
"markdown-it": "npm:markdown-it@^14.1.1",
|
||||
"openpgp": "npm:openpgp@^6.3.0",
|
||||
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
||||
|
||||
Generated
+5
@@ -5,6 +5,7 @@
|
||||
"jsr:@std/collections@^1.1.3": "1.1.6",
|
||||
"jsr:@std/internal@^1.0.12": "1.0.12",
|
||||
"jsr:@std/toml@^1.0.11": "1.0.11",
|
||||
"npm:fs@^0.0.1-security": "0.0.1-security",
|
||||
"npm:markdown-it@^14.1.1": "14.1.1",
|
||||
"npm:openpgp@^6.3.0": "6.3.0",
|
||||
"npm:pdf-lib@^1.17.1": "1.17.1",
|
||||
@@ -119,6 +120,9 @@
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw=="
|
||||
},
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w=="
|
||||
},
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
|
||||
"os": ["darwin"],
|
||||
@@ -244,6 +248,7 @@
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@1",
|
||||
"jsr:@std/toml@^1.0.11",
|
||||
"npm:fs@^0.0.1-security",
|
||||
"npm:markdown-it@^14.1.1",
|
||||
"npm:openpgp@^6.3.0",
|
||||
"npm:pdf-lib@^1.17.1",
|
||||
|
||||
+31
-13
@@ -32,17 +32,35 @@ import { PDFParse } from "pdf-parse";
|
||||
// },
|
||||
// });
|
||||
|
||||
Deno.test({
|
||||
name: "OpenPGPSigner test",
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
async fn() {
|
||||
const { OpenPGPSigner } = await import("./pdf_builder/builder.ts");
|
||||
const signer = new OpenPGPSigner(
|
||||
"./outputs/mobile.pdf",
|
||||
"./private_key.asc",
|
||||
"./outputs/mobile.pdf.sig",
|
||||
);
|
||||
await signer.sign();
|
||||
},
|
||||
// Deno.test({
|
||||
// name: "OpenPGPSigner test",
|
||||
// sanitizeOps: false,
|
||||
// sanitizeResources: false,
|
||||
// async fn() {
|
||||
// const { OpenPGPSigner } = await import("./pdf_builder/builder.ts");
|
||||
// const signer = new OpenPGPSigner(
|
||||
// "./outputs/mobile.pdf",
|
||||
// "./private_key.asc",
|
||||
// "./outputs/mobile.pdf.sig",
|
||||
// );
|
||||
// await signer.sign();
|
||||
// },
|
||||
// });
|
||||
import { writeFileSync } from "node:fs";
|
||||
Deno.test("TTS test", async () => {
|
||||
const { PiperCloudTTS } = await import("./pdf_builder/tts.ts");
|
||||
|
||||
const tts = new PiperCloudTTS("https://piper.cmlmuf.org/");
|
||||
const contents = Deno.readTextFileSync(
|
||||
"./outputs/工农解放报RSF续刊第一期-适合手机阅读.md",
|
||||
);
|
||||
|
||||
await tts.stream_to_file(
|
||||
contents,
|
||||
"zh_CN-xiao_ya-medium",
|
||||
"./outputs/test_audio.wav",
|
||||
1000,
|
||||
);
|
||||
|
||||
console.log("Audio streaming completed to ./outputs/test_audio.wav");
|
||||
});
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:488a086da08698b32c6b53d9ad7206559e69a2f13fd4571f089b9292c228044b
|
||||
size 1044017
|
||||
oid sha256:522a27fb94672c600552efa7842ce84e038f899d6894dea4327c17353011af71
|
||||
size 1039520
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:e081480ba83e85554028953bca535adf3529962cbf84454236612728832f497b
|
||||
size 1044070
|
||||
oid sha256:522a27fb94672c600552efa7842ce84e038f899d6894dea4327c17353011af71
|
||||
size 1039520
|
||||
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8fae6cce7619f098ed43ff53e556f6e2d195dc6aa918ac09bf8f128f1a211661
|
||||
size 1042003
|
||||
oid sha256:7e26e82dd37cc6f10feda34473b07d2cc0edd47757c57c5a61d1b6dccc91289f
|
||||
size 1039427
|
||||
@@ -52,8 +52,15 @@ export class DocuBuilder {
|
||||
|
||||
async as_markdown() {
|
||||
const turndownService = new TurndownService();
|
||||
const md_page = turndownService.turndown(await this.as_html_page());
|
||||
|
||||
const html_page = await this.as_html_page();
|
||||
const cleaned_html = html_page.replace(
|
||||
`/<[^>]class="[^"]\bc_title\b[^"]"[^>]>([\s\S]*?)</[^>]+>/gi`,
|
||||
"# $1",
|
||||
).replace(
|
||||
/<script\b[^>]*>[\s\S]*?<\/script>|<style\b[^>]*>[\s\S]*?<\/style>/gi,
|
||||
"",
|
||||
);
|
||||
const md_page = turndownService.turndown(cleaned_html);
|
||||
this.pages = [md_page];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
export interface TTS {
|
||||
say_all(content: string, voice: string): Promise<ArrayBuffer>;
|
||||
}
|
||||
|
||||
export class PiperCloudTTS {
|
||||
api_url: string;
|
||||
|
||||
constructor(api_url: string) {
|
||||
this.api_url = api_url;
|
||||
}
|
||||
|
||||
async say_all(content: string, voice: string): Promise<ArrayBuffer> {
|
||||
const response = await fetch(this.api_url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
},
|
||||
body: JSON.stringify({ text: content, voice: voice }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(
|
||||
`Failed to fetch ${this.api_url}: ${response.status} ${response.statusText} - ${errorText}`,
|
||||
);
|
||||
}
|
||||
|
||||
return await response.arrayBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将长文本分段并直接流式写入文件
|
||||
*/
|
||||
async stream_to_file(
|
||||
content: string,
|
||||
voice: string,
|
||||
outputPath: string,
|
||||
chunkSize = 1000,
|
||||
): Promise<void> {
|
||||
const file = await Deno.open(outputPath, {
|
||||
write: true,
|
||||
create: true,
|
||||
truncate: true,
|
||||
});
|
||||
|
||||
try {
|
||||
for (let i = 0; i < content.length; i += chunkSize) {
|
||||
const chunk = content.slice(i, i + chunkSize);
|
||||
console.log(`Processing chunk ${i / chunkSize + 1}...`);
|
||||
|
||||
const audioData = await this.say_all(chunk, voice);
|
||||
// WAV 文件通常包含头。注意:如果只是简单合并 WAV,
|
||||
// 只有第一个 chunk 的头是有效的,后续 chunk 的头会被视为数据(可能会有杂音)。
|
||||
// 如果 API 返回的是原始 PCM,则没问题。
|
||||
// 这里我们先按照用户要求的流式写入实现。
|
||||
await file.write(new Uint8Array(audioData));
|
||||
}
|
||||
} finally {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user