Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: PlaySdWav(string)

  1. #1

    PlaySdWav(string)

    Hello,
    I can not use PlaySdWav with a variable and not a "const"
    how must I do?
    Thanks for your help

  2. #2
    En francais
    bonjour,
    je n'arrive pas a utiliser PlaySdWav avec une variable et non une "const"
    comment il faut que je fasse?
    Merci pour votre aide

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,624
    First, to directly answer your question, "const" is not required. You can create the filename in a buffer, for example using sprintf() or using entry.name() as show in File > Examples > SD > listfiles.

    Now, if that isn't the answer you needed, you really must provide more info. Nobody can understand what you need if you do not explain.

    If you have a program which is not working, post the complete code. If we can copy the code into Arduino and click Verify to see the error, we can help you much better.

    Without seeing your code, and without understanding the project you are trying to make, this narrow technical answer is all anyone can do.

  4. #4
    En Francis (l’original):
    désolé,
    dans l'exemple "WavFilePlayer"
    Si je remplace "void playFile(const char *filename)" par "void playFile(String *filename)".
    Il me sort cette erreur :
    WavFilePlayer:106: error: cannot convert 'const char*' to 'String*' for argument '1' to 'void playFile(String*)'
    playFile("SDTEST1.WAV"); // filenames are always uppercase 8.3 format

    j'ai egalement essyer par "void playFile(String filename)" mais il me met cette erreur : WavFilePlayer:89: error: no matching function for call to 'AudioPlaySdWav:lay(String&)'
    playWav1.play(filename);

    In English :
    sorry,
    In the example "WavFilePlayer"
    If I replace "void playFile (const char * filename)" with "void playFile (String * filename)"
    It gets me this error :
    "WavFilePlayer: 106: error: can not convert 'const char *' to 'String *' for argument '1' to 'void playFile (String *)'
    *** playFile ("SDTEST1.WAV"); // filenames are always uppercase 8.3 format "

    I also tried by "void playFile (String filename)" but it gives me this error:
    WavFilePlayer: 89: error: no matching function for call to 'AudioPlaySdWav :: play (String &)'
    *** playWav1.play (filename);

  5. #5
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,626
    There is a difference between a String (which is an object), an array of chars (which does look like a string but isn't) and a pointer (declared by the asterisk *) towards the char array, holding the file name. So, it's fully ok for the compiler to throw an error if you use a wrong type for a variable.

    Why would you change the type of the filename variable, since the example works without modifications?

    En français
    Il y a une différence entre un objet de type "String", un tableau de caractères (ce qui se présente comme un objet String mais ne l'est pas) et finalement und pointeur vers un tel tableau comprenant le nom du fichier. Il est donc normal que le compilateur se plaigne de l'erreur si tu utilises un type inadapté pour une variable.

    Pourquoi changerais-tu le type de la variable "filename" puisque cet exemple fonctionne bien sans modifications?

  6. #6
    En Français (l’original):
    Là est tout le problème mon but est de pouvoir utiliser une variable et non une constante pour choisir le .wav a jouer, je prend cette exemple de programme c'est juste pour comprendre commen faire pour d'autre programmes

    In English
    The problem is my goal is to use a variable and not a constant to choose the .wav to play, I take this example of program is just to understand how to do it for other programs

  7. #7
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,626
    Then, you should use the example which Paul cited in post #3 above and use code from the list files example to obtain qualified filenames of all your audio files on the SD card.

    Another way would be to declare the filename which you want to use in an appropriate format (upper case, 8.3) and with the correct type (as char pointer):
    Code:
    char *monfichier = "MONAUDIO.WAV";
    playWav1.play(monfichier);

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,624
    Quote Originally Posted by Armadafg View Post
    In the example "WavFilePlayer"
    If I replace "void playFile (const char * filename)" with "void playFile (String * filename)"
    It gets me this error :
    "WavFilePlayer: 106: error: can not convert 'const char *' to 'String *' for argument '1' to 'void playFile (String *)'
    *** playFile ("SDTEST1.WAV"); // filenames are always uppercase 8.3 format "
    Please post the COMPLETE code. We can help much more when we see the complete code producing this error.

    Here is one way to solve the problem.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioPlaySdWav           playWav1;
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    AudioOutputI2S           audioOutput;
    //AudioOutputSPDIF       audioOutput;
    //AudioOutputAnalog      audioOutput;
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    AudioControlSGTL5000     sgtl5000_1;
    
    // Use these with the Teensy Audio Shield
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    // Use these with the Teensy 3.5 & 3.6 SD card
    //#define SDCARD_CS_PIN    BUILTIN_SDCARD
    //#define SDCARD_MOSI_PIN  11  // not actually used
    //#define SDCARD_SCK_PIN   13  // not actually used
    
    // Use these for the SD+Wiz820 or other adaptors
    //#define SDCARD_CS_PIN    4
    //#define SDCARD_MOSI_PIN  11
    //#define SDCARD_SCK_PIN   13
    
    void setup() {
      Serial.begin(9600);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(8);
    
      // Comment these out if not using the audio adaptor board.
      // This may wait forever if the SDA & SCL pins lack
      // pullup resistors
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        // stop here, but print a message repetitively
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    }
    
    void playFile(String filename)
    {
      Serial.print("Playing file: ");
      Serial.println(filename);
    
      // Start playing the file.  This sketch continues to
      // run while the file plays.
      playWav1.play(filename.c_str());
    
      // A brief delay for the library read WAV info
      delay(5);
    
      // Simply wait for the file to finish playing.
      while (playWav1.isPlaying()) {
        // uncomment these lines if you audio shield
        // has the optional volume pot soldered
        //float vol = analogRead(15);
        //vol = vol / 1024;
        // sgtl5000_1.volume(vol);
      }
    }
    
    
    void loop() {
      playFile(String("SDTEST1.WAV"));  // filenames are always uppercase 8.3 format
      delay(500);
      playFile(String("SDTEST2.WAV"));
      delay(500);
      playFile(String("SDTEST3.WAV"));
      delay(500);
      playFile(String("SDTEST4.WAV"));
      delay(1500);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •