I found a solution to this:
The Problem:
The scripts UnityMicrophone(by Photon) and LipSyncMicInput(part of the avatars 2 sdk) both contain this line
Microphone.Start(deviceName, looping, audioClipLength, samplingRate).
This method creates a new audio clip which gets filled with the input from your microphone. This causes the following situation:
- UnityMicrophone creates a new audio clip for itself using the Microphone.Start() function and is happily using an audio clip which is being continuously filled with the input of your microphone.
- Now LipSyncMicInput also calls the Microphone.Start(), now the input of your microphone is being stolen by LipSyncMicInput's audio clip and the audio clip which Photon's UnityMicrophone was using now helplessly loops over itself.
- The same can happen the other way around, it just depends who called Microphone.Start() first.
In summary, the audio clip which LipSyncMicInput or Photon.Recorder is using will be useless as soon as one of them calls Microphone.Start().
My Solution:
- I created a central Microphone object which would have the sole responsibility of Starting and stopping the Microphone.
- To deal with Photon, I changed the Input source from Microphone to Factory. And created a custom input source which used my central microphone as its input.
- To deal with LipSync, I REMOVED LipSyncMicInput from my LipSyncInput object, and replaced it with a script which would continuously check the LipSyncInput object's audio source clip against my central Microphone object's audio clip. If different, it would update the lip sync's audio clip to be the same as my central Microphone's. I also had to force the clip's position to match that of my microphone's.
For help creating a custom input source for the Photon Recorder I recommend just googling it.
Here's the script I used to replace LipSyncMicInput, GGMicrophone is my central microphone object.
public class RuntimeAudioClipUpdater : MonoBehaviour
{
AudioSource audioSource;
private void Start()
{
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (audioSource != null)
{
AudioClip clip = audioSource.clip;
AudioClip referenceClip = GGMicrophone.Instance.GetMicrophoneAudioClip();
int referenceClipId = referenceClip.GetInstanceID();
if (clip != null)
{
int currentClipId = clip.GetInstanceID();
if (currentClipId != referenceClipId)
{
//audioSource.Stop();
audioSource.clip = referenceClip;
audioSource.Play();
}
}
else
{
//audioSource.Stop();
audioSource.clip = referenceClip;
audioSource.Play();
}
}
audioSource.timeSamples = GGMicrophone.Instance.GetPosition();
}
}