Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Media Framework: extract audio info from mp3 file

Tags:

java

audio

mp3

jmf

I'm analyzing music mp3 files. What I'm doing is extracting the audio data from the file and computing music similarity.

I've been using javazoom in order to handle mp3 files. By using audioFormat I'm extracting the raw data from the mp3 file:

byte[] audioBytes = new byte[numBytes];
in_format_init = AudioSystem.getAudioInputStream(musicFile);
AudioFormat formatInit = in_format_init.getFormat(); 
AudioFormat formatFinal = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            formatInit.getSampleRate(),
            16,
            formatInit.getChannels(),
            formatInit.getChannels()*2,
            formatInit.getSampleRate(),
            false);
AudioInputStream streamIn = AudioSystem.getAudioInputStream(formatFinal,     in_format_init);
while (((numBytesRead = streamIn.read(audioBytes)) != -1))
{...}

By doing this I store the audio data (without headers or tags) in audioBytes and then the info stored in the array is processed.

My question is: is it posible to extract the audio information from an mp3 audio file and store it as I do it in my example? I've been reading about JMF, but it's confusing for me.

Thanks.

like image 685
dedalo Avatar asked Apr 09 '26 15:04

dedalo


1 Answers

I've just had a quick look at the JMF API so I'm not a 100% sure this will be correct or even work at all, but try something like this:

try {
  File f = new File ("/path/to/my/audio.mp3");
  DataSource ds = Manager.createDataSource(f.toURI().toURL());
  ds.connect();
  ds.start();
  ...
} catch (java.io.IOException e) {
  ...
} catch (NoDataSourceException e) {
  ...
}

After this try getting the controls from the DataSource: ds.getControls(), and see if any of the controls allows you to read the raw audio data.

You'll probably have to do all kinds of cleanup as well, e.g. ds.disconnect(), after you're done reading the audio.

Also, don't forget to install the JMF MP3 plugin

-- Lauri

like image 198
liwp Avatar answered Apr 11 '26 03:04

liwp