Wav file and PWM Atmega128 ?

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

Guys,

Is it possible for me playing Wav file using PWM or CTC on ATMEGA128 ?

Any experiences ?

Thanks

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

I had a pretty good experience. My wav file was 8 bit 16KHz mono. I set up the PWM to use 8 bit fast pwm. No interrupt. I used timer0 to get a 16KHz interrupt. On each interrupt, I load the next sample from the data array. I had my data array in flash, so I didn't have to deal with sd cards, fat file system, double buffered reads. get the simple version working first. Save that version. Then start adding the complicated stuff.

Imagecraft compiler user

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

is it possible reading wav file (byte by byte) and put it on PWM directly ?
for example :

timer1_ctc_init()
{
	TCCR1A =  // Mode 4 CTC 1:64 Prescaler
	_BV(COM1A0);   // set OC1A on compare match, clear them at top
	//| _BV(COM1A1);   // set OC1A on compare match, clear them at top
	
	//TCCR1B |= _BV(CS11) | _BV(CS10) | _BV(WGM12);
	TCCR1B |= _BV(CS11) | _BV(WGM12); //1:8 pre scaler

}

void tone(int freq)
{
	DDRB = (1<<5); // OC1A output

	timer1_ctc_init();
	OCR1A=freq;

}

void tone(int freq)

freq = data from sdcard ?

thanks

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

Yikes. You ask about experiences playing a wav file, then you ask about playing a tone at a specified frequency. I guess I should ask you about what kind of tone, square wave or trumpet wave or oboe wave. Over to you Bianchi.

Imagecraft compiler user

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

that's the function I have and I'm thinking if I can use it with piano file (wav)...which wav file can I use ?

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

Ray Kurzweil figured out how to play wav files of piano notes that get louder depending on how hard you push the key down. Not only that, the tone is different depending on the speed of the keypress.
I don't know if he used the famous Peter Danneger debounce algorithm.

Do you want to play a 'beep' at a specified frequency, where the beep is a square wave or a triangle wave? That's easy. Playing one cycle of an arbitrary waveform at the different frequency of keyboard notes is possible with Direct Digital Synthesys, and you can find examples by searching for DDS. Perhaps you want to just play back a recording "Hello there!". All these are possible on an AVR. Can you give me/us an idea of which you want to try first?

Imagecraft compiler user

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

Ok, I can try playing "Hello there!", but I retrieve "Hello there!" from a file on SDcard...(buffer)

thanks

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

Did you ever think of Google? You can find tons of projects that play sounds using PWM (Atmel's AVR335 is just one example). Make the leap from there to getting the PCM samples from WAV is simply knowing that a PCM encoded WAV has a 62 byte header that must be skipped. After that you are only faced with whether the data is stereo or mono and whether it is 8 or 16 bit samples. If 8bit the "mid point" is at 0x80, If 16 bit is is at 0x0000.

You set up a timer that runs at the same as the original WAV sample rate (8kHz or whatever) and each time the timer "fires" you load a new value into the OCR of the register doing the PWM

And that's pretty much all there is to it.

Do note that WAV is simply a RIFF container format. What's encoded can actually be one of about 50 different formats. You need to ensure it's just plain PCM.