1111using UnityEngine . InputSystem ;
1212using PlayerModel . Player ;
1313using PlayerModel . Utils ;
14- using System . Net ;
15- using UnityEngine . XR ;
1614using System . Linq ;
15+ using UnityEngine . Audio ;
1716
1817
1918namespace PlayerModel
2019{
21- [ BepInDependency ( "org.legoandmars.gorillatag.utilla" , "1.6.0 " ) ]
20+ [ BepInDependency ( "org.legoandmars.gorillatag.utilla" , "1.6.8 " ) ]
2221 [ BepInPlugin ( PluginInfo . GUID , PluginInfo . Name , PluginInfo . Version ) ]
2322 public class Plugin : BaseUnityPlugin
2423 {
25-
26- public bool Cheaking = true ;
24+ static public AudioClip _audioClip ;
25+ static public bool _useMic = true ;
26+ static public string _selectedDevice ;
27+ static public AudioMixerGroup _mixerGroupMic ;
28+ static public bool Cheaking = true ;
2729
2830 public void Start ( )
2931 {
32+ Debug . Log ( "wokring iwkjebfiqwjuhebfiuwqbhefiuwhbeofijhwbefihbwerf" ) ;
3033 if ( STARTPLAYERMOD )
3134 return ;
3235
@@ -59,8 +62,17 @@ void OnGameInitialized(object sender, EventArgs e)
5962 GameObject gorillaface = GorillaTagger . Instance . offlineVRRig . mainSkin . transform . parent . Find ( "rig/body/head/gorillaface" ) . gameObject ;
6063 setmatalpha ( gorillaface . GetComponent < Renderer > ( ) . material ) ;
6164
65+
66+ LipSyncStart ( ) ;
67+
6268
6369
70+ for ( int thisReading = 0 ; thisReading < samples ; thisReading ++ )
71+ {
72+ readings [ thisReading ] = 0 ;
73+ }
74+
75+
6476
6577 StartCoroutine ( StartPlayerModel ( ) ) ;
6678 }
@@ -233,6 +245,88 @@ IEnumerator StartPlayerModel()
233245
234246 yield break ;
235247 }
248+
249+ public static AudioSource _audioSource ;
250+ public static bool vflag = true ;
251+ public static GameObject voiceprefab ;
252+ public static AudioMixer _audioMixer ;
253+
254+ public static GameObject voiceObject ;
255+
256+ public static int clipPosition ;
257+
258+ public static void restartMic ( )
259+ {
260+ _audioSource . Stop ( ) ;
261+ _audioSource . clip = null ;
262+ _audioSource . clip = Microphone . Start ( _selectedDevice , true , 5 , AudioSettings . outputSampleRate ) ;
263+ _audioSource . Play ( ) ;
264+ }
265+ static public void LipSyncStart ( )
266+ {
267+ if ( _useMic )
268+ {
269+ if ( Microphone . devices . Length > 0 )
270+ {
271+ voiceObject = new GameObject ( ) ;
272+ _audioSource = voiceObject . AddComponent < AudioSource > ( ) ;
273+
274+ _selectedDevice = Microphone . devices [ 0 ] . ToString ( ) ;
275+ Debug . Log ( _selectedDevice ) ;
276+ _audioSource . clip = Microphone . Start ( _selectedDevice , true , 5 , AudioSettings . outputSampleRate ) ;
277+
278+ _audioSource . spatialBlend = 1f ;
279+ _audioSource . minDistance = 0f ;
280+ _audioSource . maxDistance = 1f ;
281+ _audioSource . playOnAwake = true ;
282+ _audioSource . loop = true ;
283+ _audioSource . Play ( ) ;
284+
285+ voiceObject = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
286+ voiceObject . transform . position = new Vector3 ( - 66 , 12 , - 82 ) ;
287+
288+ }
289+ else
290+ {
291+ _useMic = false ;
292+ Debug . LogError ( "Micropohone is missing kwjebfiwujeenbfiubwefijbwefijbwefijwbefijwbefijwbefijwbefijwbefijwbef" ) ;
293+ }
294+ }
295+ }
296+ public static GameObject mouth ;
297+ public static int sampleWindow = 64 ;
298+
299+ public static float loudnessSensitivity = 100 ;
300+ public static float threshold = 0.2f ;
301+
302+ public static AudioClip MicClip ;
303+ public static float GetLoudnessFromMic ( )
304+ {
305+ return AudioClipLoudness ( Microphone . GetPosition ( Microphone . devices [ 0 ] ) , _audioSource . clip ) ;
306+ }
307+ static public float AudioClipLoudness ( int clipPosition , AudioClip clip )
308+ {
309+ int startPosition = clipPosition - sampleWindow ;
310+
311+ if ( startPosition < 0 )
312+ return 0 ;
313+
314+ float [ ] waveData = new float [ sampleWindow ] ;
315+ clip . GetData ( waveData , startPosition ) ;
316+
317+ //compute loudness
318+
319+ float totalLoudness = 0 ;
320+
321+ for ( int i = 0 ; i < sampleWindow ; i ++ )
322+ {
323+ totalLoudness += Mathf . Abs ( waveData [ i ] ) ;
324+
325+ }
326+
327+ return totalLoudness / sampleWindow ;
328+ }
329+
236330 static string textfilename = "PlayerModelDefault.pmdefault" ;
237331 static string split = "," ;
238332 public static void writeToText ( int index_ , int IsGorilla_ , string name )
@@ -368,8 +462,17 @@ public void showchest()
368462 //Debug.Log("material set to gorilla");
369463 }
370464
465+ public float voiceDelay = 1 ;
466+
467+
468+
371469 public void Update ( )
372470 {
471+
472+
473+
474+
475+
373476 if ( Time . time < currentTime )
374477 return ;
375478
@@ -379,14 +482,69 @@ public void Update()
379482 PlayerModelController . localPositionY = - 1f ;
380483 else
381484 PlayerModelController . localPositionY = 1f ;
485+
486+
487+
382488 }
383489
490+ public const int samples = 10 ;
491+
492+ float [ ] readings = new float [ samples ] ;
493+ int readIndex = 0 ;
494+ float total = 0 ;
495+ public float avg = 0 ;
496+
497+
498+
499+ public void voiceSmoothing ( )
500+ {
501+
502+ total -= readings [ readIndex ] ;
503+
504+ readings [ readIndex ] = loudness ;
505+ // add the reading to the total:
506+ total = total + readings [ readIndex ] ;
507+ // advance to the next position in the array:
508+ readIndex ++ ;
509+
510+ // if we're at the end of the array...
511+ if ( readIndex >= samples )
512+ {
513+ // ...wrap around to the beginning:
514+
515+ readIndex = 0 ;
516+ }
517+
518+ // calculate the average:
519+ avg = total / samples ;
520+
521+ }
522+ public static float loudness ;
523+
524+ public float LipSyncWeight = 0 ;
525+
526+ public bool LipSyncFlag = true ;
384527 public void FixedUpdate ( )
385528 {
386529
387530 if ( ! STARTPLAYERMOD )
388531 return ;
389532
533+ loudness = GetLoudnessFromMic ( ) * loudnessSensitivity ;
534+
535+ voiceSmoothing ( ) ;
536+
537+ if ( avg < threshold )
538+ avg = 0 ;
539+
540+ Vector3 minScale = new Vector3 ( .1f , .1f , .1f ) ;
541+ Vector3 maxScale = new Vector3 ( 1f , 1f , 1f ) ;
542+
543+ voiceObject . transform . localScale = Vector3 . Lerp ( minScale , maxScale , avg ) ;
544+
545+ LipSyncWeight = Mathf . Lerp ( 0 , 100 , avg ) ;
546+
547+
390548 if ( Keyboard . current . jKey . wasPressedThisFrame )
391549 SelectButton . GetComponent < PlayerModelButton > ( ) . Press ( ) ;
392550
@@ -398,8 +556,34 @@ public void FixedUpdate()
398556
399557 PlayerModelController . rotationY -= 0.5f ;
400558 //Debug.Log(IsGorilla);
401- if ( PhotonNetwork . InRoom )
559+
560+ if ( GorillaParent . instance . vrrigs . Count >= 1 )
402561 {
562+ if ( Time . time > voiceDelay )
563+ {
564+
565+ voiceDelay += 1 ;
566+ Debug . Log ( "new time " + voiceDelay ) ;
567+ restartMic ( ) ;
568+
569+
570+ }
571+
572+ }
573+ else
574+ {
575+ if ( ! LipSyncFlag )
576+ {
577+
578+ restartMic ( ) ;
579+ LipSyncFlag = true ;
580+ Debug . Log ( "Mic Restart" ) ;
581+ }
582+ }
583+
584+ if ( PhotonNetwork . InRoom )
585+ {
586+
403587 if ( IsGorilla == true ) //in a room, is gorilla model
404588 {
405589 PlayerModelAppearance . ShowOnlineRig ( ) ;
@@ -424,6 +608,11 @@ public void FixedUpdate()
424608 if ( PlayerModelController . GameModeTextures )
425609 clone_body = GameObject . Find ( "Global/GorillaParent/GorillaVRRigs/Gorilla Player Networked(Clone)/gorilla" ) ;
426610 PlayerModelAppearance . AssignMaterial ( clone_body , playermodel ) ;
611+
612+ if ( PlayerModelController . LipSync )
613+ {
614+ playermodel . GetComponent < SkinnedMeshRenderer > ( ) . SetBlendShapeWeight ( 0 , LipSyncWeight ) ;
615+ }
427616 }
428617
429618 if ( clone_body == null )
@@ -443,6 +632,7 @@ public void FixedUpdate()
443632 else if ( ! PhotonNetwork . InRoom )
444633 {
445634
635+
446636 flag_inroom = false ;
447637 clone_body = null ;
448638 if ( IsGorilla == true ) //not in a room, is gorilla model
@@ -465,6 +655,12 @@ public void FixedUpdate()
465655
466656 if ( PlayerModelController . CustomColors )
467657 PlayerModelAppearance . AssignColor ( ) ;
658+
659+ if ( PlayerModelController . LipSync )
660+ {
661+ playermodel . GetComponent < SkinnedMeshRenderer > ( ) . SetBlendShapeWeight ( 0 , LipSyncWeight ) ;
662+ }
663+
468664 }
469665 else //redundency
470666 {
0 commit comments