Please professionalize the script and add easy-to-understand comments

using UnityEngine;
using UnityEngine.UI;

public class WaveformRenderer : MonoBehaviour
{
    [Header("References")]
    [Tooltip("UI Image component where the waveform will be rendered")]
    public Image waveformImage;

    [Tooltip("Audio clip to render the waveform from")]
    public AudioClip audioClip;

    [Header("Waveform Settings")]
    [Tooltip("Color of the waveform")]
    public Color waveformColor = Color.green;

    public void RenderWaveform()
    {
        if (audioClip == null)
        {
            Debug.LogError("[WaveformRenderer] AudioClip is not assigned!");
            return;
        }

        if (waveformImage == null)
        {
            Debug.LogError("[WaveformRenderer] UI Image component is not assigned!");
            return;
        }

        // Get texture size from Image's RectTransform size in pixels.
        RectTransform rt = waveformImage.rectTransform;

        // Canvas may be scaled, convert size to absolute pixel size on screen.
        Vector2 size = new Vector2(rt.rect.width * rt.lossyScale.x, rt.rect.height * rt.lossyScale.y);

        int width = Mathf.Max(1, Mathf.RoundToInt(size.x));
        int height = Mathf.Max(1, Mathf.RoundToInt(size.y));

        Texture2D waveformTexture = GenerateWaveformTexture(audioClip, width, height, waveformColor);
        ApplyTextureToImage(waveformTexture);
    }

    private Texture2D GenerateWaveformTexture(AudioClip clip, int width, int height, Color color)
    {
        Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);

        Color32 transparent = new Color32(0, 0, 0, 0);
        Color32[] clearColors = new Color32[width * height];
        for (int i = 0; i < clearColors.Length; i++)
            clearColors[i] = transparent;

        texture.SetPixels32(clearColors);

        float[] samples = new float[clip.samples * clip.channels];
        clip.GetData(samples, 0);

        int channels = clip.channels;
        int midHeight = height / 2;
        int samplesPerPixel = Mathf.Max(samples.Length / channels / width, 1);

        for (int x = 0; x < width; x++)
        {
            int start = x * samplesPerPixel * channels;
            int end = Mathf.Min(start + samplesPerPixel * channels, samples.Length);

            float maxAmplitude = 0f;
            for (int i = start; i < end; i += channels)
            {
                float amplitude = Mathf.Abs(samples[i]);
                if (amplitude > maxAmplitude)
                    maxAmplitude = amplitude;
            }

            int amplitudeHeight = (int)(maxAmplitude * midHeight);

            // Draw vertical line
            for (int y = midHeight - amplitudeHeight; y <= midHeight + amplitudeHeight; y++)
            {
                if (y >= 0 && y < height)
                    texture.SetPixel(x, y, color);
            }
        }

        texture.Apply();
        return texture;
    }

    private void ApplyTextureToImage(Texture2D texture)
    {
        Sprite newSprite = Sprite.Create(texture,
            new Rect(0, 0, texture.width, texture.height),
            new Vector2(0.5f, 0.5f));

        waveformImage.sprite = newSprite;
        waveformImage.color = Color.white;
        waveformImage.preserveAspect = true;
    }
}