Skip to content

Commit

Permalink
Change: Coeiroink側のstyleIdを使うように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Nov 26, 2023
1 parent 125d07b commit 3f033dc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 52 deletions.
47 changes: 32 additions & 15 deletions providers/info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { dirname, toBase64 } from "../deps.ts";
import { getIdFromSpeaker, getOrAppendSpeaker } from "../store.ts";
import { Provider } from "./index.ts";

let speakers: {
Expand All @@ -19,7 +18,7 @@ const infoProvider: Provider = ({ baseClient, app }) => {
app.get("/version", async (c) => {
await baseClient.get("");

return c.json("0.1.1");
return c.json("0.2.0");
});

app.get(
Expand Down Expand Up @@ -52,6 +51,28 @@ const infoProvider: Provider = ({ baseClient, app }) => {
],
contributors: [],
},
{
version: "0.2.0",
descriptions: [
"Change: Coeiroink側のstyleIdを使うように変更",
],
contributors: ["sevenc-nanashi"],
},
{
version: "0.1.3",
descriptions: [
"Fix: 読点周りの挙動を修正",
],
contributors: ["sevenc-nanashi"],
},
{
version: "0.1.2",
descriptions: [
"Add: mutexを追加",
"Add: 自動起動を追加",
],
contributors: ["sevenc-nanashi"],
},
{
version: "0.1.1",
descriptions: [
Expand Down Expand Up @@ -102,19 +123,15 @@ const infoProvider: Provider = ({ baseClient, app }) => {
speakers = await baseClient.get("v1/speakers").json();

return c.json(
await Promise.all(
speakers.map(async (speaker) => ({
name: speaker.speakerName,
speaker_uuid: speaker.speakerUuid,
styles: await Promise.all(
speaker.styles.map(async (style) => ({
name: style.styleName,
id: await getOrAppendSpeaker(speaker.speakerUuid, style.styleId),
})),
),
version: speaker.version,
speakers.map((speaker) => ({
name: speaker.speakerName,
speaker_uuid: speaker.speakerUuid,
styles: speaker.styles.map((style) => ({
name: style.styleName,
id: style.styleId,
})),
),
version: speaker.version,
})),
);
});

Expand All @@ -131,7 +148,7 @@ const infoProvider: Provider = ({ baseClient, app }) => {
policy: "https://coeiroink.com/terms を参照して下さい。",
portrait: speaker.base64Portrait,
style_infos: speaker.styles.map((style) => ({
id: getIdFromSpeaker(speakerUuid, style.styleId),
id: style.styleId,
icon: style.base64Icon,
portrait: style.base64Portrait,
voice_samples: [],
Expand Down
17 changes: 15 additions & 2 deletions providers/synthesis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Provider } from "./index.ts";
import { getSpeakerFromId } from "../store.ts";
import { AsyncLock, wanakana } from "../deps.ts";

type Prosody = {
Expand Down Expand Up @@ -169,7 +168,21 @@ const synthesisProvider: Provider = ({ baseClient, app }) => {
}
const accentPhrases = audioQuery.accent_phrases;
const prosody = accentPhrasesToProsody(accentPhrases);
const [speakerUuid, styleId] = getSpeakerFromId(speakerId) ?? [];
const speakers = await baseClient.get("v1/speakers").json<{
speakerUuid: string;
styles: {
styleId: number;
}[];
}[]>();
let speakerUuid: string | undefined;
let styleId: number | undefined;
for (const speaker of speakers) {
if (speaker.styles.find((style) => style.styleId === speakerId)) {
speakerUuid = speaker.speakerUuid;
styleId = speakerId;
break;
}
}
if (!speakerUuid) {
c.status(400);
return c.json({
Expand Down
37 changes: 2 additions & 35 deletions store.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
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);
}
export let store: Store = { enginePath: undefined };

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 + ".tmp", JSON.stringify(store));
await Deno.rename(filePath + ".tmp", filePath);
Expand Down

0 comments on commit 3f033dc

Please sign in to comment.