added library for audio-processing

This commit is contained in:
Raybz@Raybz committed 2013-05-16 10:43:33 +02:00
1 parent 7975382705
commit 6eabb15f07
20 files changed
+4174

No files matched your search

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.wyrez.audio.util;
/**
* Defines a band between of two frequencies.
*
* @author Darth Affe
*/
public class Band {
private float lowFrequenzy;
private float highFrequenzy;
public Band(float lowFrequenzy, float highFrequenzy) {
if (lowFrequenzy <= highFrequenzy) {
this.lowFrequenzy = lowFrequenzy;
this.highFrequenzy = highFrequenzy;
} else {
this.lowFrequenzy = highFrequenzy;
this.highFrequenzy = lowFrequenzy;
}
}
/**
* Returns the lower frequency of the band.
*
* @return The frequency as float
*/
public float getLowFrequenzy() {
return lowFrequenzy;
}
/**
* Returns the higher frequency of the band.
*
* @return The frequency as float
*/
public float getHighFrequenzy() {
return highFrequenzy;
}
}
@@ -0,0 +1,54 @@
/*
* This Code is taken from the source provided with a tutorial by
* http://www.badlogicgames.com (as of 16.05.2013).
* The original source can be found here:
* http://code.google.com/p/audio-analysis/source/browse/trunk/src/com/badlogic/audio/io/EndianDataInputStream.java
*/
package org.wyrez.audio.util;
import java.io.DataInputStream;
import java.io.InputStream;
public class EndianDataInputStream extends DataInputStream {
public EndianDataInputStream(InputStream in) {
super(in);
}
public String read4ByteString() throws Exception {
byte[] bytes = new byte[4];
readFully(bytes);
return new String(bytes, "US-ASCII");
}
public short readShortLittleEndian() throws Exception {
int result = readUnsignedByte();
result |= readUnsignedByte() << 8;
return (short) result;
}
public int readIntLittleEndian() throws Exception {
int result = readUnsignedByte();
result |= readUnsignedByte() << 8;
result |= readUnsignedByte() << 16;
result |= readUnsignedByte() << 24;
return result;
}
public int readInt24BitLittleEndian() throws Exception {
int result = readUnsignedByte();
result |= readUnsignedByte() << 8;
result |= readUnsignedByte() << 16;
if ((result & (1 << 23)) == 8388608) {
result |= 0xff000000;
}
return result;
}
public int readInt24Bit() throws Exception {
int result = readUnsignedByte() << 16;
result |= readUnsignedByte() << 8;
result |= readUnsignedByte();
return result;
}
}
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.wyrez.audio.util;
import java.util.ArrayList;
import java.util.List;
import org.wyrez.audio.decoder.Decoder;
/**
* Helper class for sample-processing.
*
* @author Darth Affe
*/
public class SampleHelper {
/**
* Converts a list of floats to an array of floats.
*
* @param samples The List to convert
* @returns The new float array
*/
public static float[] convertToFloatArray(List<Float> samples) {
float[] array = new float[samples.size()];
for (int i = 0; i < array.length; i++) {
array[i] = samples.get(i);
}
return array;
}
/**
* Creates an float array with the given length containing only the one
* given value.
*
* @param f The value to fill the array with
* @param length The length of the array
* @returns The new array
*/
public static float[] createFloatArray(float f, int length) {
float[] result = new float[length];
for (int i = 0; i < length; i++) {
result[i] = f;
}
return result;
}
/**
* Splits an array of floats.
*
* @param samples The source array to split
* @param offset The offset to split from
* @param length The length of the new array
*
* @retrn The new array
*
* @throws IllegalArgumentException Thrown if there aren't enough samples to
* proceed
*/
public static float[] splitSampleArray(float[] samples, int offset, int length)
throws IllegalArgumentException {
if (offset + length > samples.length) {
throw new IllegalArgumentException("There aren't enough samples in order to do that");
}
float[] result = new float[length];
for (int i = 0, j = offset; i < length; i++, j++) {
result[i] = samples[j];
}
return result;
}
/**
* Reads all samples mono merged from the given decoder.
*
* @param deocder The decoder to read from
* @returns The float array containing the samples
*/
public static float[] readAllSamples(Decoder decoder) {
List<Float> samples = new ArrayList<Float>();
float[] buffer = new float[1024];
while (decoder.readSamplesMergedMono(buffer) > 0) {
for (int i = 0; i < buffer.length; i++) {
samples.add(buffer[i]);
}
}
return convertToFloatArray(samples);
}
}