Anatomy of a WAV player
John A. Zoidberg
Posts: 514
Note: This "Wav Player" applies on all the microcontrollers, including the 8-bit PIC/AVR, 16-bit DSPIC and the other such as ARM and also finally, the SX/Propeller as well.
Out of my sleeplessness, I managed to cook up a simple WAV player using an SD-Card, a PWM and using a PIC18F45K20. It works when I used the wav files which contains the format: 8KHz, 8bit and mono, just to start with it.
However, during the playing of the sound file, I noticed that I needed to ramp up the timer interrupt (or the sampling) to 12KHz for it to be playing at a normal pitch instead of the 8KHz, and I hear click-click-click sound all the way. The PWM is 64KHz.
There is the song played, but I hear consistent click-click-click and only sounds good (not slowed down nor not sped up) when it's at 12KHz sampling rate.
In the code I employed that pseudocode:
0.) Open Wav file.
1.) Prepare 512bytes buffer.
2.) Check first 44 bytes header first. If not correct, discontinue program immediately.
3.) After 44 bytes, dump the first 512 bytes from the wav file into the buffer.
4.) Start the sampling and put bytes of data into PWM.
5.) When the playing of the buffer reaches after 1/2 of the capacity, copy the another next 256 bytes from the wav file into the 0~255th bytes of the buffer.
6.) After the 512nd byte, the buffer wraps around back to 0th byte and the another next 256 bytes from the wav file is copied into the 256~511 bytes of the buffer.
7.) Process repeats from (5) to (7) until file ends.
It may looks like a circular buffer to me if I'm not mistaken. The clicking might be caused by the sound of the filling of the buffer, but I may be wrong.
Any other approaches of playing a WAV file effectively? I would use a DMA but right now there is not a DMA in all the microcontrollers I'm using now.
Thanks for the opinions.
Out of my sleeplessness, I managed to cook up a simple WAV player using an SD-Card, a PWM and using a PIC18F45K20. It works when I used the wav files which contains the format: 8KHz, 8bit and mono, just to start with it.
However, during the playing of the sound file, I noticed that I needed to ramp up the timer interrupt (or the sampling) to 12KHz for it to be playing at a normal pitch instead of the 8KHz, and I hear click-click-click sound all the way. The PWM is 64KHz.
There is the song played, but I hear consistent click-click-click and only sounds good (not slowed down nor not sped up) when it's at 12KHz sampling rate.
In the code I employed that pseudocode:
0.) Open Wav file.
1.) Prepare 512bytes buffer.
2.) Check first 44 bytes header first. If not correct, discontinue program immediately.
3.) After 44 bytes, dump the first 512 bytes from the wav file into the buffer.
4.) Start the sampling and put bytes of data into PWM.
5.) When the playing of the buffer reaches after 1/2 of the capacity, copy the another next 256 bytes from the wav file into the 0~255th bytes of the buffer.
6.) After the 512nd byte, the buffer wraps around back to 0th byte and the another next 256 bytes from the wav file is copied into the 256~511 bytes of the buffer.
7.) Process repeats from (5) to (7) until file ends.
It may looks like a circular buffer to me if I'm not mistaken. The clicking might be caused by the sound of the filling of the buffer, but I may be wrong.
Any other approaches of playing a WAV file effectively? I would use a DMA but right now there is not a DMA in all the microcontrollers I'm using now.
Thanks for the opinions.
Comments
Or, you could use a Propeller and let one cog do the audio while the other does SD access, and they hand off in Hub RAM.
I see. I just actually took some wav player examples from the OBEX, and modified it so that it fits into the casual 8-bit microcontroller.
However, it works only at 8KHz and mono. Anything higher than 8KHz, the system wouldn't play. Is it because the 8-bit microcontroller isn't fast enough (@ 20MHz) or it doesn't suit to do so?
I will try the same configuration on the Prop or on a faster microcontroller and see.