krispy krispy - 10 months ago 53
Android Question

Decode remote control codes through audio input

I am currently writing an Android app that functions as an internet radio. Part of the functionality of the app is that it is controllable via infrared remote. The infrared remote codes come in as analogue audio through the microphone port. If I record some of the signals and look at them in Audacity, it's very easy for me to see what each code is. For example, the following is 0111111010011001011111101000001 or 0x3F4CBF41.
enter image description here
My question is how can I programmatically detect these signals when they come in and convert them to integer code numbers. I have looked into some packaged solutions like LIRC, but they're written in C and would be difficult to integrate. It also seems to me like native would be over kill to do such simple analysis. I also looked into libraries like musicg, but I couldn't find any easy way to convert the codes in real time.

Answer Source

It seems clear from your question that you are familiar with PWM and the method of reading the signal received from the IR receiver. As such, I have looked in to how you can use java to store the data in a method you can interpret as easily as your audacity window.

There are a couple methods in javax.sound.sampled.spi That I think would help.

The first is getAudioStream() in AudioFileReader. This returns an AudioStream which has a few more methods which may prove useful. The one I see as most handy for your purpose is read() which returns the next byte in the stream. You can provide a byte array to read parts or even the whole stream in to as well if that is more convenient.

So now you have a list or array of bytes, and you can interpret them as high or low at regular intervals (because this is sampled audio, the time between each byte is constant) which is all you need for PWM decoding!

By finding an approximate endpoint, you can turn a byte pattern over time to a pattern of high/low durations. Since I know you can read that (demonstrated in your question) I will skip the pedantic conversion, but it is trivial to convert hhhlhllhhhhhhlllhh into a hexadecimal representation of the code.

TL;DR: Capture the stream, read each byte, convert via time, convert to binary.