private void DrawCurve(Rect r, float[] curve, float yoffset, float yscale, Color col, float labeloffset)
    {
        int   numsamples       = curve.Length - 1; // the last sample in buffer is counter for how much we could read
        float transitionPixels = 30.0f;
        float x0      = 1.0f - ((float)curve[numsamples] / (float)curve.Length);
        float xr      = transitionPixels / r.width;
        float xscale  = numsamples - 2;
        float thr_dB  = -120.0f;
        float 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)
        {
            float f      = Mathf.Clamp(x * xscale, 0.0f, xscale);
            color        = col;
            color.a      = col.a * Mathf.Clamp(x - x0, 0, xr);
            int i        = (int)Mathf.Floor(f);
            float mag    = curve[i] + (curve[i + 1] - curve[i]) * (f - i);
            float mag_dB = (mag < thr_lin) ? thr_dB : (20.0f * Mathf.Log10(mag));
            float 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);
        }
    }
Esempio n. 2
0
    public void DrawControl(IAudioEffectPlugin plugin, Rect r, float samplerate)
    {
        Event     evt       = Event.current;
        int       controlID = GUIUtility.GetControlID(FocusType.Passive);
        EventType 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);
            float x       = r.x + r.width * (ofs - rofs);
            float mindist = Mathf.Abs(x - evt.mousePosition.x);
            dragOperation = DragOperation.RandomOffset;
            x             = r.x;
            for (DragOperation i = DragOperation.Offset; i <= DragOperation.RandomWindowLen; i++)
            {
                float value;
                plugin.GetFloatParameter(DragParams[(int)i], out value);
                x += r.width * value;
                float 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)
        {
            float blend = plugin.IsPluginEditableAndEnabled() ? 1.0f : 0.5f;

            int     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);

            float x1 = r.x + r.width * (ofs - rofs);
            float x2 = r.x + r.width * ofs;
            float x3 = x2 + r.width * wlen;
            float 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);

            string name = "Sample: " + Marshal.PtrToStringAnsi(Granulator_GetSampleName((int)useSample));
            GUIHelpers.DrawText(r2.x + 5, r2.y - 5, r2.width, name, Color.white);
        }
        AudioCurveRendering.EndCurveFrame();
    }