Skip to content

Instantly share code, notes, and snippets.

@gtk2k
Last active December 10, 2023 04:21
Show Gist options
  • Save gtk2k/c3522c57e9730fce30e4a5913b4d949e to your computer and use it in GitHub Desktop.
Save gtk2k/c3522c57e9730fce30e4a5913b4d949e to your computer and use it in GitHub Desktop.
うえぞうさんの uLipSyncWebGL (https://github.com/uezo/uLipSyncWebGL) の以下のファイルを以下のコードに上書き + WorkletProcessor.js は StreamingAssets に配置
using AOT;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using uLipSyncScript = uLipSync.uLipSync;
namespace ChatdollKit.Extension.uLipSync
{
public class uLipSyncWebGL : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void InitWebGLuLipSync(Action callback, float[] samplingData);
private static uLipSyncScript ulipSyncScript;
private static float[] samplingData;
private void Awake()
{
#if UNITY_WEBGL && !UNITY_EDITOR
samplingData = new float[128];
ulipSyncScript = GetComponent<uLipSyncScript>();
if (ulipSyncScript != null)
{
InitWebGLuLipSync(SetAudioSampleData, samplingData);
}
#endif
}
[MonoPInvokeCallback(typeof(Action))]
public static void SetAudioSampleData()
{
//Debug.Log($"=== SetAudioSampleData: {samplingData[0]}");
if (!ulipSyncScript.audioSourceProxy)
{
ulipSyncScript.OnDataReceived(samplingData, 1);
}
}
}
}
mergeInto(LibraryManager.library, {
$managed: {
buffer: null,
stream: null,
source: null,
worklet: null
},
InitWebGLuLipSync__deps: ['$managed'],
InitWebGLuLipSync: function (notifyPtr, bufferPtr) {
debugger;
managed.buffer = new Float32Array(buffer, bufferPtr, 128);
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
managed.stream = stream;
managed.source = WEBAudio.audioContext.createMediaStreamSource(stream);
console.log(`=== sampleRate: ${WEBAudio.audioContext.sampleRate}`);
return WEBAudio.audioContext.audioWorklet.addModule('/StreamingAssets/WorkletProcessor.js');
}).then(_ => {
managed.worklet = new AudioWorkletNode(WEBAudio.audioContext, 'ProcessAudioData');
managed.source.connect(managed.worklet);
managed.worklet.connect(WEBAudio.audioContext.destination);
managed.worklet.port.onmessage = (e) => {
managed.buffer.set(e.data);
dynCall('v', notifyPtr);
};
});
}
});
class ProcessAudioData extends AudioWorkletProcessor {
process([input], [output], parameters) {
this.port.postMessage(input[0]);
output[0].set(input[0]);
return true;
}
}
registerProcessor('ProcessAudioData', ProcessAudioData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment