Go Down

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


Hi xl97,

1) The lib is tested on Arduinos using mega328 or mega2560 Arduino based plattforms. I don't know if it will work on things like Leonardo or those USB-supporting-AVR-Devices.
If you want to use a 8 MHz Arduino you need to convert the samples using the batch-files for 8 MHz, this will half the sampling rate but is often still good enough. If you use a 3.3V Arduino you can attach the SD card without those resistor-divider-stuff directly to the Arduino.

2) Audio-Lib takes about 1.3KB of RAM, which is a lot especially on mega328 devices. The timer that belongs to the audio-output-pin is fully utilized by the lib and therefore not available anymore. The processor time amount needed is also very high, I guess at nearly 80-90% for highest quality audio stereo. But often that is enough for applications that does not need much power.

3) I've added some stuff to allow for work in versions prior 1.x, but I never tested that. I think I have heard other people got it to work with v23.

The cap is by the way only to block DC currents from the speaker. It does not act as a low-pass so far as the PWM frequency is out of the hearing range for 16 MHz Arduinos. For 8 MHz you should use the highest rate, then it should be ok as well.




Any suggestions for changing the code so multiple files could be played at once/asynchronous from each other. Would I just keep track of a "_fileinfo" object and a buffer for each file playing? Then the worker routine would service each buffer as necessary and the PWM output would be some sort of summation of the individual outputs?

Note that this is for my port of your library to the faster ChipKit compatible microcontrollers. My ideal scenario would be to create a library capable of 4-8 channels of audio. You could designate 1 or more of those channels to be protected, by which I mean they would not stop playing if the user requests another file to be played. Instead, the next file would go to the next available unprotected channel, where "next" is determined by a ring buffer.

As an example, Super Mario Brothers sounds. Assuming 4 channel audio. You would want the background music to be protected, because you don't want it to stop when the sound effects play, so maybe it plays through channel 0. If you jump, then the jump sound effect would play in channel 1. Then you shoot two fireballs while you are in the air, whose sounds play in channels 2 and 3. If the first fireball hits a bad guy, then the jump sound would be stopped and the hit sound would play back in channel 1.



I also thought about extending the library to multi-playback, even on AVR based plattforms. On AVR, the interrupt itself creates big overhead due the many PUSH and POP stuff (I think ARM has a somewhat nicer architecture there), so doing things in interrupt is not the best way. On Arduino, if sample buffer is not changed, I works a little like a ping-pong buffer, because a whole sector of samples is read before the buffer is released for playback. If multi-playback of different sounds starts sector aligned, two different sectors can be read and added together in playback buffer and the buffer can be released as one block. This become more complex when not sector aligned.

For your approach I like your idea of buffering the sounds in different memory locations and maybe adding them in interrupt (maybe also scale the values for volume control). SD cards tend to be damn slow if sectors are not read in order, to overcome this your approach could lead to read n sectors from file 1 and then n sectors from file 2. With n being a number bigger than 1, you can trade more possible simultaneos channels at the cost of a bigger starting delay.

BTW: But decide and try yourself, getting the best compromise involved a huge efford of research even for that simple "SimpleSDLibrary" on AVRs. It was a long way to find out that simply using a high PWM rate even at low sampling rates is the most important factor on the way to get nicer audio output. On that way I also tried things like storing 1 bit delta-sigma-coded audio on a SD card and playing that back at high rates through SPI output. It worked good, but involved heavy conversion on PC side and adding sounds or even modifying the volume is not possible anymore. In the end I considered it not simple enough for Arduino world...


I got this working on an Arduino Uno (R3) in under 30 minutes this morning, very happy to hear the audio coming out of my speaker - however, when I tried connecting an LM386N-3 based 1W amplifier (http://ebookbrowse.com/n48fl-pdf-d393297795) with a set gain of 50 (adjustable input attentuation via POT) to the output pin (using the recommendations in the docs for powered speakers), all I got was beeping noises rather than audio?

Any advice appreciated!


Go Up