Android audio port communication (I)-2PSK signal modulation

Source: Internet
Author: User

Reprinted, please indicate the source and author of the article!

Source: http://blog.csdn.net/xl19862005

By xandy)

I. Preface

Previously has been in the blog Park mixed (Address: http://www.cnblogs.com/xl19862005), recently moved to csdn, due to the previous few months just new job, for a long time did not update the blog, sorry!

Today, I want to share with you an android audio port communication project that I recently developed with fully proprietary intellectual property rights, and seek cooperation from interested colleagues and friends!

We all know La Kara, But I believe few people can say a thing or two about its specific technical implementation. It took me a month or two to embrace the spirit of a technical man who was brave enough to meet the problem, it is completely transparent and has initially implemented full-duplex communication using the audio port of the Android mobile phone (headphone output input). Once the communication protocol is established, there are more fun things to come up !!

You don't have to worry about your TV, air conditioner, and other devices that use infrared remote control for a moment, but you cannot find the remote control. In addition, I have developed the audio port extension header, insert it into the ear port of your Android phone and install a special app. You can use it to remotely control any electrical appliance with the infrared remote control function in your home!

You can also leave the monotonous ear plug and the corresponding extension header to know the temperature, humidity, and other indicators of your surroundings in real time ......

There are countless such applications. I will not list them one by one here. Let's turn to our topic below (^_^ sorry, I have a lot to talk about, haha ......)

2. Communication basis-signals transmitted online on headphones

We know that headphones are used to listen to music and make calls. Since they are sound-related, audio signals are transmitted online, common audio signals are generally within the range of 100Hz-10 kHz, so the amplitude-frequency characteristics (bandwidth) of the audio output system (DA and Audio Amplifier) in the mobile phone) it must also be in this scope (this is my conjecture that due to limited equipment and equipment, there is no system test, interested friends can use the relevant testing equipment for testing), then, since there is bandwidth, good guy, we can achieve our communication channel through this band! It is also worth mentioning that the audio signal transmitted online on the headset is communication!

Next, let's take a look at the pin definitions of the common in-ear monitoring plug-ins (Public headers) on the market. For Android phones, most of the headphones are 5mm quad-core, which are: the earth, left channel, right channel, and line control switch (MIC) are arranged as follows:

1. National Standards


2. International standards

The mic And Gnd of the international standard earphone slot are opposite to the domestic standard. Others are the same!

It is not difficult to find that there is a left-right channel, and the corresponding communication line is supported for power supply and data transmission to the peripheral expansion header. In addition, the MIC signal channel is used to input the voice when a call is made, then the mobile phone receives data with support from the hardware channel!

3. How to modulated data

When it comes to signal modulation and demodulation, many people will think of the Radio. Yes, the reason why the radio can hear singing thousands of miles away is that it uses electromagnetic waves as the transmission media, loads the signal of a sound source to an electromagnetic wave through specific modulation, and then transmits the signal to a wide space. The modulation of a radio includes AM and FM) the modulation is divided into Analog modulation and digital modulation. Analog modulation directly loads analog signals (such as voice) into electromagnetic waves, this changes the characteristics of electromagnetic waves with the changes of sound sources. Digital Modulation is developed only recently, especially the development of DSP (yes, digital signal processing) technology, in digital modulation, FM has 2PSK (binary modulation), 4psk (4-phase modulation), 8PSK (8-phase modulation), and so on.

In the digital system, the binary data represented by 0 and 1 is used. Here, I use 2PSK as the signal modulation.

3.1 What is 2PSK

2 (Binary System) frequency-Shift Keying (Binary Frequency Shift Keying) is to use 0 and 1 in binary to control the carrier frequency, to achieve the purpose of communication! For example, we use 1 ms long, 1 kHz sine signal to represent 1 in binary; 1 ms long, 2 kHz sine to represent 0 in binary; then, a series of signals of 1 kHz and 2 kHz are demodulated by a series of signals of 0 and 1. Has this achieved the goal of binary modulation?

Next let's look at a GIF animation with a more image.


3.2 2PSK in Android (pure software Modulation)

To achieve 2PSK, first we need to obtain a base-level sine signal generator in the Android system. With this base-level sine signal generator, we only need to give it a binary value (0 or 1 ), the output frequency can be changed to achieve the desired signal modulation.

The following figure shows the signal output waveform after modulation: (the actual output waveform is yellow, and the Red Square Wave is the binary data that I used later on p)


This is part of the communication protocol header I defined: 0x5aa5

3.3 audio playback in Android

The basic steps for playing audio in Android are as follows:

① Obtain the minimum buffer size for the corresponding audio Sampling Rate

public static int pwMinBufferSize = AudioTrack.getMinBufferSize(EncoderCore.getPowerSupplySamplerate(),AudioFormat.CHANNEL_OUT_STEREO,AudioFormat.ENCODING_PCM_8BIT);

② Create audiotrack

AudioTrack pwAT = new AudioTrack(AudioManager.STREAM_MUSIC,EncoderCore.getPowerSupplySamplerate(),AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_8BIT, pwMinBufferSize*2,AudioTrack.MODE_STATIC);

③ Write the audio data in the buffer into the audio playing thread and set the left-right audio volume.

Pwat. write (carriersignal, 0, encodercore. getpowersupplybuffersize (); // write the audio data pwat. flush (); // refresh pwat. setstereovolume (1, 0); // sets the left-right audio playback volume pwat. setlooppoints (0, encodercore. getpowersupplybuffersize (),-1); // sets the audio playback cycle point pwat. play (); // start playing

This part of the code is left-channel power supply for the external extension header, so you need to set loop playback, if it is to send messages, it does not need to loop!
The signal modulation is here. The next section will share the 2PSK signal demodulation.

 

Example source code: http://download.csdn.net/detail/xl19862005/5016843

Latest variant modulation and demodulation APK

Http://download.csdn.net/download/xl19862005/5333232


Related Article

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.