1

I am attempting to write a program to get the ATmega328P to play audio. I first tried to use this library , but I could not get it to work, so I am now attempting to program my own. The problem is that I don't know how to interpret the audio data that I have:

const unsigned char samples[] PROGMEM = {
  128, 128, 128, 130, 129, 127, 127, 128, 
  127, 127, 128, 130, 128, 126, 125, 125, /* ... additional 26 kilobytes of numbers ... */
};

This is the same data used by the library, and it came from an audio file that had a bitrate of 8 kilobits per second. I then used an executable from a tutorial to extract the data from an MP3.

I first tried to see if these were the frequencies that needed to be played each for 1 thousandth of a second (calculated from the bitrate), but it sounded like static and didn't seem to work.

The wiring is pretty simple, just a transistor with the base going to the output pin and the speaker being controlled by this transistor. I know that this part works because I can output a PWM signal on the pin and it sounds as you would expect.

What is this data and how would I write a program to play it?

1
  • 4
    I'm confused about all this MP3 talk. It's mentioned in the tutorial, too, but it's clearly PCM data. What's the link to MP3?
    – n00dles
    Mar 11, 2023 at 17:52

1 Answer 1

3

Your data have relation neither to bitrate, nor to frequencies. The name samples[] suggests that they are samples, i.e. amplitude values in particular time points:

                 enter image description here

These time points are determined by sample rate — e.g., if the sample rate is 48,000 Hz (48,000 samples per second), time points are 0 s, (1/48000) s, (2/48000) s, etc.

As your numbers are defined as unsigned char, the bit depth is 8 bits (1 byte) and their values are in the range from 0 to 255. Because the original audio oscillates between negative and positive values — for your 8-bit audio between -128 and +127 — you will eventually need to convert them into this range.

4
  • I wouldn't call them "velocities". May confuse people thinking of piano velocity. It also suggests movement and inertia. I'd call them amplitude values. Samplitudes! New word. Coined date of comment.
    – n00dles
    Mar 11, 2023 at 18:02
  • @n00dles, you're right, I edited my answer, thanks. (Sorry for not using your new word :-))
    – MarianD
    Mar 11, 2023 at 19:13
  • Lol it's fine. Soon EVERYONE will know!
    – n00dles
    Mar 12, 2023 at 16:08
  • I suspect converting to signed ints doesn’t have to be done in the digital domain. Simply converting to analog and then running the signal through an appropriate capacitor would remove the DC offset. As long as the DAC doesn’t mind outputting the analog signal with the DC offset. Mar 14, 2023 at 1:05

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.