views:

75

answers:

2

Is there an API in android to access the headphone hardware input? Ideally I'd like to be able to directly read the incoming voltage/current. Accessing this in either Java or the NDK would be fine. Preferably if there's a way to do this on non rooted phones.

A: 

I haven't seen one yet, but I wouldn't count on it.

An audio input goes through a band-pass filter and some automatic gain control. You can sample the audio input, but you won't be able to determine the actual voltage since the sampled value depends on the frequency of the complete signal.

Lio
Yeah I understand that, but I'm looking at using the jack as an input instead of an output. So I'd need access to the port to read voltage levels
Falmarri
Lio's answer is about input as you requested, not about output.
Chris Stratton
A: 

If you need to recover a low frequency or "DC" component which is not making it through filters (do try it and verify that it is not, you never know) then here are two things you could try:

  • Use a chopper circuit which alternately connects the input normally and then inverted, or simply alternates the input with ground. In software you will then measure the difference between the two readings and apply some pre-measured calibration.

  • Use a voltage controlled oscillator to make a voltage to frequency converter and measure the frequency of the resulting audio tone in software rather than its amplitude (ie, you will use FM modulation)

For the first, if you care about the current you will need to characterize the input impedance of the phone, or more likely use an outboard op amp circuit to present whatever impedance you would like to have to the test signal, and then drive the phone's input from the low impedance op amp output, increased as desired with a resistor. Do exercise some care to determine and not exceed maximum voltage on phone's input. For the second, the impedance presented would be a function of your voltage to frequency modulator's input.

Chris Stratton
That's all well and good, but first I need access to the hardware.
Falmarri
Actually you don't. My suggestions were all predicated on what could be done with the java audio apis. Access to the hardware is possible of course, but you are into the territory of customizing the platform rather than writing an android application. It may be as simple as using the private audio libraries, or if you have a rooted phone you can do things up to and including changing the audio drivers (though realize that the radio coprocessor for which you don't have source code is effectively acting as the sound card).
Chris Stratton
See my comment on Lio's answer. I don't want to do anything with the audio that the phone is outputting or any audio APIs. I want to have access to the hardware port so that I can use it as an input. Your answer only includes hardware discussion, which is fine and will definitely be my next step to think about, but I need to be able to capture the input first.
Falmarri
The audio that the phone is INPUTTING is the input from the port. You will need to do some measurements to be able to apply a calibration to convert back to voltage, and this probably will be device (and even serial number) specific.
Chris Stratton
Essentially, you need to model the combination of the phone hardware and the audio api's as a transfer function, and apply the inverse of that function. Up to the exception of course that your inverse can't involve dividing by zero to recover filtered out frequencies, which is what the chopping or fm modulating hardware techniques might be needed to change the signal before it goes through the transfer function so that you will be able to recover needed parts with an inverse function.
Chris Stratton
Maybe I'm missing something from your answer, but I need to know how to access the signals coming in from the jack. As far as I know, the android APIs assume that the audio jack is an output port. And if I query the audio APIs, I can get the audio levels that the phone thinks it's sending out of the audio port, and weather or not the headphones are connected. How do I make android think it's getting input from the jack?
Falmarri
The audio jack is both an input and and output, for obvious reasons that it wouldn't be much use as a telephone otherwise. Try doing some reasearch on developer.android.com
Chris Stratton