Implement simple oscilloscope through the recording function

Source: Internet
Author: User

Some people used the PC sound card input (microphone hole) to simulate the oscilloscope, but few were implemented using mobile phones. Using MMAPI of j2m's to simulate the oscilloscope, the specific effect is slightly inferior to that of a smart machine, because the smart machine can read the microphone input stream in real time, while j2m's still need a short buffer to constitute a blocking, however, it is enough to implement it.

First Post:

 

The picture on the left shows the program in WTK.

The running result is the waveform of the audio input port read by Audition on the right. The signal source is a pressure sensor that is amplified by a signal.

The program uses the NetBeans + LWUIT class library, and then paste all the code:

View plaincopy to clipboardprint?
Import com. sun. lwuit. Command;
Import com. sun. lwuit. Display;
Import com. sun. lwuit. Form;
Import com. sun. lwuit. events. ActionEvent;
Import com. sun. lwuit. events. ActionListener;
Import com. sun. lwuit. layouts. BorderLayout;
Import java. io. ByteArrayOutputStream;
Import javax. microedition. media. Manager;
Import javax. microedition. media. Player;
Import javax. microedition. media. control. RecordControl;

/**
* @ Author Zhang Guowei
*/
Public class Frm_MainMenu extends javax. microedition. midlet. MIDlet implements ActionListener {
Public Form form;
Private Command cmdExit = new Command ("quit", 1 );
Private ThreadReceive threadReceive = new ThreadReceive (); // receives data from the thread.
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 (); // achieve full screen effect
Cmp_HeartWave = new Cmp_Wave (form. getHeight (), form. getWidth ());
Form. getStyle (). setBgImage (null); // The background is not required for this form.
Form. addCommand (cmdExit );
Form. setCommandListener (this );
Form. setLayout (new BorderLayout ());
// Set the canvas Control
Form. addComponent (BorderLayout. CENTER, cmp_HeartWave); // Add a control
Form. show ();
Try {
CapturePlayer = Manager. createPlayer ("capture: // audio? Rate = 8000 & bits = 8 & channels = 1 "); // PCM, 8-bit, 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 (); // start the thread
}
/*
* Convert byte to int function, because the byte range of JAVA is from-127 ~ 127
*/
Public static int unsignedByteToInt (byte B ){
Return (int) B & 0xFF;
}
Class ThreadReceive extends Thread {
Private boolean isRuning = true; // The while loop in the default thread can be executed.
Public void StopThread ()
{
IsRuning = false;
}
Public void run (){
//************************************** ***********************
// Draw the waveform data
//************************************** ***********************
Try {
CapturePlayer. start ();
While (isRuning)
{
RecordControl = (RecordControl) capturePlayer. getControl ("javax. microedition. media. control. RecordControl ");
RecordControl. setRecordStream (bos );
RecordControl. startRecord ();
Thread. sleep (25); // pause a 25 ms recording
RecordControl. stopRecord ();
RecordControl. commit ();
// Because the acquisition frequency is too high and the mobile phone cannot be fully displayed, the average filtering is required to reduce the resolution.
Int Zoom_out = 200; // scale down by 200 times
Int [] bits = new int [bos. toByteArray (). length/Zoom_out];
For (int I = 0, total = 0, index = 0; I <bos. toByteArray (). length; I ++)

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.