- Home /
Major Latency on Android when using Microphone
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 { /// <summary> /// Object scale waveform. /// Scale objects along a given axis, to create an audio waveform. Objects are typically arranged in a line or close together. /// </summary> 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?\n Usually objects are placed in a line")] public List<GameObject> cubeobjects; // objects to scale with the music public List<GameObject> 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[i].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[i] * sensitivity * AudioSampler.instance.globalSensitivity; float sampleScale = Mathf.Min(scale, 1); // % of maxHiehgt, via th audio sample float currentHeight = sampleScale * maxHeight; Vector3 desiredScale = startingScales[i] + currentHeight * new Vector3(0,0, increase);//transform.InverseTransformDirection(scaleAxis); // get desired scale, in correct direction, using audio cubeobjects[i].transform.localScale = Vector3.Lerp(cubeobjects[i].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[i].transform.localScale - startingScales[i]).y; // get change in scale Vector3 direction = cubeobjects[i].transform.TransformDirection(scaleAxis); // get movement direction, relative to object cubeobjects[i].transform.localPosition = startingPositions[i] + distanceScaled * new Vector3(0, 0, increase); // move the object } } } }
Answer by wsdfz · Aug 07, 2019 at 03:10 AM
also has this problem when playing microphone input in real time on android platform, have you fixed this problem?
this post is about this problem: https://support.unity3d.com/hc/en-us/articles/206485253-How-do-I-get-Unity-to-playback-a-Microphone-input-in-real-time-
but no help for me.
Your answer
Follow this Question
Related Questions
Music between scenes. Works in editor not after building for androit? 0 Answers
How can I allow audio playback when my android app is out of focus? 1 Answer
When exporting android project to an eclipse project the music doesn't export 0 Answers
How to integrate android visualizer into unity? 1 Answer
Microphone Input Latency Android 1 Answer