I am having an issue with my unity project where I am making a music visualizer and picking up the audio with the Android Microphone (Samsung Note 9). The audio has a delay when making my gameobjects react (simple equalizer effect on a grid of cubes). Really not sure what the issue is other than the obvious issue that Android just has latency issues? Attaching my script in case anything stands out that would make the latency worse…
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AudioVisualizer
{
///
/// Object scale waveform.
/// Scale objects along a given axis, to create an audio waveform. Objects are typically arranged in a line or close together.
///
public class Master_ObjectScale_WaveForm_Grid_Material : MonoBehaviour
{
//____________Public Variables
[Header(“AUDIO SETTINGS”)]
[Tooltip(“Index into the AudioSampler audioSources or audioFiles list”)]
public int audioIndex = 0; // index into audioSampler audioSources or audioFIles list. Determines which audio source we want to sample
[Tooltip(“The frequency range you want to listen to”)]
public FrequencyRange frequencyRange = FrequencyRange.Decibal; // what frequency will we listen to?
[Tooltip(“How sensitive to the audio are we?”)]
public float sensitivity = 2; // how sensitive is this script to the audio. This value is multiplied by the audio sample data.
[Tooltip(“What objects should we scale to create a waveform?
Usually objects are placed in a line”)]
public List cubeobjects; // objects to scale with the music
public List cubefaces; //cube faces to change material of
public string CubeTag;
public string FaceTag;
[Tooltip(“Scale objects along this axis”)]
public Vector3 scaleAxis = Vector3.up; // the direction we scale each object.
[Tooltip(“Max scale along ‘scaleAxis’”)]
public float maxHeight = 10; // the max scale along the scaleAxis. i.e. if scaleAxis is(0,1,0), this is our maxHeight
[Tooltip(“How fast do we move the objects?”)]
public float lerp;// speed at which we scale, from currentScale to nextScale.
public float minimum;
public float maximum;
public float timelerp;
[Tooltip(“Take the absolute value of samples?”)]
public bool absoluteVal = true; // /use absolute value of audio decibal levels
[Tooltip(“Sample from a recorded AudioFile?”)]
public bool useAudioFile = false; // flag saying if we should use a pre-recorded audio file
//____________Delegates/Actions
//____________Protected Variables
//____________Private Variables
[Header("GRID SETTINGS")]
public Transform brick;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 1f;
private List<Vector3> startingScales; // the scale of each object on game start
private List<Vector3> startingPositions; // the position of each object on game start
/*________________Monobehaviour Methods________________*/
// Use this for initialization
void Start()
{
StartCoroutine(NumberGen());
for (int y = 0; y < gridY; y++)
{
for (int x = 0; x < gridX; x++)
{
{
var newInstance = Instantiate(brick, this.gameObject.transform);
newInstance.localPosition = new Vector3(x * spacing, y * spacing, 0);
}
}
}
cubeobjects.AddRange(GameObject.FindGameObjectsWithTag(CubeTag));
cubefaces.AddRange(GameObject.FindGameObjectsWithTag(FaceTag));
//initialize starting scales and positions
startingScales = new List<Vector3>();
startingPositions = new List<Vector3>();
foreach (GameObject obj in cubeobjects)
{
startingScales.Add(obj.transform.localScale);
startingPositions.Add(obj.transform.localPosition);
}
}
IEnumerator NumberGen()
{
while (true)
{
lerp = Random.Range(minimum, maximum);
yield return new WaitForSeconds(timelerp);
}
}
// Update is called once per frame
void Update()
{
UpdateValueIncrease();
ScaleObjects();
ChangeMaterial();
}
/*________________Public Methods________________*/
/*________________Protected Methods________________*/
/*________________Private Methods________________*/
void ChangeMaterial()
{
MeshRenderer my_renderer = GetComponent<MeshRenderer>();
if (my_renderer != null)
{
Material my_material = my_renderer.material;
}
for (int i = 0; i < cubefaces.Count; i++)
{
//Other.GetComponent<MeshRenderer>().material = my_renderer.material;
cubefaces*.GetComponent<MeshRenderer>().material = my_renderer.material;*
}
}
void ScaleObjects()
{
float[] audioSamples;
if (frequencyRange == FrequencyRange.Decibal)
{
audioSamples = AudioSampler.instance.GetAudioSamples(audioIndex, cubeobjects.Count, absoluteVal, useAudioFile);
}
else
{
audioSamples = AudioSampler.instance.GetFrequencyData(audioIndex, frequencyRange, cubeobjects.Count, absoluteVal, false);
}
//for each object
for (int i = 0; i < cubeobjects.Count; i++)
{
float scale = audioSamples * sensitivity * AudioSampler.instance.globalSensitivity;
float sampleScale = Mathf.Min(scale, 1); // % of maxHiehgt, via th audio sample
float currentHeight = sampleScale * maxHeight;
Vector3 desiredScale = startingScales + currentHeight * new Vector3(0,0, increase);//transform.InverseTransformDirection(scaleAxis); // get desired scale, in correct direction, using audio
cubeobjects.transform.localScale = Vector3.Lerp(cubeobjects_.transform.localScale, desiredScale, Time.smoothDeltaTime * lerp); // lerp from current scale to desired scale_
//reposition the object after scaling, so that it’s seemingly in the same place.
float distanceScaled = (cubeobjects_.transform.localScale - startingScales*).y; // get change in scale*
Vector3 direction = cubeobjects*.transform.TransformDirection(scaleAxis); // get movement direction, relative to object*
cubeobjects.transform.localPosition = startingPositions + distanceScaled * new Vector3(0, 0, increase); // move the object
}
}
}
}
`_