Android uses Audiotrack to send infrared signals

Source: Internet
Author: User

Recently to do a project, the use of the phone's headphone port output infrared signal, thereby turning the phone into an infrared remote control, signal processing knowledge is basically returned to the teacher, just started really a headache. Find a lot of information to study a bit, finally a little experience, here to do a memo.

First, the audio signal output principle

The principle of output signal of audio headphone port already has the article of Daniel, refer to http://blog.csdn.net/xl19862005/article/details/8522869

To add a bit of personal understanding, the Android audio output sampling rate is generally limited to the maximum sampling rate of 48kHz 44.1khz,audiotrack source code, that is, the headphone audio port output audio frequency should be around 20kHz, basically is the Android audio output frequency limit. Infrared signal carrier is generally 38kHz, so simply want to through the audio signal is not up to the requirements, need to rely on external hardware, find a bit, found some treasure on the sale, supporting the app also has, down tried a bit, the effect is good. However, since it is their own development, need to understand the principle, anti-compilation to see a bit, the core code is native codes, or go the way, their own research how to achieve.

Find some relevant information on the Internet, the Linux platform has the relevant open source project LIRC (Linux infrared Remote Control): http://www.lirc.org/, support various types of hardware, Through the configuration file to support various types of remote control devices, TV, DVD and so on, is a relatively mature project, Google Play currently has a lot of infrared remote applications are based on this project development. But I'm only interested in the headphone port output infrared, LIRC about audio headphone port output schematic (http://www.lirc.org/html/audio.html):

Using the left and right channels of the headset, output 19kHz audio, through the left circuit diagram, output 38kHz.

Second, the realization

The principle is clear, the next realization, through the audiotrack output 19kHz sine wave shape can be. There is no overall transplant lirc to Android, one project is too large, and I just use headphones this one, the other is not used, the main reference to the audio output principle. Lirc porting to Android can refer to open source projects irdroid:http://www.irdroid.com/

Core code, Output 19kHz sine, reference foreign Daniel: Http://stackoverflow.com/questions/2413426/playing-an-arbitrary-tone-with-android

 PublicSignalprocessor (Final intfrequency) {    intBuffsize = Audiotrack.getminbuffersize ( This. Samplerate, Audioformat.channel_configuration_stereo, Audioformat.encoding_pcm_16bit) /c6>* 4; Gensignal=New byte[Buffsize]; Genspace=New byte[Buffsize];  for(intj = 0; J <buffsize;) {        DoubleDval = Math.sin (2 * Math.PI * (Double) j)/4.0/((Double) samplerate)/((Double)) (frequency)) ; Final  Shortval = ( Short) ((Dval * 32767)); Final  ShortVal_minus = ( Short) -Val; //in-bit WAV PCM, first byte is the low order byteGENSPACE[J] = 0; Gensignal[j++] = (byte) (Val & 0x00ff); GENSPACE[J]= 0; Gensignal[j++] = (byte) ((Val & 0xff00) >>> 8); GENSPACE[J]= 0; Gensignal[j++] = (byte) (Val_minus & 0X00FF); GENSPACE[J]= 0; Gensignal[j++] = (byte) ((Val_minus & 0xff00) >>> 8); } audiotrack=NewAudiotrack (Audiomanager.stream_music, This. Samplerate, Audioformat.channel_configuration_stereo, Audioformat.encoding_pcm_16bit, buffSize    , Audiotrack.mode_stream); Audiotrack.play ();} Public voidPlayFinalArraylist<integer>signalspacelist) {        BooleanSignal =true; intCount=0;  for(Integer d:signalspacelist) {Final intStop = (int) (((Double) (d * samplerate))/1000000.0) ; if(signal) for(inti = 0; I <stop;) {                                if(Stop-i <buffsize) Count= Audiotrack.write (gensignal, 0, stop-i); ElseCount= Audiotrack.write (gensignal, 0, buffsize); if(count>0) I+=count; }                Else                         for(inti = 0; I <stop;) {                                if(Stop-i <buffsize) Count= Audiotrack.write (genspace, 0, stop-i); ElseCount= Audiotrack.write (genspace, 0, buffsize); if(count>0) I+=count; } Signal= !signal; }}

Explain:

 Public void Play (final arraylist<integer> signalspacelist)

A list of incoming signals and idle time, such as the NEC code

Head signal time 9MS, idle time 4.5ms,list incoming 9000,4500

To test the headphone port mono output waveform:

After connecting the peripheral, the output square wave:

Android uses Audiotrack to send infrared signals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.