annotate 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
rev   line source
samer@0 1 /*
samer@0 2 * AudioSource.java
samer@0 3 *
samer@0 4 * Copyright (c) 2012, Samer Abdallah
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@46 12 package ishara.audio;
samer@0 13
samer@0 14 public class Util
samer@0 15 {
samer@0 16 /* NB. These copying functions allow NEGATIVE offset (off<0). The semantics
samer@0 17 * of this is are that n values are copied from source to destination, writing
samer@0 18 * into the destination starting at the negative offset, but that values before
samer@0 19 * index 0 will never be accessed, hence, they are not actually copied. Only
samer@0 20 * the last n-off values will be available in dst starting from index 0.
samer@0 21 * This is useful as it allows block-wise audio input where the block lenght
samer@0 22 * is smaller than the step length.
samer@0 23 */
samer@0 24
samer@0 25 public static void intToDouble(byte [] src, double [] dst, int off, int n) {
samer@0 26 int i, j;
samer@0 27 if (off<0) { i= 4*(-off); j=0; } else { i=0; j=off; }
samer@0 28 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);
samer@0 29 }
samer@0 30
samer@0 31 public static void mediumToDouble(byte [] src, double [] dst, int off, int n) {
samer@0 32 int i, j;
samer@0 33 if (off<0) { i= 3*(-off); j=0; } else { i=0; j=off; }
samer@0 34 while (j<n+off) dst[j++] = (1.0/8388608.0)*((src[i++]&0xff) | (src[i++]&0xff)<<8 | src[i++]<<16);
samer@0 35 }
samer@0 36
samer@0 37 public static void shortToDouble(byte [] src, double [] dst, int off, int n) {
samer@0 38 int i, j;
samer@0 39 if (off<0) { i= 2*(-off); j=0; } else { i=0; j=off; }
samer@0 40 while (j<n+off) dst[j++] = (1.0/32768.0)*((src[i++]&0xff) | src[i++]<<8);
samer@0 41 }
samer@0 42
samer@0 43 public static void byteToDouble(byte [] src, double [] dst, int off, int n) {
samer@0 44 int i, j;
samer@0 45 if (off<0) { i= -off; j=0; } else { i=0; j=off; }
samer@0 46 while (j<n+off) dst[j++] = (1.0/128.0)*src[i++];
samer@0 47 }
samer@0 48
samer@0 49 public static void doubleToInt(double[] src, byte [] dst, int off, int n, double k) {
samer@0 50 k*=65536.0*32768;
samer@0 51 for (int i=0, j=off; j<n+off; j++) {
samer@0 52 int y = (int)(k*src[j]);
samer@0 53 dst[i++] = (byte)(y&0xff);
samer@0 54 dst[i++] = (byte)(y>>8 & 0xff);
samer@0 55 dst[i++] = (byte)(y>>16 & 0xff);
samer@0 56 dst[i++] = (byte)(y>>24);
samer@0 57 }
samer@0 58 }
samer@0 59
samer@0 60 public static void doubleToMedium(double[] src, byte [] dst, int off, int n, double k) {
samer@0 61 k*=256.0*32768;
samer@0 62 for (int i=0, j=off; j<n+off; j++) {
samer@0 63 int y = (int)(k*src[j]);
samer@0 64 dst[i++] = (byte)(y&0xff);
samer@0 65 dst[i++] = (byte)(y>>8 & 0xff);
samer@0 66 dst[i++] = (byte)(y>>16);
samer@0 67 }
samer@0 68 }
samer@0 69
samer@0 70 public static void doubleToShort(double[] src, byte [] dst, int off, int n, double k) {
samer@0 71 k*=32768;
samer@0 72 for (int i=0, j=off; j<n+off; j++) {
samer@0 73 int y = (int)(k*src[j]);
samer@0 74 dst[i++] = (byte)(y&0xff);
samer@0 75 dst[i++] = (byte)(y>>8 & 0xff);
samer@0 76 }
samer@0 77 }
samer@0 78
samer@0 79 public static void doubleToByte(double [] src, byte[] dst, int off, int n, double k) {
samer@0 80 k*=128;
samer@0 81 for (int i=0, j=off; j<n+off; j++) {
samer@0 82 int y = (int)(k*src[j]) + 128;
samer@0 83 dst[i++] = (byte)(y&0xff);
samer@0 84 }
samer@0 85 }
samer@0 86 }
samer@0 87