Adafruit Arcada HallowingM0 full board test

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tanmanh0707
 
Posts: 6
Joined: Sat Aug 27, 2022 6:26 am

Adafruit Arcada HallowingM0 full board test

Post by tanmanh0707 »

Hello,

I have a Hallowing M0 Express board and I'm working with Library -> Adafruit Arcada Library -> full_board_test -> hallowingm0_arcadatest
There is a test play_tune as below

Code: Select all

void play_tune(const uint8_t *audio, uint32_t audio_length) {
  uint32_t t;
  uint32_t prior, usec = 1000000L / SAMPLE_RATE;
  
  for (uint32_t i=0; i<audio_length; i++) {
    while((t = micros()) - prior < usec);
    analogWrite(A0, (uint16_t)audio[i] / 8);
    prior = t;
  }
}
I would like to ask why we need to divide the audio sample by 8 in the analogWrite?
I tried to remove " / 8 " and the sound is not played properly, it played a noise sound.
As I know, the audio samples in audio.h is an unsigned 8 bit raw data.

Thank you and best regards,
Manh

User avatar
dastels
 
Posts: 15658
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit Arcada HallowingM0 full board test

Post by dastels »

It's taking the unsigned 8 bit value from the audio array and casting it to an unsigned 16 bit value. That serves to add 8 high zeros. E.g. 0x4f is converted to 0x004f. Then it's divided by 8, i.e. shifted right 3 binary digits. So 0x004f becomes 0x0009, so 0x09 gets written to A0.

Why? No solid idea, but it might be to avoid overdriving the amplifier. Try changing the /8 to /4 and/or /2 to see what effect that has. It will make it louder, but might also resume in noise.

Dave

User avatar
tanmanh0707
 
Posts: 6
Joined: Sat Aug 27, 2022 6:26 am

Re: Adafruit Arcada HallowingM0 full board test

Post by tanmanh0707 »

Hi, thank you for your instruction. I tried with divide by 4, 2 and you're right. The audio is working louder.
Still have no idea why we need to make the divide here.

User avatar
dastels
 
Posts: 15658
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit Arcada HallowingM0 full board test

Post by dastels »

As I conjectured, it may be to avoid overdriving the amplifier by reducing the volume of the signal being fed to it. If you overdrive an amp you'll get distortion. When you removed the division entirely and got noise, that pretty much proved the point. Dividing by a smaller power of 2 still reduces the volume, but not as much. Dividing by powers of 2 is teh same as shifting the binary number right so it's fast to do. A good compiler will recognize that's what happening and replace the potentially expensive division, by an essentially free shift.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”