Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method for missing voices #538

Merged
merged 4 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { UserModel } from "../strapi-model";
import { IUserInfo } from "../stores/user";
import { useRootStore } from "../stores/root";
import { XMLParser } from "fast-xml-parser";
import { fixVoiceUrl } from "./voiceFinder";

export function useRefState<S>(
initialValue: S
Expand Down Expand Up @@ -426,6 +427,10 @@ export function useProcessedScenarioData() {
FirstBackground,
} = data;

const voiceMap: {
[key: string]: Record<string, string>;
} = {};

if (FirstBackground) {
ret.actions.push({
body: FirstBgm,
Expand Down Expand Up @@ -488,36 +493,66 @@ export function useProcessedScenarioData() {
case SnippetAction.Talk:
{
const talkData = TalkData[snippet.ReferenceIndex];

// try get character
let chara2d: ICharacter2D | undefined;
const chara = { id: 0, name: "" };
if (talkData.TalkCharacters[0].Character2dId) {
const chara2d = chara2Ds.find(
chara2d = chara2Ds.find(
(ch) => ch.id === talkData.TalkCharacters[0].Character2dId
)!;
chara.id = chara2d.characterId;
}

chara.name = talkData.WindowDisplayName;
let voiceUrl = talkData.Voices.length
? `sound/${isCardStory ? "card_" : ""}${
isActionSet ? "actionset" : "scenario"
}/voice/${ScenarioId}_rip/${talkData.Voices[0].VoiceId}.mp3`
: "";

if (
talkData.Voices.length &&
talkData.Voices[0].VoiceId.startsWith("partvoice") &&
!isActionSet
) {
const chara2d = chara2Ds.find(
(ch) => ch.id === talkData.TalkCharacters[0].Character2dId
);
if (chara2d) {
voiceUrl = `sound/scenario/part_voice/${chara2d.assetName}_${chara2d.unit}_rip/${talkData.Voices[0].VoiceId}.mp3`;

let voiceUrl = "";
if (talkData.Voices.length) {
const VoiceId = talkData.Voices[0].VoiceId;
const isPartVoice =
VoiceId.startsWith("partvoice") && !isActionSet;
if (isPartVoice) {
// part_voice
if (chara2d) {
voiceUrl = `sound/scenario/part_voice/${chara2d.assetName}_${chara2d.unit}_rip/${VoiceId}.mp3`;
}
} else {
voiceUrl = "";
// card, actionset, scenario
voiceUrl = `sound/${isCardStory ? "card_" : ""}${
isActionSet ? "actionset" : "scenario"
}/voice/${ScenarioId}_rip/${VoiceId}.mp3`;
}

// Get asset list in directory
voiceUrl = await fixVoiceUrl(
voiceMap,
region,
VoiceId,
voiceUrl
);
}

// Original codes
// let voiceUrl = talkData.Voices.length
// ? `sound/${isCardStory ? "card_" : ""}${
// isActionSet ? "actionset" : "scenario"
// }/voice/${ScenarioId}_rip/${talkData.Voices[0].VoiceId}.mp3`
// : "";
// if (
// talkData.Voices.length &&
// talkData.Voices[0].VoiceId.startsWith("partvoice") &&
// !isActionSet
// ) {
// const chara2d = chara2Ds.find(
// (ch) => ch.id === talkData.TalkCharacters[0].Character2dId
// );
// if (chara2d) {
// voiceUrl = `sound/scenario/part_voice/${chara2d.assetName}_${chara2d.unit}_rip/${talkData.Voices[0].VoiceId}.mp3`;
// } else {
// voiceUrl = "";
// }
// }

action = {
body: talkData.Body,
chara,
Expand Down
98 changes: 98 additions & 0 deletions src/utils/voiceFinder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { XMLParser } from "fast-xml-parser";
import { IListBucketResult, ServerRegion } from "../types";
import axios from "axios";
import { assetUrl } from "./urls";

// const errorVoices = {
// // Master assetBundleName error?
// "sound/scenario/voice/part_voice_v2_24luka_light_sound_rip/partvoice_20_025.mp3":
// "sound/scenario/voice/part_voice_v2_24luka_light_sound_rip/partvoice_20_024.mp3"
// }

export const normalizeVoiceName = function (str: string) {
return str.replace(/_([0-9]{1,3})[abc]_/, "_$1_").replace(/_[abc0-9]$/, "");
};

export const getVoiceListElements = async function (
acc: Record<string, string>,
region: ServerRegion,
pathname: string,
token?: string
) {
const parser = new XMLParser({
isArray: (name) => {
if (["CommonPrefixes", "Contents"].includes(name)) return true;
return false;
},
});

const baseURL = assetUrl.minio[region];

const result = (
await axios.get<string>(`/`, {
baseURL,
params: {
"continuation-token": token,
delimiter: "/",
"list-type": "2",
"max-keys": "500",
prefix: pathname,
},
responseType: "text",
})
).data;

const data = parser.parse(result).ListBucketResult as IListBucketResult;
if (data.Contents) {
for (const item of data.Contents) {
const pth = item.Key;
if (!pth.endsWith(".mp3")) {
continue;
}
const keys = item.Key.replace(".mp3", "")
.replace(pathname, "")
.split(";");

for (let k of keys) {
k = normalizeVoiceName(k.trim());
if (k.length > 0 && !acc[k]) {
acc[k] = item.Key;
}
}
}
}

if (data.NextContinuationToken) {
acc = await getVoiceListElements(
acc,
region,
pathname,
data.NextContinuationToken
);
}

return acc;
};

export const fixVoiceUrl = async function (
voiceMap: {
[key: string]: Record<string, string>;
},
region: ServerRegion,
voiceId: string,
voiceUrl: string
) {
// Tested only "jp" region
if (region !== "jp") {
return voiceUrl;
}
const dirUrl = voiceUrl.split("/").slice(0, -1).join("/") + "/";
let voiceList;
if (voiceMap[dirUrl]) {
voiceList = voiceMap[dirUrl];
} else {
voiceMap[dirUrl] = await getVoiceListElements({}, region, dirUrl);
voiceList = voiceMap[dirUrl];
}
return voiceList[normalizeVoiceName(voiceId)] || voiceUrl;
};
Loading