private void DrawCurve( Rect r, float[] curve, float yoffset, float yscale, Color col, float labeloffset) { var numsamples = curve.Length - 1; // the last sample in buffer is counter for how much we could read var transitionPixels = 30.0f; var x0 = 1.0f - (curve[numsamples] / curve.Length); var xr = transitionPixels / r.width; float xscale = numsamples - 2; var thr_dB = -120.0f; var thr_lin = 20.0f * Mathf.Pow(10.0f, thr_dB * 0.05f); col.a /= xr; float peakVal = -100000.0f, peakPos = -1000000.0f; AudioCurveRendering.DrawFilledCurve( r, delegate(float x, out Color color) { var f = Mathf.Clamp(x * xscale, 0.0f, xscale); color = col; color.a = col.a * Mathf.Clamp(x - x0, 0, xr); var i = (int)Mathf.Floor(f); var mag = curve[i] + ((curve[i + 1] - curve[i]) * (f - i)); var mag_dB = mag < thr_lin ? thr_dB : 20.0f * Mathf.Log10(mag); var pos = (mag_dB - yoffset) * yscale; peakVal = Mathf.Max(peakVal, mag_dB); peakPos = Mathf.Max(peakPos, pos); return(pos); } ); peakPos = r.y + (r.height * (0.5f - (0.5f * peakPos))); if ((peakPos >= r.y) && (peakPos <= (r.y + r.height))) { var col2 = new Color(col.r, col.g, col.b, 0.7f); GUIHelpers.DrawLine(r.x, peakPos, r.x + r.width, peakPos, col2); GUIHelpers.DrawText((r.x + labeloffset) - 30, peakPos + 6, 60, string.Format("{0:F1} dB", peakVal), col2); } }
public void DrawControl(IAudioEffectPlugin plugin, Rect r, float samplerate) { var evt = Event.current; var controlID = GUIUtility.GetControlID(FocusType.Passive); var evtType = evt.GetTypeForControl(controlID); r = AudioCurveRendering.BeginCurveFrame(r); if ((evtType == EventType.MouseDown) && r.Contains(evt.mousePosition) && (evt.button == 0)) { float ofs, rofs; plugin.GetFloatParameter(DragParams[(int)DragOperation.Offset], out ofs); plugin.GetFloatParameter(DragParams[(int)DragOperation.RandomOffset], out rofs); var x = r.x + (r.width * (ofs - rofs)); var mindist = Mathf.Abs(x - evt.mousePosition.x); dragOperation = DragOperation.RandomOffset; x = r.x; for (var i = DragOperation.Offset; i <= DragOperation.RandomWindowLen; i++) { float value; plugin.GetFloatParameter(DragParams[(int)i], out value); x += r.width * value; var dx = Mathf.Abs(x - evt.mousePosition.x); if (dx < mindist) { mindist = dx; dragOperation = i; } } GUIUtility.hotControl = controlID; EditorGUIUtility.SetWantsMouseJumping(1); evt.Use(); } if ((evtType == EventType.MouseDrag) && (GUIUtility.hotControl == controlID)) { if (dragOperation != DragOperation.None) { float value; plugin.GetFloatParameter(DragParams[(int)dragOperation], out value); value = Mathf.Clamp( value + ((dragOperation == DragOperation.RandomOffset ? -evt.delta.x : evt.delta.x) * 0.001f), 0.0f, 1.0f ); plugin.SetFloatParameter(DragParams[(int)dragOperation], value); evt.Use(); } } else if ((evtType == EventType.MouseUp) && (GUIUtility.hotControl == controlID)) { dragOperation = DragOperation.None; GUIUtility.hotControl = 0; EditorGUIUtility.SetWantsMouseJumping(0); evt.Use(); } else if (Event.current.type == EventType.Repaint) { var blend = plugin.IsPluginEditableAndEnabled() ? 1.0f : 0.5f; var numsamples = (int)r.width; float[] wave1; plugin.GetFloatBuffer("Waveform0", out wave1, numsamples); float[] wave2; plugin.GetFloatBuffer("Waveform1", out wave2, numsamples); float ofs; plugin.GetFloatParameter(DragParams[(int)DragOperation.Offset], out ofs); float rofs; plugin.GetFloatParameter(DragParams[(int)DragOperation.RandomOffset], out rofs); float wlen; plugin.GetFloatParameter(DragParams[(int)DragOperation.WindowLen], out wlen); float rwlen; plugin.GetFloatParameter(DragParams[(int)DragOperation.RandomWindowLen], out rwlen); float shape; plugin.GetFloatParameter("Shape", out shape); float useSample; plugin.GetFloatParameter("Use Sample", out useSample); m_Wave1Color.a = blend; m_Wave2Color.a = blend; m_Wave3Color.a = blend; m_Wave4Color.a = blend; var r2 = new Rect(r.x, r.y, r.width, r.height * 0.5f); DrawCurve( r2, wave1, 1.0f, m_Wave1Color, m_Wave2Color, m_Wave3Color, m_Wave4Color, 90, rofs, ofs, wlen, rwlen, shape ); r2.y += r2.height; DrawCurve( r2, wave2, 1.0f, m_Wave1Color, m_Wave2Color, m_Wave3Color, m_Wave4Color, 150, rofs, ofs, wlen, rwlen, shape ); var x1 = r.x + (r.width * (ofs - rofs)); var x2 = r.x + (r.width * ofs); var x3 = x2 + (r.width * wlen); var x4 = x3 + (r.width * rwlen); GUIHelpers.DrawLine(x1, r.y, x1, r.y + r.height, m_Wave1Color); GUIHelpers.DrawLine(x2, r.y, x2, r.y + r.height, m_Wave2Color); GUIHelpers.DrawLine(x3, r.y, x3, r.y + r.height, m_Wave3Color); GUIHelpers.DrawLine(x4, r.y, x4, r.y + r.height, m_Wave4Color); var name = "Sample: " + Marshal.PtrToStringAnsi(Granulator_GetSampleName((int)useSample)); GUIHelpers.DrawText(r2.x + 5, r2.y - 5, r2.width, name, Color.white); } AudioCurveRendering.EndCurveFrame(); }