在java中获取mp3音频信号作为数组

发布于 2024-12-15 09:07:07 字数 3795 浏览 2 评论 0 原文

我一直在尝试将 mp3 的音频流作为浮点数组获取。我已经得到了带有以下示例代码的数组。我不确定是否可以使用这个数组来应用 FFT。因为这个数组与我从使用 LAME 的 C++ 代码中获得的数组不匹配[或相似]。

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

import org.tritonus.share.sampled.FloatSampleTools;



public class onjava {



    public static void main(String[] args) {
        try {

        File file = new File("mymp3file.mp3");
        MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader();
        int fl = mpegAudioFileReader.getAudioFileFormat(file).getFrameLength();
        System.out.println(fl);
        AudioInputStream in= AudioSystem.getAudioInputStream(file);
        AudioInputStream din = null;
        AudioFormat baseFormat = in.getFormat();
        onjava oj = new onjava();
        AudioFormat decodedFormat =
            new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                            baseFormat.getSampleRate(),
                            16,
                            baseFormat.getChannels(),
                            baseFormat.getChannels() * 2,
                            baseFormat.getSampleRate(),
                            false);
        int len = (int)file.length();
        din = AudioSystem.getAudioInputStream(decodedFormat, in);

        oj.rawplay(decodedFormat, din, len, fl);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    private void rawplay(AudioFormat targetFormat, AudioInputStream din , int len, int frameLength)
            throws IOException, LineUnavailableException {
        byte[] data = new byte[len-1];
        float[] floatArray = new float[len-1];
        SourceDataLine line = getLine(targetFormat);
        File textFile = new File("outputfile.txt");
        PrintStream printStream = new PrintStream(textFile);
        System.setOut(printStream);

        if (line != null) {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, 8000);
                if (nBytesRead != -1) {
                //Please tell me if something is wrong with the arguments passed below
                    FloatSampleTools.byte2floatGeneric(data, 0, targetFormat.getFrameSize(), floatArray, 0, 1000, targetFormat);

                    for (int i = 0; i < nBytesRead; i++) {
                        if(floatArray[i] != 0.0)
                        System.out.println(floatArray[i]);
                    }

                }
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }


    }


    private SourceDataLine getLine(AudioFormat audioFormat)
            throws LineUnavailableException {
        SourceDataLine res = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
    }

}

以上代码如有错误,请指出。另外,是否有任何其他纯 java API 可用于处理 mp3 文件。我需要 mp3 音频流中的浮点数数组。

如果有的话,还请让我了解 LAME 的纯 java 实现。

谢谢!!

I have been trying to get audio stream of mp3 as array of floating points. I have got the array with the below sample code. I am not sure whether I can use this array for applying FFT. Because this array is not matching[or similar] to the one which I got from C++'s code which uses LAME.

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

import org.tritonus.share.sampled.FloatSampleTools;



public class onjava {



    public static void main(String[] args) {
        try {

        File file = new File("mymp3file.mp3");
        MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader();
        int fl = mpegAudioFileReader.getAudioFileFormat(file).getFrameLength();
        System.out.println(fl);
        AudioInputStream in= AudioSystem.getAudioInputStream(file);
        AudioInputStream din = null;
        AudioFormat baseFormat = in.getFormat();
        onjava oj = new onjava();
        AudioFormat decodedFormat =
            new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                            baseFormat.getSampleRate(),
                            16,
                            baseFormat.getChannels(),
                            baseFormat.getChannels() * 2,
                            baseFormat.getSampleRate(),
                            false);
        int len = (int)file.length();
        din = AudioSystem.getAudioInputStream(decodedFormat, in);

        oj.rawplay(decodedFormat, din, len, fl);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    private void rawplay(AudioFormat targetFormat, AudioInputStream din , int len, int frameLength)
            throws IOException, LineUnavailableException {
        byte[] data = new byte[len-1];
        float[] floatArray = new float[len-1];
        SourceDataLine line = getLine(targetFormat);
        File textFile = new File("outputfile.txt");
        PrintStream printStream = new PrintStream(textFile);
        System.setOut(printStream);

        if (line != null) {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, 8000);
                if (nBytesRead != -1) {
                //Please tell me if something is wrong with the arguments passed below
                    FloatSampleTools.byte2floatGeneric(data, 0, targetFormat.getFrameSize(), floatArray, 0, 1000, targetFormat);

                    for (int i = 0; i < nBytesRead; i++) {
                        if(floatArray[i] != 0.0)
                        System.out.println(floatArray[i]);
                    }

                }
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }


    }


    private SourceDataLine getLine(AudioFormat audioFormat)
            throws LineUnavailableException {
        SourceDataLine res = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
    }

}

Please suggest if anything is wrong in the above code. Also if any other pure java API is available for processing mp3 file. I need array of floats from mp3 audio stream.

Also let me know about pure java implementation of LAME if available.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2024-12-22 09:07:07
javazoom.spi.mpeg.sampled.file.MpegAudioFileReader

&

org.tritonus.share.sampled.FloatSampleTools

没有找到。指定您用于这些功能的库。

javazoom.spi.mpeg.sampled.file.MpegAudioFileReader

&

org.tritonus.share.sampled.FloatSampleTools

are not found. Specify the library you have used for those functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文