xiphonics / picoTracker

BSD 3-Clause "New" or "Revised" License
213 stars 18 forks source link

sample streaming from sdcard #2

Open maks opened 1 year ago

maks commented 1 year ago

It would be very useful to be able to stream samples from the sdcard as this remove current flash size limits on sample sizes. However the performance requirements to do this are significant.

From what I've read of the M8 docs and discussions it seems that the specific sdcard used plays a significant part in available performance and there is a specific list of cards that are recommended by Dirtywave for use with the M8.

Also I know @democloid you've mentioned that you have some ideas about this and using SDIO vs SPI and your previous experiments with using one over the other.

democloid commented 1 year ago

Main issue with this is that the SPI port (nor the PIO peripherals, which is how the SDIO is implemented) are memory mapped, so streaming samples from SD is not as simple as having a pointer to the memory location.

I imagine the way to implement it would require some kind of read ahead buffers (or read back if sample is playing backwards, ping ponging, etc). This adds an additional problem of memory needed for buffers in each channel. Potentially it could be streamed into the play buffers and not need additional memory.

I tried briefly to implement this but wasn't sure how to do it right, I can point in the direction. I think a POC can be implemented using SPI, and if it works right with some short enough and small enough sample, then we could explore if faster access is needed (I imagine so).

maks commented 1 year ago

Thanks for the explanation, to be honest I haven't yet looked into how SDIO works, I've only worked with SPI before, so I'll get myself up to speed with it first! and if you could give me any pointers of what you've tried already that would be great and save me repeating it all myself 🙏🏻 .

My C/C++ is very rusty, so for to get myself back into it I had in mind doing a little beginners exercise (I'll open a new issue for that then a PR) before I dive into a really complex and tricky area like sdcard streaming. I also spotted that you added 16MB of flash to the portable edition PCB, so that certainly makes this less pressing for now as that's a fair bit more samples space to work with! 👍🏻 With the portable PCB, I meant to ask: looking at the kicad schematic, it looks like its wired up the same way with all 6 lines, so there's the option to use either SPI or SDIO?

democloid commented 1 year ago

Yes, using SPI or SDIO is software configurable, but I think the SDIO toggle option might have broken at some point in development, so might have to look into that.

The pointer to the samples basically comes from "Application/Instruments/WavFile.cpp", methods GetBuffer or LoadInFlash (my own). That's where the pointer to the memory location for the sample is loaded. Samples are used in "Application/Instruments/SampleInstrument.cpp" and the pointer to play from is obtained using the WavFile::GetSampleBuffer method.

If the SD card were memory mapped, the only thing you'd have to do is to point at the memory location on the SD card, but since it's not, you'd probably have to load the appropriate data at the start of the Render method. This would need an additional buffer on the already scarse memory. The good thing is that each Render call for each channel is done sequentially, so it would be just one additional buffer needed.

Another potential class to look into is the AudioFileStreamer, this class is used when previewing samples before actually loading them, and it's effectively streaming from the filesystem. It's a much simpler playback tho, no effects, bpm changes, etc.

As a last note, the reason I didn't pursue this further, other than my technical ability is that streaming from flash seemed good enough and with 16MB as you say, the need became less important. Another reason is that I also started pursuing adding synth instruments for some future version. This, while moving further away from the "spirit" of LGPT, makes more sense, I think, in such a memory constrained platform.

maks commented 11 months ago

Since Deluge open sourced their Synthstrom firmware theres some docs about their use of sdcard streaming that might be useful