From a9ffb6b044ec077ecd855e870e6825fff11de86c Mon Sep 17 00:00:00 2001 From: "Raybz@Raybz" Date: Thu, 13 Jun 2013 11:50:47 +0200 Subject: [PATCH] improved sample helper (normalization is now possible) --- .../org/wyrez/audio/util/SampleHelper.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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); + } + } }