Case statement error

I am moving my case statement function around. This case statement i have every file (wav) listed under a.b.c...but im having to add about 100 more audio files to the sd to complete an objective in my work. long story short, i am in the process of changing over to names like below,

case 'alarm':
      file = new AudioFileSourceSD("/wav/alarm.wav");
      break;

    case 'voice_alarm_high':
      file = new AudioFileSourceSD("/wav/alarmhigh.wav");
      break;

    case 'voice_alarm_low':
      file = new AudioFileSourceSD("/wav/alarmlow.wav");
      break;

    case 'error':
      file = new AudioFileSourceSD("/wav/error.wav");
      break;
  
    case 'voice_error':
      file = new AudioFileSourceSD("/wav/error1.wav");
      break;
  
    case 'no_reading':
      file = new AudioFileSourceSD("/wav/noread.wav");
      break;
      
    case 'voice_no_reading':
      file = new AudioFileSourceSD("/wav/noreadings.wav");
      break;
      
    case 'normal_range':
      file = new AudioFileSourceSD("/wav/normalrange.wav");
      break;
      
    case 'warning':
      file = new AudioFileSourceSD("/wav/warning.wav");
      break;

    case 'voice_warning_high':
      file = new AudioFileSourceSD("/wav/warninghigh.wav");
      break;
      
    case 'voice_warning_low':
      file = new AudioFileSourceSD("/wav/warninglow.wav");
      break;
      
    case 'startup_sound':
      file = new AudioFileSourceSD("/wav/startup.wav");
      break;   
      
      case 'voice_update':
      file = new AudioFileSourceSD("/wav/update.wav");
      break;  
      case 'startup_developer':
      file = new AudioFileSourceSD("/wav/startup_dev.wav");  

everything "looks" fine, until i go to compose it, then i get this error log

/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:221:5: error: duplicate case value
     case 'voice_error':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:217:5: error: previously used here
     case 'error':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:229:5: error: duplicate case value
     case 'voice_no_reading':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:225:5: error: previously used here
     case 'no_reading':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:241:5: error: duplicate case value
     case 'voice_warning_high':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:209:5: error: previously used here
     case 'voice_alarm_high':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:245:5: error: duplicate case value
     case 'voice_warning_low':
     ^
C:/Users/Administrator.MYTDS/Documents/PlatformIO/Projects/paci_1248/src/paci2.ino:213:5: error: previously used here
     case 'voice_alarm_low':

I don't know what its talking about, its clearly only used once...does it not allow a word to be repeated??
FULL header file (i run my audio variables in another file)

void playWAV (int bg1248_file_select)
{
  Serial.println("Entered playWAV");
  switch (bg1248_file_select)
  {
    case 'alarm':
      file = new AudioFileSourceSD("/wav/alarm.wav");
      break;

    case 'voice_alarm_high':
      file = new AudioFileSourceSD("/wav/alarmhigh.wav");
      break;

    case 'voice_alarm_low':
      file = new AudioFileSourceSD("/wav/alarmlow.wav");
      break;

    case 'error':
      file = new AudioFileSourceSD("/wav/error.wav");
      break;
  
    case 'voice_error':
      file = new AudioFileSourceSD("/wav/error1.wav");
      break;
  
    case 'no_reading':
      file = new AudioFileSourceSD("/wav/noread.wav");
      break;
      
    case 'voice_no_reading':
      file = new AudioFileSourceSD("/wav/noreadings.wav");
      break;
      
    case 'normal_range':
      file = new AudioFileSourceSD("/wav/normalrange.wav");
      break;
      
    case 'warning':
      file = new AudioFileSourceSD("/wav/warning.wav");
      break;

    case 'voice_warning_high':
      file = new AudioFileSourceSD("/wav/warninghigh.wav");
      break;
      
    case 'voice_warning_low':
      file = new AudioFileSourceSD("/wav/warninglow.wav");
      break;
      
    case 'startup_sound':
      file = new AudioFileSourceSD("/wav/startup.wav");
      break;   
      
      case 'voice_update':
      file = new AudioFileSourceSD("/wav/update.wav");
      break;  
      case 'startup_developer':
      file = new AudioFileSourceSD("/wav/startup_dev.wav");  
      }

float volumeGain = ((float)bg1248_general_volume / 100.0) * 39.0;
  Serial.print("volumeGain:");
  Serial.println(volumeGain);
  id3 = new AudioFileSourceID3(file);
  out = new AudioOutputI2S(0, 0); // Output to builtInDAC
  out->SetPinout(12, 0, 2);
  out->SetOutputModeMono(true);
  out->SetGain(volumeGain);
  wav = new AudioGeneratorWAV();
  wav->begin(id3, out);
}

lastly i want to say sorry to the people i havnt been so friendly with as of late, i have a short fuse when i cant comprehend whats being said. so again sorry,

You are better off using a look up table array for this.
See Arrays

ive reviewed that page before but its not making sense to me.

Hello,
you can make an enumeration for the errors to be used in the switch/case instruction.

I don't follow im sorry, what do you mean?

You need to read it rather than review it. Or read some other stuff about arrays using a search engine, other Googles are available.

ok, i will look into it, thank you.

Hello
Take a view here:

In your other topic on this subject, I wrote that switch/case only works with compile-time integer constants.

'alarm' is what is known as a multi-character constant. it's constant, which is good, and it's an integer type, which is also good.
Unfortunately, the maximum size of a multi-character constant is the size of an int, so on an AVR, just two characters.
So, as far as the compiler is concerned, 'alarm' is exactly the same as 'all_the_young_dudes'

Your right, sorry wasn't paying attention good enough :slight_smile: I have taken advice from others....and i think you mentioned an array as well. I called a friend and we worked on a enum and a double array. I now have both to use....problem is I was controlling volume through a variable and im not sure how to do it now....

love the reference :slight_smile:

Here are the new enum and double array.

enum soundEffects {
    alarm,
    alarm_high,
    alarm_low,
    Dev_startup,
    error,
    error1,
    noread,
    noreadings,
    normalrange,
    startup,
    startup_dev,
    update,
    warning,
    warning_high,
    warning_low,
};

void playWav (enum soundEffects effect, int volume)
{
    //Serial.println("Entered paciplay_day");
    //printf("Entered paciplay_day\n");

    switch (effect)
    {
        case alarm:
            //file = new AudioFileSourceSD("/wav/alarm.wav");
            printf("/wav/alarm.wav\n");
            break;

        case alarm_high:
            //file = new AudioFileSourceSD("/wav/alarmhigh.wav");
            printf("/wav/alarmhigh.wav\n");
            break;

        case alarm_low:
            //file = new AudioFileSourceSD("/wav/alarmlow.wav");
            printf("/wav/alarmlow.wav\n");
            break;

        case error:
            //file = new AudioFileSourceSD("/wav/error.wav");
            printf("/wav/error.wav\n");
            break;

        case error1:
            //file = new AudioFileSourceSD("/wav/error1.wav");
            printf("/wav/error1.wav\n");
            break;

        case noread:
            //file = new AudioFileSourceSD("/wav/noread.wav");
            printf("/wav/noread.wav\n");
            break;

        case noreadings:
            //file = new AudioFileSourceSD("/wav/noreadings.wav");
            printf("/wav/noreadings.wav\n");
            break;

        case normalrange:
            //file = new AudioFileSourceSD("/wav/normalrange.wav");
            printf("/wav/normalrange.wav\n");
            break;

        case warning:
            //file = new AudioFileSourceSD("/wav/warning.wav");
            printf("/wav/warning.wav\n");
            break;

        case warning_high:
            //file = new AudioFileSourceSD("/wav/warninghigh.wav");
            printf("/wav/warninghigh.wav\n");
            break;

        case warning_low:
            //file = new AudioFileSourceSD("/wav/warninglow.wav");
            printf("/wav/warninglow.wav\n");
            break;

        case startup:
            //file = new AudioFileSourceSD("/wav/startup.wav");
            printf("/wav/startup.wav\n");
            break;

        case update:
            //file = new AudioFileSourceSD("/wav/update.wav");
            printf("/wav/update.wav\n");
            break;
    }
}

int main()
{
    //using direct numbers
    for (int i = 0; i<14; i++)
    {
        printf("enum value: %d\n", i);
        playWav(i, 50);
    }

    //directly calling
    playWav(startup, 100);

    // Other way of calling the value
    enum soundEffects sound;
    sound = error;
    playWav(sound, 100);
    return 0;
}

and the double array

void playWav (const char *filename, int volume)
{
    //Serial.println("Entered paciplay_day");
    printf("Entered paciplay_day\n");

    //file = new AudioFileSourceSD("/wav/update.wav");
    printf("/wav/%s\n", filename);
}

int main()
{
    const char list_of_files[][25] = { "startup.wav", "update.wav", "warning.wav", "alarm.wav" };
    for (int i = 0; i<4; i++)
    {
        printf("name: %s\n", list_of_files[i]);
        playWav(list_of_files[i], 50);
    }
    return 0;
}

Now i used to control volume via

float volumeGain = ((float) bg1248_volume/ 100.0) * 39.0;

Serial.print("volumeGain:");

Serial.println(volumeGain);

id3 = new AudioFileSourceID3(file);

out = new AudioOutputI2S(0, 0); // Output to builtInDAC

out->SetPinout(12, 0, 2);

out->SetOutputModeMono(true);

out->SetGain(volumeGain);

wav = new AudioGeneratorWAV();

wav->begin(id3, out);

However im not sure how to do that anymore....

Why are you using printf?
What platform are you using?
On the other topic, I showed a method to stop yourself repeating the string "/wav/.wav", and save precious RAM

that was my friends doing....im assuming he was working with esp32 like i am...it hasnt thrown any warnings during build so idk

Or are you referring to what platform we coded on... Like platform io VS arduino ide

OK I'll go back and look at that.

I am curious if I use the enum. As it's got the file equals new..... In each case I should still be able to use the old function to control the volume right?

I don't really see the connection between those statements, but I'd probably say, yes, you can use the method you used before to control the volume.

Don't forget all those "new"s are going to come back to bite you on the ass, unless you're doing something you're not showing us.

my understanding from the library im using, the new command clears the old file and replaces it with the new one.

Think of it like the advice your mother gave you, of making sure you put on a clean pair of underwear everyday
After a week, you can't get your jeans on over seven pairs of knickers.

...and after two weeks, your knicker drawer is empty.

nice one :slight_smile: yes i do get your point. after so many new events it will cause problems and or crash the app entirely. I just looked it up again to be sure how it worked. it writes the file to a i2s buffer, then when the next file is selected it clears the buffer and fills it back up with the new file..

file = new AudioFileSourceSD("/wav/alarm.wav");

I warned you in one of your other posts about using dynamic allocation when you don't know what you're doing.

Those buffers probably aren't a problem. I'm guess they're continually reused to push data out the I2S port via DMA. The problem is failing to recover the memory that you endlessly allocate for the AudioFileSourceSD objects.

From your last half dozen posts over the past week I know two things: (1) You didn't create that code yourself. (2) You won't post the complete code.

Actually, I wouldn't really want you to do the latter as looking at it would likely make my brain bleed.

So, do this --- Post the section of code where you define the 'file' variable. AND post a link to the complete audio playing code that you copied and attempted to modify for your project.