public static float GetAveragedVolume(AudioSource audio, int samples) { float[] data = new float[samples]; float a = 0; audio.GetOutputData(data,0); foreach(float s in data) { a += Mathf.Abs(s); } return a/samples; }
public void getVolume(){ audio = GetComponent<AudioSource> (); audio.GetOutputData(samples, 0); float sum = 0; for(int i=0; i< qsamples; i++){ sum += samples[i]*samples[i]; } rmsvalue = Mathf.Sqrt(sum/qsamples); //rmsvalue += 1; dbvalue = 20 * Mathf.Log10(rmsvalue/refvalue); if(dbvalue<-160){ dbvalue = -160; } }
static public int GetOutputData(IntPtr l) { try { UnityEngine.AudioSource self = (UnityEngine.AudioSource)checkSelf(l); System.Single[] a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.GetOutputData(a1, a2); return(0); } catch (Exception e) { return(error(l, e)); } }
static int GetOutputData(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.AudioSource obj = (UnityEngine.AudioSource)ToLua.CheckObject(L, 1, typeof(UnityEngine.AudioSource)); float[] arg0 = ToLua.CheckNumberArray <float>(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); obj.GetOutputData(arg0, arg1); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
float GetSamplesVolume(AudioSource audio) { audio.GetOutputData(waveData, chanel); float sum = 0; foreach (float s in waveData) { sum += Mathf.Abs(s); } return (sum / smaples); }
/// <summary> /// Analyze upcoming audio signal and find the ticks. /// /// The algorithm is simple : we analyze the upcoming signal in many parts with a size of a half tick (~44 samples at 44100 Hz); /// We take each time two parts of samples and if the first part has less gain than the second's, /// we consider that there is a tick in the second. /// /// When the tick has been found, we approximatively skip to a place where the next tick could be /// in order to optimise the research. /// Otherwise we continue from the second part. /// </summary> /// <param name="audio">Audio source (Unity).</param> public static void Analyze(AudioSource audio) { int currentSample = audio.timeSamples; int samples = currentSample - lastAnalyzedSample; // We must analyze a range which can contain at least one tick. if (samples < tickMinDelta || samples == 0) return; // Copy the audio data in our buffer. float[] data = new float[samples]; audio.GetOutputData (data, 0); data.CopyTo (_buffer, lastAnalyzedSample); // Ignore filters processes if audio source is a file. // The file is already processed. if (!sourceIsFile) Process (data); float[] means = new float[2]; // Two parts of samples. int d = tickSamples / 2; // Half tick. int parts = samples / d; // Number of parts to process. // Process the parts. for (int i = 0; i < parts - 1;) { means [0] = FlapiUtils.AbsMean (data, i * d, (i + 1) * d - 1); // Mean gain of first part. means [1] = FlapiUtils.AbsMean (data, (i + 1) * d, (i + 2) * d - 1); // Mean gain of second part. // Second part has more gain than first's ? if (means [1] > _threshold * means [0]) { _ticks [lastAnalyzedSample + (i + 2) * d] = true; // Here's a tick ! i += 2 * d; //(tickMinDelta / d - 1); // Process to the approximative next tick. } else { i++; } } _frequency = GetTicksFrequency (currentSample); lastAnalyzedSample = currentSample - (samples % tickMinDelta); // Next call skip after the last analyzed buffer. }