Powered by Invision Power Board


Forum Rules Forum Rules (Please read before posting)
  Reply to this topicStart new topicStart Poll

> Raspberry Pi 3 Gpio And Pwm Audio
evildragon
Posted: December 20, 2016 06:41 am
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 5,832
Member No.: 6,862
Joined: October 27, 2006




Wasn't too sure if I should put this in the digital or analog section, and my god it may be my most serious post ever. Only took me what, 10 years? Well then.

I was given a Pi 3 from a ham friend for early Christmas, and I've been experimenting with the GPIO's lately, and two pins are shared with the analog audio output jack. At first I was confused as there's no DAC on this thing, but then I saw it's PWM, literally simple PWM and a simple filter and that's how the analog output jack works. However, there's a hiss on it with low volume parts of music when using the built in analog outputs. So I found out which pins are sharing this PWM output, and tried to make my own filter, but, no matter what combination of resistors or caps I use, it's the same hiss, either louder, or softer (and sacrificing treble), but the hiss is there.

Can PWM ever sound.......good? Or would it be better to just slap on a real DAC like either a Cirrus Logic or a Burr Brown on the GPIO's and just use a real PCM DAC?

The end goal was to make a simple Pi home stereo media player, with a custom front end, using a custom enclosure, and a alpha numeric POS VFD display.

I have the DAC's required if I need to, but I was hoping to just use the PWM outputs. It'll be plugged into an 80's amplifier so while SPDIF was a thing back then, this amp does not have it.
PM
Top
Sch3mat1c
Posted: December 20, 2016 09:18 am
Reply to this postQuote Post


Forum Addict ++
Group Icon

Group: Moderators
Posts: 20,594
Member No.: 73
Joined: July 24, 2002




Do you know how many bits the PWM is worth?

Or, how fast is the timer/counter clock speed?

If the output function is truncating bits, then the noise is 100% quantization noise. For a random input (not a terrible assumption), that's white noise (i.e., the rounded-off remainder is rand() * 2^-(ENOB)).

Instead of dumping a value to a PWM counter and truncating the remainder, you can sum the remainders into an accumulator, and every time it overflows (on average, every other sample, because the average remainder is 1/2 of an LSB), you carry that into the PWM count -- it's like rounding off rather than truncating (because obviously, when the remainder is 0.5 to 0.999..., you should at least round up the rest of the number out of courtesy), but instead of being a per-sample decision, you use a running decision -- which way you round (down a.k.a. truncate, or up a.k.a. carry and increment) depends on the history of samples.

And because it depends on history, it's a low frequency thing. If you try to emit a DC value that has a remainder of 0.333, then every third sample produces a carry, and rounds up. Over time (over 3 samples or more), this averages out exactly! Sweet, no?

The downside to it being a low frequency thing is, your audio signal isn't just low frequencies. So it will still be inaccurate at high frequencies -- but if that's over 10kHz, say, it may not be very bothersome.

(You can take it further and accumulate the remainders of the remainders, as it were; you can also measure the output, perhaps using a more accurate ADC, like 12 to 16 bits, so you can put it in a control/feedback loop besides. But, that's getting pretty advanced, so don't worry about it.)

Tim


--------------------
Answering questions is a tricky subject to practice. Not due to the difficulty of formulating or locating answers, but due to the human inability of asking the right questions; a skill that, were one to possess, would put them in the "answering" category.
PMEmail PosterUsers Website
Top
evildragon
Posted: December 20, 2016 12:23 pm
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 5,832
Member No.: 6,862
Joined: October 27, 2006




From what I've read the pwm clock is 19.2Mhz (but I have yet to determine what the divisor is, for it's built in audio output in Linux). As far as bits I believe it is 12.
PM
Top
phin
Posted: December 21, 2016 10:44 pm
Reply to this postQuote Post


Newbie
**

Group: Members+
Posts: 11
Member No.: 40,315
Joined: December 19, 2016




Your question raised my interest as I hope to do some work on Raspberry Pi. I did some web lookin and found a discussion on this.

http://raspberrypi.stackexchange.com/quest...he-audio-output

Bottom line, better audio can be found on the HDMI port.

If you intend to use the PWM, well, what you see is what you get I guess. I was really surprised that you can actually get music from a PWM. Then again, people get music from a kazoo!
PMEmail Poster
Top
evildragon
Posted: December 22, 2016 10:44 pm
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 5,832
Member No.: 6,862
Joined: October 27, 2006




Either HDMI I guess, or just slap on a real DAC onto the GPIO pins (or even USB), and just set Linux to use those rather than the PWM. The player I was going to build was going to use analog audio outputs so I thought I could get away with using the PWM for it, looks like it'd be the same as using a cheap Chinese mp3 player, so I'll just design in a DAC.
PM
Top
phin
Posted: December 23, 2016 02:25 am
Reply to this postQuote Post


Newbie
**

Group: Members+
Posts: 11
Member No.: 40,315
Joined: December 19, 2016




QUOTE (evildragon @ December 22, 2016 04:44 pm)
so I'll just design in a DAC.

I have not done music, but have done real time I/O under an OS. You need to be careful that you don't let the pipe go dry. I have seen the OS (not Linux) go off to do it's thing while my code waited to execute. Check to see that the OS will buffer for you OR you may need to use interrupts to keep in charge.
PMEmail Poster
Top
evildragon
Posted: January 16, 2017 09:47 am
Reply to this postQuote Post


Forum Addict ++
*******

Group: Trusted Members
Posts: 5,832
Member No.: 6,862
Joined: October 27, 2006




Update: Apparently there's a hidden option on the Pi that switches the PWM driver to "7-bit 2nd-order Sigma-Delta modulated at 781.25kHz".

That has removed all of the hissing completely it seems and it sounds massively better.
PM
Top
Sch3mat1c
Posted: January 16, 2017 07:45 pm
Reply to this postQuote Post


Forum Addict ++
Group Icon

Group: Moderators
Posts: 20,594
Member No.: 73
Joined: July 24, 2002




That does sound good. smile.gif


--------------------
Answering questions is a tricky subject to practice. Not due to the difficulty of formulating or locating answers, but due to the human inability of asking the right questions; a skill that, were one to possess, would put them in the "answering" category.
PMEmail PosterUsers Website
Top
alicechan11
Posted: February 10, 2017 03:03 pm
Reply to this postQuote Post


Newbie
*

Group: Members+
Posts: 4
Member No.: 40,347
Joined: January 25, 2017




QUOTE (evildragon @ December 20, 2016 06:41 am)
Wasn't too sure if I should put this in the digital or analog section, and my god it may be my most serious post ever. Only took me what, 10 years? Well then.

I was given a Pi 3 from a ham friend for early Christmas, and I've been experimenting with the GPIO's lately, and two pins are shared with the analog audio output jack. At first I was confused as there's no DAC on this thing, but then I saw it's PWM, literally simple PWM and a simple filter and that's how the analog output jack works. However, there's a hiss on it with low volume parts of music when using the built in analog outputs. So I found out which pins are sharing this PWM output, and tried to make my own filter, but, no matter what combination of resistors or caps I use, it's the same hiss, either louder, or softer (and sacrificing treble), but the hiss is there.

Can PWM ever sound.......good? Or would it be better to just slap on a real DAC like either a Cirrus Logic or a Burr Brown on the GPIO's and just use a real PCM DAC?

The end goal was to make a simple Pi home stereo media player, with a custom front end, using a custom enclosure, and a alpha numeric POS VFD display.

I have the DAC's required if I need to, but I was hoping to just use the PWM outputs. It'll be plugged into an 80's amplifier so while SPDIF was a thing back then, this amp does not have it.
Thanks for sharing

This post has been edited by alicechan11 on February 13, 2017 11:14 am


--------------------
PMEmail Poster
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll

 


:: support us ::




ElectronicsSkin by DutchDork & The-Force