关于pcm转G711a的问题

小小禾子 2017-11-06 10:17:35
网上找到两种方法,但是都没有注释,看不懂,希望有做过的大佬能够指点一下



方法一:

public class G711Code {private final static int SIGN_BIT = 0x80;private final static int QUANT_MASK = 0xf;
private final static int SEG_SHIFT = 4;
private final static int SEG_MASK = 0x70;

static short[] seg_end = {0xFF, 0x1FF, 0x3FF, 0x7FF,0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};

static short search(short val,short[] table,short size){

for (short i = 0 ; i < size; i++) {
if(val <= table){
return i;
}
}
return size;
}

static byte linear2alaw(short pcm_val){
short mask;
short seg;
char aval;
if(pcm_val >= 0){
mask = 0xD5;
}else{
mask = 0x55;
pcm_val = (short) (-pcm_val - 1);
if(pcm_val < 0){
pcm_val = 32767;
}
}

/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_end, (short) 8);

/* Combine the sign, segment, and quantization bits. */

if (seg >= 8) /* out of range, return maximum value. */
return (byte) (0x7F ^ mask);
else {
aval = (char) (seg << SEG_SHIFT);
if (seg < 2)
aval |= (pcm_val >> 4) & QUANT_MASK;
else
aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
return (byte) (aval ^ mask);
}
}


static short alaw2linear(byte a_val){
short t;
short seg;

a_val ^= 0x55;

t = (short) ((a_val & QUANT_MASK) << 4);
seg = (short) ((a_val & SEG_MASK) >> SEG_SHIFT);
switch (seg) {
case 0:
t += 8;
break;
case 1:
t += 0x108;
break;
default:
t += 0x108;
t <<= seg - 1;
}
return (a_val & SIGN_BIT) != 0 ? t : (short) -t;
}

/**
* pcm 转 G711 a率
* @param pcm
* @param code
* @param size
*/
public static void G711aEncoder(short[] pcm,byte[] code,int size){
for(int i=0;i<size;i++){
code=linear2alaw(pcm);
}
}


这是csdn上的某位大佬的原创,我原封不动的搬过来了,关于这个方法,有一些疑问:
1,我得到的pcm是一个文件,但这里的方法里的pcm是一个short[],请问是怎么办到的?2,pcm转g711a方法的第二、三个参数是什么意思。


方法二:

public class PCMA extends G711Base{

public static void alaw2linear(byte alaw[],short lin[],int frames) {
int i;
for (i = 0; i < frames; i++)
lin[i] = a2s[alaw[i] & 0xff];
}

public static byte[] alaw2linear(byte alaw[],int frames) {
int i;
byte[] retArr = new byte[frames*2];
short linTmp ;
int retArrPos = 0;

for (i = 0; i < frames; i++)
{
//lin[i] = a2s[alaw[i] & 0xff];
linTmp = a2s[alaw[i] & 0xff];
retArrPos = i*2;
retArr[retArrPos] = (byte)(linTmp & 0xff);
retArr[retArrPos+1] = (byte)((linTmp >> 8) & 0xff);
}

return retArr;
}

public static void alaw2linear(byte alaw[],short lin[],int frames,int mu) {
int i;
for (i = 0; i < frames; i++)
lin[i] = a2s[alaw[i/mu] & 0xff];
}

public static void linear2alaw(short lin[],int offset,byte alaw[],int frames) {
int i;
for (i = 0; i < frames; i++)
alaw[i] = s2a[lin[i+offset] & 0xffff];
}

}


这个方法更是不知道参数什么意思,头疼,初次接触这个音频转码,网上的源码基本都是c、c++的,个人比较菜也看不懂,于是就来求助各位大佬啦、、、、、、、

求指点,感激不尽、、、
...全文
268 回复 点赞 打赏 收藏 举报
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2017-11-06 10:17
社区公告
暂无公告