Skip to content

Instantly share code, notes, and snippets.

@RFullum
Created June 26, 2020 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RFullum/a1bee89fc289572e0d7bc5d2d533bde8 to your computer and use it in GitHub Desktop.
Save RFullum/a1bee89fc289572e0d7bc5d2d533bde8 to your computer and use it in GitHub Desktop.
Unity Objects React to Microphone Input Values
/**
* Check your Microphone Permissions, especially on Mac.
* I had to delete Unity Hub, run the unity project, allow mic access when the warning pops up,
* and then reinstall Unity Hub or restore it from the trash after mic access is granted.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class CandleBlow1 : MonoBehaviour
{
AudioSource audioSource;
// Default Mic
public string selectedDevice;
// Block for audioSource.GetOutputData()
public static float[] samples = new float[128];
void Start()
{
audioSource = GetComponent<AudioSource>();
/**
* if there are microphones,
* select the default mic,
* set the audioSource.clip to the default mic, looped, for 1 second, at the sampleRate
* set loop to true
*/
if (Microphone.devices.Length > 0)
{
selectedDevice = Microphone.devices[0].ToString();
audioSource.clip = Microphone.Start(selectedDevice, true, 1, AudioSettings.outputSampleRate);
audioSource.loop = true;
/**
* While the position of the mic in the recording is greater than 0,
* play the clip (that should be the mic)
*/
while (! (Microphone.GetPosition(selectedDevice) > 0) )
{
audioSource.Play();
}
}
}
void Update()
{
getOutputData();
}
/**
* Load the block samples with data from the audioSource output
* Average the values across the size of the block.
* vals is the volume of the mic, used to control block height
* Block height represents candle flame getting larger
*/
void getOutputData()
{
audioSource.GetOutputData(samples, 0);
float vals = 0.0f;
for (int i=0; i<128; i++)
{
vals += Mathf.Abs(samples[i]);
}
vals /= 128.0f;
gameObject.transform.localScale = new Vector3(1.0f, 1.0f + (vals * 10.0f), 1.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment