-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
240 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
/gui/src-tauri/target/ | ||
/gui/dist | ||
/gui/.vite | ||
/.spg-TTS-cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ModOptions } from "../types.ts"; | ||
import { cacheTtsFile, useCachedTtsFile } from "./tts_cache.ts"; | ||
import { getCoquiCommand } from "../utils/external_commands.ts"; | ||
import $ from "@david/dax"; | ||
import { bgRed } from "@std/fmt/colors"; | ||
|
||
export async function generate_audio_with_coqui( | ||
title: string, | ||
opt: ModOptions, | ||
outputPath: string, | ||
) { | ||
const cacheKey = [ | ||
"CoquiTts", | ||
title, | ||
opt.coquiTtsSpeakerIdx, | ||
opt.coquiTtsLanguageIdx, | ||
opt.coquiTtsModel, | ||
]; | ||
if (opt.skipReadTtsCache || !await useCachedTtsFile(outputPath, cacheKey)) { | ||
const coquiCommand = await getCoquiCommand(); | ||
const cmd = [ | ||
...coquiCommand, | ||
"--text", | ||
title, | ||
"--model_name", | ||
opt.coquiTtsModel, | ||
"--out_path", | ||
outputPath, | ||
]; | ||
if (opt.coquiTtsLanguageIdx) { | ||
cmd.push("--language_idx", opt.coquiTtsLanguageIdx); | ||
} | ||
if (opt.coquiTtsSpeakerIdx) { | ||
cmd.push("--speaker_idx", opt.coquiTtsSpeakerIdx); | ||
} | ||
const res = await $`${cmd}`.noThrow(true); | ||
if (res.code === 0) { | ||
if (!opt.skipWriteTtsCache) { | ||
await cacheTtsFile(outputPath, cacheKey); | ||
} | ||
} else { | ||
console.log(bgRed(`Coqui gen KO for "${title}"`)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { getSpgDirPath } from "../utils/utils.ts"; | ||
import { crypto } from "@std/crypto/crypto"; | ||
import { encodeHex } from "@std/encoding/hex"; | ||
import $ from "@david/dax"; | ||
import { green, yellow } from "@std/fmt/colors"; | ||
|
||
export function getDefaultTtsPath() { | ||
return getSpgDirPath().resolve(".spg-TTS-cache"); | ||
} | ||
|
||
export function getCachePath(key: (string | boolean | undefined)[]) { | ||
const data = new TextEncoder().encode(JSON.stringify(key)); | ||
const sum = encodeHex(crypto.subtle.digestSync("MD5", data)); | ||
return getDefaultTtsPath().join(sum.substring(0, 2)).join(sum); | ||
} | ||
|
||
export async function cacheTtsFile( | ||
output: string, | ||
key: (string | undefined | boolean)[], | ||
) { | ||
const cachePath = getCachePath(key); | ||
await cachePath.resolve("..").mkdir({ recursive: true }); | ||
await $.path(output).copyFile(cachePath); | ||
} | ||
|
||
export async function useCachedTtsFile( | ||
output: string, | ||
key: (string | undefined | boolean)[], | ||
): Promise<boolean> { | ||
const cachePath = getCachePath(key); | ||
if (await cachePath.exists()) { | ||
await cachePath.copyFile(output); | ||
console.log(green(`use TTS cached for ${output}`)); | ||
return true; | ||
} else { | ||
console.log(yellow(`no TTS cache found for ${output}`)); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { getCachePath, getDefaultTtsPath } from "./tts_cache.ts"; | ||
|
||
Deno.test("getCachePath", () => { | ||
assertEquals( | ||
(getCachePath(["aa", "bb"])).toString(), | ||
getDefaultTtsPath().join("e9/e9350e939ff0f72285307e1a792fe739").toString(), | ||
); | ||
}); |
Oops, something went wrong.