view audio/java/Util.java @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 63cefb01cbab
children
line wrap: on
line source
/*
 *	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 ishara.audio;

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);
		}
	}
}