Read mp3 binary data for visualization
Asked Answered
A

1

7

In my previous post, I had a little trouble trying to read a mp3 file. Now I am able to do that and I want to be able to render the data from the mp3 with java swing. And it would be nice to play the mp3 and visualize at the same time.

I have the binary data (which I piped to the outputstream) but I don't know how to interpret the data.

Essentially, at around LINE57, what do I need to do with the byte array so I can interpret the data as db values?

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
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;

public class MainSound {

    public static void main(final String [] args) throws Exception {
        System.out.println("Running");        
        System.out.println(System.getProperty("java.version"));        
        final AudioFileFormat.Type [] types = AudioSystem.getAudioFileTypes();
        for (final AudioFileFormat.Type t : types) {
            System.out.println("Returning Type : " + t);
        } // End of the for //                
        final String PATH = "C:\\Users\\bbrown\\Downloads\\swing-hacks-examples-20060109\\Ch10-Audio\\75\\soundcloud2.mp3";             
        final File file = new File(PATH);
        final AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));

        AudioInputStream din = null;
        final AudioFormat baseFormat = in.getFormat();
        final AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);

        System.out.println("Channels : " + baseFormat.getChannels());                
        din = AudioSystem.getAudioInputStream(decodedFormat, in);        
        rawplay(decodedFormat, din);
        in.close();       
        System.out.println("Done");
    }    

    private static synchronized void rawplay(final AudioFormat targetFormat, final AudioInputStream din) throws IOException, LineUnavailableException {              
        final byte[] data = new byte[4096];
        final SourceDataLine line = getLine(targetFormat);               
        if (line != null) {
            System.out.println("Entering ...");
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, data.length);
                if (nBytesRead != -1) {
                    // LINE57, HOW CAN INTERPRET this data for VISUALIZATION.
                    nBytesWritten = line.write(data, 0, nBytesRead);
                    System.out.println("... -->" + data[0] + " bytesWritten:" + nBytesWritten);
                }                                           
            } // End of while //            
            System.out.println("Done ...");
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        } // End of the if //
    }

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

} // End of the class //
Antihistamine answered 21/10, 2012 at 6:47 Comment(0)
T
9

Have a look at Extreme Media Player source code which is a cross Platform Media Player that supports visualizations, and of course written in Java.

A study of the code should help you understand how to read the decibels of the byte data being played. (I loved the question as I haven't seen this asked before).

As seen here:

enter image description here

UPDATE:

Its a pity Java does not natively support MP3 format, but have a look at this link showing us JMF (Java Media Framework) which is a plugin for j2SE which enables MP3 support, with help of extra MP3 Plugin for JMF.

Trotline answered 21/10, 2012 at 7:39 Comment(4)
@MadProgrammer +1 thank you, I have been wanting to code my own MediaPlayer so I had a sneek peak at the competitors ;)Trotline
Good example, readable code and exactly what I need. Still had a little trouble with the mp3 player. I had to include a mp3 plugin (external).Antihistamine
@BerlinBrown +1 thank you, yes its a pity Java does not natively support MP3 format, but have a look at this link showing us JMF (Java Media Framework) which is a plugin for j2SE which enables MP3 support: mkyong.com/applet/how-to-play-mp3-file-in-applet-jmfTrotline
Maybe I am slow, but I don't see any link with a mp3 jar. I just keep getting redirected. Oracle site bug? E.g. do you have a full path to the mp3plugin.jarAntihistamine

© 2022 - 2024 — McMap. All rights reserved.