Java SourceDataLine 播放音频 显示频谱
Java SourceDataLine 显示频谱
·
Java SourceDataLine 播放MP3音频 显示频谱
| 项目 | Value |
|---|---|
| 音频格式 添加依赖 | |
| *.wav | (JDK 原生支持) |
| *.pcm | (JDK 原生支持) |
| *.au | (JDK 原生支持) |
| *.aiff | (JDK 原生支持) |
| *.mp3 | mp3spi.jar |
| *.flac | jflac-codec.jar |
1 添加依赖
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5.4</version>
</dependency>
<!-- 如果需要解码播放flac文件则引入这个jar包 -->
<dependency>
<groupId>org.jflac</groupId>
<artifactId>jflac-codec</artifactId>
<version>1.5.2</version>
</dependency>
2 快速傅里叶变换
Java FFT 代码来源
Java Complex 代码来源
2.1 FFT.java
package com.xu.music.player.fft;
import java.util.stream.Stream;
public class FFT {
/**
* compute the FFT of x[], assuming its length is a power of 2
*
* @param x
* @return
*/
public static Complex[] fft(Complex[] x) {
int n = x.length;
// base case
if (n == 1) {
return new Complex[]{x[0]};
}
// radix 2 Cooley-Tukey FFT
if (n % 2 != 0) {
throw new RuntimeException("N is not a power of 2");
}
// fft of even terms
Complex[] even = new Complex[n / 2];
for (int k = 0; k < n / 2; k++) {
even[k] = x[2 * k];
}
Complex[] q = fft(even);
// fft of odd terms
Complex[] odd = even; // reuse the array
for (int k = 0; k < n / 2; k++) {
odd[k] = x[2 * k + 1];
}
Complex[] r = fft(odd);
// combine
Complex[] y = new Complex[n];
for (int k = 0; k < n / 2; k++) {
double kth = -2 * k * Math.PI / n;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + n / 2] = q[k].minus(wk.times(r[k]));
}
return y;
}
/**
* compute the inverse FFT of x[], assuming its length is a power of 2
*
* @param x
* @return
*/
public static Complex[] ifft(Complex[] x) {
int n = x.length;
Complex[] y = new Complex[n];
// take conjugate
for (int i = 0; i < n; i++) {
y[i] = x[i].conjugate();
}
// compute forward FFT
y = fft(y);
// take conjugate again
for (int i = 0; i < n; i++) {
y[i] = y[i].conjugate();
}
// divide by N
for (int i = 0; i < n; i++) {
y[i] = y[i].scale(1.0 / n);
}
return y;
}
/**
* compute the circular convolution of x and y
*
* @param x
* @param y
* @return
*/
public static Complex[] cconvolve(Complex[] x, Complex[] y) {
// should probably pad x and y with 0s so that they have same length and are powers of 2
if (x.length != y.length) {
throw new RuntimeException("Dimensions don't agree");
}
int n = x.length;
// compute FFT of each sequence,求值
Complex[] a = fft(x);
Complex[] b = fft(y);
// point-wise multiply,点值乘法
Complex[] c = new Complex[n];
for (int i = 0; i < n; i++) {
c[i] = a[i].times(b[i]);
}
// compute inverse FFT,插值
return ifft(c);
}
/**
* compute the linear convolution of x and y
*
* @param x
* @param y
* @return
*/
public static Complex[] convolve(Complex[] x, Complex[] y) {
Complex zero = new Complex(0, 0);
// 2n次数界,高阶系数为0.
Complex[] a = new Complex[2 * x.length];
for (int i = 0; i < x.length; i++) {
a[i] = x[i];
}
for (int i = x.length; i < 2 * x.length; i++) {
a[i] = zero;
}
Complex[] b = new Complex[2 * y.length];
for (int i = 0; i < y.length; i++) {
b[i] = y[i];
}
for (int i = y.length; i < 2 * y.length; i++) {
b[i] = zero;
}
return cconvolve(a, b);
}
/**
* Complex[] to double array for MusicPlayer
*
* @param x
* @return
*/
public static Double[] array(Complex[] x) {//for MusicPlayer
int len = x.length;//修正幅过小 输出幅值 * 2 / length * 50
return Stream.of(x).map(a -> a.abs() * 2 / len * 50).toArray(Double[]::new);
}
/**
* display an array of Complex numbers to standard output
*
* @param x
* @param title
*/
public static void show(Double[] x, String... title) {
for (String s : title) {
System.out.print(s);
}
System.out.println();
System.out.println("-------------------");
for (int i = 0, len = x.length; i < len; i++) {
System.out.println(x[i]);
}
System.out.println();
}
/**
* display an array of Complex numbers to standard output
*
* @param x
* @param title
*/
public static void show(Complex[] x, String title) {
System.out.println(title);
System.out.println("-------------------");
for (int i = 0, len = x.length; i < len; i++) {
// 输出幅值需要 * 2 / length
System.out.println(x[i].abs() * 2 / len);
}
System.out.println();
}
/**
* 将数组数据重组成2的幂次方输出
*
* @param data
* @return
*/
public static Double[] pow2DoubleArr(Double[] data) {
// 创建新数组
Double[] newData = null;
int dataLength = data.length;
int sumNum = 2;
while (sumNum < dataLength) {
sumNum = sumNum * 2;
}
int addLength = sumNum - dataLength;
if (addLength != 0) {
newData = new Double[sumNum];
System.arraycopy(data, 0, newData, 0, dataLength);
for (int i = dataLength; i < sumNum; i++) {
newData[i] = 0d;
}
} else {
newData = data;
}
return newData;
}
/**
* 去偏移量
*
* @param originalArr 原数组
* @return 目标数组
*/
public static Double[] deskew(Double[] originalArr) {
// 过滤不正确的参数
if (originalArr == null || originalArr.length <= 0) {
return null;
}
// 定义目标数组
Double[] resArr = new Double[originalArr.length];
// 求数组总和
Double sum = 0D;
for (int i = 0; i < originalArr.length; i++) {
sum += originalArr[i];
}
// 求数组平均值
Double aver = sum / originalArr.length;
// 去除偏移值
for (int i = 0; i < originalArr.length; i++) {
resArr[i] = originalArr[i] - aver;
}
return resArr;
}
}
2.2 Complex.java
package com.xu.music.player.fft;
import java.util.Objects;
public class Complex {
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
re = real;
im = imag;
}
// a static version of plus
public static Complex plus(Complex a, Complex b) {
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(3.0, 4.0);
Complex b = new Complex(-3.0, 4.0);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Re(a) = " + a.re());
System.out.println("Im(a) = " + a.im());
System.out.println("b + a = " + b.plus(a));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.times(b));
System.out.println("b * a = " + b.times(a));
System.out.println("a / b = " + a.divides(b));
System.out.println("(a / b) * b = " + a.divides(b).times(b));
System.out.println("conj(a) = " + a.conjugate());
System.out.println("|a| = " + a.abs());
System.out.println("tan(a) = " + a.tan());
}
// return a string representation of the invoking Complex object
@Override
public String toString() {
if (im == 0) {
return re + "";
}
if (re == 0) {
return im + "i";
}
if (im < 0) {
return re + " - " + (-im) + "i";
}
return re + " + " + im + "i";
}
// return abs/modulus/magnitude
public double abs() {
return Math.hypot(re, im);
}
// return angle/phase/argument, normalized to be between -pi and pi
public double phase() {
return Math.atan2(im, re);
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
// return a new object whose value is (this * alpha)
public Complex scale(double alpha) {
return new Complex(alpha * re, alpha * im);
}
// return a new Complex object whose value is the conjugate of this
public Complex conjugate() {
return new Complex(re, -im);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re * re + im * im;
return new Complex(re / scale, -im / scale);
}
// return the real or imaginary part
public double re() {
return re;
}
public double im() {
return im;
}
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of
// this
public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan() {
return sin().divides(cos());
}
// See Section 3.3.
@Override
public boolean equals(Object x) {
if (x == null) {
return false;
}
if (this.getClass() != x.getClass()) {
return false;
}
Complex that = (Complex) x;
return (this.re == that.re) && (this.im == that.im);
}
// See Section 3.3.
@Override
public int hashCode() {
return Objects.hash(re, im);
}
}
3 音频播放
3.1 Player.java
package com.xu.music.player.player;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioInputStream;
import java.io.File;
import java.net.URL;
/**
* Java 音频播放
*
* @author hyacinth
* @date 2019年10月31日19:06:39
*/
public interface Player {
/**
* Java Music 加载音频
*
* @param url 音频文件url
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(URL url) throws Exception;
/**
* Java Music 加载音频
*
* @param file 音频文件
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(File file) throws Exception;
/**
* Java Music 加载音频
*
* @param path 文件路径
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(String path) throws Exception;
/**
* Java Music 加载音频
*
* @param stream 音频文件输入流
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(AudioInputStream stream) throws Exception;
/**
* Java Music 加载音频
*
* @param encoding Encoding
* @param stream AudioInputStream
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(Encoding encoding, AudioInputStream stream) throws Exception;
/**
* Java Music 加载音频
*
* @param format AudioFormat
* @param stream AudioInputStream
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void load(AudioFormat format, AudioInputStream stream) throws Exception;
/**
* Java Music 暂停播放
*
* @date 2019年10月31日19:06:39
*/
void pause();
/**
* Java Music 继续播放
*
* @date 2019年10月31日19:06:39
*/
void resume();
/**
* Java Music 开始播放
*
* @throws Exception 异常
* @date 2019年10月31日19:06:39
*/
void play() throws Exception;
/**
* Java Music 结束播放
*
* @description: Java Music 结束播放
* @date 2019年10月31日19:06:39
*/
void stop();
}
3.1 XPlayer.java
package com.xu.music.player.player;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.CharSequenceUtil;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
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;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
/**
* Java 音频播放
*
* @author hyacinth
* @date 2019年10月31日19:06:39
*/
public class XPlayer implements Player {
private static SourceDataLine data = null;
private static AudioInputStream audio = null;
public static volatile LinkedList<Double> deque = new LinkedList<>();
public void put(Double v) {
synchronized (deque) {
deque.add(Math.abs(v));
if (deque.size() > 90) {
deque.removeFirst();
}
}
}
private XPlayer() {
}
public static XPlayer createPlayer() {
return XPlayer.SingletonHolder.player;
}
private static class SingletonHolder {
private static final XPlayer player = new XPlayer();
}
@Override
public void load(URL url) throws Exception {
load(AudioSystem.getAudioInputStream(url));
}
@Override
public void load(File file) throws Exception {
String name = file.getName();
if (CharSequenceUtil.endWithIgnoreCase(name, ".mp3")) {
AudioInputStream stream = new MpegAudioFileReader().getAudioInputStream(file);
AudioFormat format = stream.getFormat();
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(),
format.getChannels() * 2, format.getSampleRate(), false);
stream = AudioSystem.getAudioInputStream(format, stream);
load(stream);
} else if (CharSequenceUtil.endWithIgnoreCase(name, ".flac")) {
AudioInputStream stream = AudioSystem.getAudioInputStream(file);
AudioFormat format = stream.getFormat();
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(),
format.getChannels() * 2, format.getSampleRate(), false);
stream = AudioSystem.getAudioInputStream(format, stream);
load(stream);
} else {
load(AudioSystem.getAudioInputStream(file));
}
}
@Override
public void load(String path) throws Exception {
load(new File(path));
}
@Override
public void load(AudioInputStream stream) throws Exception {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), AudioSystem.NOT_SPECIFIED);
data = (SourceDataLine) AudioSystem.getLine(info);
data.open(stream.getFormat());
audio = stream;
}
@Override
public void load(AudioFormat.Encoding encoding, AudioInputStream stream) throws Exception {
load(AudioSystem.getAudioInputStream(encoding, stream));
}
@Override
public void load(AudioFormat format, AudioInputStream stream) throws Exception {
load(AudioSystem.getAudioInputStream(format, stream));
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void play() throws IOException {
if (null == audio || null == data) {
return;
}
data.start();
byte[] buf = new byte[4];
int channels = audio.getFormat().getChannels();
float rate = audio.getFormat().getSampleRate();
while (audio.read(buf) != -1) {
if (channels == 2) {//立体声
if (rate == 16) {
put((double) ((buf[1] << 8) | buf[0]));//左声道
//put((double) ((buf[3] << 8) | buf[2]));//右声道
} else {
put((double) buf[1]);//左声道
put((double) buf[3]);//左声道
//put((double) buf[2]);//右声道
//put((double) buf[4]);//右声道
}
} else {//单声道
if (rate == 16) {
put((double) ((buf[1] << 8) | buf[0]));
put((double) ((buf[3] << 8) | buf[2]));
} else {
put((double) buf[0]);
put((double) buf[1]);
put((double) buf[2]);
put((double) buf[3]);
}
}
data.write(buf, 0, 4);
}
}
@Override
public void stop() {
if (null == audio || null == data) {
return;
}
IoUtil.close(audio);
data.stop();
IoUtil.close(data);
}
}
4 Java SWT 显示频谱
package com.xu.music.player.test;
import cn.hutool.core.collection.CollUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.xu.music.player.fft.Complex;
import com.xu.music.player.fft.FFT;
import com.xu.music.player.player.Player;
import com.xu.music.player.player.XPlayer;
/**
* SWT Composite 绘画
*
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
public class SwtDraw {
private Shell shell = null;
private Display display = null;
private Composite composite = null;
private final Random random = new Random();
private final List<Integer> spectrum = new LinkedList<>();
public static void main(String[] args) {
SwtDraw test = new SwtDraw();
test.open();
}
/**
* 测试播放
*/
public void play() {
try {
Player player = XPlayer.createPlayer();
player.load(new File("D:\\Kugou\\梦涵 - 加减乘除.mp3"));
new Thread(() -> {
try {
player.play();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();
} catch (Exception e) {
}
}
/**
* 打开 SWT 界面
*
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
play();
task();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* 设置 SWT Shell内容
*
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
protected void createContents() {
shell = new Shell(display);
shell.setSize(900, 500);
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
// 创建一个Composite
composite = new Composite(shell, SWT.NONE);
// 添加绘图监听器
composite.addPaintListener(listener -> {
GC gc = listener.gc;
int width = listener.width;
int height = listener.height;
int length = width / 25;
if (spectrum.size() >= length) {
for (int i = 0; i < length; i++) {
draw(gc, i * 25, height, 25, spectrum.get(i));
}
}
});
}
/**
* 模拟 需要绘画的数据 任务
*
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
public void task() {
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
display.asyncExec(() -> {
if (!composite.isDisposed()) {
// 在这里调用你更新数据的方法
updateData();
// 重绘
composite.redraw();
}
});
}
}, 0, 100);
}
/**
* 模拟 更新绘画的数据
*
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
public void updateData() {
spectrum.clear();
if (CollUtil.isEmpty(XPlayer.deque)) {
return;
}
Complex[] x = new Complex[XPlayer.deque.size()];
for (int i = 0; i < x.length; i++) {
try {
x[i] = new Complex(XPlayer.deque.get(i), 0);
} catch (Exception e) {
x[i] = new Complex(0, 0);
}
}
Double[] value = FFT.array(x);
for (double v : value) {
spectrum.add((int) v);
}
}
/**
* Composite 绘画
*
* @param gc GC
* @param x x坐标
* @param y y坐标
* @param width 宽度
* @param height 高度
* @date 2024年2月2日19点27分
* @since V1.0.0.0
*/
private void draw(GC gc, int x, int y, int width, int height) {
// 设置条形的颜色
Color color = new Color(display, random.nextInt(255), random.nextInt(255), random.nextInt(255));
gc.setBackground(color);
// 绘制条形
Rectangle draw = new Rectangle(x, y, width, -height);
gc.fillRectangle(draw);
// 释放颜色资源
color. Dispose();
}
}
5 结果

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)