improved sample helper (normalization is now possible)

This commit is contained in:
Raybz@Raybz 2013-06-13 11:50:47 +02:00
parent 153452d10e
commit a9ffb6b044

View File

@ -100,11 +100,31 @@ public class SampleHelper {
/**
* Creates a SampleBuffer from the data provided by the given decoder.
*
*
* @param deocder The decoder to read from
* @returns The new SampleBuffer
*/
public static SampleBuffer createSampleBuffer(Decoder decoder) {
return new SampleBuffer(readAllSamples(decoder), decoder.getSamplingRate());
}
/**
* Normalizes a given float array.
*
* @param data The float array to normalize
* @param lowScale The lower bound of the normalized array
* @param highScale The higher bound of the normalized array
*/
public static void normalize(float[] data, float lowScale, float highScale) {
float minValue = Float.MAX_VALUE;
float maxValue = Float.MIN_VALUE;
for (float f : data) {
minValue = Math.min(minValue, f);
maxValue = Math.max(maxValue, f);
}
for (int i = 0; i < data.length; i++) {
data[i] = lowScale + (data[i] - minValue) * (highScale - lowScale) / (maxValue - minValue);
}
}
}