Thursday, April 16, 2015

A Simple Headphone Amplifier

I was recently presented with a situation that may have need for a headphone amplifier.  The incoming signal will be from a mixing console, either an Aux or Sub output, and possibly in parallel with a power amplifier providing audio elsewhere.  And that pretty much defines the application.
So time for a little background research.
Mixing Consoles  I'd expect that most people know what a mixer/mixing console is, but for those who don't: a mixer takes in all the sound sources (microphones, guitars, keyboards, pre-recorded or streamed audio, etc.) of a performance (music, theater, radio show, etc.) and puts it all together into a either a single mono or stereo feed for the house speakers or for a broadcast (they are used in studios for all parts of recording too, but I'm pretending we're just considering the live case).  The mixer allows you to set the volumes of each part, pan the sound to the left or right speaker, and usually allow some basic EQ.  Most mixers beyond your basic ones will also have a whole host of auxiliary inputs and outputs for adding extra effects, sending out monitor mixes (so the musicians can hear what they're playing or singing), and pretty much any other reason you could think to send different mixes around.  It's these auxiliary outputs that will be of interests to us.  If you really want to know more, just google "mixing consoles" and you'll find plenty of info.
Just to get a feel for the console outputs, I looked at the specs for a number of mixer manufacturers including Mackie, Peavey, Yamaha, and Behringer and found that typical outputs are around 22dBu max output level with an source impedance of 120R.  I'll go into what that means later.
Amplifier Or perhaps more specifically, a power amplifier.  Amplifiers do pretty much what you expect; make something bigger.  But what actually is that "something"?  Well often within electrical circuits it will simply be a voltage.  However, when you want to make that voltage do something in the real world you need power, and that is where things get interesting.  You will need more than voltage alone.  Take the starter motor in your car, for instance.  The vehicle battery is only 12V but can pump out the 1.5HP (746Watts/HP) or so to get the engine started.  The 48V phantom power from a mixing console?  It can usually illuminate a few LED's.  That source impedance I mentioned earlier will come into play....
What I am showing above is the scenario I just presented.  You should notice that there is a series resistance internal to both the car battery and the mixing console phantom power supply.  In the case of the former, the internal resistance is simply a fact of life due to battery chemistry and physical interconnects and ideally is as low as possible.  For the latter, the internal resistance is intentionally added to protect the internal circuitry from supplying larger currents than intended (including short circuit loads) as well as possibly protect musicians from handling a faulty microphone that might now expose them to 48V.  I've been zapped in the face by 120V due to a combination of bad guitar amp wiring and a microphone.  It sucked.
Anyhow, for the car battery we have a low internal resistance compared to the load.  And we see that most of the power is delivered to the starter motor.  Looking at the values, the battery is pumping out 200W + 1000W for a total of 1200W.  That's quite a bit.  If you were to take a multimeter and probe the battery terminals, you would see 10V instead of 12V as shown in the diagram.  When you first turn on the starter its resistance is closer to a dead short meaning that you could measure something like 4V across that battery.  That's why all your dash lights dim.  Another thing to note is that the 200W internally absorbed turns into heat.... so don't crank on it too long.  But you get the idea, voltage sources have internal resistances that absorb some of the total power and drop some of the voltage.
The phantom power example just demonstrates the opposite.  In this case, the internal resistance dominates, leaving little for the actual load (the LED).  R_internal limits the available current and in turn limits the deliverable power.
This brings us to the "Maximum Power Transfer Theorem":  to deliver the most power, the load needs to be equal to the source impedance.  However, that doesn't mean that is the goal of a power amp design.  Let's look back to the battery.  If R_Starter was set equal to R_Battery we'd see 1800W delivered to the starter.  But we'd also see 1800W delivered to the battery's internal resistance.  Not so good.  At these power levels this is woefully inefficient.  You can afford to throw away a few mW lighting an LED, but 1800W?  You can run a microwave off of less.  And here we are just letting it go to waste.  Not to mention that it's all turning into heat....  Something to consider for design later.
Our amplifier will need to be able to deliver enough current at a given voltage to power the load: headphones.

Headphones  Seems kinda self explanatory.  The headphones convert the electrical signal to physical vibration.  Sound.  While the mechanism is actually rather simple, I won't go into how they work here as it's not critical.  Feel free to investigate on your own.  But what kind of voltage levels do you throw at headphones?  What kind of impedance do they have?  What is loud?
Well I own a pair of Sennheiser EH-150's.  Nothing fancy, but a starting point.  Fortunately, Sennheiser has specs on their website.  These headphones have a 32R impedance and a sensitivity of 115dB.  That's loud.  Even if you don't understand the math.
Let's look at the impedance first.  The mixer has an internal impedance of 120R, and from the previous discussion, we know that the smaller impedance of the headphones will mean that it'll be difficult to deliver power to them.  At least we've got a feel for that side of things.
Now let's consider the sensitivity spec.  Often this spec is given as the output in dB @ 1mW, but Sennheiser actually specs it as the output dB @ 1Vrms.  This will actually be convenient for later analysis.  So we know that a 1Vrms signal will output 115dB.  I want to stop here as this post is getting long and further discussion may need more math primers.  But, we've got a better understanding of real world values and considerations for the problem at hand.

Monday, April 13, 2015

Virtual Whammy Bar Part 6

Now that the hardware is complete it's time to program our PIC16F916 chip. In reality, I was actually writing short program snippets for each of the sub-circuits previously covered to verify their operation. After a little work it was all pieced together and tweaked to produce the working software.

So here is the general operation:

Not a whole lot going on here. There isn't even a state machine implemented, though the interrupt routine kinda resembles one.
Maybe a point of interest, when the IR strobe is turned on, the timer is set for a very short time to allow the circuit to settle. The ADC conversion is then started and the timer is then set for a much longer interval, much longer than the conversion takes in fact. It's no big deal to let the ADC result go slightly stale, as it's still too fast to be perceptible. The overall sequence of turning the IR LED on, starting the ADC, and doing something with the result happens around 120 times a second (about 8.33ms). This is actually slightly more frequent than necessary; while you can send about 1000 MIDI messages per second, 100% utilization is unrealistic if you want real-time feel. For example, if you play 100 notes simultaneously from a keyboard, the resultant audio will not be simultaneous as the last note will turn about 100ms later than the first one. And yes, that is quite noticeable. I know that I've played a keyboard thru a PC with 25-30ms latency (delay) and the disconnect between my fingers and ears makes it very difficult maintain tempo and rhythm. But my main point is that firstly you don't want to update the MIDI channel faster than you can send messages across that channel, and secondly it is not preferable to flood the channel with messages as the other side also has to respond to all those messages. Conversely, you don't want to update too slowly due to my little story above. So 8.33ms is fast enough to seem instantaneous, but 10-15 would suffice.
The LED digit displays are driven via a typical though crudely implemented software PWM. Each digit is only active half the time (since I only have one display's worth of output) but at a fast enough rate that they look continuously illuminated. I've also included adjustable brightness, so that while each digit is "active" half the time, they may only be illuminated for a portion of the time they are active, hence a PWM drive. Shown below is "half" brightness.
Not much else of interest software-wise. The buttons are checked on a software maintained timer that is.... loosely consistent. The ADC result is checked against the previous result so as not to continually send the same value if unchanged. Also, only the upper 7 bits of the 10 bit result are used (MIDI values are typically 0-127) so the three LSB's are sorta like a noise filter. I can explain the "User Interface" in a later post, but I've touched on all I've wanted to for now.