How to Improve the analog-signal oscilloscope in Android 11th

Source: Internet
Author: User

The last time I briefly introduced the use of AudioRecord and AudioTrack, this time I used SurfaceView to implement an Android mobile phone analog signal oscilloscope (PS: I have also talked about the mobile phone oscilloscope OF THE j2_version before ). Recently, the Internet of Things has become very popular. As mobile phone software developers, how can they combine with third-party sensors without modifying the hardware circuit of mobile phones? The microphone is a good ADC interface. By combining the microphone with a third-party sensor, and then processing analog signals in the software, you can provide richer sensing applications.

Let's take a look at the program running in this article (the screen recording speed is slow, and the real machine runs more smoothly ):

In this article, the program uses the 8000hz sampling rate, which requires a high real-time performance for the x-axis drawing. If the resolution of the x-axis is not reduced, the program's real-time performance is poor. Therefore, the program reduces the x-axis data range by 8 times ~ 16 times. Because the 16-bit sampling is used, the height of the Y-axis data is also larger than that of the mobile phone screen, and the program also reduces the y-axis data, with a range of 1 ~ 10 times. The position adjustment of the waveform baseline is added to the OnTouchListener method of SurfaceView. You can directly touch the SurfaceView control to control the overall display of the waveform to the top or bottom.

The source code of main. xml is as follows:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent"
Android: orientation = "horizontal">
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnStart"
Android: text = "" android: layout_width = "80dip"> </Button>
<Button android: layout_height = "wrap_content" android: text = "stop"
Android: id = "@ + id/btnExit" android: layout_width = "80dip"> </Button>
<ZoomControls android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/zctlX"> </ZoomControls>
<ZoomControls android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/zctlY"> </ZoomControls>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_height = "fill_parent" android: layout_width = "fill_parent"> </SurfaceView>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent"
Android: orientation = "horizontal">
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnStart"
Android: text = "" android: layout_width = "80dip"> </Button>
<Button android: layout_height = "wrap_content" android: text = "stop"
Android: id = "@ + id/btnExit" android: layout_width = "80dip"> </Button>
<ZoomControls android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/zctlX"> </ZoomControls>
<ZoomControls android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/zctlY"> </ZoomControls>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_height = "fill_parent" android: layout_width = "fill_parent"> </SurfaceView>
</LinearLayout>
 

ClsOscilloscope. java is the class library that implements the oscilloscope, including the implementation of the AudioRecord operation thread and the SurfaceView drawing thread. The Code is as follows:

View plaincopy to clipboardprint?
Package com. testOscilloscope;
Import java. util. ArrayList;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. media. AudioRecord;
Import android. view. SurfaceView;
Public class ClsOscilloscope {
Private ArrayList <short []> inBuf = new ArrayList <short []> ();
Private boolean isRecording = false; // thread control flag
/**
* Scale down the X axis
*/
Public int rateX = 4;
/**
* Scale down the Y axis
*/
Public int rateY = 4;
/**
* Y-axis baseline
*/
Public int baseLine = 0;
/**
* Initialization
*/
Public void initOscilloscope (int rateX, int rateY, int baseLine ){
This. rateX = rateX;
This. rateY = rateY;
This. baseLine = baseLine;
}
/**
* Start
*
* @ Param recBufSize
* MinBufferSize of AudioRecord
*/
Public void Start (AudioRecord audioRecord, int recBufSize, SurfaceView sfv,
Paint mPaint ){
IsRecording = true;
New RecordThread (audioRecord, recBufSize). start (); // start the recording thread
New DrawThread (sfv, mPaint). start (); // start the drawing thread
}
/**
* Stop
*/
Public void Stop (){
IsRecordi

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.