Streaming WAV Files Using MMC/SD

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
1
14%
5
6
86%
 
Total votes: 7

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Streaming WAV Files Using MMC/SD

Post by Benj »

Image

MP3 players are very popular in today's technology market and come in many shapes and sizes. Using the MMC/SD card reader E-block EB037 it is possible to incorporate files straight into the microcontroller devices ready for streaming out to a pair of headphones or a loudspeaker. To save having to buy an expensive dedicated MP3 chip we will instead be using WAV files as they are uncompressed and easy to stream out via a DAC or PWM output. It would not be much of a push to forward the audio data onto an MP3 chip once the operation of the FAT16 code is understood. The FAT16 driver will only work with the PIC18 series and the ARM devices. AVR device support is on route.

The WAV files can be created using Windows Sound Recorder, Winamp, Audio Recorder for Free plus many other free audio software packages. The format we need is either an 8-bit @ 8khz mono for the PICmicro example or an 8-bit @16khz mono for the ARM example. The WAV files are then placed onto the MMC/SD ready for streaming.

Both the PICmicro and the ARM Flowcode examples employ a buffering scheme to allow the audio data to be streamed without causing ticks or glitches in the playback audio signal. The files are read from the memory card in chunks of 512 bytes called sectors. Two neighbouring sectors of the file may not be located one after another on the physical card memory so there is a small amount of time to seek out and read the next sector for the card. Don't worry this is all handled automatically by the Flowcode FAT16 driver. However it does mean that the previously mentioned buffering technique needs to be used to keep the audio streaming correctly. As each set of 512 bytes is loaded there is a timer interrupt that starts streaming out the data. While this is happening a section at the end of the 512 bytes is stored into a local array. This is done so that when the timer interrupt is nearing the end of the 512 bytes it can stream the audio from the local buffer while the FAT routine is moving and collecting the next file sector.

Image

The audio output can be driven by using an 8-bit R2R Ladder DAC connected to one of the chips ports or by using a single PWM output as the speaker device will remove all of the high frequency noise created. Both the example files use the PWM method as this seems to be less susceptible to noise and it only uses one I/O pin instead of eight. To take the design further an audio amplifier chip can be used connected to the PWM output to push and pull the signal high and low with more power. The NJM2073D is a 2 Watt audio amplifier that can be purchased from Rapid for £0.79 order code 82-2154.

Image

To get the examples working simply copy the custom_code.c file into your Flowcode/Components folder. For the ARM version you will also need to copy the FCD file into your Flowcode/FCD folder before running the Flowcode file.
Attachments
WAV Player.zip
(403.2 KiB) Downloaded 1207 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Streaming WAV Files Using MMC/SD

Post by Benj »

Here is an updated example which uses the official FAT component rather then the older v3 custom component.
Attachments
7. Streaming a WAV file using PWM.fcf
(16.21 KiB) Downloaded 669 times

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Streaming WAV Files Using MMC/SD

Post by Enamul »

Hi,
1. the chip config in example is shaded out, what is the clock speed (internal,rc,xtal)
The example 7. Streaming a WAV file using PWM to play audio uses ECIO-40 where 48MHz clock was used as system clock. But anyone can use the same code with 4 to 48MHz external clock source with PIC18f4455.
2. the sound output is on what pin (pwm)
The example uses channel 1 as PWM output which is connected to RC2 (PIN17 of PIC18f4455 but PIN 29 of ECIO-40).
3.what input is card reader on
Card reader has following connections:
Chip select>>PORTA<2>
Clock >>PORTA<3>
Data In >>PORTA<4>
Data Out >>PORTA<5>
4.what kind of circuit did you use to play the sound
As this is audio signal any simple audio driver using transistor from RC2 which will drive a speaker is enough to play the sound..
Please see above diagram for smart way.
5. what is the jumper position on the board as could not see the ic listed
http://www.matrixmultimedia.com/resourc ... 7-30-1.pdf
You can see the IC list with jumper position here in the above datasheet...
Attachments
Sound Wav Player FAT16 V0.4.fcf
PIC18f4455 version with 20MHz external crystal
(16.04 KiB) Downloaded 696 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
STibor
Posts: 263
Joined: Fri Dec 16, 2011 3:20 pm
Has thanked: 116 times
Been thanked: 113 times
Contact:

Re: Streaming WAV Files Using MMC/SD

Post by STibor »

Thank you for the example program.
I made pic18f2620 controller's clock frequency 40MHz, it works.
How can we increase the sampling frequency?
How can we increase the buffer memory?
I'd like a little better quality sound to create.

Post Reply