2023-02-16 18:09:56 +03:00
|
|
|
import { ClientState } from "@dannadori/voice-changer-client-js";
|
2023-11-08 13:54:13 +03:00
|
|
|
import { VoiceChangerJSClient } from "@dannadori/voice-changer-js";
|
2023-02-22 01:35:26 +03:00
|
|
|
import React, { useContext, useEffect, useRef } from "react";
|
2023-02-16 18:09:56 +03:00
|
|
|
import { ReactNode } from "react";
|
2023-03-09 00:47:11 +03:00
|
|
|
import { useVCClient } from "../001_globalHooks/001_useVCClient";
|
2023-02-16 18:09:56 +03:00
|
|
|
import { useAppRoot } from "./001_AppRootProvider";
|
2023-06-11 07:25:44 +03:00
|
|
|
import { useMessageBuilder } from "../hooks/useMessageBuilder";
|
2023-02-16 18:09:56 +03:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
type AppStateValue = ClientState & {
|
2023-09-27 22:19:41 +03:00
|
|
|
audioContext: AudioContext;
|
|
|
|
initializedRef: React.MutableRefObject<boolean>;
|
|
|
|
};
|
2023-02-16 18:09:56 +03:00
|
|
|
|
|
|
|
const AppStateContext = React.createContext<AppStateValue | null>(null);
|
|
|
|
export const useAppState = (): AppStateValue => {
|
|
|
|
const state = useContext(AppStateContext);
|
|
|
|
if (!state) {
|
|
|
|
throw new Error("useAppState must be used within AppStateProvider");
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AppStateProvider = ({ children }: Props) => {
|
2023-09-27 22:19:41 +03:00
|
|
|
const appRoot = useAppRoot();
|
|
|
|
const clientState = useVCClient({ audioContext: appRoot.audioContextState.audioContext });
|
|
|
|
const messageBuilderState = useMessageBuilder();
|
2023-11-03 19:41:51 +03:00
|
|
|
const voiceChangerJSClient = useRef<VoiceChangerJSClient>();
|
2023-04-04 21:35:16 +03:00
|
|
|
|
2023-06-11 07:25:44 +03:00
|
|
|
useEffect(() => {
|
|
|
|
messageBuilderState.setMessage(__filename, "ioError", {
|
2023-09-27 22:19:41 +03:00
|
|
|
ja: "エラーが頻発しています。対象としているフレームワークのモデルがロードされているか確認してください。",
|
|
|
|
en: "Frequent errors occur. Please check if the model of the framework being targeted is loaded.",
|
|
|
|
});
|
|
|
|
}, []);
|
2023-02-16 18:09:56 +03:00
|
|
|
|
2023-09-27 22:19:41 +03:00
|
|
|
const initializedRef = useRef<boolean>(false);
|
2023-06-25 20:40:44 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (clientState.clientState.initialized) {
|
2023-09-27 22:19:41 +03:00
|
|
|
initializedRef.current = true;
|
|
|
|
clientState.clientState.getInfo();
|
2023-06-25 20:40:44 +03:00
|
|
|
// clientState.clientState.setVoiceChangerClientSetting({
|
|
|
|
// ...clientState.clientState.setting.voiceChangerClientSetting
|
|
|
|
// })
|
|
|
|
}
|
2023-09-27 22:19:41 +03:00
|
|
|
}, [clientState.clientState.initialized]);
|
2023-02-22 01:35:26 +03:00
|
|
|
|
2023-06-11 04:55:26 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (clientState.clientState.ioErrorCount > 100) {
|
2023-09-27 22:19:41 +03:00
|
|
|
alert(messageBuilderState.getMessage(__filename, "ioError"));
|
|
|
|
clientState.clientState.resetIoErrorCount();
|
2023-06-11 04:55:26 +03:00
|
|
|
}
|
2023-09-27 22:19:41 +03:00
|
|
|
}, [clientState.clientState.ioErrorCount]);
|
2023-06-11 04:55:26 +03:00
|
|
|
|
2023-11-14 02:17:27 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (clientState.clientState.initialized) {
|
|
|
|
// const baseUrl = "https://192.168.0.247:18888";
|
|
|
|
// const baseUrl = "https://192.168.0.247:8080";
|
|
|
|
const baseUrl = window.location.origin;
|
|
|
|
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvc2v_40k_f0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvc2v_40k_nof0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvc2v_16k_f0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv2_amitaro_v2_40k_f0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv2_amitaro_v2_40k_nof0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv2_amitaro_v2_32k_f0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv2_amitaro_v2_32k_nof0_24000.bin`;
|
2023-11-08 13:54:13 +03:00
|
|
|
|
2023-11-14 02:17:27 +03:00
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv1_amitaro_v1_32k_f0_24000.bin`;
|
|
|
|
const modelUrl = `${baseUrl}/models/rvcv1_amitaro_v1_32k_nof0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv1_amitaro_v1_40k_f0_24000.bin`;
|
|
|
|
// const modelUrl = `${baseUrl}/models/rvcv1_amitaro_v1_40k_nof0_24000.bin`;
|
2023-11-08 13:54:13 +03:00
|
|
|
|
2023-11-14 02:17:27 +03:00
|
|
|
voiceChangerJSClient.current = new VoiceChangerJSClient();
|
|
|
|
voiceChangerJSClient.current.initialize(
|
|
|
|
{
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
inputSamplingRate: 48000,
|
|
|
|
outputSamplingRate: 48000,
|
|
|
|
},
|
|
|
|
modelUrl,
|
|
|
|
);
|
|
|
|
clientState.clientState.setInternalAudioProcessCallback({
|
|
|
|
processAudio: async (data: Uint8Array) => {
|
|
|
|
const audioF32 = new Float32Array(data.buffer);
|
|
|
|
const converted = await voiceChangerJSClient.current!.convert(audioF32);
|
|
|
|
const res = new Uint8Array(converted.buffer);
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [clientState.clientState.initialized]);
|
2023-02-22 01:35:26 +03:00
|
|
|
|
2023-02-16 18:09:56 +03:00
|
|
|
const providerValue: AppStateValue = {
|
|
|
|
audioContext: appRoot.audioContextState.audioContext!,
|
|
|
|
...clientState.clientState,
|
2023-04-04 10:38:30 +03:00
|
|
|
initializedRef,
|
2023-02-16 18:09:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return <AppStateContext.Provider value={providerValue}>{children}</AppStateContext.Provider>;
|
|
|
|
};
|