Last active
June 29, 2018 05:52
-
-
Save togucchi/8f2802e44e96bae1de1c55dec12e9acf to your computer and use it in GitHub Desktop.
【Unity】Live2DCubismでシンプルなリップシンク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Live2D.Cubism.Framework.MouthMovement; | |
[RequireComponent(typeof(CubismMouthController))] | |
public class CubismSimpleLipSync : MonoBehaviour { | |
[SerializeField] | |
CubismMouthController mouthController; | |
[SerializeField] | |
SimpleMicrophone mic; | |
public float minVolume = 0.05f; | |
public float maxVolume = 0.2f; | |
public float fps = 30f; | |
float[] volumes = new float[50]; | |
// Use this for initialization | |
void Start () { | |
if(mouthController == null) | |
mouthController = GetComponent<CubismMouthController>(); | |
StartCoroutine(ControlMouth()); | |
} | |
// Update is called once per frame | |
void Update () { | |
for(int i = 0;i < 49;i++) | |
{ | |
volumes[50 - 1 - i] = volumes[50 - 2 - i]; | |
} | |
volumes[0] = mic.GetVolume(); | |
} | |
IEnumerator ControlMouth() | |
{ | |
while(true) | |
{ | |
float open; | |
// open = (mic.GetVolume() >= minVolume) ? mic.GetVolume() / maxVolume : 0f; | |
open = (GetVolumeAverage(1f / fps) >= minVolume) ? GetVolumeAverage(1f/ fps) / maxVolume : 0f; | |
if(open >= 1f) open = 1f; | |
if(mouthController != null) mouthController.MouthOpening = open; | |
yield return new WaitForSeconds(1f / fps); | |
} | |
} | |
float GetVolumeAverage(float time) | |
{ | |
int frame = (int)(1f / Time.deltaTime * time); | |
float vol = 0; | |
for(int i = 0; i < frame; i++) | |
{ | |
vol += volumes[i]; | |
} | |
vol = vol / frame; | |
return vol; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent(typeof(AudioSource))] | |
public class SimpleMicrophone : MonoBehaviour { | |
AudioSource audio; | |
float volume = 0f; | |
void Start() { | |
audio = GetComponent<AudioSource>(); | |
audio.clip = Microphone.Start(null, true, 999, 44100); | |
audio.loop = true; | |
audio.mute = false; //trueにすると音量が取得できない場合があるので,AudioMixerで調節する | |
while (!(Microphone.GetPosition("") > 0)){} | |
audio.Play(); | |
} | |
void Update() | |
{ | |
volume = GetAveragedVolume(); | |
//Debug.Log(volume.ToString()); | |
} | |
float GetAveragedVolume() | |
{ | |
float[] data = new float[256]; | |
float a = 0; | |
audio.GetOutputData(data,0); | |
foreach(float s in data) | |
{ | |
a += Mathf.Abs(s); | |
} | |
return a/256.0f; | |
} | |
public float GetVolume() | |
{ | |
return volume; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment