📄 gsmdecoder.java
字号:
// $Id: GSMDecoder.java,v 1.1 2000/08/10 17:47:08 pfisterer Exp $ // This file is part of the GSM 6.10 audio decoder library for Java// Copyright (C) 1998 Steven Pickles (pix@test.at)// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Library General Public// License as published by the Free Software Foundation; either// version 2 of the License, or (at your option) any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Library General Public License for more details.// You should have received a copy of the GNU Library General Public// License along with this library; if not, write to the Free// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.// This software is a port of the GSM Library provided by// Jutta Degener (jutta@cs.tu-berlin.de) and // Carsten Bormann (cabo@cs.tu-berlin.de), // Technische Universitaet Berlinpackage org.tritonus.lowlevel.gsm;public final class GSMDecoder{ private static final byte GSM_MAGIC = 0x0d; private static final int[] FAC = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 }; private static final int[] QLB = { 3277, 11469, 21299, 32767 }; // TODO: can be replaced by Short.MIN_VALUE and Short.MAX_VALUE ? private static final int MIN_WORD = -32767 - 1; private static final int MAX_WORD = 32767; private int dp0[] = new int[280]; private int u[] = new int[8]; private int LARpp[][] = new int[2][8]; private int j; private int nrp; private int v[] = new int[9]; private int msr; public void GSM() { nrp = 40; } public final int[] decode(byte c[]) throws InvalidGSMFrameException { if (c.length != 33) { throw new InvalidGSMFrameException(); } int i=0; if (((c[i]>>4) & 0xf) != GSM_MAGIC) { throw new InvalidGSMFrameException(); } int LARc[] = new int[8]; int Nc[] = new int[4]; int Mc[] = new int[4]; int bc[] = new int[4]; int xmaxc[] = new int[4]; int xmc[] = new int[13 * 4]; LARc[0] = ((c[i++] & 0xF) << 2); /* 1 */ LARc[0] |= ((c[i] >> 6) & 0x3); LARc[1] = (c[i++] & 0x3F); LARc[2] = ((c[i] >> 3) & 0x1F); LARc[3] = ((c[i++] & 0x7) << 2); LARc[3] |= ((c[i] >> 6) & 0x3); LARc[4] = ((c[i] >> 2) & 0xF); LARc[5] = ((c[i++] & 0x3) << 2); LARc[5] |= ((c[i] >> 6) & 0x3); LARc[6] = ((c[i] >> 3) & 0x7); LARc[7] = (c[i++] & 0x7); Nc[0] = ((c[i] >> 1) & 0x7F); bc[0] = ((c[i++] & 0x1) << 1); bc[0] |= ((c[i] >> 7) & 0x1); Mc[0] = ((c[i] >> 5) & 0x3); xmaxc[0] = ((c[i++] & 0x1F) << 1); xmaxc[0] |= ((c[i] >> 7) & 0x1); xmc[0] = ((c[i] >> 4) & 0x7); xmc[1] = ((c[i] >> 1) & 0x7); xmc[2] = ((c[i++] & 0x1) << 2); xmc[2] |= ((c[i] >> 6) & 0x3); xmc[3] = ((c[i] >> 3) & 0x7); xmc[4] = (c[i++] & 0x7); xmc[5] = ((c[i] >> 5) & 0x7); xmc[6] = ((c[i] >> 2) & 0x7); xmc[7] = ((c[i++] & 0x3) << 1); /* 10 */ xmc[7] |= ((c[i] >> 7) & 0x1); xmc[8] = ((c[i] >> 4) & 0x7); xmc[9] = ((c[i] >> 1) & 0x7); xmc[10] = ((c[i++] & 0x1) << 2); xmc[10] |= ((c[i] >> 6) & 0x3); xmc[11] = ((c[i] >> 3) & 0x7); xmc[12] = (c[i++] & 0x7); Nc[1] = ((c[i] >> 1) & 0x7F); bc[1] = ((c[i++] & 0x1) << 1); bc[1] |= ((c[i] >> 7) & 0x1); Mc[1] = ((c[i] >> 5) & 0x3); xmaxc[1] = ((c[i++] & 0x1F) << 1); xmaxc[1] |= ((c[i] >> 7) & 0x1); xmc[13] = ((c[i] >> 4) & 0x7); xmc[14] = ((c[i] >> 1) & 0x7); xmc[15] = ((c[i++] & 0x1) << 2); xmc[15] |= ((c[i] >> 6) & 0x3); xmc[16] = ((c[i] >> 3) & 0x7); xmc[17] = (c[i++] & 0x7); xmc[18] = ((c[i] >> 5) & 0x7); xmc[19] = ((c[i] >> 2) & 0x7); xmc[20] = ((c[i++] & 0x3) << 1); xmc[20] |= ((c[i] >> 7) & 0x1); xmc[21] = ((c[i] >> 4) & 0x7); xmc[22] = ((c[i] >> 1) & 0x7); xmc[23] = ((c[i++] & 0x1) << 2); xmc[23] |= ((c[i] >> 6) & 0x3); xmc[24] = ((c[i] >> 3) & 0x7); xmc[25] = (c[i++] & 0x7); Nc[2] = ((c[i] >> 1) & 0x7F); bc[2] = ((c[i++] & 0x1) << 1); /* 20 */ bc[2] |= ((c[i] >> 7) & 0x1); Mc[2] = ((c[i] >> 5) & 0x3); xmaxc[2] = ((c[i++] & 0x1F) << 1); xmaxc[2] |= ((c[i] >> 7) & 0x1); xmc[26] = ((c[i] >> 4) & 0x7); xmc[27] = ((c[i] >> 1) & 0x7); xmc[28] = ((c[i++] & 0x1) << 2); xmc[28] |= ((c[i] >> 6) & 0x3); xmc[29] = ((c[i] >> 3) & 0x7); xmc[30] = (c[i++] & 0x7); xmc[31] = ((c[i] >> 5) & 0x7); xmc[32] = ((c[i] >> 2) & 0x7); xmc[33] = ((c[i++] & 0x3) << 1); xmc[33] |= ((c[i] >> 7) & 0x1); xmc[34] = ((c[i] >> 4) & 0x7); xmc[35] = ((c[i] >> 1) & 0x7); xmc[36] = ((c[i++] & 0x1) << 2); xmc[36] |= ((c[i] >> 6) & 0x3); xmc[37] = ((c[i] >> 3) & 0x7); xmc[38] = (c[i++] & 0x7); Nc[3] = ((c[i] >> 1) & 0x7F); bc[3] = ((c[i++] & 0x1) << 1); bc[3] |= ((c[i] >> 7) & 0x1); Mc[3] = ((c[i] >> 5) & 0x3); xmaxc[3] = ((c[i++] & 0x1F) << 1); xmaxc[3] |= ((c[i] >> 7) & 0x1); xmc[39] = ((c[i] >> 4) & 0x7); xmc[40] = ((c[i] >> 1) & 0x7); xmc[41] = ((c[i++] & 0x1) << 2); xmc[41] |= ((c[i] >> 6) & 0x3); xmc[42] = ((c[i] >> 3) & 0x7); xmc[43] = (c[i++] & 0x7); /* 30 */ xmc[44] = ((c[i] >> 5) & 0x7); xmc[45] = ((c[i] >> 2) & 0x7); xmc[46] = ((c[i++] & 0x3) << 1); xmc[46] |= ((c[i] >> 7) & 0x1); xmc[47] = ((c[i] >> 4) & 0x7); xmc[48] = ((c[i] >> 1) & 0x7); xmc[49] = ((c[i++] & 0x1) << 2); xmc[49] |= ((c[i] >> 6) & 0x3); xmc[50] = ((c[i] >> 3) & 0x7); xmc[51] = (c[i] & 0x7); /* 33 */ return decoder(LARc, Nc, bc, Mc, xmaxc, xmc); } public final static void print(String name, int data[]) { System.out.print("["+name+":"); for(int i=0;i<data.length;i++) { System.out.print(""+data[i]); if (i<data.length-1) { System.out.print(","); } else { System.out.println("]"); } } } public final static void print(String name, int data) { System.out.println("["+name+":"+data+"]"); } private final int[] decoder(int LARcr[], int Ncr[], int bcr[], int Mcr[], int xmaxcr[], int xMcr[]) { int j, k; int erp[] = new int[40]; int wt[] = new int[160]; // drp is just dp0+120 //print("LARcr",LARcr); //print("Ncr",Ncr); //print("bcr",bcr); //print("Mcr",Mcr); //print("xmaxcr",xmaxcr); //print("xMcr",xMcr); for (j=0;j<4;j++) { // find out what is done with xMcr RPEDecoding(xmaxcr[j],Mcr[j],xMcr,j*13,erp); //print("erp",erp); longTermSynthesisFiltering(Ncr[j],bcr[j],erp,dp0); for (k=0;k<40;k++) { wt[j*40+k] = dp0[120+k]; } } //print("LARcr",LARcr); //print("wt",wt); int s[] = shortTermSynthesisFilter(LARcr,wt); //print("s",s); postprocessing(s); return s; } private final void RPEDecoding(int xmaxcr, int Mcr, int xMcr[], int xMcrOffset, int erp[]) { int expAndMant[]; int xMp[] = new int[13]; expAndMant = xmaxcToExpAndMant(xmaxcr); //System.out.println("[e&m:"+expAndMant[0]+","+expAndMant[1]+"]"); APCMInverseQuantization(xMcr,xMcrOffset,expAndMant[0],expAndMant[1],xMp); //print("xMp",xMp); RPE_grid_positioning( Mcr, xMp, erp); } private final int[] xmaxcToExpAndMant(int xmaxc) { int exp, mant; exp = 0; if (xmaxc>15) { exp = ((xmaxc>>3)-1); } mant=(xmaxc-(exp<<3)); if (mant==0) { exp = -4; mant = 7; } else { while (mant <= 7) { mant = (mant << 1 | 1); exp--; } mant -= 8; } //assert(exp>=-4 && exp <= 6); //assert(mant>=0 && mant<=7); int result[] = new int[2]; result[0] = exp; result[1] = mant; return result; } //private void assert(boolean test) { // if (!test) { // System.out.println("assertion error"); // } //} private final void APCMInverseQuantization(int xMc[], int xMcOffset, int exp, int mant, int xMp[]) { int i,p; int temp, temp1, temp2, temp3; int ltmp; //assert(mant >0 && mant <= 7 ); temp1 = FAC[mant]; temp2 = sub(6,exp); temp3 = asl(1,sub(temp2,1)); //System.out.println("temp1="+temp1); //System.out.println("temp2="+temp2); //System.out.println("temp3="+temp3); p = 0; for (i=13;i-->0;) { //assert(xMc[xMcOffset] <= 7 && xMc[xMcOffset] >= 0); temp = ((xMc[xMcOffset++] << 1) - 7); //System.out.println("s1:temp="+temp); //assert(temp<=7 && temp >= -7); temp = (temp<<12);//&0xffff; //System.out.println("s2:temp="+temp); temp = mult_r(temp1, temp); //System.out.println("s3:temp="+temp); temp = add(temp, temp3); //System.out.println("s4:temp="+temp); xMp[p++] = asr(temp, temp2); } } private final static int saturate(int x)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -