<commit_before>﻿using NoteEditor.Model;
using NoteEditor.Utility;
using System.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace NoteEditor.GLDrawing
{
    public class WaveformRenderer : MonoBehaviour
    {
        [SerializeField]
        Color color;

        void Awake()
        {
            var waveData = new float[500000];
            var skipSamples = 50;
            var lines = Enumerable.Range(0, waveData.Length / skipSamples)
                .Select(_ => new Line(Vector3.zero, Vector3.zero, color))
                .ToArray();


            this.LateUpdateAsObservable()
                .Where(_ => EditorState.WaveformDisplayEnabled.Value)
                .Where(_ => Audio.Source.clip != null)
                .Subscribe(_ =>
                {
                    var timeSamples = Mathf.Min(Audio.SmoothedTimeSamples.Value, Audio.Source.clip.samples - 1);
                    Audio.Source.clip.GetData(waveData, Mathf.RoundToInt(timeSamples));

                    var x = (NoteCanvas.Width.Value / Audio.Source.clip.samples) / 2f;
                    var offsetX = NoteCanvas.OffsetX.Value;
                    var offsetY = 200;

                    var min = NoteCanvas.OffsetX.Value;
                    var max = Screen.width / NoteCanvas.ScaleFactor.Value * 1.3f;

                    for (int li = 0, wi = skipSamples / 2, l = waveData.Length; wi < l; li++, wi += skipSamples)
                    {
                        lines[li].start.x = lines[li].end.x = wi * x + offsetX;
                        lines[li].end.y = waveData[wi] * 45 - offsetY;
                        lines[li].start.y = waveData[wi - skipSamples / 2] * 45 - offsetY;
                        lines[li].start = ConvertUtils.CanvasToScreenPosition(lines[li].start);
                        lines[li].end = ConvertUtils.CanvasToScreenPosition(lines[li].end);

                        var posX = lines[li].start.x;
                        if (min < posX && posX < max)
                        {
                            GLLineDrawer.Draw(lines[li]);
                        }
                    }
                });
        }
    }
}<commit_msg>Speed up the waveform renderer<commit_after>﻿using NoteEditor.Model;
using NoteEditor.Utility;
using System.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace NoteEditor.GLDrawing
{
    public class WaveformRenderer : MonoBehaviour
    {
        [SerializeField]
        Color color;

        void Awake()
        {
            var waveData = new float[500000];
            var skipSamples = 50;
            var lines = Enumerable.Range(0, waveData.Length / skipSamples)
                .Select(_ => new Line(Vector3.zero, Vector3.zero, color))
                .ToArray();

            this.LateUpdateAsObservable()
                .Where(_ => EditorState.WaveformDisplayEnabled.Value)
                .Where(_ => Audio.Source.clip != null)
                .Subscribe(_ =>
                {
                    var timeSamples = Mathf.Min(Audio.SmoothedTimeSamples.Value, Audio.Source.clip.samples - 1);
                    Audio.Source.clip.GetData(waveData, Mathf.RoundToInt(timeSamples));

                    var x = (NoteCanvas.Width.Value / Audio.Source.clip.samples) / 2f;
                    var offsetX = NoteCanvas.OffsetX.Value;
                    var offsetY = 200;
                    var max = Screen.width / NoteCanvas.ScaleFactor.Value * 1.3f;

                    for (int li = 0, wi = skipSamples / 2, l = waveData.Length; wi < l; li++, wi += skipSamples)
                    {
                        var pos = wi * x + offsetX;

                        if (pos > max)
                            break;

                        lines[li].start.x = lines[li].end.x = pos;
                        lines[li].end.y = waveData[wi] * 45 - offsetY;
                        lines[li].start.y = waveData[wi - skipSamples / 2] * 45 - offsetY;
                        lines[li].start = ConvertUtils.CanvasToScreenPosition(lines[li].start);
                        lines[li].end = ConvertUtils.CanvasToScreenPosition(lines[li].end);
                        GLLineDrawer.Draw(lines[li]);
                    }
                });
        }
    }
}
{'fraction_non_alphanumeric': 0.10516387644423485, 'fraction_numerical': 0.01178967224711153, 'mean_word_length': 1.4115974985787378, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4616477', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1102, 'n_words': 232}