Example #1
0
    void DrawMinMax(Rect waveArea, WaveDisplayState displayState)
    {
        UnityEditor.Handles.color = new Color(1f, 104f / 255f, 0f, KoreographerColors.HandleFullAlpha);

        int amplitude = (int)(channelAmplitudePercent * (waveArea.height / 2f));

        Vector2 startPoint = Vector2.zero;
        Vector2 endPoint = Vector2.zero;

        float minSample, maxSample, curSample;
        int sampleIdxForPixel;

        int cacheIdxOffset = displayState.firstSamplePackToDraw / displayState.samplesPerPixel;

        for (int i = 0; i < waveArea.width && displayState.firstSamplePackToDraw + (i * displayState.samplesPerPixel) < sampleData.Length; ++i)
        {
            WaveformCacheEntry entry;

            if (cachedData[cacheIdxOffset + i] == null)
            {
                entry = new WaveformCacheEntry();
                cachedData[cacheIdxOffset + i] = entry;
            }
            else
            {
                entry = cachedData[cacheIdxOffset + i];
            }

            if (!entry.minMaxValid)
            {
                minSample = 1f;
                maxSample = -1f;

                sampleIdxForPixel = i * displayState.samplesPerPixel;

                for (int j = 0; j < displayState.samplesPerPixel && displayState.firstSamplePackToDraw + sampleIdxForPixel + j < sampleData.Length; j++)
                {
                    curSample = sampleData[displayState.firstSamplePackToDraw + sampleIdxForPixel + j];
                    minSample = Mathf.Min(minSample, curSample);
                    maxSample = Mathf.Max(maxSample, curSample);
                }

                // Subtract because positive is down!
                entry.minMaxValues.x = waveArea.center.y - (maxSample * amplitude);
                entry.minMaxValues.y = waveArea.center.y - (minSample * amplitude);

                // Update the cache entry!
                entry.minMaxValid = true;
            }

            // Draw a vertical line.
            startPoint.x = waveArea.x + i;
            endPoint.x = startPoint.x;

            startPoint.y = entry.minMaxValues.x;
            endPoint.y = entry.minMaxValues.y;

            UnityEditor.Handles.DrawLine(startPoint, endPoint);
        }
    }
Example #2
0
    void DrawRMS(Rect waveArea, WaveDisplayState displayState)
    {
        UnityEditor.Handles.color = new Color(1f, 0.58431f, 0f, KoreographerColors.HandleFullAlpha);

        int amplitude = (int)(channelAmplitudePercent * (waveArea.height / 2f));

        Vector2 startPoint = Vector2.zero;
        Vector2 endPoint = Vector2.zero;

        float curSample, peak;
        int sampleIdxForPixel;

        int cacheIdxOffset = displayState.firstSamplePackToDraw / displayState.samplesPerPixel;

        // Calculate the waveform via RMS!
        for (int i = 0; i < waveArea.width && displayState.firstSamplePackToDraw + (i * displayState.samplesPerPixel) < sampleData.Length; ++i)
        {
            WaveformCacheEntry entry;

            if (cachedData[cacheIdxOffset + i] == null)
            {
                entry = new WaveformCacheEntry();
                cachedData[cacheIdxOffset + i] = entry;
            }
            else
            {
                entry = cachedData[cacheIdxOffset + i];
            }

            if (!entry.rmsValid)
            {
                peak = 0;

                sampleIdxForPixel = i * displayState.samplesPerPixel;

                for (int j = 0; j < displayState.samplesPerPixel && displayState.firstSamplePackToDraw + sampleIdxForPixel + j < sampleData.Length; j++)
                {
                    curSample = sampleData[displayState.firstSamplePackToDraw + sampleIdxForPixel + j];

                    peak += (curSample * curSample);
                }

                peak = Mathf.Sqrt(peak / (float)displayState.samplesPerPixel);

                entry.rmsValues.x = waveArea.center.y - (peak * amplitude);
                entry.rmsValues.y = waveArea.center.y + (peak * amplitude);

                // Update the cache entry!
                entry.rmsValid = true;
            }

            // Draw vertical lines.
            startPoint.x = waveArea.x + i;
            endPoint.x = startPoint.x;

            startPoint.y = entry.rmsValues.x;
            endPoint.y = entry.rmsValues.y;

            UnityEditor.Handles.DrawLine(startPoint, endPoint);
        }
    }