我想从给定的mp3文件中提取字节数组,以便对后者应用快速傅立叶变换。执行的FFT将为我的宠物项目音乐推荐系统提供一些功能。
我编写了以下代码来从给定的mp3文件中提取字节:
public class TrackSample {
private static byte[] readBytesInPredefinedFormat(TargetDataLine format, InputStream inStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) > 0) {
int count = format.read(buffer, 0, buffer.length);
if (count > 0) {
byteArrayOutputStream.write(buffer, 0, count);
}
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
inStream.close();
return bytes;
}
public static byte[] getTrackBytes(String pathToTrackSample) throws IOException, LineUnavailableException {
FileInputStream fileInputStream = new FileInputStream(pathToTrackSample);
final AudioFormat format = CurrentAudioFormat.getAudioFormat(); //Fill AudioFormat with the wanted settings
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
return readBytesInPredefinedFormat(line, fileInputStream);
}
}
指定的音频格式为
public class CurrentAudioFormat {
public static AudioFormat getAudioFormat(){
float sampleRate = 44100;
int sampleSizeInBits = 8;
int channels = 1; //mono
boolean signed = true;
boolean bigEndian = true;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
}
我尝试在以下mp3文件中测试此代码:
File type ID: MPG3
Num Tracks: 1
----
Data format: 2 ch, 44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
no channel layout.
estimated duration: 104.176325 sec
audio bytes: 4167053
audio packets: 3988
bit rate: 320000 bits per second
packet size upper bound: 1052
maximum packet size: 1045
audio data file offset: 3169
optimized
audio 4591692 valid frames + 576 priming + 1908 remainder = 4594176
系统特征是:
从这个mp3文件中提取字节数组大约需要5分钟。
可能出现的瓶颈是什么?如何改进它们?
答案 0 :(得分:2)
读取您需要的字节
while ((bytesRead = inStream.read(buffer)) > -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
我不知道你为什么要读两次。
要确保您所获得的是正确的,请尝试将其重新保存为新的音频文件。
-
读取音频文件的标准方法是
AudioInputStream audioInputStream=null;
try {
audioInputStream=AudioSystem.getAudioInputStream(new File(file));
}
catch(UnsupportedAudioFileException auf) { auf.printStackTrace(); }
然后将此audioInputStream传递给您的阅读方法。