Go to the documentation of this file.00001
00002 package audio;
00003
00004 import utils.OctetBuffer;
00005
00006
00007
00008
00009
00010
00011 public class AudioCodecAlaw extends AbstractCODEC
00012 {
00013 private int sampleSize;
00014
00015
00016
00017
00018 public AudioCodecAlaw( AudioInterfacePCM audio )
00019 {
00020 this.audio = audio;
00021 this.sampleSize = this.audio.getSampleSize ();
00022 this.outputPcmBuf = new byte[ this.sampleSize ];
00023 this.inputPcmBuf = new byte[ this.sampleSize ];
00024 }
00025
00026
00027
00028
00029 public int getVoicePduSubclass ()
00030 {
00031 return protocol.VoicePDU.ALAW;
00032 }
00033
00034
00035
00036
00037 public int getSampleSize ()
00038 {
00039 return 160;
00040 }
00041
00042
00043
00044
00045 public void convertToPCM( byte[] in, byte[] out )
00046 {
00047 OctetBuffer bb = OctetBuffer.wrap( out );
00048
00049 for ( int i = 0; i < in.length; ++i )
00050 {
00051 bb.putShort( alaw2linear( in[i] ) );
00052 }
00053 }
00054
00055
00056
00057
00058 public void convertFromPCM( byte[] in, byte[] out )
00059 {
00060 OctetBuffer bb = OctetBuffer.wrap( in );
00061
00062 for( int i = 0; i < in.length / 2; ++i )
00063 {
00064 out[i] = linear2alaw( bb.getShort () );
00065 }
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 private final static int SIGN_BIT = 0x80;
00093
00094
00095 private final static int QUANT_MASK = 0xf;
00096
00097
00098 private final static int SEG_SHIFT = 4;
00099 private final static int SEG_MASK = 0x70;
00100
00101
00102 private final static int[] seg_end = {
00103 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF
00104 };
00105
00106
00107
00108
00109 private static byte linear2alaw( short pcm_value )
00110 {
00111
00112 int pcm = pcm_value >> 3;
00113
00114 int mask;
00115 if ( pcm >= 0 )
00116 {
00117 mask = 0xD5;
00118 }
00119 else
00120 {
00121 mask = 0x55;
00122 pcm = -pcm - 1;
00123 }
00124
00125
00126
00127 int seg = 8;
00128
00129 for ( int i = 0; i < 8; ++i ) {
00130 if ( pcm <= seg_end[i] ) {
00131 seg = i;
00132 break;
00133 }
00134 }
00135
00136
00137
00138 if ( seg >= 8 ) {
00139 return (byte)( ( 0x7F ^ mask ) & 0xFF - 0x100 );
00140 }
00141
00142 int aval = ( seg << SEG_SHIFT ) & 0xFF;
00143
00144 if ( seg < 2 ) {
00145 aval |= (pcm >> 1) & QUANT_MASK;
00146 } else {
00147 aval |= (pcm >> seg) & QUANT_MASK;
00148 }
00149
00150 return (byte)( ( aval ^ mask ) & 0xFF - 0x100 );
00151 }
00152
00153
00154
00155
00156 private static short alaw2linear( byte alaw_value )
00157 {
00158
00159 int a_val = ( ( alaw_value + 0x100 ) & 0xFF ) ^ 0x55;
00160
00161 int t = ( a_val & QUANT_MASK ) << 4;
00162 int seg = ( a_val & SEG_MASK ) >> SEG_SHIFT;
00163
00164 switch ( seg )
00165 {
00166 case 0:
00167 t += 8;
00168 break;
00169 case 1:
00170 t += 0x108;
00171 break;
00172 default:
00173 t += 0x108;
00174 t <<= seg - 1;
00175 }
00176
00177 return (short)( ( a_val & SIGN_BIT ) != 0 ? t : -t );
00178 }
00179 }
00180