Issue with audio.h in esp32

Try renaming your src/audio.h file to something else.

oh my goodness, dah!, that was a sample audio file got put there by mistake. Thanks for your help.

I’m actually trying to store the audio in SPIFF that I got from And play the audio by reading from SPIFF. I’m struggling to save the file in the correct format so it can be playback, any suggestion?

Some predetermined audio file thaht you place in the SPIFFS? Per example

you should be store the audio in either MP3, AAC or FLAC format (see decoders in library source) in the SPIFFSa and use code above, while changing SD to SPIFFS of course, and .begin()-ing it before. Simply creating a folder called data, putting the file in there and using the ‘Upload File System Image’ will put it on the ESP, as documented. Careful however that you do not surpass the SPIFFS partiion limit as defined by the partition table (or change it otherwise).

Why make it two-stage? The Audio API already has the code to fetch and play it, no itermediate storage required.

I’m just exploring how I can store short sentence response I got from such as saving it as mp3 encoded or decoded. But when I tried to read from SPIFFS and play the audio, nothing gets played.

obj = JSON.parse(payload);
mp3Value = obj["audioContent"];
char buffer[sizeof(mp3Value)];
memcpy(buffer, &mp3Value, sizeof(mp3Value));
unsigned char * decoded = base64_decode((const unsigned char *)buffer, sizeof(buffer), &outputLength);

// file.println(mp3Value);
file.write((uint8_t *)decoded, sizeof(decoded));

This doesn’t look like it’s saving the entire MP3 answer, but rather the size of some static type. Of what type is mp3Value? Are you sure the memory is even sufficient for first requesting it a JSON response, then parsing the entire JSON response, then allocating a huge buffer for the Base64 decoded value, and then storing it?

You may want to use a streaming HTTP client to read it blockwise in a file, and decoding it later or on-the-fly, like arduino-esp32/StreamHttpClient.ino at master · espressif/arduino-esp32 · GitHub

Yes, I have a custom csv for board_build.partitions to allocate enough space for SPIFFS. It’s storing the response content, like you said, it may not be storing the entire content, could be something wrong with my logic. The response json will look something like this

“audioContent”: "//NExAASUoIEABhGudERHFu6E7u8QiASmN4AAB/4x5AB//8AP9z3dz3RELdz/+8T+Ju57nERC6hV/RP3f+I//…

If you have the full Base64 string you can always decode it yourself, either online or through the b64decode commandline utility. Does that result in a playable / recognizable mp3 file? If not, the download logic may be incomplete.

On second thought, the code I referenced above with connecttospeach pretty much already does what you want, maybe you can copy it and slightly modify it sothat it doesn’t decode it directly (by writing it to the MP3 buffer), you can redirect it to the SPIFFS file instead. Then there’s also no base64 decoding involved because the client requests

already in MP3 format.

Thanks @maxgerhardt, I took a look at the reference code.

Testing out from Postman with the follow and saving to a file it works

I coded the following but what it prints out is not quite the same as the output from Postman. And I also want to be able to write the proper output to a file using SPIFFS. Do you see anything wrong?

String httpGetRequest(String msg, String lang) {
  String payload = "{}";

  String tts =  ttsServer + "?ie=UTF-8&q=" + urlencode(msg) +
                "&tl=" + lang + "&client=tw-ob";

  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    int len = http.getSize();
    uint8_t buff[128] = {0};
    WiFiClient *stream = http.getStreamPtr();
    // read all data from server
    while(http.connected() && (len > 0 || len == -1)) {
        // get available data size
        size_t size = stream->available();

        if(size) {
          while(size > 0) {
            // read up to 128 byte
            int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

            // write it to Serial
            // Serial.printf("\n%d, %d, %d, %d\n", c, size, sizeof(buff), len);
            // Serial.printf("%s", buff);
            Serial.write(buff, c);
            size -= c;
            if(len > 0) {
                len -= c;
    // payload = http.getString();
    // Serial.println(payload);
  else {
    Serial.print("Error code: ");
  // Free resources
  return payload;

Output from Postman

Output from code