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 https://texttospeech.googleapis.com/v1/text:synthesize. 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 https://texttospeech.googleapis.com/v1/text:synthesize 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);
Serial.println(buffer);

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

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/libraries/HTTPClient/examples/StreamHttpClient/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

Successā€¦{
ā€œ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
ā€œhttps://translate.google.com/translate_tts?ie=UTF-8&tl=en_US&client=tw-ob&q=hello%20worldā€

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";

  http.begin(tts.c_str());
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    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;
            }
          }
        }
        
        delay(1);
    }    
    // payload = http.getString();
    // Serial.println(payload);
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();
  return payload;
}

Output from Postman

Output from code