Using a String to call a sound file - call of overloaded 'playTrack(const char*)

I have a project that uses an Adafruit Sound FX board. I have a project that requires hundreds of audiophiles stored on the sound effects board. Each filename has a number. I would like to be able to play a track by the filename number (not the track number) without needing to have a bunch of switch cases.

I've tried this:

x = 1001;

String fullFilename = (x + "WAV");

sfx.playTrack( fullFilename.c_str());

and I get error:

call of overloaded 'playTrack(const char*) is ambiguous.

What does this mean, and what do I need to change to make this work?

I have a project that requires hundreds of audiophiles stored on the sound effects board.

An audiophile is someone who loves good audio. How are you storing a person on the board?

An audio file, on the other hand...

Each filename has a number.

No, each file has a name that contains the string representation of a number.

x = 1001;

String fullFilename = (x + "WAV");

So, the file name is 1001WAV? That seems unlikely. More likely, the name is 1001.WAV.

In any case, there is no reason to piss away resources on the String class.

char fileName[16];
sprintf(fileName, "%d.wav", x);
sfx.playTrack(fileName);

Remove the . if you REALLY didn't use the . in the file name.

What does this mean

It means that the function takes a char *, not a const char *. The String::c_str() method returns a const char *.

and what do I need to change to make this work?

Quit using Strings. Sledgehammers are not flyswatters.

Thanks, it does what I needed.... But I have to wonder why when I use your method it makes my sketch significantly larger than with 20 switch cases.

But I have to wonder why when I use your method it makes my sketch significantly larger than with 20 switch cases.

Because you are doing something wrong. I presume you know that, which is why you didn't share the code.

Lol. You caught me. Here you go. The code is below. Since you seem fond of jumping to conclusions, I figured I'd also include a screen capture to save you from needing to actually run the sketch for yourself. The sound library is on adafruit's site.

9,480bytes:

          #include <SoftwareSerial.h>
          #include "Adafruit_Soundboard.h"
          #define SFX_TX 10
          #define SFX_RX 11
          #define SFX_RST 12
       
         SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
      Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
      
  int soundtrack = 1007; 

     void setup(){
 
 
  ss.begin(9600);delay(2000);
 
 sfx.reset();
 
}



void loop(){
  
 

char fileName[16];
sprintf(fileName, "%d    WAV", soundtrack);
sfx.playTrack(fileName);
                    
                    
  delay(5000);                
    
                      }

versus 8,192 bytes:

          #include <SoftwareSerial.h>
          #include "Adafruit_Soundboard.h"
          #define SFX_TX 10
          #define SFX_RX 11
          #define SFX_RST 12
       
         SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
      Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
      
  int soundtrack = 1007; 

     void setup(){
 
 
  ss.begin(9600);delay(2000);
 
 sfx.reset();
 
}



void loop(){
  
 
  
  
                    switch (soundtrack){
    
                           case (1001): { sfx.playTrack("1001    WAV");  break;}//Alarm
                           case (1004): { sfx.playTrack("1004    WAV");  break;}// calibrating
                           case (1005): { sfx.playTrack("1005    WAV");  break;}// complete
                           case (1006): { sfx.playTrack("1006    WAV");  break;}// demo mode
                           case (1007): { sfx.playTrack("1007    WAV");  break;}// final snooze
                           case (1008): { sfx.playTrack("1008    WAV");  break;}// off
                           case (1009): { sfx.playTrack("1009    WAV");  break;}// on
                           case (1010): { sfx.playTrack("1010    WAV");  break;}// percent
                           case (1011): { sfx.playTrack("1011    WAV");  break;}// power off
                           case (1012): { sfx.playTrack("1012    WAV");  break;}// release time
                           case (1013): { sfx.playTrack("1013    WAV");  break;}// reset
                           case (1015): { sfx.playTrack("1015    WAV");  break;}// settings saved
                           case (1017): { sfx.playTrack("1017    WAV");  break;}// settings
                           case (1018): { sfx.playTrack("1018    WAV");  break;}// sit time
                           case (1021): { sfx.playTrack("1021    WAV");  break;}// waiting
                           case (1022): { sfx.playTrack("1022    WAV");  break;}// seconds
                           case (1023): { sfx.playTrack("1023    WAV");  break;}// minutes
                           case (1025): { sfx.playTrack("1025    WAV");  break;}// sound
                           case (1026): { sfx.playTrack("1026    WAV");  break;}// snooze
                           case (1027): { sfx.playTrack("1027    WAV");  break;}// connected
    
                    }

                    
  delay(5000);                
    
                      }

Does the second code even work? Spaces in file names are highly unusual in 8.3 format file names.

No dot in the file name is highly unusual in 8.3 format file names.

The spaces without the dot ARE necessary for this board.

Now, how about the differences in sketch sizes? What can you tell me about that?

Just noticed the audiophile thing... that's sire's little joke I guess. This board doesn't have the storage capacity to store audiophiles.

Now, how about the differences in sketch sizes? What can you tell me about that?

sprintf() is a powerful function. Along with that power comes an increase in code size.

But, what you are comparing, size-wise, is not code that you posted initially. The code you posted last does not use the String class, while the first code you posted does.

If you don't need all the power of sprintf(), you could use itoa() to convert the int to a string (unformatted) and then use strlen(), strcpy(), and strcat() to construct the file name. The resulting code will be smaller than with sprintf(), but will be more complex.