Java实现歌曲可视化效果

更新时间:2023-07-10 01:24:01 阅读: 评论:0

Java实现歌曲可视化效果
mport 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;
Graphics();
}
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(Width()/256,drawl+100,(i+1)*Width()/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 = AudioInputStream(new File( "E:/a.wav"));// 获得⾳频输⼊流
AudioInputStream(AudioFormat.Encoding.PCM_SIGNED,ais);
AudioFormat baFormat = Format();// 指定声⾳流中特定数据安排
System.out.println("baFormat="+baFormat);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, baFormat);
System.out.println("info="+info);
SourceDataLine line = (SourceDataLine) Line(info);
// 从混频器获得源数据⾏
System.out.println("line="+line);
line.open(baFormat);// 打开具有指定格式的⾏,这样可使⾏获得所有所需的系统资源并变得可操作。
line.start();// 允许数据⾏执⾏数据 I/O
int BUFFER_SIZE = 256;
audioData = new byte[BUFFER_SIZE];
while (intBytes != -1)
{
intBytes = ad(audioData, 0, BUFFER_SIZE);// 从⾳频流读取指定的最⼤数量的数据字节,并将其放⼊给定的字节数组中。
if (intBytes >= 0)
{
int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据⾏将⾳频数据写⼊混频器。
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapd" desc=" ⽣成的代码 ">
private void initComponents()
{
tDefaultCloOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().tLayout(layout);
layout.tHorizontalGroup(
layout.ateParallelGroup(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.tVisible(true);
Thread yh=new Thread(msl);
yh.start();
msl.play();
}
}
package com.xiaoer.rvice;
import java.awt.*;
import javax.swing.*;
public class Func extends JFrame
{
/**
*<br>⽅法说明:主⽅法滞笨
*<br>输⼊参数:
*<br>返回类型:
*/
广西阳朔public static void main(String[] args)
{
Func db = new Func();
db.update();
}
/
**
*<br>⽅法说明:构造器,显⽰窗体
*<br>输⼊参数:
*<br>返回类型:
牛肉滑蛋*/
Func()
{
super("Function");
tDefaultCloOperation(JFrame.DISPOSE_ON_CLOSE);  tSize(310,310);
黑虎泉show();
}
/
**
*<br>⽅法说明:更新画⾯
*<br>输⼊参数:
*<br>返回类型:
*/
public void update()
{
repaint();
}
/**
*<br>⽅法说明:转换坐标点
*<br>输⼊参数:
*<br>返回类型:
*/
double f(double x)
{
return (Math.sin(2*x)+s(3*x));
}
/**
*<br>⽅法说明:绘制坐标图
*<br>输⼊参数:
*<br>返回类型:
*/
public void paint(Graphics g)
{
double x0,x1,y0,y1,xd,yd;
double max=5.0;
int w,h,hw,hh;
//获取屏幕的尺⼨
w=getSize().width;
h=getSize().height;
hw=w/2; hh=h/2;
//在屏幕上输出字符
g.drawString("Sin[2x]+Cos[3x]",10,40);
g.d);
g.d);
g.drawString("0",hw+5,hh+12);
g.drawString(""+max,hw-20,40);
g.drawString(""+max,w-20,hh+12);
//绘制X轴坐标
g.drawLine(0,hh,w,hh);
//绘制Y轴坐标
g.drawLine(hw,0,hw,h);
xd=2*max/w;//计算X增量
yd=hh/max; //计算y增量
俞山岛g.tColor(Color.blue);
//使⽤增量绘制⼀⼩段线,最终组成曲线图形。        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));        }
}
}

本文发布于:2023-07-10 01:24:01,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1088271.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   指定   坐标   获得   屏幕   说明   绘制   系统资源
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图