NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » from ivonv's post

October 13, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

I am programming a game and there are graphics and sound as well (the sound I have stored in an external dataflash)

Would you like to expand on that a bit? You can start with, how... the when and why can wait. When you say dataflash is that attached to the pcb?

Curious farmer

October 13, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

How....just google it. Sorry can't tie my shoes either.

October 14, 2009
by IvonV
IvonV's Avatar

If I understand your question, you are asking how am I storing sound?

To begin with the sounds are all sound effects. First, I find the WAV files I want (google searches to WAV sites). Then I convert the WAV files to 8 bit PCM at 8,000 Hz sampling using Adobe Audition. In most cases I'll play with the sound a bit in Audition to make it sound right because the reduced sampling and resolution alter the original sound quite a bit. I found a neat little program called pcm2h online and I use that to convert my .pcm files created in Audition to C char arrays. At this point I read the arrays into dataflash using a program I wrote. When a sound needs to be played, my game application reads the data out of flash at 8,000 samples a second (via a timer interrupt) and another timer generates a PWM signal based on the sample read from memory. I pass the output through a low pass filter which I exactly duplicated from AVR 335 and voila... sound effect.

In fact, AVR 335 is a great application note to understand all of this, along with some good sample code I found on Arduino Playground (search PCM Audio).

I should point out that the voltage levels coming out of the filter are quite low, I doubt you could hook a speaker to them and hear anything, but they are at a perfect level for line inputs to my tv which is where I play them.

October 18, 2009
by Farmerjoecoledge
Farmerjoecoledge's Avatar

That's all very good. At the cost is too much for my hobbie but quite interesting to say the least.

October 18, 2009
by IvonV
IvonV's Avatar

Well, the cost of Adobe Audition is probably a bit more than you'd want to spend, but there are many free applications online that would suffice for the task of converting from WAV to PCM. Maybe someone in the forums has a recommendation.

The parts for the low pass filter only cost a few bucks from digikey (I think the shipping was as much as the parts :-))

October 19, 2009
by pbfy0
pbfy0's Avatar

you can use libsndfile for wav-to-pcm conversion. install libsndfile, then run

sndfile-convert -pcmu8 <infile> <outfile.raw>

to convert to PCM. you can get pcm2h here. Then compile it (gcc pcm2h.c) and run

./a.out <infile.raw> <outfile.h>

then enter 1 for eight bit. <infile.raw> is the outfile from sndfile-convert. you can copy a.out to a different name or the PATH for easier use. I haven't tried using external memory yet, but here is an arduino program for a flash stick.

October 19, 2009
by rusirius
rusirius's Avatar

cough .torrent cough

December 08, 2009
by pbfy0
pbfy0's Avatar

I have a program for sound on flash. here it is. read this post for details.

Post a Reply

Please log in to post a reply.

Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...