Go Down

Topic: New library for PWM playback from SD cards: SimpleSDAudio (Read 124192 times) previous topic - next topic


Mar 06, 2013, 01:45 am Last Edit: Mar 06, 2013, 10:27 pm by Tuttut Reason: 1
there is no standard way, but you can try this patch (untested yet!)

Code: [Select]

void SdPlayClass::worker(void) {
  if(!_WorkerRunning && _pBuf && _fileinfo.Size) {
    uint16_t buflencpy;
    _WorkerRunning = 1;
      buflencpy = _Buflen;
    if(_fileinfo.ActBytePos < _fileinfo.Size) {
        // At least space for 1 sector?
        if(buflencpy < _BufsizeMinus512) {
            int16_t ret;
            ret = SD_L1_ReadBlock(_fileinfo.ActSector++, _pBuf + _Bufin);
            if(!ret) {
               uint32_t BytesLeft = _fileinfo.Size - _fileinfo.ActBytePos;
               _Bufin += 512;
               _fileinfo.ActBytePos += 512;
               if(_Bufin >= _Bufsize) _Bufin -= _Bufsize;
               if(BytesLeft > 512UL) {
                     _Buflen += 512;
                } else {
                     _Buflen += BytesLeft;
                   while(1) {   
                        buflencpy = _Buflen;
                     if(buflencpy == 0) break;
            } else {
              _lastError = ret;
    } else {
      // Playback done
      if(buflencpy <= 1) {
    _WorkerRunning = 0;

Replace the worker-function inside the lib (file SimpleSDAudio.cpp) with the one above. To stop playback you need to use stop() then.

Please try it and tell if it works...



   Sorry, that patch did not work.
  Result : Audio file not played.Instead, there were lot of glitches-only glitches.I have noticed that the glitches came out as a stream (loudest at the start and diminished along the time line and died in few seconds).But that was from "Bare minimum" sketch.
  When I tried the " Bare minimum with Debug", it gave a continuous tone of 125 hz approx.(No audio file content played).
But, sketch examples with worker (bare minimum with worker,bare minimum with worker with debug),worked normally,of course with the glitch.
Could you please explain what exactly the patch modifies.
Anyway, thank you for trying to help me.



Mar 06, 2013, 10:26 pm Last Edit: Mar 06, 2013, 10:39 pm by Tuttut Reason: 1
sorry, I did the patch using a too old local copy of the library on my smartphone on-the-go and got an too old lib file. Now I edited my former post, please try to apply the patch again on SimpleSDAudio-Lib V1.03 (the current one). This time I tested it a little. Unfortunately actually there is no flag that tells you if the file played at least once...
Please try again and tell me if it is working for you.

BTW: The patch catches the moment when the worker function puts the last sector (or part of it) of the audio file in the playback buffer. In this case, it waits endless (and therefore precise) until the buffer is completely empty (_Buflen == 0). In this moment it triggers the play() function, which starts the playback again. This is how the play() function works - if playback is still playing it restarts playing from start again (done by calling internally the stop() function). The stop() function resets all the pointer and the buffer for the playback. 




    I'm still getting audio glitch.May be,something wrong (sd card format etc)with my setup.Could you please confirm that the test file I am using (please see attachment)works Ok in your set up?

:  Thank you for the explanation about the patch.Now I understand little more about the 'worker' function and I try to understand your libs more deep.




I got it finally working.

But not with that FAKEduino.
I made my own board.

Sadly, outputting an audio is just a part of the project. The whole project isn't working yet.

Question: Are there pins affected when using this library? For example, some digital pins won't work...something like that?


@manmachine: Hm, I thought about my quick solution and it is wrong - the issue is that the buffer is completely empty when issuing play and that play first reads a whole sector of 512 bytes from card before it starts playback again. This should lead to a fast but not gapless start. I think it will need a bigger overwork to get around this. I could think about modifying the lib to read the sectors byte-wise which will make the lib slower but more flexible. Audio processing will then become possible like gapless playback, fine-tuned playback tempo and mixing. But the lib might be then not as simple to use as now.

@meow...: Please provide more info what is working and what not. Error code of lib or just no sound? Or just no sound in bigger software project?


   @Tuttut:Thank you for your reply.So, its going to be a different approach!Well,Could you post a FLOW CHART of the proposed lib implementation,what might be a possible solution.



Tuttut, thanks for the great library. I have an alpha release of my ChipKit port, BasicSDAudio, available on GitHub. I am also working on an amplifier board that would work for any PWM audio output.

Check out the release at:

another demo at:

and info on the amplifier/filter at:

I have yet to incorporate the 1.03 improvements.


I'd like to see/learn more about making this Awesome library better sounding with the use of filter/amp?

Small? (all SMD?)

Is there a schematic posted for it?

I am VERY happy Tuttut released this lib.. it was a HUGE missing part of the Arduino community (easy audio)..
but as 'stock' the volume is just 'meh'...  so it needs some filters and an amp for sure for more sound hungry projects..

still learning/finding my way through filters and amps..



What timers are used in this library for 16MHz, Full rate mono?

I'm hoping there is a timer or two open that I can use to output PWM so I can brighten and fade an LED while the audio file is playing.

In the case that there is a timer I can use, would using the timer affect the audio too much?



The lib use only the timer that is bound to the output port. It might be possible to even use analogWrite on neighbor ports that use thevsame timer but I never tested it, so please do so. But in this case the PWM is fixed to 8-Bit and the sampling rate of audio-playback, which should be no issue for LED dimming.


Apr 11, 2013, 04:29 pm Last Edit: Apr 11, 2013, 04:37 pm by xl97 Reason: 1
I have done/tried a set-up using the SimpleSDAudio lib as the audio output.

the most simple design, straight outout put on pin9 via a 100uF polarized cap.

the pwm 'works'...  but you get noise in the audio.



+3.3v @ 8MHz internal clock  (ie; no crystal, cap or resonator)
straight connect microSD (ie: no level shifter or voltage divider)
3 x leds on PWM fade
5 x leds (digital on/off)
no filter
no amp

(I need to learn more about low-pass filter set-up and using an amp better)  :(


is there any schematics or anything for your BabyBOOMER?

Does it need the 'dual' output?  I only have  set-up where Im outputting on pin9 only (most basic)..

looking for the smallest way to get better sound.. :)



the thing is that people often don't understand where the noise in such systems come from and try to build filters to try to filter out 8-bit noise. This is just stupid, as the noise of having too less bits is usually even distributed over the audible spectrum and not only at the high frequencies. With very good algorithms that do dithering you can try to buy better noise performance at the cost of spectrum - in such a case the noise is not longer evenly distributed over the spectrum (this is called noise-shaping, SOX can do this under special conditions, see http://sox.sourceforge.net/SoX/NoiseShaping. But this only makes sense if you have a high sampling rate AND you are using an external filter. You can do a simple experiment to understand 8-bit noise: Convert your audio file at your PC to 8-bit and listen to it and you will hear the static noise. I tried this using Audacity and the result is pretty much what I get with the Arduino 8-Bit output.

But: If you want an easy way to get rid of the noise go for double-PWM-Ouput aka 16-bit mode. Often you only need one additional output port and one resistor to get a much better sound with so less noise that you don't even think about filtering (which is often useless anyway!). If you want it cheap try my circuit from this entry (2nd post circuit for 16-bit): http://arduino.cc/forum/index.php/topic,158024.0.html. Just build one half of it if you stick with Mono-Output. The nice thing is: It is much louder on a speaker than a port-pin, so no need for additional amplifier and much less noise and still cheap. I tested it several times, I'll try if I can do it a little smaller using transistors.


thanks for the reply..

for myself..

1.) can 'dual' output be done on an UNO/Duemilanove?  (I though I had read only on Mega's?   could be wrong of course.. I thought i had 'tried' it originally and dont recall seeing (hearing) a difference.. however that could also have been user/set-up error.. LOL

also.. as to the noise I guess I was referring to.. I mean more specifically wen you run/do PWM actions 'while' playing audio.. you can hear the hum/buzz..etc..etc  playing over the audio or interfering...etc..

unfortunately.. I dont have any 74AC14 chip..  (never even heard of them before now) :)..

but I do have a LM386 amp laying around..?

form the schematic though.. looks like only a:

coupling cap on the power rails
2 x resistors..
and the DC blocking cap on the PIN9 output?

actually looking again.. is that two caps for the decoupling?  hard to see in the fritzing image) :)

Are you saying that just using the DUAL PWM output will (not only help eliminate the noise) but increase the volume.. to where you say/think/believe no amp is even needed to boost volume?

I currently only have things set-up for single (pin9) output.. and actually used pin10 as the CS pin for my SD card..

but next revision could have that changed easy enough......

I'll have to go back and try the dual output again?


Go Up