Klaas Robers wrote:But I got the idea that reading an SD-card can be stopped and continued at will, isn't it? I hope that you can read byte by byte. Then it is easiest to have the buffer most of the time filled by 99% with "new" data. Only if the data cannot be found fast enough, due to the internal architecture of the card, you need the buffer.
I do indeed read byte by byte and yes, I have the buffer most of the time filled with 99% new data. But because there is little RAM on the Arduino (about 2.5K, but I can only really afford about 1K for audio/video buffers) and because there are 100ms (or more) delays on reading from the SD card, this means that I sometimes don't have enough data. Currently data is 4 bytes per sample (audio 16 bits plus video 16 bits). With a 512 byte buffer that means at best I have 128 samples which from a 22050Hz stream is roughly 6ms of data. If I hit a 100ms delay from the SD card... that's 1/10th of a second, so 2205 samples.... and I am screwed! It doesn't happen frequently, but it does happen too often. MOST of the time the delta (bytes needing to be read from SD card to fill the buffer) is around 10, but occasionally it jumps to 400 and very occasionally it overflows and I get a video glitch.
By reducing audio/video to 8 bit and doubling the buffer size (to 1024) I will effectively significantly reduce the problem to 25% of what it is now.
As to sound - well, it's either just going to have to glitch, or I am going to have to separate out the audio and sound into different files and handle them differently, perhaps with a larger buffer for the audio. I'll cross that bridge when I come to it!