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.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion" 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 group: 'com.googlecode.soundlibs', name: 'jlayer', version: '1.0.1-1'
compile "org.apache.commons:commons-math3:3.2" compile "org.apache.commons:commons-math3:3.2"

View File

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