Began work on music selection UI

This commit is contained in:
2017-04-18 23:34:39 -05:00
parent fa87f5e8cf
commit 5a472b21e6
5 changed files with 203 additions and 114 deletions

View File

@@ -0,0 +1,114 @@
package zero1hd.wavedecoder;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidParameterException;
public class WavInfo {
private int channels;
private double sampleRate;
private int dataSize;
private int byteRate;
private FileInputStream audioFile;
private DataInputStream readStream;
public WavInfo(File file) throws InvalidParameterException {
try {
audioFile = new FileInputStream(file);
initDataStream();
getHeaderInfo();
closeStreams();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void getHeaderInfo() throws IOException {
if (!readBytesToString(4).equals("RIFF")) { //4
throw new InvalidParameterException("RIFF tag not found in header.");
}
dataSize = littleEndianIntBytes(); //4
if (!readBytesToString(4).equals("WAVE")) { //4
throw new InvalidParameterException("WAVE format tag not found.");
}
if (!readBytesToString(4).equals("fmt ")) { //4
throw new InvalidParameterException("fmt header not found.");
}
if (readStream.readByte() != 16) { //1
throw new InvalidParameterException("Data not pcm?");
}
readStream.skipBytes(3); //3
if (readStream.readByte() != 1) { //1
throw new InvalidParameterException("Data not pcm?");
}
readStream.skipBytes(1); //1
channels = readStream.readByte(); //1
readStream.skipBytes(1); //1
sampleRate = littleEndianIntBytes(); //4
byteRate = littleEndianIntBytes(); //4
readStream.skipBytes(38); //38
if (!readBytesToString(4).equals("data")) { // 4
throw new InvalidParameterException("initial data section tag not found");
}
}
protected void initDataStream() {
readStream = new DataInputStream(audioFile);
}
protected void closeStreams() throws IOException {
readStream.close();
audioFile.close();
}
private String readBytesToString(int bytesToRead) throws IOException {
byte byteString[] = new byte[bytesToRead];
readStream.read(byteString);
return new String(byteString);
}
private int littleEndianIntBytes() throws IOException {
int data = readStream.readInt();
return Integer.reverseBytes(data);
}
protected short readLittleEndianShort() throws IOException {
short data = readStream.readShort();
return Short.reverseBytes(data);
}
protected DataInputStream getReadStream() {
return readStream;
}
public int getChannels() {
return channels;
}
public int getByteRate() {
return byteRate;
}
public int getDataSize() {
return dataSize;
}
public double getSampleRate() {
return sampleRate;
}
public long getDurationInSeconds() {
return (long) (dataSize/sampleRate);
}
}