switch statement leads to duplicate the function

Hello All,

Although I am getting the output, I want to optimize the code. I have a sampler class and created 3 objects for 3 audio samples example 1.wav,2.wav,3.wav. I am using switch statment whenever my button is pressed 1 time it will access 1.wav pressed 2 time will access 2.wav, 3times will access 3.wav,now the most imp case pressed 4 times will access 1.wav and 2.wav and if pressed 7 times will access 1,2,3.wav.

I said switch duplicate the function because I am accessing the same function only wav files and sampler objects are are changed it is kind of hard coding, sorry if my my coding skills are not so good but I would love to make it more optimized. So, now I am planning to use a universal algorithm which will accessible to all the wave files. please find the code below so that it would be very clear to understand. If my coding approach is wrong please let me know. Thanks in advance

//main.ino file


Sampler1 sampler[samplernumber];
const int samplernumber = 3;

 case 1: {


        // Sound Profile 1 : 

        if (currentSoundProfile != oldProfile) {

          for (int i = 0 ; i < samplernumber ; i++) {

            sampler[i].sstop();
          }

          sampler[0].init();
          sampler[0].load("/SoundP~1/bell.wav");
          sampler[0].splay();
          sampler[0].buffill();
          oldProfile = currentSoundProfile;
          Serial.println(F("Sound Profile ? "));
        } else {
          sampler[0].splay();
          sampler[0].buffill();
          oldProfile = currentSoundProfile;

        }

        //Set Parameters
        // vol     //Speed     // silence
        A = 10;     C = 0;      E = 0;
        B = 0;     D = 1;      F = 1000;

        break;
      }

    case 2 : {
        

        if (currentSoundProfile != oldProfile) {


          for (int i = 0 ; i < samplernumber ; i++) {

            sampler[i].sstop();
          }

          sampler[1].init();
          sampler[1].load("/SoundP~1/steam.wav");
          sampler[1].splay();
          sampler[1].buffill();
          oldProfile = currentSoundProfile;
          Serial.println(F("Sound Profile ? "));
        } else {

          sampler[1].splay();
          sampler[1].buffill();
          oldProfile = currentSoundProfile;

        }

        //Set Parameters A,B,C
        // vol     //Speed     // silence
        A = 1;      C = 20;     E = 1;
        B = 1;      D = 0;     F = 0;




        break;
      }

 case 4: {   MIXing of wav file

        if (currentSoundProfile != oldProfile) {
          for (int i = 0 ; i < samplernumber ; i++) {
            sampler[i].sstop();
          }

          sampler[0].init();
          sampler[0].load("/SoundP~1/bell.wav");
          sampler[0].splay();
          sampler[0].buffill();

          sampler[1].init();
          sampler[1].load("/SoundP~1/ferrari.wav");
          sampler[1].splay();
          sampler[1].buffill();

          int a = sampler[0].num_samples;
          int b = sampler[1].num_samples;


          if (a > b) { // comparing the highest length of samples

            for (int c = b ; c <= a; c++ ) {
              sampler[1].buf[c] = 0;
            }
            sampler[1].num_samples = a;

          } else {

            for (int c = a ; c <= b; c++ ) {
              sampler[0].buf[c] = 0;
            }
            sampler[0].num_samples = b;
          }

          oldProfile = currentSoundProfile;
          Serial.println(F("Sound Profile ? "));

        } else {

          sampler[0].splay();
          sampler[0].buffill();
          sampler[1].splay();
          sampler[1].buffill();
          oldProfile = currentSoundProfile;

        }

        //vol         Speed                 //    sil                 Mix
        A = 1;      C = 20;                E = 1;               H0 = 0;       H1 = 1;
        B = 1;      D = 0;                 F = 0;             H00 = 0.20;      H11 = 0;

        break;
      }
//Sampler Class

  // Init of the sampler object. Here you can put your default values.
    void init()
    {
      play = false;
      openfile = false;
      closefile = false;
    }

    // Play the sample making true
    void splay()
    {
      play = true;
    }


    void load(const char* samplename)
    {
      samplen = samplename;
      closefile = false;
      openfile = true;
    }



  boolean buffill()
    {
      boolean ret = false;
      if (play)
      {
        ret = true;

        // OPEN FILE
        if (openfile)
        {


          myFile = SD.open(samplen, O_READ);


          if (myFile == NULL) {
            printf("Error opening in file\n");
            exit(1);
          }


          // Read header parts

          // Read RIFF
          myFile.read(header.RIFF, sizeof(header.RIFF - 1));


          // Compare RIFF
          if (!strcmp(header.RIFF, "RIFF")) {
            printf("(1-4): %s \n", header.RIFF);

..........................
.........................
........................

 for (int i = 1; i <= num_samples; i++) {
            //                                printf("==========Sample %ld / %ld============= \n", i, num_samples);
            myFile.read(data_buffer, sizeof(data_buffer));
            // convert data from little endian to big endian based on bytes in each channel sample
            data_in_channel =  data_buffer[0] | (data_buffer[1] << 8);

            //                           printf(" %d\n ", data_in_channel);

            //  Data stored in buffer
            buf[i] = ((data_in_channel >> 4 ));

            //                                                            printf("%d\n", buf[i]);


          }


         


          myFile.close();
          openfile = false;



        }

        else {

          play = true;
         
        }

      }
      return ret;

    }

I’d say you are doing something wrong.

First off, it makes absolutely no sense to include code within a Switch case structure which repeats itself identically for every possible case. Code such as

if (currentSoundProfile != oldProfile) {
for (int i = 0 ; i < samplernumber ; i++) {
sampler*.sstop();*

  • }*
    should be put BEFORE the Switch and not copied many times at the beginning of each case.
    Similarly, code such as:
  • oldProfile = currentSoundProfile;*
    could be put once AFTER the Switch structure.
    If that code doesn’t ALWAYS apply (case 3 maybe) then exclude it using an if.
    It makes no sense to test for something and do the same Thing if it is true or not. Therefore:
    ```
  • if (currentSoundProfile != oldProfile) {

for (int i = 0 ; i < samplernumber ; i++) {

sampler[i].sstop();
         }

sampler[0].init();
         sampler[0].load("/SoundP~1/bell.wav");
         sampler[0].splay();
         sampler[0].buffill();
         oldProfile = currentSoundProfile;
         Serial.println(F("Sound Profile ? "));
       } else {
         sampler[0].splay();
         sampler[0].buffill();
         oldProfile = currentSoundProfile;

}*
* *should be reduced to* *
*        if (currentSoundProfile != oldProfile) {

for (int i = 0 ; i < samplernumber ; i++)
            sampler[i].sstop();

sampler[0].init();
          sampler[0].load("/SoundP~1/bell.wav");
          Serial.println(F("Sound Profile ? "));
        }
        sampler[0].splay();
        sampler[0].buffill();
        oldProfile = currentSoundProfile;*
```
Remember that a case can handle multiple cases
You can probably eliminate the entire case 4: if you simple use case 1: case 4: and case 2: case 4: instead. If you do so, you also remove the break Statements so that for case 4 both of the routines are executed.