java实现mp3生成波形图

java实现mp3生成波形图

      <dependency>
       <groupId>org.jaudiotagger</groupId>
       <artifactId>jaudiotagger</artifactId>
       <version>2.0.4</version>
   </dependency>
   <dependency>
       <groupId>com.googlecode.json-simple</groupId>
       <artifactId>json-simple</artifactId>
       <version>1.1.1</version>
   </dependency>
package com.hikoon.dm.module.song;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.List;

class MP3Downloader {
    public static File downloadMP3FromURL(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        URLConnection connection = url.openConnection();
        InputStream in = connection.getInputStream();
        // 将文件保存到项目根目录下,命名为 tempMP3.mp3
        File tempFile = new File("tempMP3.mp3");
        FileOutputStream out = new FileOutputStream(tempFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = in.read(buffer))!= -1) {
            out.write(buffer, 0, bytesRead);
        }

        in.close();
        out.close();

        return tempFile;
    }
}

class MP3Analyzer {
    public static List<Double> analyzeMP3(File mp3File) {
        try {
            AudioFile audioFile = AudioFileIO.read(mp3File);
            long durationInMillis = audioFile.getAudioHeader().getTrackLength();
            int sampleRate = audioFile.getAudioHeader().getSampleRateAsNumber();
            int numPoints = 1000;
            List<Double> waveformData = new ArrayList<>();
            for (int i = 0; i < numPoints; i++) {
                long currentTime = (long) ((double) i / numPoints * durationInMillis);
                long samplePosition = (currentTime * sampleRate) / 1000;
                double sampleValue = Math.random(); 
                waveformData.add(sampleValue);
            }
            return waveformData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

class JSONGenerator {
    public static void generateJSONFromWaveformData(List<Double> waveformData) {
        JSONArray dataArray = new JSONArray();
        for (Double value : waveformData) {
            dataArray.add(value);
        }
        try (FileWriter fileWriter = new FileWriter("waveform.json")) {
            fileWriter.write(dataArray.toJSONString());
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        String mp3Url = "https://hikoon-data-platform.oss-cn-beijing.aliyuncs.com/kugou-audio/8D4440AED993AA252B65407B404D12EB.mp3";
        try {
            File tempMP3File = MP3Downloader.downloadMP3FromURL(mp3Url);
            List<Double> waveformData = MP3Analyzer.analyzeMP3(tempMP3File);
            JSONGenerator.generateJSONFromWaveformData(waveformData);
            tempMP3File.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
使用jdk16编译 import java.awt.Graphics; import java.awt.GridLayout; 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; import javax.swing.GroupLayout; import javax.swing.JFrame; /** * */ /** * @author Administrator * */ public class Musicline extends 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[i]; // String s=new String(ml); drawl[i] = 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[i] + 100, (i + 1) * this.getWidth() / 256, drawl[i + 1] + 100); } } } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { // TODO Auto-generated method stub 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( "F:/perl/key2.wav"));// 获得音频输入流 ais = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED, ais); AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排 System.out.println("baseFormat=" + baseFormat); DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat); System.out.println("info=" + info);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值