import java.io.*; // I den här klassen antar vi att varje sample är på 16 bitar och sparas alltså i två bytes ej big endian. public class DSP { public static byte[] distort(byte[] b1, String distortion) { if(distortion.equals("backward")) { byte[] b2 = new byte[b1.length]; for(int i = 0; i < b1.length; i = i + 2) { b2[i] = b1[b1.length - i - 2]; b2[i + 1] = b1[b1.length - i - 1]; } return b2; } if(distortion.equals("slow")) { byte[] b2 = new byte[b1.length * 2]; for(int i = 0; i < b1.length; i = i + 2) { b2[2 * i] = b1[i]; b2[2 * i + 1] = b1[i + 1]; b2[2 * i + 2] = b1[i]; b2[2 * i + 3] = b1[i + 1]; } return b2; } if(distortion.equals("fast1")) { byte[] b2 = new byte[b1.length / 2]; for(int i = 0; i < b1.length / 2; i = i + 2) { b2[i] = b1[2 * i]; b2[i + 1] = b1[2 * i + 1]; } return b2; } // Visar hur man kan göra det lättare med short-array if(distortion.equals("fast2")) { short[] s = byte2short(b1); short[] sNew = new short[s.length / 2]; for(int i = 0; i < sNew.length; i++) { sNew[i] = s[2 * i]; } byte[] b2 = short2byte(sNew); return b2; } if(distortion.equals("echo")) { int echos = 4; int delay = 10000; double decrease = 0.85; short[] s = byte2short(b1); short[] sNew = new short[s.length]; for(int i = 0; i < sNew.length; i++) { for(int ii = 0; ii < echos; ii++) { if(i > (ii * delay)) { sNew[i] = (short)(s[i] + Math.pow(decrease, ii) * s[i - (ii * delay)]); } } } byte[] b2 = short2byte(sNew); return b2; } return b1; } private static byte[] short2byte(short[] sa) { if(sa == null) return null; byte[] ba = new byte[sa.length * 2]; for(int i = 0; i < sa.length; i++) { short anChar = sa[i]; ba[i * 2] = (byte)(anChar & 0xFF); ba[i * 2 + 1] = (byte)((anChar >>> 8) & 0xFF); } return ba; } private static short[] byte2short(byte[] ba) { if(ba == null) return null; short[] sa = new short[ba.length / 2]; for(int i = 0; i < ba.length; i = i + 2) { sa[i / 2] = (short)((ba[i] & 0xFF) + ((((short)ba[i + 1]) << 8) & 0xFF00)); } return sa; } }