Android提高之模拟信号示波器的实现

时间:2021-11-16 08:43:33

前面简单地介绍了android程序开发中audiorecord和audiotrack的使用,这次再结合surfaceview实现一个android版的手机模拟信号示波器。最近物联网炒得很火,作为手机软件开发者,如何在不修改手机硬件电路的前提下实现与第三方传感器结合呢?麦克风就是一个很好的adc接口,通过麦克风与第三方传感器结合,再在软件里对模拟信号做相应的处理,就可以提供更丰富的传感化应用。

先来看看本文程序运行的效果图(屏幕录像截图的速度较慢,真机实际运行起来会很流畅):

Android提高之模拟信号示波器的实现

本文程序使用8000hz的采样率,对x轴方向绘图的实时性要求较高,如果不降低x轴的分辨率,程序的实时性较差,因此程序对x轴数据缩小区间为8倍~16倍。由于采用16位采样,因此y轴数据的高度相对于手机屏幕来说也偏大,程序也对y轴数据做缩小,区间为1倍~10倍。在surfaceview的ontouchlistener方法里加入了波形基线的位置调节,直接在surfaceview控件上触摸即可控制整体波形偏上或偏下显示。

main.xml源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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="停止"
  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是实现示波器的类库,包含audiorecord操作线程和surfaceview绘图线程的实现,两个线程同步操作,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;// 线程控制标记
 /**
 * x轴缩小的比例
 */
 public int ratex = 4;
 /**
 * y轴缩小的比例
 */
 public int ratey = 4;
 /**
 * y轴基线
 */
 public int baseline = 0;
 /**
 * 初始化
 */
 public void initoscilloscope(int ratex, int ratey, int baseline) {
 this.ratex = ratex;
 this.ratey = ratey;
 this.baseline = baseline;
 }
 /**
 * 开始
 *
 * @param recbufsize
 *      audiorecord的minbuffersize
 */
 public void start(audiorecord audiorecord, int recbufsize, surfaceview sfv,
  paint mpaint) {
 isrecording = true;
 new recordthread(audiorecord, recbufsize).start();// 开始录制线程
 new drawthread(sfv, mpaint).start();// 开始绘制线程
 }
 /**
 * 停止
 */
 public void stop() {
 isrecording = false;
 inbuf.clear();// 清除
 }
 /**
 * 负责从mic保存数据到inbuf
 *
 * @author gv
 *
 */
 class recordthread extends thread {
 private int recbufsize;
 private audiorecord audiorecord;
 public recordthread(audiorecord audiorecord, int recbufsize) {
  this.audiorecord = audiorecord;
  this.recbufsize = recbufsize;
 }
 public void run() {
  try {
  short[] buffer = new short[recbufsize];
  audiorecord.startrecording();// 开始录制
  while (isrecording) {
   // 从mic保存数据到缓冲区
   int bufferreadresult = audiorecord.read(buffer, 0,
    recbufsize);
   short[] tmpbuf = new short[bufferreadresult / ratex];
   for (int i = 0, ii = 0; i < tmpbuf.length; i++, ii = i
    * ratex) {
   tmpbuf[i] = buffer[ii];
   }
   synchronized (inbuf) {//
   inbuf.add(tmpbuf);// 添加数据
   }
  }
  audiorecord.stop();
  } catch (throwable t) {
  }
 }
 };
 /**
 * 负责绘制inbuf中的数据
 *
 * @author gv
 *
 */
 class drawthread extends thread {
 private int oldx = 0;// 上次绘制的x坐标
 private int oldy = 0;// 上次绘制的y坐标
 private surfaceview sfv;// 画板
 private int x_index = 0;// 当前画图所在屏幕x轴的坐标
 private paint mpaint;// 画笔
 public drawthread(surfaceview sfv, paint mpaint) {
  this.sfv = sfv;
  this.mpaint = mpaint;
 }
 public void run() {
  while (isrecording) {
  arraylist<short[]> buf = new arraylist<short[]>();
  synchronized (inbuf) {
   if (inbuf.size() == 0)
   continue;
   buf = (arraylist<short[]>) inbuf.clone();// 保存
   inbuf.clear();// 清除
  }
  for (int i = 0; i < buf.size(); i++) {
   short[] tmpbuf = buf.get(i);
   simpledraw(x_index, tmpbuf, ratey, baseline);// 把缓冲区数据画出来
   x_index = x_index + tmpbuf.length;
   if (x_index > sfv.getwidth()) {
   x_index = 0;
   }
  }
  }
 }
 /**
  * 绘制指定区域
  *
  * @param start
  *      x轴开始的位置(全屏)
  * @param buffer
  *      缓冲区
  * @param rate
  *      y轴数据缩小的比例
  * @param baseline
  *      y轴基线
  */
 void simpledraw(int start, short[] buffer, int rate, int baseline) {
  if (start == 0)
  oldx = 0;
  canvas canvas = sfv.getholder().lockcanvas(
   new rect(start, 0, start + buffer.length, sfv.getheight()));// 关键:获取画布
  canvas.drawcolor(color.black);// 清除背景
  int y;
  for (int i = 0; i < buffer.length; i++) {// 有多少画多少
  int x = i + start;
  y = buffer[i] / rate + baseline;// 调节缩小比例,调节基准线
  canvas.drawline(oldx, oldy, x, y, mpaint);
  oldx = x;
  oldy = y;
  }
  sfv.getholder().unlockcanvasandpost(canvas);// 解锁画布,提交画好的图像
 }
 }
}

testoscilloscope.java是主程序,控制ui和clsoscilloscope,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.testoscilloscope;
import android.app.activity;
import android.graphics.color;
import android.graphics.paint;
import android.media.audioformat;
import android.media.audiorecord;
import android.media.mediarecorder;
import android.os.bundle;
import android.view.motionevent;
import android.view.surfaceview;
import android.view.view;
import android.view.view.ontouchlistener;
import android.widget.button;
import android.widget.zoomcontrols;
public class testoscilloscope extends activity {
  /** called when the activity is first created. */
 button btnstart,btnexit;
 surfaceview sfv;
  zoomcontrols zctlx,zctly;
  
  clsoscilloscope clsoscilloscope=new clsoscilloscope();
  
 static final int frequency = 8000;//分辨率
 static final int channelconfiguration = audioformat.channel_configuration_mono;
 static final int audioencoding = audioformat.encoding_pcm_16bit;
 static final int xmax = 16;//x轴缩小比例最大值,x轴数据量巨大,容易产生刷新延时
 static final int xmin = 8;//x轴缩小比例最小值
 static final int ymax = 10;//y轴缩小比例最大值
 static final int ymin = 1;//y轴缩小比例最小值
 
 int recbufsize;//录音最小buffer大小
 audiorecord audiorecord;
 paint mpaint;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    //录音组件
 recbufsize = audiorecord.getminbuffersize(frequency,
  channelconfiguration, audioencoding);
 audiorecord = new audiorecord(mediarecorder.audiosource.mic, frequency,
  channelconfiguration, audioencoding, recbufsize);
 //按键
 btnstart = (button) this.findviewbyid(r.id.btnstart);
 btnstart.setonclicklistener(new clickevent());
 btnexit = (button) this.findviewbyid(r.id.btnexit);
 btnexit.setonclicklistener(new clickevent());
 //画板和画笔
 sfv = (surfaceview) this.findviewbyid(r.id.surfaceview01);
 sfv.setontouchlistener(new touchevent());
    mpaint = new paint();
    mpaint.setcolor(color.green);// 画笔为绿色
    mpaint.setstrokewidth(1);// 设置画笔粗细
    //示波器类库
    clsoscilloscope.initoscilloscope(xmax/2, ymax/2, sfv.getheight()/2);
    
    //缩放控件,x轴的数据缩小的比率高些
 zctlx = (zoomcontrols)this.findviewbyid(r.id.zctlx);
 zctlx.setonzoominclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if(clsoscilloscope.ratex>xmin)
   clsoscilloscope.ratex--;
  settitle("x轴缩小"+string.valueof(clsoscilloscope.ratex)+"倍"
   +","+"y轴缩小"+string.valueof(clsoscilloscope.ratey)+"倍");
  }
 });
 zctlx.setonzoomoutclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if(clsoscilloscope.ratex<xmax)
   clsoscilloscope.ratex++;
  settitle("x轴缩小"+string.valueof(clsoscilloscope.ratex)+"倍"
   +","+"y轴缩小"+string.valueof(clsoscilloscope.ratey)+"倍");
  }
 });
 zctly = (zoomcontrols)this.findviewbyid(r.id.zctly);
 zctly.setonzoominclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if(clsoscilloscope.ratey>ymin)
   clsoscilloscope.ratey--;
  settitle("x轴缩小"+string.valueof(clsoscilloscope.ratex)+"倍"
   +","+"y轴缩小"+string.valueof(clsoscilloscope.ratey)+"倍");
  }
 });
 
 zctly.setonzoomoutclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if(clsoscilloscope.ratey<ymax)
   clsoscilloscope.ratey++;
  settitle("x轴缩小"+string.valueof(clsoscilloscope.ratex)+"倍"
   +","+"y轴缩小"+string.valueof(clsoscilloscope.ratey)+"倍");
  }
 });
  }
 @override
 protected void ondestroy() {
 super.ondestroy();
 android.os.process.killprocess(android.os.process.mypid());
 }
 
 /**
 * 按键事件处理
 * @author gv
 *
 */
 class clickevent implements view.onclicklistener {
 @override
 public void onclick(view v) {
  if (v == btnstart) {
  clsoscilloscope.baseline=sfv.getheight()/2;
  clsoscilloscope.start(audiorecord,recbufsize,sfv,mpaint);
  } else if (v == btnexit) {
  clsoscilloscope.stop();
  }
 }
 }
 /**
 * 触摸屏动态设置波形图基线
 * @author gv
 *
 */
 class touchevent implements ontouchlistener{
 @override
 public boolean ontouch(view v, motionevent event) {
  clsoscilloscope.baseline=(int)event.gety();
  return true;
 }
 }
}

希望本文实例对于读者进行android项目开发能起到一定的借鉴与帮助作用。