public bool DrawControl(IAudioEffectPlugin plugin, Rect r, float samplerate) { Event evt = Event.current; int dragControlID = GUIUtility.GetControlID(FocusType.Passive); r = AudioCurveRendering.BeginCurveFrame(r); float windowMin, windowMax, windowDef; plugin.GetFloatParameterInfo("Window", out windowMin, out windowMax, out windowDef); float yscaleMin, yscaleMax, yscaleDef; plugin.GetFloatParameterInfo("YScale", out yscaleMin, out yscaleMax, out yscaleDef); float yoffsetMin, yoffsetMax, yoffsetDef; plugin.GetFloatParameterInfo("YOffset", out yoffsetMin, out yoffsetMax, out yoffsetDef); float window; plugin.GetFloatParameter("Window", out window); float yscale; plugin.GetFloatParameter("YScale", out yscale); float yoffset; plugin.GetFloatParameter("YOffset", out yoffset); float blend = plugin.IsPluginEditableAndEnabled() ? 1.0f : 0.5f; switch (evt.GetTypeForControl(dragControlID)) { case EventType.MouseDown: if (evt.button == 0 && r.Contains(evt.mousePosition) && GUIUtility.hotControl == 0) { GUIUtility.hotControl = dragControlID; evt.Use(); } break; case EventType.MouseUp: if (evt.button == 0 && GUIUtility.hotControl == dragControlID) { GUIUtility.hotControl = 0; evt.Use(); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == dragControlID) { window = Mathf.Clamp(window + evt.delta.x * 0.1f, windowMin, windowMax); if (evt.shift) { yoffset = Mathf.Clamp(yoffset - (0.5f * evt.delta.y / yscale), yoffsetMin, yoffsetMax); } else { yscale = Mathf.Clamp(yscale - evt.delta.y * 0.01f, yscaleMin, yscaleMax); } plugin.SetFloatParameter("Window", window); plugin.SetFloatParameter("YScale", yscale); plugin.SetFloatParameter("YOffset", yoffset); evt.Use(); } break; case EventType.ScrollWheel: if (r.Contains(evt.mousePosition)) { window = Mathf.Clamp(window + evt.delta.x * 0.1f, windowMin, windowMax); yoffset = Mathf.Clamp(yoffset - (0.5f * evt.delta.y / yscale), yoffsetMin, yoffsetMax); plugin.SetFloatParameter("Window", window); plugin.SetFloatParameter("YScale", yscale); plugin.SetFloatParameter("YOffset", yoffset); evt.Use(); } break; case EventType.Repaint: { float yscaleDraw = yscale * 0.05f; // Background grid and values Color lineColor = new Color(0, 0, 0, 0.2f); Color textColor = new Color(1.0f, 1.0f, 1.0f, 0.3f * blend); GUIHelpers.DrawDbTickMarks(r, yoffset, yscaleDraw, textColor, lineColor); GUIHelpers.DrawTimeTickMarks(r, window, textColor, lineColor); // Curves int numsamples = (int)r.width; float[] mcurve; plugin.GetFloatBuffer("MomentaryRMS", out mcurve, numsamples); float[] scurve; plugin.GetFloatBuffer("ShortTermRMS", out scurve, numsamples); float[] icurve; plugin.GetFloatBuffer("IntegratedRMS", out icurve, numsamples); DrawCurve(r, mcurve, yoffset, yscaleDraw, new Color(1.0f, 0.0f, 0.0f, blend * 0.5f), 90); DrawCurve(r, scurve, yoffset, yscaleDraw, new Color(0.0f, 1.0f, 0.0f, blend * 0.3f), 150); DrawCurve(r, icurve, yoffset, yscaleDraw, new Color(0.0f, 0.0f, 1.0f, blend * 0.3f), 210); } break; } AudioCurveRendering.EndCurveFrame(); return(false); }
public bool DrawControl(IAudioEffectPlugin plugin, Rect r, float samplerate, int channel) { r = AudioCurveRendering.BeginCurveFrame(r); if (Event.current.type == EventType.Repaint) { float blend = plugin.IsPluginEditableAndEnabled() ? 1.0f : 0.5f; float window, scale, mode; plugin.GetFloatParameter("Window", out window); plugin.GetFloatParameter("Scale", out scale); plugin.GetFloatParameter("Mode", out mode); float[] buffer; int numsamples = (mode >= 1.0f) ? maxspeclen : (int)(window * samplerate); plugin.GetFloatBuffer("Channel" + channel.ToString(), out buffer, numsamples); numsamples = buffer.Length; if (mode < 2.0f) { Color lineColor = new Color(1.0f, 0.5f, 0.2f, blend); if (mode >= 1.0f) { scale *= 0.1f; AudioCurveRendering.DrawFilledCurve(r, delegate(float x) { float f = Mathf.Clamp(x * (numsamples - 2) * window * 0.5f, 0, numsamples - 2); int i = (int)Mathf.Floor(f); float s1 = 20.0f * Mathf.Log10(buffer[i] + 0.0001f); float s2 = 20.0f * Mathf.Log10(buffer[i + 1] + 0.0001f); return((s1 + (s2 - s1) * (f - i)) * scale); }, lineColor); GUIHelpers.DrawFrequencyTickMarks(r, samplerate * window * 0.5f, false, Color.red); GUIHelpers.DrawDbTickMarks(r, 1.0f / scale, scale, Color.red, new Color(1.0f, 0.0f, 0.0f, 0.25f)); } else { AudioCurveRendering.DrawCurve(r, delegate(float x) { return(scale * buffer[(int)Mathf.Floor(x * (numsamples - 2))]); }, lineColor); GUIHelpers.DrawTimeTickMarks(r, window, Color.red, new Color(1.0f, 0.0f, 0.0f, 0.25f)); } } else { scale *= 0.1f; for (int i = 0; i < maxspeclen; i++) { float v = 20.0f * Mathf.Log10(buffer[i] + 0.0001f) * scale; spec[i] = new Color( Mathf.Clamp(v * 4.0f - 1.0f, 0.0f, 1.0f), Mathf.Clamp(v * 4.0f - 2.0f, 0.0f, 1.0f), 1.0f - Mathf.Clamp(Mathf.Abs(v * 4.0f - 1.0f), 0.0f, 1.0f) * Mathf.Clamp(4.0f - 4.0f * v, 0.0f, 1.0f), 1.0f); } if (spectex[channel] == null) { spectex[channel] = new Texture2D(maxspeclen, scopeheight); } specpos[channel] = (specpos[channel] + 1) % scopeheight; spectex[channel].SetPixels(0, specpos[channel], maxspeclen, 1, spec); spectex[channel].Apply(); Color oldColor = GUI.color; GUI.color = new Color(1.0f, 1.0f, 1.0f, blend); Rect r2 = new Rect(r.x, r.y + specpos[channel], r.width / (window * 0.5f), scopeheight); GUI.DrawTexture(r2, spectex[channel], ScaleMode.StretchToFill, false, 1.0f); r2.y -= scopeheight; GUI.DrawTexture(r2, spectex[channel], ScaleMode.StretchToFill, false, 1.0f); GUI.color = oldColor; GUIHelpers.DrawFrequencyTickMarks(r, samplerate * window * 0.5f, false, Color.red); } } AudioCurveRendering.EndCurveFrame(); return(false); }