2023-01-11 21:49:22 +03:00
|
|
|
import { VoiceChangerWorkletProcessorRequest } from "./@types/voice-changer-worklet-processor";
|
2023-02-12 07:24:30 +03:00
|
|
|
import { WorkletSetting } from "./const";
|
2023-01-11 21:49:22 +03:00
|
|
|
|
2023-02-12 11:07:28 +03:00
|
|
|
export type VoiceChangerWorkletListener = {
|
2023-01-05 05:45:42 +03:00
|
|
|
notifyVolume: (vol: number) => void
|
2023-02-12 11:07:28 +03:00
|
|
|
notifyOutputRecordData: (data: Float32Array[]) => void
|
2023-01-05 05:45:42 +03:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:28:36 +03:00
|
|
|
export class VoiceChangerWorkletNode extends AudioWorkletNode {
|
2023-02-12 11:07:28 +03:00
|
|
|
private listener: VoiceChangerWorkletListener
|
|
|
|
constructor(context: AudioContext, listener: VoiceChangerWorkletListener) {
|
2023-01-04 20:28:36 +03:00
|
|
|
super(context, "voice-changer-worklet-processor");
|
|
|
|
this.port.onmessage = this.handleMessage.bind(this);
|
2023-01-05 05:45:42 +03:00
|
|
|
this.listener = listener
|
2023-01-04 20:28:36 +03:00
|
|
|
console.log(`[worklet_node][voice-changer-worklet-processor] created.`);
|
|
|
|
}
|
|
|
|
|
2023-02-12 07:29:50 +03:00
|
|
|
postReceivedVoice = (data: ArrayBuffer) => {
|
|
|
|
const req: VoiceChangerWorkletProcessorRequest = {
|
|
|
|
requestType: "voice",
|
|
|
|
voice: data,
|
|
|
|
numTrancateTreshold: 0,
|
|
|
|
volTrancateThreshold: 0,
|
|
|
|
volTrancateLength: 0
|
|
|
|
}
|
|
|
|
this.port.postMessage(req)
|
2023-01-04 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(event: any) {
|
2023-01-05 05:45:42 +03:00
|
|
|
// console.log(`[Node:handleMessage_] `, event.data.volume);
|
2023-02-12 07:24:30 +03:00
|
|
|
if (event.data.responseType === "volume") {
|
|
|
|
this.listener.notifyVolume(event.data.volume as number)
|
|
|
|
} else if (event.data.responseType === "recordData") {
|
2023-02-12 11:07:28 +03:00
|
|
|
this.listener.notifyOutputRecordData(event.data.recordData as Float32Array[])
|
2023-02-12 07:24:30 +03:00
|
|
|
} else {
|
|
|
|
console.warn(`[worklet_node][voice-changer-worklet-processor] unknown response ${event.data.responseType}`, event.data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configure = (setting: WorkletSetting) => {
|
|
|
|
const req: VoiceChangerWorkletProcessorRequest = {
|
|
|
|
requestType: "config",
|
|
|
|
voice: new ArrayBuffer(1),
|
|
|
|
numTrancateTreshold: setting.numTrancateTreshold,
|
|
|
|
volTrancateThreshold: setting.volTrancateThreshold,
|
|
|
|
volTrancateLength: setting.volTrancateLength
|
|
|
|
}
|
|
|
|
this.port.postMessage(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
startOutputRecordingWorklet = () => {
|
|
|
|
const req: VoiceChangerWorkletProcessorRequest = {
|
|
|
|
requestType: "startRecording",
|
|
|
|
voice: new ArrayBuffer(1),
|
|
|
|
numTrancateTreshold: 0,
|
|
|
|
volTrancateThreshold: 0,
|
|
|
|
volTrancateLength: 0
|
|
|
|
}
|
|
|
|
this.port.postMessage(req)
|
|
|
|
|
|
|
|
}
|
|
|
|
stopOutputRecordingWorklet = () => {
|
|
|
|
const req: VoiceChangerWorkletProcessorRequest = {
|
|
|
|
requestType: "stopRecording",
|
|
|
|
voice: new ArrayBuffer(1),
|
|
|
|
numTrancateTreshold: 0,
|
|
|
|
volTrancateThreshold: 0,
|
|
|
|
volTrancateLength: 0
|
|
|
|
}
|
|
|
|
this.port.postMessage(req)
|
2023-01-04 20:28:36 +03:00
|
|
|
}
|
|
|
|
}
|