pwm audio generation

Go To Last Post
34 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to generate pwm audio accurately?

I see that there are two parameters in audio files

1. sample rate
2. bit rate

What are those things? How do they relate with pwm rate?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See AVR335.

1) PCM audio consists of samples of the audio amplitude taken at regular intervals. How fast these samples are taken is the sample rate. According to Nyquist you can only record a bandwidth that is half the sample rate. So if you want an audio range of about 0-4kHz (which is like tlephone/AM radio) then you sample at 8000 times per second (8kHz). If you want FM radio quality you might sample at 16kHz..20kHz and if you want CD quality you sample at 44.1kHz. That gives an audio range up to about 22kHz which is about as high as the best human ears can hear (unless you are really a bat!). There is a trade-off to be made. The higher the sample rate you use the more storage is need to hold the audio (these days maybe not such an issue with SD cards) but also the playback code may not be able to reach the fastest speeds possible.

2) The "height" of the audio signal when sampled is divided into a number of steps. Typically either 256 steps (and thus held in 8bits) or 65536 steps (held in 16 bits). More steps means a better quality of playback signal but 16 bit data will take twice the space (and twice the amount of effort to playback) compared to 8bit. 8bit is almost always unsigned which means the very bottom of the sampled wave is held as 0x00 and the top as 0xFF. While 16bit sampling is almost always signed with 0x0000 representing the "half height" point of the wave. The samples are really -32768..0..+32767 in this case.

The way these things relate to the PWM is that you want the PWM frequency to be some multiple of the sample frequency and you want the number of steps for the duty cycle (effectively "pulse height") to be either 256 (8bit) or 65536 (16bit).

An AVR cannot really do 16bit PWM at audio frequencies so you are likely to use 8bit audio. As for sampling frequency that depends on the quality of audio you want. I would say that 44.1kHz is unachievable but having used 8bit 8KhZ myself played thorough a small LM386 amplifier it's actually pretty reasonable for an 8bit micro if you just want to play speech or AM radio quality music.

If you want quality audio (a) use a DAC or (b) consider a better micro for the job.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply clawson.Can you tell me how to achive 44.1kHz pwm? (cannot achieve 44.1kHz pwm deviding by pre scalars).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, if your have 8(or more) unused pins , running a 8 bits R2R DAC is quite simple, inexpensive http://en.wikipedia.org/wiki/Res....... If you get a functionality (having your avr play music) easier and if this functionality is important???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am planning to build simple wav player using pwm So generating a pwm in the uc and generating audio is easy and inexpensive..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If a route is long and complicated, and the other soft, short and comfortable, which one would you choose???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
If a route is long and complicated, and the other soft, short and comfortable, which one would you choose???

Ok.. I will take the complicated one...Because I like challenges.. :lol:
I am making a inexpensive wav player for a project of my university.So do you know how togenerate 44.1kHz pwm?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forget 44.1kHz PWM on a 16Mhz micro. If you want to play 44.1kHz attach a DAC (which can be as simple as dbrion606's R-2R link).

I suppose if you use an 11.289MHz crystal (do such things exist?) then with a /1 prescaler you can get 8bit PWM at 44.1kHz.

EDIT OK, it seems there are 11.289MHz crystals so maybe that is the way to go though I bet an R-2R may work out just as cheap and actually reduce the amount of ancillary circuitry.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I some how manage to get 44.1kHz pwm frequency what would be the interrupt rate to feed the wav data to the pwm ? is it equal to the pwm rate?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wav files are stored in little endian manner.So do I want to restore it in the big endian way and feed it to interrupt?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If I some how manage to get 44.1kHz pwm frequency what would be the interrupt rate to feed the wav data to the pwm ? is it equal to the pwm rate?

You have no choice at 44.1kHz - it has to be. Use the TOP interrupt to feed the values into OCR.

Endian-ness does not apply to 8bit samples (which is what you must use). Use a WAV editor (audacity perhaps) to get the WAV to PCM with the correct sample rate and 8bit mono (don't make the mistake of trying to use stereo samples!).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't worry too much about getting exactly 44,100HZ. All that'll happen is that there will be a slight pitch shift on playback.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

All that'll happen is that there will be a slight pitch shift on playback.

Indeed. Most of the time when doing the 7.8kHz on a 31.25kHz PWM clock the audio I was playing was 8kHz not 7.8kHz as it was easier to down-sample to that (a standard choice). That meant it was being played a little too slow so would have been a little lower pitch than intended. This was barely noticeable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think little endianess will occur when come to play 8kHz sampled 8bit pcm sound.When I try to play the wav data in order I only here a noisy sound ,But when I skip a one and play a one byte I could here a sound which is near to the original wav.....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Big and little endian isnt a problem when you only have one byte. Its a problem when you have data in two bytes. Which one is first? The big end or the little end? If you files are 8 bit 8KHz, make sure they are MONO and not STEREO, and make sure they are unipolar... quiet stuff is in the middle near 128. Maybe post one of the files and if we can play it 8 bit 8khz mono, the files are good.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I think little endianess will occur when come to play 8kHz sampled 8bit pcm sound.When I try to play the wav data in order I only here a noisy sound ,But when I skip a one and play a one byte I could here a sound which is near to the original wav.....

That sounds either like it's actually stereo with one channel "empty" or you are really reading 16bit data though the 0x8000 signed offset wouldn't (I would have thought) make the high bytes alone work - though I suppose they'll all just be offset by 0x80.

Are you sure the program that created the WAV has created 8bit, mono samples? Because of this 16bit, stereo world in which we live it's actually quite tricky to find WAV editors that can do 8bit mono. I remember that Audacity lost the ability all together at one stage but later versions then restored this. I used to use CoolEdit but then Adobe bought the program and assimilated it (and the programmers) so it no longer exists though it was far better than anything Adobe have/had. I think you can still download copies of it if you search though.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can any body check and see if this song is 8bit mono pcm...

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the function "_delay_us()" depend on the crystal or is it a constant for any crystal?

When try to plot the the Fourier graph in Proteus it will give varies frequencies...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is the function "_delay_us()" depend on the crystal or is it a constant for any crystal?

Just as its user manual tells you it is dependent on the crystal frequency which is why you have to define the F_CPU macro to the right value. If you don't define F_CPU it assumes 1MHz which could well be wrong.

BTW your attached zip files are corrupt. They cannot be opened with any of ark (Linux), winrar (Windows), 7-Zip (Windows), Power-Archiver (Windows).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanking to you guys I manage to play 8kHz pcm audio.... :D
Now I want to improve my player.I need to play 44.1kHz audio.So do i want to care about endianess..
As you guys told, while playing 8kHz pcm endianess proublems didn't occur...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:?: How can I convert little endian to big endian and play the wav audio?
Is there any algorithm to do that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Now I want to improve my player.I need to play 44.1kHz audio.So do i want to care about endianess..

A change from 8kHz to 44.1kHz has nothing to do with endianism. You'll still be using 8bit data and 8bits are inherently not affected by byte endianism.

Quote:

How can I convert little endian to big endian and play the wav audio?

IFF you have 16 bit audio (two bytes per sample) then you can switch the endianism with something like:
unsigned char lowbyte, highbyte;
fin = fopen("foo.wav", "rb");
fout = fopen("foo.$$$", "wb");
while (!feof(fin)) {
  lowbyte = fgetc(fin);
  highbyte = fgetc(fin);
  fputc(highbyte, fout);
  fputc(lowbyte, fout);
}
fclose(fin);
fclose(fout);
unlink("foo.wav");
rename("foo.$$$", "foo.wav");

That's untested - I just wrote it but it's going to be something along those lines.

BTW why are you so hung up on endianism anyway? For one thing you will always use 8bit data but for another both PC's (and hence WAV files) and AVRs are all little endian so why would you ever need to change? And if you did why bother to write a utility to do it separately? Just have the FatFs code on the AVR do something like:

char buff[N];
f_read(fil, buffer, N, &read);
for (i=0; i

which switches the byte pairs in the buffer before you play it. Or perhaps even easier, because you are going to combine data into 16 bits anyway, when you come to play the data use either:

// data is little endian:
word16_to_play = buffer[i] + (buffer[i+1] << 8);

or

// data is big endian:
word16_to_play = (buffer[i] << 8) + buffer[i+1];

But endianism is NOT an issue here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow...I successfully played the 44.1kHz wav using 44.1kHz interrupt and 62.5kHz pwm....Signal quality is good...
How can this be happen? because 44.1kHz is not equal or a multiple of 62.5kHz pwm frequency?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How many bits? 8? 16? (Normally, a 44.1KHz wave file is 16 bit). I guess if you just use the top 8 bits of the 16 it would be the same as 'converting from 16 bit to 8 bit'. But the top byte of the 16 bit data is signed... your 8 bit data should be biased to 128. This is confusing all of a sudden.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you play 44.1kHz samples at 62.5kHz doesn't everyone sound like Pinky and Perky?

And in case anyone doesn't know Pinky and Perky:

http://en.wikipedia.org/wiki/Pin...

And to hear what I mean:

http://www.youtube.com/watch?v=M...

(guess what trick they used to create the Pinky and Perky voices!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
How many bits? 8? 16? (Normally, a 44.1KHz wave file is 16 bit).

ow...I randomly used the 8bit 44.1KHz pcm wav.I used "Audacity" to create those samples.That's why I think I got some quality sound.
I haven't tried 16bit one.I think It could not play as u told me......

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good. At least you know how to run audacity and you know exactly what the specs are. I think for speech 8KHz frequency response using 16K samples per sec will sound good. 44KHz sample rate gives 22KHz freq responce, and there isnt anything in a speech waveform much about 8KHz. "SSSSS" is white noise with some hi freq content for example. 16K sample rate takes less flash than 44.1KHz.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
If you play 44.1kHz samples at 62.5kHz doesn't everyone sound like Pinky and Perky?


Yes I played the 44.1kHz 8bit pcm wav using 44.1kHz interrupt and 62.5kHz pwm.According to my knowledge as 62.5kHz pwm frequency is higher than or equal to the 44.1kHz interrupt(which will feed the samples to pwm frequency), we can use to regenerate the original sound.
(Here..carrier wave frequency(pwm)is greater than twise of the audio frequency.nyquist theorem is satisfied.)Am I wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to determine the end of a wav file?
The c function

"while((x=getc(f))!=EOF){}"

is not working for wav files..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are some projects (in the projects section ) that output audio using pwm.To get good quality sound you need a avr with high speed pwm like the atmega16u4.The usb audio device i did outputs high quality stereo sound
with the cpu runing at only 8Mhz and timer4 at 96mhz.But i'm not sure if this can be done with C language.
For a wav player there could be problems of bufer underflow because avr has very litle memory.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Milprasad wrote:
clawson wrote:
If you play 44.1kHz samples at 62.5kHz doesn't everyone sound like Pinky and Perky?


Yes I played the 44.1kHz 8bit pcm wav using 44.1kHz interrupt and 62.5kHz pwm.According to my knowledge as 62.5kHz pwm frequency is higher than or equal to the 44.1kHz interrupt(which will feed the samples to pwm frequency), we can use to regenerate the original sound.
(Here..carrier wave frequency(pwm)is greater than twise of the audio frequency.nyquist theorem is satisfied.)Am I wrong?

Is my assumption wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please tell me how did you do this?

I have done a digital clock using atmega32 and ds1307.

I want to add a feature with my clock,that is when I press a button It will speak the current time.

If you share your source code,circuit diagram and your project details,I would try to make a avr talking clock.

did you add any sd card to store audio?

 

My email:  pathor.iit@gmail.com

Abdur Rahman

Jahangirnagar University, Savr,Dhaka,Bangladesh.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please do NOT double post, you already have a topic on your talking clock.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly