Java媒体框架:从mp3文件中提取音频信息

3
我正在分析音乐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,但对我来说很困惑。
谢谢。
1个回答

3

我刚刚快速浏览了JMF API,所以我不能百分之百确定这个方法是否正确或者是否能够正常工作,但你可以尝试以下方法:

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

尝试从 DataSource 中获取控件:ds.getControls(),看看是否有任何控件允许您读取原始音频数据。

在完成音频读取后,您可能还需要进行各种清理,例如 ds.disconnect()

此外,不要忘记安装JMF MP3插件

--Lauri


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接