Java draws WAV audio Waveforms

Source: Internet
Author: User
Font size :{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Changefont (1 );
}
}
}
} "> Large {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Changefont (2 );
}
}
}
} "> Medium {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Changefont (3 );
}
}
}
} "> Small

Import java. AWT. graphics;
Import java. Io. file;
Import javax. Sound. sampled. audioformat;
Import javax. Sound. sampled. audioinputstream;
Import javax. Sound. sampled. audiosystem;
Import javax. Sound. sampled. dataline;
Import javax. Sound. sampled. sourcedataline;

Public class musicline extends javax. Swing. jframe implements runnable {

Private byte [] audiodata = NULL;
Private int intbytes = 0;
Private byte [] ML = new byte [1];
Private int [] drawl = NULL;
/** Creates new form musicline */
Public musicline (){
Initcomponents ();
Graphics g;
G = This. getgraphics ();
}
Public void paint (Graphics g)
{
G. clearrect (0, 0, 900,900 );
// System. Out. Print (drawl. Length );
If (audiodata! = NULL)
{
Drawl = new int [audiodata. Length];
For (INT I = 0; I <audiodata. length; I ++)
{
ML [0] = audiodata;
// String S = new string (ML );
Drawl = math. Abs (INT) ml [0]);
}
System. Out. println (drawl [0]);
For (INT I = 0; I <drawl. Length-1; I ++)
{
G. drawline (I * This. getwidth ()/256, drawl + 100, (I + 1) * This. getwidth ()/256, drawl [I + 1] + 100 );
}
}

}
Public void run ()
{
While (intbytes! =-1)
{
Try {
Synchronized (this ){
This. Wait (10 );
}
} Catch (interruptedexception ex ){
Ex. printstacktrace ();
}
Repaint ();
}
}
Public void play ()
{
Try {

Audioinputstream AIS = audiosystem. getaudioinputstream (new file (
"E:/a.wav"); // obtain the audio input stream
AIS = audiosystem. getaudioinputstream (audioformat. encoding. pcm_signed, AIS );
Audioformat baseformat = AIs. getformat (); // specifies the specific data arrangement in the audio stream
System. Out. println ("baseformat =" + baseformat );
Dataline. info = new dataline. Info (sourcedataline. Class,
Baseformat );
System. Out. println ("info =" + info );
Sourcedataline line = (sourcedataline) audiosystem. Getline (Info );
// Obtain the source data line from the mixer
System. Out. println ("line =" + line );
Line. Open (baseformat); // open the row with the specified format, so that the row can obtain all the required system resources and become operable.
Line. Start (); // allows data rows to execute data I/O
Int buffer_size = 256;

Audiodata = new byte [buffer_size];
While (intbytes! =-1 ){
Intbytes = AIs. Read (audiodata, 0, buffer_size); // read the specified maximum number of data bytes from the audio stream and put it into the given byte array.
If (intbytes> = 0 ){
Int outbytes = line. Write (audiodata, 0, intbytes); // This source data line writes audio data to the mixer.
}
}

} Catch (exception e ){
E. printstacktrace ();
}
}
/** This method is called from within the constructor
* Initialize the form.
* Warning: do not modify this Code. The content of this method is
* Always regenerated by the form editor.
*/
// <Editor-fold defaultstate = "Collapsed" DESC = "generated code">
Private void initcomponents (){

Setdefaclocloseoperation (javax. Swing. windowconstants. exit_on_close );
Javax. Swing. grouplayout layout = new javax. Swing. grouplayout (getcontentpane ());
Getcontentpane (). setlayout (layout );
Layout. sethorizontalgroup (
Layout. createparallelgroup (javax. Swing. grouplayout. Alignment. Leading)
. Addgap (0,400, short. max_value)
);
Layout. setverticalgroup (
Layout. createparallelgroup (javax. Swing. grouplayout. Alignment. Leading)
. Addgap (0,300, short. max_value)
);
Pack ();
} // </Editor-fold>

/**
* @ Param ARGs the command line arguments
*/
Public static void main (string ARGs []) {
Musicline MSL = new musicline ();
MSL. setvisible (true );
Thread YH = new thread (MSL );
YH. Start ();
MSL. Play ();

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Import java. AWT .*;
Import javax. Swing .*;
 
Public class func extends jframe {
/**
* <Br> method Description: Main Method
* <Br> input parameters:
* <Br> return type:
*/
Public static void main (string [] ARGs ){
Func DB = new func ();
DB. Update ();
}
/**
* <Br> method Description: constructor, display form
* <Br> input parameters:
* <Br> return type:
*/
Func (){
Super ("function ");
Setdefaclocloseoperation (jframe. dispose_on_close );
Setsize (310,310 );
Show ();
}
/**
* <Br> method Description: update the screen
* <Br> input parameters:
* <Br> return type:
*/
Public void Update (){
Repaint ();
}
/**
* <Br> method Description: coordinate point conversion
* <Br> input parameters:
* <Br> return type:
*/
Double F (Double X ){
Return (math. Sin (2 * x) + math. Cos (3 * X ));
}
/**
* <Br> method Description: Draw a coordinate chart.
* <Br> input parameters:
* <Br> return type:
*/
Public void paint (Graphics g ){
Double x0, X1, y0, Y1, XD, YD;
Double max = 5.0;
Int W, H, HW, HH;
// Obtain the screen size
W = getsize (). width;
H = getsize (). height;
HW = W/2; HH = H/2;
// Output characters on the screen
G. drawstring ("sin [2x] + cos [3x]", 10, 40 );
G. setcolor (color. Red );
G. drawstring ("0", HW + 5, HH + 12 );
G. drawstring ("" + Max, hw-20, 40 );
G. drawstring ("" + Max, w-20, HH + 12 );
// Draw the X axis coordinates
G. drawline (0, HH, W, HH );
// Draw Y axis
G. drawline (HW, 0, HW, H );
XD = 2 * max/W; // calculate x increments
YD = HH/max; // calculate y increments
G. setcolor (color. Blue );
// Draw a small line in increments to form a curve graph.
For (INT x = 0; x <W-1; X ++ ){
X0 =-MAX + x * XD; Y0 = f (x0) * YD;
X1 = x0 + XD; Y1 = f (X1) * YD;
G. drawline (x, (INT) (hh-y0), x + 1, (INT) (hh-y1 ));
}
}
}

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.