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

@@ -1,96 +1,16 @@
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 WavDecoder {
DataInputStream readStream;
private int channels;
private double sampleRate;
private int dataSize;
private int byteRate;
private WavInfo initialData;
public void setAudioFile(File file) throws InvalidParameterException {
try {
FileInputStream audioFile = new FileInputStream(file);
readStream = new DataInputStream(audioFile);
if (!readBytesToString(4).equals("RIFF")) {
throw new InvalidParameterException("RIFF tag not found in header.");
}
dataSize = littleEndianIntBytes();
if (!readBytesToString(4).equals("WAVE")) {
throw new InvalidParameterException("WAVE format tag not found.");
}
if (!readBytesToString(4).equals("fmt ")) {
throw new InvalidParameterException("fmt header not found.");
}
if (readStream.readByte() != 16) {
throw new InvalidParameterException("Data not pcm?");
}
readStream.skipBytes(3);
if (readStream.readByte() != 1) {
throw new InvalidParameterException("Data not pcm?");
}
readStream.skipBytes(1);
channels = readStream.readByte();
readStream.skipBytes(1);
sampleRate = littleEndianIntBytes();
byteRate = littleEndianIntBytes();
readStream.skipBytes(38);
if (!readBytesToString(4).equals("data")) {
throw new InvalidParameterException("initial data section tag not found");
}
readStream.skipBytes(4);
} catch (IOException e) {
e.printStackTrace();
}
public void setAudioFile(WavInfo info) throws InvalidParameterException, IOException {
initialData = info;
initialData.initDataStream();
initialData.getHeaderInfo();
}
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);
}
private short readLittleEndianShort() throws IOException {
short data = readStream.readShort();
return Short.reverseBytes(data);
}
public int getChannels() {
return channels;
}
public double getSampleRate() {
return sampleRate;
}
public int getByteRate() {
return byteRate;
}
public int getDataSize() {
return dataSize;
}
public int readSamples(float[] samples) {
int samplesRead = 0;
@@ -98,13 +18,18 @@ public class WavDecoder {
try {
int currentSample = 0;
for (int channel = 0; channel < channels; channel++) {
currentSample += readLittleEndianShort();
for (int channel = 0; channel < initialData.getChannels(); channel++) {
currentSample += initialData.readLittleEndianShort();
}
currentSample /= channels*Short.MAX_VALUE+1;
currentSample /= initialData.getChannels()*Short.MAX_VALUE+1;
samples[i] = currentSample;
samplesRead++;
} catch (IOException e) {
try {
initialData.closeStreams();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}

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);
}
}