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