diff --git a/AudioProcessing/src/main/java/org/wyrez/audio/util/SampleHelper.java b/AudioProcessing/src/main/java/org/wyrez/audio/util/SampleHelper.java index eb06f8e..85e77e1 100644 --- a/AudioProcessing/src/main/java/org/wyrez/audio/util/SampleHelper.java +++ b/AudioProcessing/src/main/java/org/wyrez/audio/util/SampleHelper.java @@ -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); + } + } }