HardBirch

通过J2ME的录音功能实现简易示波器

时间:10-08-27 栏目:安卓入门与提高 作者:张飞不张,文采横飞 评论:23 点击: 8,580 次

       早就有人通过PC声卡的输入(麦克风孔)来做模拟示波器,但是用手机来实现的比较少。用J2ME的MMAPI实现模拟示波器,具体效果稍逊于智能机,因为智能机可以实时读取麦克风输入流,而J2ME还需要有短暂的缓冲构成了阻塞,不过,实现出来玩一下还是足够了。

先贴出效果图:

左图是程序在WTK运行的结果,右图是Audition读取音频输入口的波形,信号源是一个经过信号放大的压力传感器。

程序使用NetBeans + LWUIT类库,接下来贴出全部代码:










/**
* @author 张国威
*/
public class Frm_MainMenu extends javax.microedition.midlet.MIDlet implements ActionListener {
public Form form ;
private Command cmdExit = new Command("退出", 1);
private ThreadReceive threadReceive =new ThreadReceive();//接收数据线程
private Cmp_Wave cmp_HeartWave=null;
private Player capturePlayer = null;
private RecordControl recordControl = null;
private ByteArrayOutputStream bos = new ByteArrayOutputStream();
public void startApp() {
Display.init(this);

form = new Form();//达到全屏的效果
cmp_HeartWave=new Cmp_Wave(form.getHeight(),form.getWidth());
form.getStyle().setBgImage(null);//本窗体不需要背景
form.addCommand(cmdExit);
form.setCommandListener(this);
form.setLayout(new BorderLayout());
//设置画板控件
form.addComponent(BorderLayout.CENTER,cmp_HeartWave);//添加控件
form.show();
try {
capturePlayer = Manager.createPlayer("capture://audio?rate=8000&bits=8&channels=1");//PCM,8位,8kH
if (capturePlayer != null) {
capturePlayer.realize();
recordControl = (RecordControl) capturePlayer
.getControl("javax.microedition.media.control.RecordControl");
if (recordControl == null)
throw new Exception("No RecordControl available");
recordControl.setRecordStream(bos);
} else {
throw new Exception("Capture Audio Player is not available");
}
} catch (Exception e) {}
threadReceive.start();//开始启动线程
}
/*
* byte转为int的函数,因为JAVA的byte范围从-127~127
*/
public static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
class ThreadReceive extends Thread {
private boolean isRuning=true;//默认线程内部while循环可以执行
public void StopThread()
{
isRuning=false;
}
public void run(){
//*************************************************************
//绘制波形数据
//*************************************************************
try {
capturePlayer.start();
while(isRuning)
{
recordControl = (RecordControl) capturePlayer.getControl("javax.microedition.media.control.RecordControl");
recordControl.setRecordStream(bos);
recordControl.startRecord();
Thread.sleep(25);//停顿25ms录音
recordControl.stopRecord();
recordControl.commit();
//由于采集频率太高,手机不能完全显示,所以需要通过均值滤波来降低分辨率
int Zoom_out=200;//缩小200倍
int[] bits=new int[bos.toByteArray().length/Zoom_out];
for(int i=0,total=0,index=0;i<bos.toByteArray().length;i++)
{
total=total+unsignedByteToInt(bos.toByteArray()[i]);
if(i%Zoom_out==0 && i!=0)
{
bits[index]=total/Zoom_out;
total=0;
index++;
}
}
cmp_HeartWave.UpdateVerticalWave(bits);
bos.reset();
}
capturePlayer.stop();
capturePlayer.close();
} catch (Exception e) {}
}
}
public void actionPerformed(ActionEvent arg0) {
Command command=arg0.getCommand();
if(command==cmdExit)//退出程序
notifyDestroyed();
}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) {}

}

 

以下代码为画波形图的代码:





































if(point==null)//如果point还没初始化
return;//不执行下面语句
for(int i=0;i<point.length;i++)
{
if(old_xPos>=width //如果xPos超过屏幕宽度
|| old_xPos==-1) //如果PaintVerticalWave是第一次运行
{
old_xPos=0;//归零画图
old_yPos=height/2;//中心点画图
canvas.setColor(0x444444);//设置背景色
canvas.fillRect(0, 0, width, height);
}
int new_yPosPoint=point[i] ;
canvas.setColor(0xCCCCCC);//线条色
canvas.drawLine(old_xPos,old_yPos,old_xPos+1,new_yPosPoint);
old_xPos++;
old_yPos=new_yPosPoint;
}
//把内存中的波形图画到控件上
g.drawImage(imgWaveCanvas, 0, 0);
}
};
cmpWaveCanvas.setFocusPainted(false);
cmpWaveCanvas.setFocusable(false);
return cmpWaveCanvas;
}
public void UpdateVerticalWave(int []yPos) {
//更新绘图数组
int []tmpPos=new int[yPos.length];//临时数组,只取原数组的waveN分之一
for(int i=0,ii=0;i<tmpPos.length;i++,ii+=1)
tmpPos[i]=yPos[ii];
point=tmpPos;
//重新绘图
cmpWaveCanvas.repaint();
}
}
  

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: 通过J2ME的录音功能实现简易示波器

通过J2ME的录音功能实现简易示波器:目前有23 条留言

  1. [e03][e03][e03][e03]呵呵第一个做沙发&#183;&#183;&#183;

    2010-08-27 13:53 [回复]
  2. 22楼
    qigaosheng:

    猛[e01]

    2010-08-27 15:41 [回复]
  3. 我编译了一下 出错啊 一下是出错的信息
    Running with storage root C:/Users/Administrator/j2mewtk/2.5.2/appdb/temp.DefaultColorPhone128
    Running with locale: Chinese (Simplified)_People's Republic of China.936
    Running in the identified_third_party security domain
    java.lang.NoClassDefFoundError: com/sun/lwuit/uidemo/UIDemoMIDlet: com/sun/lwuit/events/ActionListener
    at com.sun.midp.midlet.MIDletState.createMIDlet(+29)
    at com.sun.midp.midlet.Scheduler.schedule(+52)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+80)
    Execution completed.
    3399893 bytecodes executed
    22 thread switches
    1706 classes in the system (including system classes)
    17867 dynamic objects allocated (551200 bytes)
    6 garbage collections (461448 bytes collected)

    2010-08-28 19:24 [回复]
  4. 还请楼主指教啊

    2010-08-28 19:25 [回复]
  5. 会不会是编译的 新旧JDK与SUN的兼容问题呢

    2010-08-28 19:25 [回复]
  6. 前面是sun的一个程序 下面是本人编译楼主的程序现实的问题
    Running with storage root C:/Users/Administrator/j2mewtk/2.5.2/appdb/temp.DefaultColorPhone129
    Running with locale: Chinese (Simplified)_People's Republic of China.936
    Running in the identified_third_party security domain
    java.lang.NoClassDefFoundError: test1/Frm_MainMenu: com/sun/lwuit/events/ActionListener
    at com.sun.midp.midlet.MIDletState.createMIDlet(+29)
    at com.sun.midp.midlet.Scheduler.schedule(+52)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+80)
    Execution completed.
    3409556 bytecodes executed
    24 thread switches
    1679 classes in the system (including system classes)
    17735 dynamic objects allocated (535180 bytes)
    3 garbage collections (459376 bytes collected)

    2010-08-28 19:29 [回复]
  7. 还请知道的联系下我 谢谢
    QQ 944711953

    2010-08-28 20:18 [回复]
  8. 16楼
    hellogv:

    回复 wangweitingaabbcc:
    我用NetBeans6.5.1设计这个程序没遇到这个问题,看看是否IDE的问题。

    2010-08-28 20:24 [回复]
  9. 在Netbeans中运行时一下结果

    Expanding: D:/NetBeans 6.8/mobility8/modules/ext/netbeans_midp_components_basic.jar into C:/Users/Administrator/Documents/NetBeansProjects/MobileApplication1/build/compiled
    Expanding: C:/Users/Administrator/Desktop/LWUIT_1_3/util/ResourceEditor.jar into C:/Users/Administrator/Documents/NetBeansProjects/MobileApplication1/build/compiled
    do-compile:
    post-compile:
    compile:
    pre-obfuscate:
    proguard-init:
    skip-obfuscation:
    proguard:
    post-obfuscate:
    obfuscate:
    lwuit-build:
    pre-preverify:
    do-preverify:
    Preverifying 5034 file(s) into C:/Users/Administrator/Documents/NetBeansProjects/MobileApplication1/build/preverified directory.
    Error preverifying class com.sun.jwt.resource.util.BlockingAction$1
    java/lang/NoClassDefFoundError: javax/swing/JComponent
    C:/Users/Administrator/Documents/NetBeansProjects/MobileApplication1/nbproject/build-impl.xml:461: Preverification failed with error code 1.
    生成失败(总时间:34 秒)

    2010-08-28 22:54 [回复]
  10. [e07]

    2010-08-28 22:55 [回复]
  11. 还是出错啊 看不出出错的原因

    2010-08-28 22:56 [回复]
  12. 12楼
    hellogv:

    回复 wangweitingaabbcc:
    你之前用过LWUIT没?

    2010-08-29 08:15 [回复]
  13. 回复 hellogv:没用过啊 但有在学j2me

    2010-08-29 15:23 [回复]
  14. 回复 hellogv:你知道那代码出错的原因吗

    2010-08-29 15:24 [回复]
  15. 回复 hellogv:我很喜欢语音判别的,所以还请您多多指教

    2010-08-29 15:26 [回复]
  16. 8楼
    hellogv:

    回复 wangweitingaabbcc:
    那你先花半个月的时间学习一下LWUIT吧,这个代码的绘图部分就是用了LWUIT类库的。LWUIT的配置和使用对于初学者来说,很容易出问题。

    2010-08-29 18:24 [回复]
  17. 回复 hellogv:谢谢了[e01]

    2010-08-29 18:32 [回复]
  18. 6楼
    a2066858:

    只用过C啊 但有在学j2me

    2010-08-30 04:01 [回复]
  19. 5楼
    a800633:

    [e08]

    2010-09-01 10:58 [回复]
  20. 好了 ,现在调试好了。但怎样让输出的波形更加类似于像播放器一样,可以通过波形进行判别呢?恳请指教![e03]

    2010-09-03 00:02 [回复]
  21. 地板
    qianguozheng:

    回复 wangweitingaabbcc:估计楼主也没有想过,或者是想了,但是没有必要!

    2010-11-08 22:36 [回复]
  22. 板凳
    tingshuai:

    楼主可以给一些麦克风ad的输入电压的资料吗?我想自己做个信号源

    2011-03-14 17:53 [回复]
  23. 沙发
    jacken2008:

    [e01][e01]

    2011-04-12 13:50 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐