wav decoding optimization works
This commit is contained in:
@@ -6,6 +6,7 @@ import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
|
||||
public class WavDecoder {
|
||||
@@ -13,16 +14,20 @@ public class WavDecoder {
|
||||
|
||||
private int channels;
|
||||
private double sampleRate;
|
||||
private int dataSize;
|
||||
private int byteRate;
|
||||
private String fileName;
|
||||
private byte[] buffer;
|
||||
private AudioInputStream audioInputStream;
|
||||
|
||||
public WavDecoder(FileHandle file) throws IOException {
|
||||
this.file = file;
|
||||
try {
|
||||
audioInputStream = AudioSystem.getAudioInputStream(file.read());
|
||||
audioInputStream = AudioSystem.getAudioInputStream(file.file());
|
||||
Gdx.app.debug("WAVDecoder", String.valueOf(audioInputStream.getFormat().getFrameSize()));
|
||||
buffer = new byte[audioInputStream.getFormat().getFrameSize()];
|
||||
|
||||
channels = audioInputStream.getFormat().getChannels();
|
||||
sampleRate = audioInputStream.getFormat().getSampleRate();
|
||||
fileName = file.name();
|
||||
} catch (UnsupportedAudioFileException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -32,20 +37,16 @@ public class WavDecoder {
|
||||
return channels;
|
||||
}
|
||||
|
||||
public int getByteRate() {
|
||||
return byteRate;
|
||||
}
|
||||
|
||||
public int getDataSize() {
|
||||
return dataSize;
|
||||
}
|
||||
|
||||
public double getSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
public long getDurationInSeconds() {
|
||||
return (long) (dataSize/byteRate);
|
||||
public float getDurationInSeconds() {
|
||||
return audioInputStream.getFrameLength()/audioInputStream.getFormat().getFrameRate();
|
||||
}
|
||||
|
||||
public long getFrameCount() {
|
||||
return audioInputStream.getFrameLength();
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
@@ -57,22 +58,23 @@ public class WavDecoder {
|
||||
}
|
||||
|
||||
public int readSamples(float[] samples) throws IOException {
|
||||
int samplesRead = 0;
|
||||
int framesRead = 0;
|
||||
|
||||
for (int sampleID = 0; sampleID < samples.length; sampleID++) {
|
||||
audioInputStream.read(buffer);
|
||||
|
||||
samples[sampleID] += (short) ((buffer[1] << 8) + (buffer[0] & 0xff));
|
||||
if (audioInputStream.getFormat().getChannels() > 1) {
|
||||
samples[sampleID] = (short) ((buffer[3] << 8) + (buffer[2] & 0xff));
|
||||
samples[sampleID] /= 2;
|
||||
if (audioInputStream.read(buffer) > 0) {
|
||||
samples[sampleID] += (buffer[1] << 8) + (buffer[0] & 0x00ff);
|
||||
if (audioInputStream.getFormat().getChannels() > 1) {
|
||||
samples[sampleID] += (buffer[3] << 8) + (buffer[2] & 0x00ff);
|
||||
samples[sampleID] /= 2;
|
||||
}
|
||||
framesRead ++;
|
||||
}
|
||||
|
||||
|
||||
samples[sampleID] /= Short.MAX_VALUE+1;
|
||||
samplesRead ++;
|
||||
}
|
||||
|
||||
return samplesRead;
|
||||
return framesRead;
|
||||
}
|
||||
|
||||
public void cleanAndClose() {
|
||||
|
Reference in New Issue
Block a user