mp3 reading progress

This commit is contained in:
Harrison Deng 2017-05-28 19:46:25 -05:00
parent a52e5fc0ed
commit c566f6b459
2 changed files with 37 additions and 52 deletions

View File

@ -84,7 +84,6 @@ project(":core") {
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'
compile group: 'com.googlecode.soundlibs', name: 'jlayer', version: '1.0.1-1'
compile "org.apache.commons:commons-math3:3.2"

View File

@ -1,6 +1,5 @@
package zero1hd.polyjet.audio;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -19,47 +18,44 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.files.FileHandle;
import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.spi.mpeg.sampled.convert.DecodedMpegAudioInputStream;
import javazoom.jl.decoder.DecoderException;
import javazoom.jl.decoder.SampleBuffer;
public class Mp3AudioData implements AudioData {
private int readWindowSize = 1024;
private AudioInputStream din;
private AudioFormat decodedFormat;
private Music playbackMusic;
private int readIndex;
private int sampleCount;
private AudioInputStream in;
DecodedMpegAudioInputStream dStream;
private Bitstream bitStream;
Decoder decoder;
Decoder decoder = new Decoder();
private FileHandle audioFile;
public Mp3AudioData(FileHandle audioFile) {
decoder = new Decoder();
this.audioFile = audioFile;
bitStream = new Bitstream(audioFile.read());
try {
sampleCount = (int) new MP3File(audioFile.file()).getMP3AudioHeader().getNumberOfFrames();
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e1) {
e1.printStackTrace();
}
reset();
try {
File file = audioFile.file();
in = AudioSystem.getAudioInputStream(file);
din = null;
AudioInputStream in = AudioSystem.getAudioInputStream(audioFile.file());
AudioFormat baseFormat = in.getFormat();
decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
} catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
in = null;
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | UnsupportedAudioFileException e1) {
e1.printStackTrace();
}
reset();
playbackMusic = Gdx.audio.newMusic(audioFile);
}
@ -79,16 +75,13 @@ public class Mp3AudioData implements AudioData {
playbackMusic.stop();
playbackMusic.dispose();
playbackMusic = null;
}
if (din != null) {
try {
din.close();
in.close();
} catch (IOException e) {
bitStream.close();
} catch (BitstreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitStream = new Bitstream(audioFile.read());
}
}
@ -111,30 +104,23 @@ public class Mp3AudioData implements AudioData {
for (int currentSample = 0; currentSample < samples.length; currentSample++) {
for (int channel = 0; channel < decodedFormat.getChannels(); channel++) {
try {
int readCount = din.read(toBeConverted, 0, toBeConverted.length);
SampleBuffer sb = (SampleBuffer) decoder.decodeFrame(bitStream.readFrame(), bitStream);
bitStream.closeFrame();
short[] sampleBuffer = sb.getBuffer();
ByteBuffer bBuffer = ByteBuffer.allocate(2);
bBuffer.order(ByteOrder.LITTLE_ENDIAN);
bBuffer.put(toBeConverted[0]);
bBuffer.put(toBeConverted[1]);
sampleAverage += bBuffer.getShort(0);
if (readCount == -1) {
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < sampleBuffer.length; i++) {
sampleAverage += sampleBuffer[i];
}
sampleAverage /= decodedFormat.getChannels() * Short.MAX_VALUE + 1;
sampleAverage /= sampleBuffer.length * Short.MAX_VALUE + 1;
samples[currentSample] = sampleAverage;
sampleAverage = 0;
samplesRead++;
} catch (DecoderException | BitstreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return samplesRead;
}