Search code examples
c#winformstimerpictureboxnaudio

More efficient code for displaying EQ images from value


Basically what im doing is using NAudio to get peak info. I've made a set of 20 images that are going to represent the EQ. I've done some code to check the peak value and change the image in the picturebox in realtime with a timer. Though sometimes I see visual lag and I think the code is quite inefficient. Is there a more efficient code than this large chunk of ifs

private void EQ_TIMER_Tick(object sender, EventArgs e)
    {
        int vol = (int)((float)(device.AudioMeterInformation.MasterPeakValue * 100));
        if ((vol > 0) && (vol < 5))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ5;
        }
        else if ((vol > 5) && (vol < 10))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ10;
        }
        else if ((vol > 10) && (vol < 15))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ15;
        }
        else if ((vol > 15) && (vol < 20))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ20;
        }
        else if ((vol > 20) && (vol < 25))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ25;
        }
        else if ((vol > 25) && (vol < 30))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ30;
        }
        else if ((vol > 30) && (vol < 35))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ35;
        }
        else if ((vol > 35) && (vol < 40))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ40;
        }
        else if ((vol > 40) && (vol < 45))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ45;
        }
        else if ((vol > 45) && (vol < 50))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ50;
        }
        else if ((vol > 50) && (vol < 55))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ55;
        }
        else if ((vol > 55) && (vol < 60))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ60;
        }
        else if ((vol > 60) && (vol < 65))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ65;
        }
        else if ((vol > 65) && (vol < 70))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ70;
        }
        else if ((vol > 70) && (vol < 75))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ75;
        }
        else if ((vol > 75) && (vol < 80))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ80;
        }
        else if ((vol > 80) && (vol < 85))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ85;
        }
        else if ((vol > 85) && (vol < 90))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ90;
        }
        else if ((vol > 90) && (vol < 95))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ95;
        }
        else if ((vol > 95) && (vol < 100))
        {
            WINGS_FRAME.Image = Properties.Resources.RES_EQ100;
        }
        else if (vol == 0)
        {
            WINGS_FRAME.Image = null;
        }
    }

Solution

  • Try something like this:

    int vol = 38;
    int imgnum = vol/5*5;
    string imgname = string.Format("RES_EQ{0}", imgnum);
    
    var image = Properties.Resources.ResourceManager.GetObject(imgname);
    

    Integer division "rounds" the volume to a step number, then multiplying by 5 gives the actual number for the image.