2023-02-20 02:21:51 +09:00
|
|
|
import { useState, useMemo, useEffect } from "react"
|
|
|
|
|
2023-06-24 09:52:21 +09:00
|
|
|
import { DefaultWorkletNodeSetting, INDEXEDDB_KEY_WORKLETNODE, WorkletNodeSetting } from "../const"
|
2023-02-20 02:21:51 +09:00
|
|
|
import { VoiceChangerClient } from "../VoiceChangerClient"
|
|
|
|
import { useIndexedDB } from "./useIndexedDB"
|
|
|
|
|
|
|
|
export type UseWorkletNodeSettingProps = {
|
|
|
|
voiceChangerClient: VoiceChangerClient | null
|
|
|
|
}
|
|
|
|
|
|
|
|
export type WorkletNodeSettingState = {
|
|
|
|
workletNodeSetting: WorkletNodeSetting;
|
|
|
|
clearSetting: () => Promise<void>
|
|
|
|
updateWorkletNodeSetting: (setting: WorkletNodeSetting) => void
|
2023-02-20 04:49:34 +09:00
|
|
|
startOutputRecording: () => void
|
|
|
|
stopOutputRecording: () => Promise<Float32Array>
|
2023-02-28 11:54:40 +09:00
|
|
|
trancateBuffer: () => Promise<void>
|
2023-02-20 02:21:51 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useWorkletNodeSetting = (props: UseWorkletNodeSettingProps): WorkletNodeSettingState => {
|
2023-03-13 00:54:49 +09:00
|
|
|
const defaultWorkletNodeSetting = useMemo(() => {
|
2023-06-24 09:52:21 +09:00
|
|
|
return DefaultWorkletNodeSetting
|
2023-03-13 00:54:49 +09:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const [workletNodeSetting, _setWorkletNodeSetting] = useState<WorkletNodeSetting>(defaultWorkletNodeSetting)
|
2023-06-24 09:52:21 +09:00
|
|
|
const { setItem, getItem, removeItem } = useIndexedDB({ clientType: null })
|
2023-02-20 02:21:51 +09:00
|
|
|
|
|
|
|
// 初期化 その1 DBから取得
|
|
|
|
useEffect(() => {
|
|
|
|
const loadCache = async () => {
|
|
|
|
const setting = await getItem(INDEXEDDB_KEY_WORKLETNODE) as WorkletNodeSetting
|
|
|
|
if (setting) {
|
2023-04-20 17:17:43 +09:00
|
|
|
_setWorkletNodeSetting({ ...setting, sendingSampleRate: 48000 }) // sample rateは時限措置
|
2023-02-20 02:21:51 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loadCache()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
// 初期化 その2 クライアントに設定
|
|
|
|
useEffect(() => {
|
|
|
|
if (!props.voiceChangerClient) return
|
|
|
|
props.voiceChangerClient.setServerUrl(workletNodeSetting.serverUrl)
|
|
|
|
props.voiceChangerClient.updateWorkletNodeSetting(workletNodeSetting)
|
|
|
|
}, [props.voiceChangerClient])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const clearSetting = async () => {
|
|
|
|
await removeItem(INDEXEDDB_KEY_WORKLETNODE)
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// 設定
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
const updateWorkletNodeSetting = useMemo(() => {
|
|
|
|
return (_workletNodeSetting: WorkletNodeSetting) => {
|
|
|
|
if (!props.voiceChangerClient) return
|
|
|
|
for (let k in _workletNodeSetting) {
|
2023-02-20 04:49:34 +09:00
|
|
|
const cur_v = workletNodeSetting[k as keyof WorkletNodeSetting]
|
|
|
|
const new_v = _workletNodeSetting[k as keyof WorkletNodeSetting]
|
2023-02-20 02:21:51 +09:00
|
|
|
if (cur_v != new_v) {
|
|
|
|
_setWorkletNodeSetting(_workletNodeSetting)
|
|
|
|
setItem(INDEXEDDB_KEY_WORKLETNODE, _workletNodeSetting)
|
|
|
|
props.voiceChangerClient.updateWorkletNodeSetting(_workletNodeSetting)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [props.voiceChangerClient, workletNodeSetting])
|
|
|
|
|
2023-02-20 04:49:34 +09:00
|
|
|
const startOutputRecording = useMemo(() => {
|
|
|
|
return () => {
|
|
|
|
if (!props.voiceChangerClient) return
|
|
|
|
props.voiceChangerClient.startOutputRecording()
|
|
|
|
}
|
|
|
|
}, [props.voiceChangerClient])
|
|
|
|
|
|
|
|
const stopOutputRecording = useMemo(() => {
|
|
|
|
return async () => {
|
|
|
|
if (!props.voiceChangerClient) return new Float32Array()
|
|
|
|
return props.voiceChangerClient.stopOutputRecording()
|
|
|
|
}
|
|
|
|
}, [props.voiceChangerClient])
|
|
|
|
|
2023-02-28 11:54:40 +09:00
|
|
|
const trancateBuffer = useMemo(() => {
|
|
|
|
return async () => {
|
|
|
|
if (!props.voiceChangerClient) return
|
|
|
|
props.voiceChangerClient.trancateBuffer()
|
|
|
|
}
|
|
|
|
}, [props.voiceChangerClient])
|
2023-02-20 02:21:51 +09:00
|
|
|
|
|
|
|
return {
|
|
|
|
workletNodeSetting,
|
|
|
|
clearSetting,
|
|
|
|
updateWorkletNodeSetting,
|
2023-02-20 04:49:34 +09:00
|
|
|
startOutputRecording,
|
2023-02-28 11:54:40 +09:00
|
|
|
stopOutputRecording,
|
|
|
|
trancateBuffer
|
2023-02-20 02:21:51 +09:00
|
|
|
}
|
|
|
|
}
|