0

I'm trying to play an audio file through a speaker with arduino. However, I do not want to use pre-constructed libraries such as the tmrpcm library.

I was instructed to perhaps even recreate the tmrpcm library, and my process so far is that I managed to extract the data chunk from the wav file in integers, and I also managed to turn the wav file into a pcm file.

I am currently studying http://playground.arduino.cc/Code/PCMAudio this link in order to gain more insight about how I might play the pcm file with the speaker. However, I'm stuck with the general concept of the code. In fact, I'm confused with the general concept of playing the pcm file through the speaker.

In my understanding, playing a music through the speaker works by outputting series of voltages to the speaker, so that the current in accordance with the magnetic field creates an outward force onto the voice coil with a certain frequency that creates the sound.

So my question is, what does the data in the pcm file actually mean? The integers cannot be frequencies, and by graphing it I can see that they are amplitudes in a waveform, but what kind of input they are continues to elude me.

So summing up my questions,

  1. What is the theoretical aspect behind the link :http://playground.arduino.cc/Code/PCMAudio

  2. What does the data in a pcm file (or even a wav file) actually represent?

Thank you so much.

3
  • Perhaps google for pcm to pwm Commented Mar 6, 2017 at 19:51
  • I read the entire first page of google on pcm to pwm, but I couldn't find a breakthrough. What would you suggest I look at closely from the google results?
    – Daniel
    Commented Mar 6, 2017 at 20:09
  • I doubt there's any "breakthrough" information out there; but look up 1-bit delta-sigma DACs, because unless you attach a DAC or use a Due or Teensy 3, you'll be using 1 bit (toggled as needed) plus a low pass filter to generate your waveforms. Also consider a simple-minded protocol where you use a fast-clocked ADC to read back output from low pass filter, turning a bit on when voltage should go up, or off when it should go down Commented Mar 6, 2017 at 22:55

1 Answer 1

3

You are right that the values in a wav file are amplitudes. 16 bit signed values to be precise.

And that amplitude is just a voltage. Negative values are normally a negative voltage, but they don't have to be. They can be just a voltage below a pre-determined mid voltage around which the wave swings (known as a DC offset).

To play the waveform you just have to generate the corresponding voltages at the right times. That is usually done using a timer so you get the frequency right. For instance a 44100Hz file has to have a sample voltage generated 44100 times per second. 44100 samples will be a sound clip lasting 1 second.

But of course the Arduino cannot generate a voltage, so instead PWM is used instead. Filtered through a low-pass filter it turns into a voltage. It also uses 50% duty cycle as "zero" for the waveform, which is scaled to fit into the resolution of the PWM. The PWM has to be running at a considerably higher frequency than the sample frequency of the waveform so it has a chance to represent a voltage before it is asked to change again.

5
  • curious: would a DC offset harm speaker coils or lower output potential? Are arduinos fast enough to dither audio frequencies using PWM? thanks!
    – dandavis
    Commented Mar 7, 2017 at 23:18
  • It can cause some distortion if the coil movement reaches its limit due to a magnetic offset. It also causes heating in the coil. You would normally remove the DC offset by passing it through a capacitor. And yes, the Arduino is fast enough for crude audio. Not wonderful, but tolerable for small projects.
    – Majenko
    Commented Mar 7, 2017 at 23:21
  • For anything better you want a 32 bit chip and I2S.
    – Majenko
    Commented Mar 7, 2017 at 23:22
  • @Majenko in case the wav file is 8bit and sampled at 33100Hz, then the wav file will contain the amplitudes(1 byte correspond to 1 amplitude level) to be sent out to the DAC every 1/44100 seconds. Please correct me if i am wrong.
    – ArduinoFan
    Commented Mar 8, 2017 at 10:20
  • That is correct, though it is very rare to find an 8-bit WAV file.
    – Majenko
    Commented Mar 8, 2017 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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