Below is c# code we just made together that will allow editing for audio files and playback of edits and stuff its all fine other than when I told you to add zooming in and out on the waveform with the scrollwheel up and down up for zoom down for but the results are when zooming too far in the entire waveform is just black and also there's no ability to zoom back out that part isnt working but zooming in is working minus going black when zooming too far in. Fix the code and return the entire code thanks.

using System;
using System.Drawing;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using Timer = System.Windows.Forms.Timer;

namespace ByteBreath
{
    // DoubleBufferedPanel to reduce flickering
    public class DoubleBufferedPanel : Panel
    {
        public DoubleBufferedPanel()
        {
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.UserPaint |
                          ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();
        }
    }

    public partial class AudioEditorForm : Form
    {
        private float[] audioSamples;      // normalized samples (-1 to 1)
        private WaveFormat waveFormat;
        private string currentFilePath;

        private bool isSelecting = false;
        private int selectionStart = -1;
        private int selectionEnd = -1;

        private int sampleRate;

        private Button btnLoad;
        private Button btnDelete;
        private Button btnSave;
        private Button btnPlayPause;
        private DoubleBufferedPanel waveformPanel;

        private WaveOutEvent waveOut;
        private AudioFileReader audioFileReader;
        private BufferedWaveProvider bufferedWaveProvider;

        private Timer playbackTimer;
        private long playStartSample = 0;
        private long playEndSample = 0;
        private bool isPlaying = false;

        private int viewStartSample = 0;
        private int viewSamples = 0; // number of samples currently displayed in waveform
        private const int minViewSamples = 1000;  // minimum zoom level (number of samples shown)
        private const int maxViewSamplesFactor = 1; // factor of audioSamples.Length to max zoom out

        public AudioEditorForm()
        {
            TopMost = true;

            InitializeComponent();

            this.Text = "Simple Audio Editor";
            this.Width = 800;
            this.Height = 400;

            btnLoad = new Button()
            {
                Text = "Load WAV",
                Left = 10,
                Top = 10,
                Width = 100
            };
            btnLoad.Click += BtnLoad_Click;
            this.Controls.Add(btnLoad);

            btnDelete = new Button()
            {
                Text = "Delete Selection",
                Left = 120,
                Top = 10,
                Width = 120,
                Enabled = false
            };
            btnDelete.Click += BtnDelete_Click;
            this.Controls.Add(btnDelete);

            btnSave = new Button()
            {
                Text = "Save",
                Left = 250,
                Top = 10,
                Width = 100,
                Enabled = false
            };
            btnSave.Click += BtnSave_Click;
            this.Controls.Add(btnSave);

            btnPlayPause = new Button()
            {
                Text = "Play",
                Left = 360,
                Top = 10,
                Width = 100,
                Enabled = false
            };
            btnPlayPause.Click += BtnPlayPause_Click;
            this.Controls.Add(btnPlayPause);

            waveformPanel = new DoubleBufferedPanel()
            {
                Left = 10,
                Top = 50,
                Width = this.ClientSize.Width - 20,
                Height = this.ClientSize.Height - 60,
                BorderStyle = BorderStyle.FixedSingle,
                BackColor = Color.Black,
                Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
            };
            waveformPanel.Paint += WaveformPanel_Paint;
            waveformPanel.MouseDown += WaveformPanel_MouseDown;
            waveformPanel.MouseMove += WaveformPanel_MouseMove;
            waveformPanel.MouseUp += WaveformPanel_MouseUp;
            waveformPanel.MouseWheel += WaveformPanel_MouseWheel;
            waveformPanel.MouseEnter += WaveformPanel_MouseEnter;
            waveformPanel.MouseLeave += WaveformPanel_MouseLeave;
            this.Controls.Add(waveformPanel);

            playbackTimer = new Timer();
            playbackTimer.Interval = 30;
            playbackTimer.Tick += PlaybackTimer_Tick;

            this.MouseWheel += AudioEditorForm_MouseWheel;
        }

        private bool isMouseInWaveform = false;

        private void WaveformPanel_MouseEnter(object sender, EventArgs e)
        {
            isMouseInWaveform = true;
            waveformPanel.Focus();
        }

        private void WaveformPanel_MouseLeave(object sender, EventArgs e)
        {
            isMouseInWaveform = false;
        }

        private void AudioEditorForm_MouseWheel(object sender, MouseEventArgs e)
        {
            if (isMouseInWaveform)
            {
                ZoomWaveform(e.Location);
            }
        }

        private void WaveformPanel_MouseWheel(object sender, MouseEventArgs e)
        {
            if (isMouseInWaveform)
            {
                ZoomWaveform(e.Location);
            }
        }

        private void ZoomWaveform(Point mouseLocation)
        {
            if (audioSamples == null || audioSamples.Length == 0)
                return;

            int w = waveformPanel.ClientSize.Width;

            // Initialize viewSamples if 0 (e.g. just loaded)
            if (viewSamples == 0)
            {
                viewSamples = audioSamples.Length;
                viewStartSample = 0;
            }

            int oldViewSamples = viewSamples;

            int delta = 0;

            if (Control.ModifierKeys.HasFlag(Keys.Control))
            {
                // If Control is held down, zoom more aggressively
                delta = -System.Math.Sign(MousePositionToWheelDelta());
            }
            else
            {
                delta = -System.Math.Sign(MousePositionToWheelDelta());
            }

            int scrollDelta = delta * 2000; // zoom factor (samples)

            // Clamp viewSamples
            int minSamples = minViewSamples;
            int maxSamples = audioSamples.Length * maxViewSamplesFactor;
            if (maxSamples < minSamples) maxSamples = minSamples;

            int newViewSamples = viewSamples + scrollDelta;
            if (newViewSamples < minSamples) newViewSamples = minSamples;
            if (newViewSamples > maxSamples) newViewSamples = maxSamples;

            if (newViewSamples == viewSamples) return;

            // Calculate sample corresponding to mouse X in audio coordinates before zooming
            double mouseRatio = (double)mouseLocation.X / w;
            long mouseSample = viewStartSample + (long)(mouseRatio * viewSamples);

            // Calculate new viewStartSample to keep mouseSample at same relative location
            long newViewStart = mouseSample - (long)(mouseRatio * newViewSamples);

            if (newViewStart < 0) newViewStart = 0;
            if (newViewStart + newViewSamples > audioSamples.Length)
                newViewStart = audioSamples.Length - newViewSamples;

            viewStartSample = (int)newViewStart;
            viewSamples = newViewSamples;

            waveformPanel.Invalidate();
        }

        private int MousePositionToWheelDelta()
        {
            return Control.MousePosition.Y > 0 ? 120 : -120;
        }

        private void BtnLoad_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "WAV files (*.wav)|*.wav";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    currentFilePath = ofd.FileName;
                    LoadWaveFile(currentFilePath);
                }
            }
        }

        private void LoadWaveFile(string path)
        {
            try
            {
                waveOut?.Stop();
                waveOut?.Dispose();
                waveOut = null;
                audioFileReader?.Dispose();
                audioFileReader = null;
                bufferedWaveProvider = null;
                isPlaying = false;
                btnPlayPause.Text = "Play";

                audioFileReader = new AudioFileReader(path);
                waveFormat = audioFileReader.WaveFormat;
                sampleRate = waveFormat.SampleRate;

                var wholeFile = new float[audioFileReader.Length / 4];
                audioFileReader.Position = 0;
                int offset = 0;
                int samplesRead;
                var buffer = new float[1024];
                while ((samplesRead = audioFileReader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Array.Copy(buffer, 0, wholeFile, offset, samplesRead);
                    offset += samplesRead;
                }

                audioSamples = wholeFile;

                audioFileReader.Position = 0;

                selectionStart = -1;
                selectionEnd = -1;

                viewStartSample = 0;
                viewSamples = audioSamples.Length;

                btnDelete.Enabled = false;
                btnSave.Enabled = true;
                btnPlayPause.Enabled = true;

                playStartSample = 0;
                playEndSample = audioSamples.Length - 1;

                waveOut = new WaveOutEvent();
                waveOut.PlaybackStopped += WaveOut_PlaybackStopped;

                bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
                bufferedWaveProvider.BufferLength = audioSamples.Length * waveFormat.BlockAlign;
                bufferedWaveProvider.ClearBuffer();

                // Fill buffer with whole audio for default playback
                byte[] byteBuffer = new byte[audioSamples.Length * 4];
                Buffer.BlockCopy(audioSamples, 0, byteBuffer, 0, byteBuffer.Length);
                bufferedWaveProvider.AddSamples(byteBuffer, 0, byteBuffer.Length);

                waveOut.Init(bufferedWaveProvider);

                waveformPanel.Invalidate();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load WAV: " + ex.Message);
            }
        }

        private void WaveformPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.Black);
            if (audioSamples == null || audioSamples.Length == 0)
            {
                using (var brush = new SolidBrush(Color.White))
                {
                    e.Graphics.DrawString("No audio loaded", this.Font, brush, new PointF(10, 10));
                }
                return;
            }

            int w = waveformPanel.ClientSize.Width;
            int h = waveformPanel.ClientSize.Height;

            using (var pen = new Pen(Color.White))
            {
                if (viewSamples == 0)
                    viewSamples = audioSamples.Length;
                if (viewStartSample + viewSamples > audioSamples.Length)
                    viewStartSample = audioSamples.Length - viewSamples;

                int samplesPerPixel = Math.Max(1, viewSamples / w);

                for (int x = 0; x < w; x++)
                {
                    int startSample = viewStartSample + x * samplesPerPixel;
                    if (startSample > audioSamples.Length - 1)
                        break;
                    int endSample = Math.Min(audioSamples.Length, startSample + samplesPerPixel);

                    float min = 1f;
                    float max = -1f;
                    for (int s = startSample; s < endSample; s++)
                    {
                        float val = audioSamples[s];
                        if (val < min) min = val;
                        if (val > max) max = val;
                    }

                    int yMax = (int)((1 - (max + 1) / 2) * h);
                    int yMin = (int)((1 - (min + 1) / 2) * h);

                    e.Graphics.DrawLine(pen, x, yMin, x, yMax);
                }
            }

            // Draw selection rectangle if any
            if (selectionStart >= 0 && selectionEnd >= 0 && selectionEnd != selectionStart)
            {
                int wSamples = viewSamples;
                int visibleStart = viewStartSample;
                int x1 = (int)((double)(selectionStart - visibleStart) / wSamples * w);
                int x2 = (int)((double)(selectionEnd - visibleStart) / wSamples * w);

                int rectX = Math.Min(x1, x2);
                int rectW = Math.Abs(x2 - x1);

                if (rectW > 0)
                {
                    using (var selectionBrush = new SolidBrush(Color.FromArgb(100, Color.Blue)))
                    {
                        e.Graphics.FillRectangle(selectionBrush, rectX, 0, rectW, h);
                    }

                    using (var pen = new Pen(Color.Blue, 2))
                    {
                        e.Graphics.DrawRectangle(pen, rectX, 0, rectW, h - 1);
                    }
                }
            }

            // Draw playback position
            if (isPlaying && audioSamples != null && waveOut != null)
            {
                long bytesPerSample = waveFormat.BitsPerSample / 8 * waveFormat.Channels;

                // Get current sample position relative to full audio
                long currentSample = 0;

                try
                {
                    currentSample = waveOut.GetPosition() / bytesPerSample + playStartSample;
                }
                catch
                {
                    currentSample = 0;
                }

                if (currentSample > audioSamples.Length)
                    currentSample = audioSamples.Length;

                long segmentLength = playEndSample - playStartSample + 1;
                if (segmentLength <= 0) segmentLength = audioSamples.Length;

                if (currentSample > playEndSample)
                    currentSample = playEndSample;

                long relativeSamplePos = currentSample - playStartSample;

                int x = 0;

                if (selectionStart >= 0 && selectionEnd >= 0 && selectionStart != selectionEnd)
                {
                    int wSamples = viewSamples;
                    int visibleStart = viewStartSample;

                    int sx1 = (int)((double)(selectionStart - visibleStart) / wSamples * w);
                    int sx2 = (int)((double)(selectionEnd - visibleStart) / wSamples * w);

                    int rectX = Math.Min(sx1, sx2);
                    int rectW = Math.Abs(sx2 - sx1);

                    int selLength = Math.Max(1, Math.Abs(selectionEnd - selectionStart));

                    // Position within selection rectangle
                    if (segmentLength > 0)
                        x = rectX + (int)((double)relativeSamplePos / segmentLength * rectW);
                    else
                        x = rectX;
                }
                else
                {
                    int wSamples = viewSamples;
                    int visibleStart = viewStartSample;
                    // No selection, position across visible waveform
                    x = (int)((double)(currentSample - visibleStart) / wSamples * w);
                }

                if (x >= 0 && x < w)
                {
                    using (var pen = new Pen(Color.Red, 2))
                    {
                        e.Graphics.DrawLine(pen, x, 0, x, h);
                    }
                }
            }
        }

        private void WaveformPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (audioSamples == null) return;

            isSelecting = true;
            int w = waveformPanel.ClientSize.Width;

            int sampleIndex = viewStartSample + (int)((double)e.X / w * viewSamples);
            sampleIndex = Math.Max(0, Math.Min(sampleIndex, audioSamples.Length - 1));

            selectionStart = sampleIndex;
            selectionEnd = sampleIndex;
            waveformPanel.Invalidate();
        }

        private void WaveformPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (audioSamples == null || !isSelecting) return;

            int w = waveformPanel.ClientSize.Width;
            int sampleIndex = viewStartSample + (int)((double)e.X / w * viewSamples);
            sampleIndex = Math.Max(0, Math.Min(sampleIndex, audioSamples.Length - 1));

            selectionEnd = sampleIndex;
            waveformPanel.Invalidate();

            btnDelete.Enabled = Math.Abs(selectionEnd - selectionStart) > 0;
        }

        private void WaveformPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (audioSamples == null) return;

            isSelecting = false;

            if (Math.Abs(selectionEnd - selectionStart) == 0)
            {
                selectionStart = -1;
                selectionEnd = -1;
                btnDelete.Enabled = false;
            }
            else
            {
                btnDelete.Enabled = true;
            }
            waveformPanel.Invalidate();
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (audioSamples == null) return;
            if (selectionStart < 0 || selectionEnd < 0) return;

            int start = Math.Min(selectionStart, selectionEnd);
            int end = Math.Max(selectionStart, selectionEnd);

            if (end == start) return;

            int newLength = audioSamples.Length - (end - start);
            float[] newSamples = new float[newLength];

            Array.Copy(audioSamples, 0, newSamples, 0, start);
            Array.Copy(audioSamples, end, newSamples, start, audioSamples.Length - end);

            audioSamples = newSamples;

            selectionStart = -1;
            selectionEnd = -1;
            btnDelete.Enabled = false;

            waveOut?.Stop();

            bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
            bufferedWaveProvider.BufferLength = audioSamples.Length * waveFormat.BlockAlign;
            bufferedWaveProvider.ClearBuffer();

            byte[] byteBuffer = new byte[audioSamples.Length * 4];
            Buffer.BlockCopy(audioSamples, 0, byteBuffer, 0, byteBuffer.Length);
            bufferedWaveProvider.AddSamples(byteBuffer, 0, byteBuffer.Length);

            waveOut?.Init(bufferedWaveProvider);

            playStartSample = 0;
            playEndSample = audioSamples.Length - 1;

            // Adjust view to show full waveform after deletion
            viewStartSample = 0;
            viewSamples = audioSamples.Length;

            btnPlayPause.Enabled = true;
            btnPlayPause.Text = "Play";
            isPlaying = false;

            waveformPanel.Invalidate();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (audioSamples == null || string.IsNullOrEmpty(currentFilePath)) return;

            try
            {
                using (var writer = new WaveFileWriter(currentFilePath, waveFormat))
                {
                    if (waveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
                    {
                        writer.WriteSamples(audioSamples, 0, audioSamples.Length);
                    }
                    else if (waveFormat.BitsPerSample == 16)
                    {
                        short[] int16Samples = new short[audioSamples.Length];
                        for (int i = 0; i < audioSamples.Length; i++)
                        {
                            float sample = audioSamples[i];
                            sample = Math.Max(-1f, Math.Min(1f, sample));
                            int16Samples[i] = (short)(sample * short.MaxValue);
                        }
                        byte[] bytes = new byte[int16Samples.Length * 2];
                        Buffer.BlockCopy(int16Samples, 0, bytes, 0, bytes.Length);
                        writer.Write(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        MessageBox.Show("Unsupported bit depth: " + waveFormat.BitsPerSample);
                        return;
                    }
                }
                MessageBox.Show("File saved to " + currentFilePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save WAV: " + ex.Message);
            }
        }

        private void BtnPlayPause_Click(object sender, EventArgs e)
        {
            if (audioSamples == null || waveOut == null) return;

            if (!isPlaying)
            {
                if (selectionStart >= 0 && selectionEnd >= 0 && selectionEnd != selectionStart)
                {
                    playStartSample = Math.Min(selectionStart, selectionEnd);
                    playEndSample = Math.Max(selectionStart, selectionEnd);
                }
                else
                {
                    playStartSample = 0;
                    playEndSample = audioSamples.Length - 1;
                }

                waveOut.Stop();

                long bytesPerSample = (waveFormat.BitsPerSample / 8) * waveFormat.Channels;
                long segmentLengthSamples = (playEndSample - playStartSample + 1);
                long lengthBytes = segmentLengthSamples * bytesPerSample;

                if (playStartSample > audioSamples.Length - 1 || segmentLengthSamples <= 0)
                {
                    MessageBox.Show("Invalid selection for playback.");
                    return;
                }

                if (lengthBytes > int.MaxValue)
                {
                    MessageBox.Show("Selection too large for playback.");
                    return;
                }

                int intLengthBytes = (int)lengthBytes;

                bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
                bufferedWaveProvider.BufferLength = intLengthBytes;
                bufferedWaveProvider.ClearBuffer();

                byte[] byteBuffer = new byte[intLengthBytes];

                Buffer.BlockCopy(audioSamples, (int)(playStartSample * 4), byteBuffer, 0, intLengthBytes);
                bufferedWaveProvider.AddSamples(byteBuffer, 0, intLengthBytes);

                waveOut.Init(bufferedWaveProvider);

                waveOut.Play();
                isPlaying = true;
                btnPlayPause.Text = "Pause";
                playbackTimer.Start();
            }
            else
            {
                waveOut.Pause();
                isPlaying = false;
                btnPlayPause.Text = "Play";
                playbackTimer.Stop();
            }
        }

        private void PlaybackTimer_Tick(object sender, EventArgs e)
        {
            if (!isPlaying) return;

            long bytesPerSample = waveFormat.BitsPerSample / 8 * waveFormat.Channels;
            long currentSample = 0;

            try
            {
                if (bufferedWaveProvider != null && waveOut != null)
                {
                    currentSample = waveOut.GetPosition() / bytesPerSample + playStartSample;
                }
                else if (audioFileReader != null)
                {
                    currentSample = audioFileReader.Position / bytesPerSample;
                }
            }
            catch
            {
                currentSample = 0;
            }

            if (currentSample > playEndSample)
            {
                waveOut.Stop();
                return;
            }

            waveformPanel.Invalidate();
        }

        private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
        {
            isPlaying = false;
            btnPlayPause.Text = "Play";
            playbackTimer.Stop();
            waveformPanel.Invalidate();
        }
    }
}