You should use an MCU with PWM generators to do that.
The problem with PWM is that the max resolution is quite low (10-bit) so you are effectively limited to 8-bit PCM.
- Use a DSP editor like Audacity to export a 16KHz unsigned 8-bit WAV.
- Import the WAV file as an array of unsigned char.
- Set your PWM freq to the resonant freq of your speaker (eg. 31250Hz)
- Set your PWM timer to fire at 16KHz and set the PWM duty cycle to the next array value.
I use 16KHz sample rate because:
- Reduced memory footprint
- Fewer timer interrupts
- PWM's limited resolution does not seem to benefit from anything above 16KHz
- I use piezo speakers directly wired to PWM pins in H-bridge.
- A separate amp module might bebefitbenefit from higher sample rates.