void OnAudioFilterRead(float[] data, int channels) { #if UNITY_EDITOR if (!runEffect) { return; } stopwatch.Reset(); stopwatch.Start(); MAX_inputGain = 0; MAX_outputGain = 0; MAX_gateState = 0; #endif for (var i = 0; i < data.Length; i += channels) { data[i] *= InputGain; data[i + 1] *= InputGain; float rms = Mathf.Sqrt(data[i] * data[i] + data[i + 1] * data[i + 1]); float env = envelope.ProcessSample(rms); float compressMod = 1; if (env < Threshold) { compressMod = Mathf.Pow(env / 4, 4); } data[i] *= compressMod * OutputGain; data[i + 1] *= compressMod * OutputGain; #if UNITY_EDITOR float mergedData = (Mathf.Abs(data[i]) + Mathf.Abs(data[i + 1])) / 2; MAX_inputGain = MAX_inputGain > Mathf.Abs(rms) ? MAX_inputGain : Mathf.Abs(rms); MAX_outputGain = MAX_outputGain > mergedData * OutputGain ? MAX_outputGain : mergedData * OutputGain; MAX_gateState = MAX_gateState > compressMod ? MAX_gateState : compressMod; #endif } #if UNITY_EDITOR stopwatch.Stop(); runTime = Mathf.Round((float)stopwatch.Elapsed.TotalMilliseconds * 100) / 100; // stopwatch.ElapsedMilliseconds; #endif }
void OnAudioFilterRead(float[] data, int channels) { #if UNITY_EDITOR if (!runEffect) { return; } stopwatch.Reset(); stopwatch.Start(); MAX_inputGain = 0; MAX_outputGain = 0; MAX_compressedGain = 0; MAX_dryGain = 0; MAX_gainReduction = 0; #endif if (envelope == null) { return; } if (input == null || input.Length != channels) { input = new float[channels]; compressed = new float[channels]; dry = new float[channels]; } for (var i = 0; i < data.Length; i += channels) { rms = 0; for (var c = 0; c < channels; c++) { input[c] = data[i + c] * InputGain; rms += input[c] * input[c]; } rms = Mathf.Pow(rms, 1f / channels); env = envelope.ProcessSample(rms); compressMod = 1; if (env > Threshold) { compressMod = Mathf.Clamp(compressMod - (env - Threshold) * Slope, 0, 1); } mergedData = 0; for (var c = 0; c < channels; c++) { compressed[c] = input[c] * compressMod; mergedData += compressed[c] * compressed[c]; data[i + c] = (compressed[c] * CompressedGain + input[c] * DryGain) * OutputGain; } mergedData = Mathf.Pow(mergedData, 1f / channels); #if UNITY_EDITOR MAX_inputGain = Mathf.Max(MAX_inputGain, rms); MAX_compressedGain = Mathf.Max(MAX_compressedGain, mergedData * CompressedGain); MAX_dryGain = Mathf.Max(MAX_dryGain, rms * DryGain); MAX_outputGain = Mathf.Max(MAX_outputGain, Mathf.Sqrt(Mathf.Pow(data[i], 2) + Mathf.Pow(data[i + 1], 2))); MAX_gainReduction = Mathf.Max(MAX_gainReduction, rms - mergedData); #endif } #if UNITY_EDITOR stopwatch.Stop(); runTime = Mathf.Round((float)stopwatch.Elapsed.TotalMilliseconds * 100) / 100; // stopwatch.ElapsedMilliseconds; #endif }