Skip to content

Instantly share code, notes, and snippets.

@m0o0scar
Last active November 16, 2017 02:27
Show Gist options
  • Save m0o0scar/42cca9fe504fcbb20b623831e0c62243 to your computer and use it in GitHub Desktop.
Save m0o0scar/42cca9fe504fcbb20b623831e0c62243 to your computer and use it in GitHub Desktop.
Unity To Playback A Microphone Input In Real Time
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Microphone : MonoBehaviour {
AudioSource source;
void Start () {
if(Microphone.devices.Length > 0) {
int minFreq, maxFreq, freq;
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
freq = Mathf.Min(44100, maxFreq);
source = GetComponent<AudioSource>();
source.clip = Microphone.Start(null, true, 5, freq);
source.loop = true;
while(!(Microphone.GetPosition(null) > 0)){}
source.Play();
}
else {
Debug.Log("No Mic connected!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment