2010-01-05 121 views
3

我正在分析音樂mp3文件。我正在做的是從文件中提取音頻數據並計算音樂相似度。Java Media Framework:從mp3文件中提取音頻信息

我一直在使用javazoom來處理mp3文件。通過使用AudioFormat的我是從MP3文件中提取原始數據:

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)) 
{...} 

通過這樣做,我的音頻數據(沒有標題或標籤)存儲在audioBytes,然後存儲在陣列中的信息進行處理。

我的問題是:是否可以從mp3音頻文件中提取音頻信息並存儲它,就像我在我的示例中那樣?我一直在閱讀關於JMF的知識,但這對我來說很困惑。

謝謝。

回答

3

我剛剛有了一個快速瀏覽一下JMF API,所以我不是100%肯定這將是正確的,甚至在所有的工作,但嘗試這樣的事:

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) { 
    ... 
} 

在此之後嘗試從DataSourceds.getControls()獲取控件,並查看是否有任何控件允許您讀取原始音頻數據。

您可能還需要進行各種清理,例如, ds.disconnect(),完成讀取音頻後。

另外,不要忘記安裝JMF MP3 plugin

- 勞裏