diff audio/java/Util.java @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children 63cefb01cbab
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/audio/java/Util.java	Wed Dec 19 22:38:28 2012 +0000
@@ -0,0 +1,87 @@
+/*
+ *	AudioSource.java	
+ *
+ *	Copyright (c) 2012, Samer Abdallah
+ *	All rights reserved.
+ *
+ *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
+ *	without even the implied warranty of MERCHANTABILITY or 
+ *	FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+package samer.audio.alt;
+
+public class Util
+{
+	/* NB. These copying functions allow NEGATIVE offset (off<0). The semantics 
+	 * of this is are that n values are copied from source to destination, writing 
+	 * into the destination starting at the negative offset, but that values before
+	 * index 0 will never be accessed, hence, they are not actually copied. Only
+	 * the last n-off values will be available in dst starting from index 0.
+	 * This is useful as it allows block-wise audio input where the block lenght
+	 * is smaller than the step length.
+	 */
+
+	public static void intToDouble(byte [] src, double [] dst, int off, int n) {
+		int i, j;
+		if (off<0) { i= 4*(-off); j=0; } else { i=0; j=off; }
+		while (j<n+off) dst[j++] = (1.0/(256.0*8388608.0))*((src[i++]&0xff) | (src[i++]&0xff)<<8 | (src[i++]&0xff)<<16 | src[i++]<<24);
+	}
+
+	public static void mediumToDouble(byte [] src, double [] dst, int off, int n) {
+		int i, j;
+		if (off<0) { i= 3*(-off); j=0; } else { i=0; j=off; }
+		while (j<n+off) dst[j++] = (1.0/8388608.0)*((src[i++]&0xff) | (src[i++]&0xff)<<8 | src[i++]<<16);
+	}
+
+	public static void shortToDouble(byte [] src, double [] dst, int off, int n) {
+		int i, j;
+		if (off<0) { i= 2*(-off); j=0; } else { i=0; j=off; }
+		while (j<n+off) dst[j++] = (1.0/32768.0)*((src[i++]&0xff) | src[i++]<<8);
+	}
+
+	public static void byteToDouble(byte [] src, double [] dst, int off, int n) {
+		int i, j;
+		if (off<0) { i= -off; j=0; } else { i=0; j=off; }
+		while (j<n+off) dst[j++] = (1.0/128.0)*src[i++];
+	}
+
+	public static void doubleToInt(double[] src, byte [] dst, int off, int n, double k) {
+		k*=65536.0*32768;
+		for (int i=0, j=off; j<n+off; j++) {
+			int y = (int)(k*src[j]);
+			dst[i++] = (byte)(y&0xff);
+			dst[i++] = (byte)(y>>8 & 0xff);
+			dst[i++] = (byte)(y>>16 & 0xff);
+			dst[i++] = (byte)(y>>24);
+		}
+	}
+
+	public static void doubleToMedium(double[] src, byte [] dst, int off, int n, double k) {
+		k*=256.0*32768;
+		for (int i=0, j=off; j<n+off; j++) {
+			int y = (int)(k*src[j]);
+			dst[i++] = (byte)(y&0xff);
+			dst[i++] = (byte)(y>>8 & 0xff);
+			dst[i++] = (byte)(y>>16);
+		}
+	}
+
+	public static void doubleToShort(double[] src, byte [] dst, int off, int n, double k) {
+		k*=32768;
+		for (int i=0, j=off; j<n+off; j++) {
+			int y = (int)(k*src[j]);
+			dst[i++] = (byte)(y&0xff);
+			dst[i++] = (byte)(y>>8 & 0xff);
+		}
+	}
+
+	public static void doubleToByte(double [] src, byte[] dst, int off, int n, double k) {
+		k*=128;
+		for (int i=0, j=off; j<n+off; j++) {
+			int y = (int)(k*src[j]) + 128;
+			dst[i++] = (byte)(y&0xff);
+		}
+	}
+}
+