New library for PWM playback from SD cards: SimpleSDAudio

Hi, I will try answer some of your questions:

@timberwolf9:
Why the argument -r 64000? If you use a 16 MHz Arduino, it should be 16 MHz / 256 = 62500. But anyway, the sound is just a little bit to slow then...

@bratan & timberwolf9:
I will tell you some internal details about my library: The library uses a ring buffer of 1024 bytes when no work buffer is set manually. As the buffer is filled in one sector blocks of 512 bytes each, the buffer became a ping-pong buffer in most cases. With 512 bytes, at 8-Bit/Mono/62.5kHz that is about 8ms time, because the data reading from sd-card also takes some time, your observation of 5ms max delay between worker()-calls is very realistic.

On mega-arduinos is still a lot of RAM availible, so you can try to increase the audiobuffer by calling setWorkBuffer(...) before calling init(). With an increased buffer you will get more time between worker-calls.

By using the function isUnderrunOccured() you can detect if you even had a slight buffer underrun. But beware, I think this function returns true on every first call after calling play().

I also thought about calling the worker-function automatically in a timer interrupt. This should be possible, but only if that interrupt is set to non-blocking to guarantee the main audio interrupt the maximum priority. Maybe I will try to implement such in a next version of my lib, so that calling worker() is not necessary then anymore.

@AllanB:
Error code 49, which is 0x31, indicates that something is wrong with the bootsector on that cards. Maybe there is more than one partition on the card or something else is wrong. That explains why other cards work for you.

@bratan:
Yes, rising the baud-rate should solve issues with print-functions, as then the worker-calls are called more often.

To your question about other Audio-Playback-Shields: As far as I have seen it, all other audio-shields contain a dedicated processor that reads the audio-data from storage and feed it to an audio-DAC. For a good sound the DAC must be feed with low jitter at the audio rate of the audio material, otherwise you may recognize audio distortions like slowdowns or stuttering. With a dedicated processor it doesn't matter what your Arduino does while audio is playing, but my library is for all those who don't want to spend extra money to such a thing. SimpleSDAudio get the most out of the AVR's by pushing the Arduino processor strongly to its limits - that's the price you have to pay if you want nice audio from that little thing.

And one detail on top: The magic trick of getting a good sound out of the AVR's PWM is that mainly PWM-frequency matters more than anything else. It is the sampling rate of >32kHz that make the big difference between this lib and the well known bad telephone-like audio-playbacks often sampled at only 8 kHz of other approaches. With PWM frequency as high as 31/62kHz a low-pass is often not necessary. And you only need more than 8-bit if you want to listen to something that has not been as fucking dynamic compressed as most of the actual radio stuff nowadays is.