Skip to content

Commit

Permalink
Add: 自動起動を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Nov 23, 2023
1 parent 0773706 commit 2211390
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 37 deletions.
6 changes: 3 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"tasks": {
"watch": "deno run --watch --allow-net --allow-env --allow-read --allow-write main.ts",
"start": "deno run --allow-net --allow-env --allow-read --allow-write main.ts",
"compile": "deno compile --allow-net --allow-env --allow-read --allow-write -o coeiroink-v2-bridge main.ts"
"watch": "deno run --watch --allow-net --allow-run --allow-env --allow-read --allow-write main.ts",
"start": "deno run --allow-net --allow-run --allow-env --allow-read --allow-write main.ts",
"compile": "deno compile --allow-net --allow-run --allow-env --allow-read --allow-write -o coeiroink-v2-bridge main.ts"
},
"lock": false
}
53 changes: 52 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Hono, honoLogger, ky, parse, serve } from "./deps.ts";
import { dirname, Hono, honoLogger, ky, parse, serve } from "./deps.ts";
import dictProvider from "./providers/dict.ts";
import infoProvider from "./providers/info.ts";
import noopProvider from "./providers/noop.ts";
import synthesisProvider from "./providers/synthesis.ts";
import { saveStore, store } from "./store.ts";

const args = parse(Deno.args, {
string: ["host", "port", "originalUrl"],
Expand All @@ -16,6 +17,56 @@ const baseClient = ky.create({
prefixUrl: args.originalUrl,
});

if (store.enginePath != undefined) {
if (await baseClient.get("").catch(() => null)) {
console.log("The server is already running, not starting the engine.");
} else if (Deno.stat(store.enginePath).catch(() => null) == undefined) {
console.log(
`The engine path ${store.enginePath} does not exist, not starting the engine.`,
);
store.enginePath = undefined;
} else {
console.log(`Starting the engine at ${store.enginePath}...`);
new Deno.Command(
store.enginePath,
{
stdout: "inherit",
stderr: "inherit",
},
).spawn();
}
} else {
console.log("No engine path, not starting the engine.");
}

while (true) {
try {
await baseClient.get("");
break;
} catch {
console.log("Waiting for the server to be ready...");
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}

const speakers: {
speakerUuid: string;
}[] = await baseClient.get("v1/speakers").json();
const path: {
speakerFolderPath: string;
} = await baseClient.get("v1/speaker_folder_path", {
searchParams: {
speakerUuid: speakers[0].speakerUuid,
},
}).json();
const speakerFolderPath = path.speakerFolderPath;
const enginePath = dirname(dirname(speakerFolderPath)) + "/engine/engine.exe";

console.log(`Engine path: ${enginePath}`);

store.enginePath = enginePath;
await saveStore();

const app = new Hono();

app.use("*", honoLogger());
Expand Down
2 changes: 1 addition & 1 deletion providers/info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, toBase64 } from "../deps.ts";
import { getIdFromSpeaker, getOrAppendSpeaker } from "../speakerMap.ts";
import { getIdFromSpeaker, getOrAppendSpeaker } from "../store.ts";
import { Provider } from "./index.ts";

let speakers: {
Expand Down
2 changes: 1 addition & 1 deletion providers/synthesis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Provider } from "./index.ts";
import { getSpeakerFromId } from "../speakerMap.ts";
import { getSpeakerFromId } from "../store.ts";
import { AsyncLock, wanakana } from "../deps.ts";

type Prosody = {
Expand Down
31 changes: 0 additions & 31 deletions speakerMap.ts

This file was deleted.

49 changes: 49 additions & 0 deletions store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { homeDir } from "./deps.ts";

type SpeakerMap = [number, [string, number]][];
type Store = {
speakerMap: SpeakerMap;
enginePath: string | undefined;
};

const filePath = homeDir + "/.coeiroink-v2.json";
export let store: Store = { speakerMap: [], enginePath: undefined };
const oldFilePath = homeDir + "/.coeiroink-v2-bridge-map.json";
if (await Deno.stat(oldFilePath).catch(() => null)) {
console.log("Migrating old speaker map...");
store.speakerMap.push(...JSON.parse(await Deno.readTextFile(oldFilePath)));
await Deno.remove(oldFilePath);
}
if (await Deno.stat(filePath).catch(() => null)) {
console.log("Loading store...");
store = JSON.parse(await Deno.readTextFile(filePath));
}

export const getSpeakerFromId = (id: number) => {
return store.speakerMap.find(([id_, _]) => id_ === id)?.[1];
};

export const getIdFromSpeaker = (speakerUuid: string, styleId: number) => {
return store.speakerMap.find(
([_, [speakerUuid_, styleId_]]) =>
speakerUuid_ === speakerUuid && styleId_ === styleId,
)?.[0];
};

export const getOrAppendSpeaker = async (
speakerUuid: string,
styleId: number,
) => {
const speakerId = getIdFromSpeaker(speakerUuid, styleId);
if (speakerId !== undefined) {
return speakerId;
}
const speakerCount = store.speakerMap.length;
store.speakerMap.push([speakerCount, [speakerUuid, styleId]]);
await saveStore();
return speakerCount;
};

export const saveStore = async () => {
await Deno.writeTextFile(filePath, JSON.stringify(store));
};

0 comments on commit 2211390

Please sign in to comment.