possible optimizations on wav reading
This commit is contained in:
parent
d32794152d
commit
a47cd1993b
@ -22,8 +22,6 @@ public class WavAudioData implements AudioData {
|
|||||||
reset();
|
reset();
|
||||||
try {
|
try {
|
||||||
decoder = new WavDecoder(file);
|
decoder = new WavDecoder(file);
|
||||||
decoder.initDataStream();
|
|
||||||
decoder.getHeaderInfo();
|
|
||||||
} catch (InvalidParameterException | IOException e) {
|
} catch (InvalidParameterException | IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -61,7 +59,13 @@ public class WavAudioData implements AudioData {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readSamples(float[] samples) {
|
public int readSamples(float[] samples) {
|
||||||
return decoder.readSamples(samples);
|
int samplesRead = 0;
|
||||||
|
try {
|
||||||
|
samplesRead = decoder.readSamples(samples);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return samplesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,7 +7,6 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
|
|||||||
import com.badlogic.gdx.utils.Pool.Poolable;
|
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||||
|
|
||||||
import zero1hd.rhythmbullet.entity.Entity;
|
import zero1hd.rhythmbullet.entity.Entity;
|
||||||
import zero1hd.rhythmbullet.entity.ally.PolyjetEntity;
|
|
||||||
|
|
||||||
public class Pellet extends Entity implements Poolable {
|
public class Pellet extends Entity implements Poolable {
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package zero1hd.wavedecoder;
|
package zero1hd.wavedecoder;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.InvalidParameterException;
|
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
|
||||||
import com.badlogic.gdx.files.FileHandle;
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
|
|
||||||
@ -13,90 +15,17 @@ public class WavDecoder {
|
|||||||
private double sampleRate;
|
private double sampleRate;
|
||||||
private int dataSize;
|
private int dataSize;
|
||||||
private int byteRate;
|
private int byteRate;
|
||||||
private DataInputStream readStream;
|
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
private byte[] buffer;
|
||||||
|
private AudioInputStream audioInputStream;
|
||||||
public WavDecoder(FileHandle file) throws IOException {
|
public WavDecoder(FileHandle file) throws IOException {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
initDataStream();
|
try {
|
||||||
getHeaderInfo();
|
audioInputStream = AudioSystem.getAudioInputStream(file.read());
|
||||||
}
|
buffer = new byte[audioInputStream.getFormat().getFrameSize()];
|
||||||
|
} catch (UnsupportedAudioFileException e) {
|
||||||
public void getHeaderInfo() throws IOException {
|
e.printStackTrace();
|
||||||
if (!readBytesToString(4).equals("RIFF")) { //4 for RIFF tag
|
|
||||||
throw new InvalidParameterException("RIFF tag not found in header.");
|
|
||||||
}
|
}
|
||||||
dataSize = littleEndianIntBytes(); //4 for Chunk size
|
|
||||||
|
|
||||||
if (!readBytesToString(4).equals("WAVE")) { //4 for WAVE tag
|
|
||||||
throw new InvalidParameterException("WAVE format tag not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!readBytesToString(4).equals("fmt ")) { //4 for 'fmt '
|
|
||||||
throw new InvalidParameterException("fmt header not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (littleEndianIntBytes() != 16) { //subchunk1size (4 bytes)
|
|
||||||
throw new InvalidParameterException("Data not pcm?");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (readStream.readByte() != 1) { //1 pcm
|
|
||||||
throw new InvalidParameterException("Data not pcm?");
|
|
||||||
}
|
|
||||||
readStream.skip(1); //1
|
|
||||||
|
|
||||||
channels = readStream.readByte(); //1 channel count
|
|
||||||
readStream.skip(1); //1
|
|
||||||
|
|
||||||
sampleRate = littleEndianIntBytes(); //4 sample rate
|
|
||||||
|
|
||||||
byteRate = littleEndianIntBytes(); //4
|
|
||||||
|
|
||||||
readStream.skip(4);
|
|
||||||
|
|
||||||
String moreInfo = readBytesToString(4);
|
|
||||||
if (moreInfo.equals("LIST")) { // 4
|
|
||||||
while (true) {
|
|
||||||
if (readBytesToString(1).equals("d")) {
|
|
||||||
if (readBytesToString(3).equals("ata")) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (!moreInfo.equals("data")) {
|
|
||||||
throw new InvalidParameterException("failed to read data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
readStream.skip(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initDataStream() {
|
|
||||||
readStream = new DataInputStream(file.read());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void closeStreams() throws IOException {
|
|
||||||
readStream.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String readBytesToString(int bytesToRead) throws IOException {
|
|
||||||
byte byteString[] = new byte[bytesToRead];
|
|
||||||
readStream.read(byteString);
|
|
||||||
return new String(byteString);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int littleEndianIntBytes() throws IOException {
|
|
||||||
int data = readStream.readInt();
|
|
||||||
return Integer.reverseBytes(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public short readLittleEndianShort() throws IOException {
|
|
||||||
short data = readStream.readShort();
|
|
||||||
return Short.reverseBytes(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DataInputStream getReadStream() {
|
|
||||||
return readStream;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getChannels() {
|
public int getChannels() {
|
||||||
@ -127,26 +56,20 @@ public class WavDecoder {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readSamples(float[] samples) {
|
public int readSamples(float[] samples) throws IOException {
|
||||||
int samplesRead = 0;
|
int samplesRead = 0;
|
||||||
|
|
||||||
for (int i = 0; i < samples.length; i++) {
|
for (int sampleID = 0; sampleID < samples.length; sampleID++) {
|
||||||
try {
|
audioInputStream.read(buffer);
|
||||||
float currentSample = 0;
|
|
||||||
for (int channel = 0; channel < getChannels(); channel++) {
|
samples[sampleID] += (short) ((buffer[1] << 8) + (buffer[0] & 0xff));
|
||||||
currentSample += readLittleEndianShort();
|
if (audioInputStream.getFormat().getChannels() > 1) {
|
||||||
}
|
samples[sampleID] = (short) ((buffer[3] << 8) + (buffer[2] & 0xff));
|
||||||
currentSample /= getChannels() * Short.MAX_VALUE+1;
|
samples[sampleID] /= 2;
|
||||||
samples[i] = currentSample;
|
|
||||||
samplesRead++;
|
|
||||||
} catch (IOException e) {
|
|
||||||
try {
|
|
||||||
closeStreams();
|
|
||||||
} catch (IOException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
samples[sampleID] /= Short.MAX_VALUE+1;
|
||||||
|
samplesRead ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return samplesRead;
|
return samplesRead;
|
||||||
@ -154,7 +77,7 @@ public class WavDecoder {
|
|||||||
|
|
||||||
public void cleanAndClose() {
|
public void cleanAndClose() {
|
||||||
try {
|
try {
|
||||||
readStream.close();
|
audioInputStream.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user