0

I'm a beginner and I've been developing my own library for a wave player. so far I have the SD card installed and OLED and rotary encoder all connected. I've successfully read the wave chunk and it's data(16bit and 44.1KHz). My question is that the data in wave file is PCM and I wish to play is using 16bit PWM and so how do I convert PCM to PWM?

5
  • An Arduino is not an audio player. It won't have enough processing power and/or memry for running an SD card, refreshing an OLED display and playing audio. Use specialized hardware and a decent DAC for audio playback. – tttapa Jun 2 '18 at 17:24
  • Or go the cheap route and use an LC filter. This video has good info on both DACs (R2R-style) and PWM+Filter "DACs": youtube.com/watch?v=Y2OPnrgb0pY What's your speaker system? You might need another OpAmp or audio amplifier. – Maximilian Gerhardt Jun 2 '18 at 19:07
  • tttapa, I've seen a lot of tutorials online. Maybe if I remove the OLED display it might be able to run it but I'm just curious and I want to get to the end of it even if the output isn't want I desire. – Sankalp Jun 5 '18 at 13:20
  • Maximilian Gerhardt, LC filter makes sense after I've generated PWM signal. My whole problem is that I'm not able to do that. I just don't know what the interface between PCM WAVE file and PWM is. – Sankalp Jun 5 '18 at 13:22
1

There is no such thing as a PCM file. PCM is completely erroneously used to describe an uncompressed digital audio file. What it actually means is "A file which contains data that can be fed directly to a PCM device without any form of intermediate conversion".

What you have in a "PCM" WAV file is just raw sample data. After your header (usually 44 bytes) you just have 16-bit values representing your sample points, with the two channels interleaved one after the other.

Since PCM is a linear audio representation, the sample data it expects is a simple linear representation of the audio (unlike µLaw or A-law) and with no compression (such as MP3 or FLAC). And that is the exact same data that PWM expects since it too is a simple linear output.

However, none of that matters, because the Arduino simply isn't powerful enough to play your file. Maybe a smaller one (8kHz, mono, 8-bit) would be playable, but not yours. The numbers just don't add up.

There is a PCMAudio example available, but it only works from audio embedded within your program.

There is also a TMRpcm library available which plays from SD card, but is limited to 8-bit mono, though it can supposedly do up to 32kHz sample rate (though I wouldn't want to try doing anything else at all at that rate).

You would be better off using a system more geared towards audio - something with an I2S CODEC chip attached would be the best solution.

1
  • Thanks for replying early. Well I was thinking if we can use 16bit PWM for audio output at 44KHz timer interrupt. And to compensate for the low memory holding capacity of the Arduino we can use double buffering from an SD card. OR Adding an external 16bit DAC IC would reduce the complexity because then maybe I don't have to worry about PWM? (I'm not sure about this part.) – Sankalp Jun 5 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.