Loading .WAV files into RAM

I need to store wav files into Daisy’s RAM, and then play those files (from RAM) on command.

Is there an example about that?

If not, how could I program this? Do I have to use the SDRAM location from 0xC0000000 to (0xC0000000 + 64Mb) ?

I haven’t found documentation about it

I made a response to the same question on GitHub, but figure it might be nice to post that here as well. So people can follow any further discussion.

The pseudocode is a bit simplified compared to what may work in practice (loading the entire file vs just samples, loading the file in smaller blocks instead of all at once, etc.)


This would make for a great example program, but it does not currently exist.

You can do this a few ways, but the simplest would require you to know roughly what size your files are going to be (or to set up the buffers for the largest expected file(s)).

You can create a buffer of 16-bit integers in the SDRAM with:

int16_t DSY_SDRAM_BSS my_sample[EXPECTED_SAMPLE_COUNT];

and then using fatfs, to read through your wave file can load the file:

disclaimer: I’m writing this off the top of my head. So it may need some tweaking,

if (f_open(&file_object, "myfile.wav", FA_READ) == FR_OK) {
  UINT bytes_read = 0;
  f_read(&file_object, my_sample,  EXPECTED_SAMPLE_COUNT * sizeof(my_sample[0]), &bytes_read);
}

Now, this will copy the entire wav file, and not just the data. You can parse the wav file first to determine the type, size, etc. and then copy only the samples themselves to the buffer(s).

Worth mentioning as well, that streaming performance when using 4-bit I/O with an SD card can be very good (though not as good as the SDRAM interface). So if you need to work with more than 64MB of audio files you can work directly off of an SD Card.

Also worth mentioning that the SDRAM is volatile memory, and does not store its state between power cycles. So you will need to load these files in from somewhere every time the module boots (it is possible to similarly load files into the 8MB QSPI, but the process is slightly different).

I’ll leave this issue open for now as a discussion, and we’ll migrate it into the guide for working with external SDRAM

Hope that helps to answer your question!

4 Likes