void DrawGraph(MidiEnvelope env, float width, float height, int controlID)
        {
            const float scale = 2;

            // Time parameters
            var t1 = scale * env.AttackTime;
            var t2 = t1 + scale * env.DecayTime;
            var t3 = t2 + scale * 0.2f;
            var t4 = t3 + scale * env.ReleaseTime;

            // Position parameters
            var x1    = 1 + width * t1;
            var x2    = 1 + width * t2;
            var x3    = 1 + width * t3;
            var x4    = 1 + width * t4;
            var sus_y = (1 - env.SustainLevel) * (height - 2) + 1;

            // ADSR graph vertices
            _graphVerts[0] = new Vector3(1, height);
            _graphVerts[1] = new Vector3(x1, 1);
            _graphVerts[2] = new Vector3(x2, sus_y);
            _graphVerts[3] = new Vector3(x3, sus_y);
            _graphVerts[4] = new Vector3(x4, height - 1);
            _graphVerts[5] = new Vector3(width, height - 1);

            // Background
            EditorGUI.DrawRect(new Rect(0, 0, width, height), backgroundColor);

            // Guide elements
            var focus = GUIUtility.keyboardControl;

            if (focus == controlID)
            {
                EditorGUI.DrawRect(new Rect(0, 0, x1, height), highlightColor);
            }
            else if (focus == controlID + 1)
            {
                EditorGUI.DrawRect(new Rect(x1, 0, x2 - x1, height), highlightColor);
            }
            else if (focus == controlID + 2)
            {
                EditorGUI.DrawRect(new Rect(0, sus_y, width, height), highlightColor);
            }
            else if (focus == controlID + 3)
            {
                EditorGUI.DrawRect(new Rect(x3, 0, x4 - x3, height), highlightColor);
            }

            Handles.color = guideColor;
            DrawAALine(x1, 0, x1, height);
            DrawAALine(x2, 0, x2, height);
            DrawAALine(x3, 0, x3, height);
            DrawAALine(x4, 0, x4, height);
            DrawAALine(0, sus_y, width, sus_y);

            // ADSR graph
            Handles.color = LineColor;
            Handles.DrawAAPolyLine(_graphVerts);
        }
Beispiel #2
0
    public void OnNoteOn(int channeIndex, byte rawKey, byte rawVel)
    {
        if (channeIndex != _channelIndex)
        {
            return;
        }

        // Try to find an inactive envelope.
        MidiEnvelope env = System.Array.Find(_envelopePlayerPool, e => !e.isActive);

        if (env == null)
        {
            return;
        }

        // Try to find an inactive visualisation object.
        Transform noteViz = System.Array.Find(_noteVizPool, n => !n.gameObject.activeSelf);

        if (!noteViz)
        {
            return;
        }

        // Copy envelope settings.
        env.CopyFrom(_envelopeSetting);

        // Start the envelope.
        env.NoteOn();

        // Use the channel info object to normalize and map incoming midi values.
        float x = Mathf.InverseLerp(_channelInfo.noteKeyMin, _channelInfo.noteKeyMax, rawKey) * 2 - 1;

        // Apply to visualisation object and activate.
        noteViz.position = Vector3.right * x * 3;
        noteViz.gameObject.SetActive(true);

        // Store in active list.
        _noteVizEnvPlaPairs.Add(new KeyValuePair <Transform, MidiEnvelope>(noteViz, env));
        _envelopPlayerLookup.Add(rawKey, env);
    }