Ejemplo n.º 1
0
    public void drawWaveform(Rect rect, int channel, Vector2 visibleRange)
    {
        var minX        = rect.x + rect.width * visibleRange.x;
        var maxX        = rect.x + rect.width * visibleRange.y;
        var visibleRect = new Rect(minX, rect.y, maxX - minX, rect.height);

        AudioCurveRendering.AudioMinMaxCurveAndColorEvaluator dlg = delegate(float x, out Color col, out float minValue, out float maxValue) {
            col = Color.yellow;

            x = Mathf.InverseLerp(rect.xMin, rect.xMax, Mathf.Lerp(minX, maxX, x));
            var divisions  = Mathf.FloorToInt(rect.width);
            var groupIndex = Mathf.Clamp(Mathf.FloorToInt(x * divisions), min: 0, max: divisions - 1);
            minMaxInInterval(groupIndex, divisions, channel, out minValue, out maxValue);
        };

        AudioCurveRendering.DrawMinMaxFilledCurve(visibleRect, dlg);
    }
Ejemplo n.º 2
0
        public static void DrawWaveForm(AudioClip clip, int channel, Rect position, float start, float length)
        {
            if (minMaxData != null && clip == storedClip)
            {
                var curveColor  = new Color(255 / 255f, 168 / 255f, 7 / 255f);
                int numChannels = clip.channels;
                int numSamples  = Mathf.FloorToInt(minMaxData.Length / (2f * numChannels) * (length / clip.length));

                AudioCurveRendering.DrawMinMaxFilledCurve(
                    position,
                    delegate(float x, out Color col, out float minValue, out float maxValue) {
                    col     = curveColor;
                    float p = Mathf.Clamp(x * (numSamples - 2), 0.0f, numSamples - 2);
                    int i   = (int)Mathf.Floor(p);
                    float s = (start / clip.length) * Mathf.FloorToInt(minMaxData.Length / (2 * numChannels) - 2);
                    int si  = (int)Mathf.Floor(s);

                    int offset1 = Mathf.Clamp(((i + si) * numChannels + channel) * 2, 0, minMaxData.Length - 2);
                    int offset2 = Mathf.Clamp(offset1 + numChannels * 2, 0, minMaxData.Length - 2);

                    minValue = Mathf.Min(minMaxData[offset1 + 1], minMaxData[offset2 + 1]);
                    maxValue = Mathf.Max(minMaxData[offset1 + 0], minMaxData[offset2 + 0]);
                    if (minValue > maxValue)
                    {
                        float tmp = minValue; minValue = maxValue; maxValue = tmp;
                    }
                }
                    );

                return;
            }

            // If execution has reached this point, the waveform data needs generating
            var path = AssetDatabase.GetAssetPath(clip); if (path == null)
            {
                return;
            }
            var importer = AssetImporter.GetAtPath(path); if (importer == null)
            {
                return;
            }
            var assembly = Assembly.GetAssembly(typeof(AssetImporter)); if (assembly == null)
            {
                return;
            }
            var type = assembly.GetType("UnityEditor.AudioUtil"); if (type == null)
            {
                return;
            }
            var audioUtilGetMinMaxData = type.GetMethod("GetMinMaxData"); if (audioUtilGetMinMaxData == null)

            {
                return;
            }

            storedClip = clip;
            minMaxData = audioUtilGetMinMaxData.Invoke(null, new object[] { importer }) as float[];
            if (minMaxData == null)
            {
                return;
            }

            DrawWaveForm(clip, channel, position, start, length);
        }