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.

  • Perhaps google for pcm to pwm – James Waldby - jwpat7 Mar 6 '17 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 Mar 6 '17 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 – James Waldby - jwpat7 Mar 6 '17 at 22:55
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.

  • curious: would a DC offset harm speaker coils or lower output potential? Are arduinos fast enough to dither audio frequencies using PWM? thanks! – dandavis Mar 7 '17 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 Mar 7 '17 at 23:21
  • For anything better you want a 32 bit chip and I2S. – Majenko Mar 7 '17 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. – Umar Mar 8 '17 at 10:20
  • That is correct, though it is very rare to find an 8-bit WAV file. – Majenko Mar 8 '17 at 10:21

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.